xref: /openbmc/linux/include/linux/err.h (revision 4d744ce9)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef _LINUX_ERR_H
31da177e4SLinus Torvalds #define _LINUX_ERR_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/compiler.h>
6a5ed3ceeSJoe Perches #include <linux/types.h>
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <asm/errno.h>
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * Kernel pointers have redundant information, so we can use a
12a5ed3ceeSJoe Perches  * scheme where we can return either an error code or a normal
131da177e4SLinus Torvalds  * pointer with the same return value.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * This should be a per-architecture thing, to allow different
161da177e4SLinus Torvalds  * error and pointer decisions.
171da177e4SLinus Torvalds  */
18fa79837dSRalf Baechle #define MAX_ERRNO	4095
19fa79837dSRalf Baechle 
20ebba5f9fSRandy Dunlap #ifndef __ASSEMBLY__
21ebba5f9fSRandy Dunlap 
22*4d744ce9SJames Seo /**
23*4d744ce9SJames Seo  * IS_ERR_VALUE - Detect an error pointer.
24*4d744ce9SJames Seo  * @x: The pointer to check.
25*4d744ce9SJames Seo  *
26*4d744ce9SJames Seo  * Like IS_ERR(), but does not generate a compiler warning if result is unused.
27*4d744ce9SJames Seo  */
28aa00edc1SLinus Torvalds #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
2907ab67c8SLinus Torvalds 
30*4d744ce9SJames Seo /**
31*4d744ce9SJames Seo  * ERR_PTR - Create an error pointer.
32*4d744ce9SJames Seo  * @error: A negative error code.
33*4d744ce9SJames Seo  *
34*4d744ce9SJames Seo  * Encodes @error into a pointer value. Users should consider the result
35*4d744ce9SJames Seo  * opaque and not assume anything about how the error is encoded.
36*4d744ce9SJames Seo  *
37*4d744ce9SJames Seo  * Return: A pointer with @error encoded within its value.
38*4d744ce9SJames Seo  */
ERR_PTR(long error)39e47103b1SJani Nikula static inline void * __must_check ERR_PTR(long error)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	return (void *) error;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
44*4d744ce9SJames Seo /**
45*4d744ce9SJames Seo  * PTR_ERR - Extract the error code from an error pointer.
46*4d744ce9SJames Seo  * @ptr: An error pointer.
47*4d744ce9SJames Seo  * Return: The error code within @ptr.
48*4d744ce9SJames Seo  */
PTR_ERR(__force const void * ptr)49e7152b97SDan Carpenter static inline long __must_check PTR_ERR(__force const void *ptr)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	return (long) ptr;
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
54*4d744ce9SJames Seo /**
55*4d744ce9SJames Seo  * IS_ERR - Detect an error pointer.
56*4d744ce9SJames Seo  * @ptr: The pointer to check.
57*4d744ce9SJames Seo  * Return: true if @ptr is an error pointer, false otherwise.
58*4d744ce9SJames Seo  */
IS_ERR(__force const void * ptr)59a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR(__force const void *ptr)
601da177e4SLinus Torvalds {
6107ab67c8SLinus Torvalds 	return IS_ERR_VALUE((unsigned long)ptr);
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64*4d744ce9SJames Seo /**
65*4d744ce9SJames Seo  * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
66*4d744ce9SJames Seo  * @ptr: The pointer to check.
67*4d744ce9SJames Seo  *
68*4d744ce9SJames Seo  * Like IS_ERR(), but also returns true for a null pointer.
69*4d744ce9SJames Seo  */
IS_ERR_OR_NULL(__force const void * ptr)70a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
71603c4ba9SPhil Carmody {
72dfffa587SViresh Kumar 	return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
73603c4ba9SPhil Carmody }
74603c4ba9SPhil Carmody 
75d1bc8e95SDavid Howells /**
76d1bc8e95SDavid Howells  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
77d1bc8e95SDavid Howells  * @ptr: The pointer to cast.
78d1bc8e95SDavid Howells  *
79d1bc8e95SDavid Howells  * Explicitly cast an error-valued pointer to another pointer type in such a
80d1bc8e95SDavid Howells  * way as to make it clear that's what's going on.
81d1bc8e95SDavid Howells  */
ERR_CAST(__force const void * ptr)82e7152b97SDan Carpenter static inline void * __must_check ERR_CAST(__force const void *ptr)
83d1bc8e95SDavid Howells {
84d1bc8e95SDavid Howells 	/* cast away the const */
85d1bc8e95SDavid Howells 	return (void *) ptr;
86d1bc8e95SDavid Howells }
87d1bc8e95SDavid Howells 
88*4d744ce9SJames Seo /**
89*4d744ce9SJames Seo  * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
90*4d744ce9SJames Seo  * @ptr: A potential error pointer.
91*4d744ce9SJames Seo  *
92*4d744ce9SJames Seo  * Convenience function that can be used inside a function that returns
93*4d744ce9SJames Seo  * an error code to propagate errors received as error pointers.
94*4d744ce9SJames Seo  * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
95*4d744ce9SJames Seo  *
96*4d744ce9SJames Seo  * .. code-block:: c
97*4d744ce9SJames Seo  *
98*4d744ce9SJames Seo  *	if (IS_ERR(ptr))
99*4d744ce9SJames Seo  *		return PTR_ERR(ptr);
100*4d744ce9SJames Seo  *	else
101*4d744ce9SJames Seo  *		return 0;
102*4d744ce9SJames Seo  *
103*4d744ce9SJames Seo  * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
104*4d744ce9SJames Seo  */
PTR_ERR_OR_ZERO(__force const void * ptr)1056e8b8726SRusty Russell static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
106fa9ee9c4SUwe Kleine-König {
107fa9ee9c4SUwe Kleine-König 	if (IS_ERR(ptr))
108fa9ee9c4SUwe Kleine-König 		return PTR_ERR(ptr);
109fa9ee9c4SUwe Kleine-König 	else
110fa9ee9c4SUwe Kleine-König 		return 0;
111fa9ee9c4SUwe Kleine-König }
112fa9ee9c4SUwe Kleine-König 
113ebba5f9fSRandy Dunlap #endif
114ebba5f9fSRandy Dunlap 
1151da177e4SLinus Torvalds #endif /* _LINUX_ERR_H */
116