xref: /linux/include/linux/err.h (revision 208eed95fc710827b100266c9450ae84d46727bd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_ERR_H
3 #define _LINUX_ERR_H
4 
5 #include <linux/compiler.h>
6 #include <linux/types.h>
7 
8 #include <asm/errno.h>
9 
10 /*
11  * Kernel pointers have redundant information, so we can use a
12  * scheme where we can return either an error code or a normal
13  * pointer with the same return value.
14  *
15  * This should be a per-architecture thing, to allow different
16  * error and pointer decisions.
17  */
18 #define MAX_ERRNO	4095
19 
20 #ifndef __ASSEMBLY__
21 
22 /**
23  * IS_ERR_VALUE - Detect an error pointer.
24  * @x: The pointer to check.
25  *
26  * Like IS_ERR(), but does not generate a compiler warning if result is unused.
27  */
28 #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
29 
30 /**
31  * ERR_PTR - Create an error pointer.
32  * @error: A negative error code.
33  *
34  * Encodes @error into a pointer value. Users should consider the result
35  * opaque and not assume anything about how the error is encoded.
36  *
37  * Return: A pointer with @error encoded within its value.
38  */
ERR_PTR(long error)39 static inline void * __must_check ERR_PTR(long error)
40 {
41 	return (void *) error;
42 }
43 
44 /**
45  * INIT_ERR_PTR - Init a const error pointer.
46  * @error: A negative error code.
47  *
48  * Like ERR_PTR(), but usable to initialize static variables.
49  */
50 #define INIT_ERR_PTR(error) ((void *)(error))
51 
52 /* Return the pointer in the percpu address space. */
53 #define ERR_PTR_PCPU(error) ((void __percpu *)(unsigned long)ERR_PTR(error))
54 
55 /* Cast an error pointer to __iomem. */
56 #define IOMEM_ERR_PTR(error) (__force void __iomem *)ERR_PTR(error)
57 
58 /**
59  * PTR_ERR - Extract the error code from an error pointer.
60  * @ptr: An error pointer.
61  * Return: The error code within @ptr.
62  */
PTR_ERR(__force const void * ptr)63 static inline long __must_check PTR_ERR(__force const void *ptr)
64 {
65 	return (long) ptr;
66 }
67 
68 /* Read an error pointer from the percpu address space. */
69 #define PTR_ERR_PCPU(ptr) (PTR_ERR((const void *)(__force const unsigned long)(ptr)))
70 
71 /**
72  * IS_ERR - Detect an error pointer.
73  * @ptr: The pointer to check.
74  * Return: true if @ptr is an error pointer, false otherwise.
75  */
IS_ERR(__force const void * ptr)76 static inline bool __must_check IS_ERR(__force const void *ptr)
77 {
78 	return IS_ERR_VALUE((unsigned long)ptr);
79 }
80 
81 /* Read an error pointer from the percpu address space. */
82 #define IS_ERR_PCPU(ptr) (IS_ERR((const void *)(__force const unsigned long)(ptr)))
83 
84 /**
85  * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
86  * @ptr: The pointer to check.
87  *
88  * Like IS_ERR(), but also returns true for a null pointer.
89  */
IS_ERR_OR_NULL(__force const void * ptr)90 static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
91 {
92 	return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
93 }
94 
95 /**
96  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
97  * @ptr: The pointer to cast.
98  *
99  * Explicitly cast an error-valued pointer to another pointer type in such a
100  * way as to make it clear that's what's going on.
101  */
ERR_CAST(__force const void * ptr)102 static inline void * __must_check ERR_CAST(__force const void *ptr)
103 {
104 	/* cast away the const */
105 	return (void *) ptr;
106 }
107 
108 /**
109  * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
110  * @ptr: A potential error pointer.
111  *
112  * Convenience function that can be used inside a function that returns
113  * an error code to propagate errors received as error pointers.
114  * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
115  *
116  * .. code-block:: c
117  *
118  *	if (IS_ERR(ptr))
119  *		return PTR_ERR(ptr);
120  *	else
121  *		return 0;
122  *
123  * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
124  */
PTR_ERR_OR_ZERO(__force const void * ptr)125 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
126 {
127 	if (IS_ERR(ptr))
128 		return PTR_ERR(ptr);
129 	else
130 		return 0;
131 }
132 
133 #endif
134 
135 #endif /* _LINUX_ERR_H */
136