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 */ 39 static inline void * __must_check ERR_PTR(long error) 40 { 41 return (void *) error; 42 } 43 44 /* Return the pointer in the percpu address space. */ 45 #define ERR_PTR_PCPU(error) ((void __percpu *)(unsigned long)ERR_PTR(error)) 46 47 /* Cast an error pointer to __iomem. */ 48 #define IOMEM_ERR_PTR(error) (__force void __iomem *)ERR_PTR(error) 49 50 /** 51 * PTR_ERR - Extract the error code from an error pointer. 52 * @ptr: An error pointer. 53 * Return: The error code within @ptr. 54 */ 55 static inline long __must_check PTR_ERR(__force const void *ptr) 56 { 57 return (long) ptr; 58 } 59 60 /* Read an error pointer from the percpu address space. */ 61 #define PTR_ERR_PCPU(ptr) (PTR_ERR((const void *)(__force const unsigned long)(ptr))) 62 63 /** 64 * IS_ERR - Detect an error pointer. 65 * @ptr: The pointer to check. 66 * Return: true if @ptr is an error pointer, false otherwise. 67 */ 68 static inline bool __must_check IS_ERR(__force const void *ptr) 69 { 70 return IS_ERR_VALUE((unsigned long)ptr); 71 } 72 73 /* Read an error pointer from the percpu address space. */ 74 #define IS_ERR_PCPU(ptr) (IS_ERR((const void *)(__force const unsigned long)(ptr))) 75 76 /** 77 * IS_ERR_OR_NULL - Detect an error pointer or a null pointer. 78 * @ptr: The pointer to check. 79 * 80 * Like IS_ERR(), but also returns true for a null pointer. 81 */ 82 static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr) 83 { 84 return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr); 85 } 86 87 /** 88 * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type 89 * @ptr: The pointer to cast. 90 * 91 * Explicitly cast an error-valued pointer to another pointer type in such a 92 * way as to make it clear that's what's going on. 93 */ 94 static inline void * __must_check ERR_CAST(__force const void *ptr) 95 { 96 /* cast away the const */ 97 return (void *) ptr; 98 } 99 100 /** 101 * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one. 102 * @ptr: A potential error pointer. 103 * 104 * Convenience function that can be used inside a function that returns 105 * an error code to propagate errors received as error pointers. 106 * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces: 107 * 108 * .. code-block:: c 109 * 110 * if (IS_ERR(ptr)) 111 * return PTR_ERR(ptr); 112 * else 113 * return 0; 114 * 115 * Return: The error code within @ptr if it is an error pointer; 0 otherwise. 116 */ 117 static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr) 118 { 119 if (IS_ERR(ptr)) 120 return PTR_ERR(ptr); 121 else 122 return 0; 123 } 124 125 #endif 126 127 #endif /* _LINUX_ERR_H */ 128