xref: /linux-6.15/include/linux/err.h (revision 18311a76)
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 
224d744ce9SJames Seo /**
234d744ce9SJames Seo  * IS_ERR_VALUE - Detect an error pointer.
244d744ce9SJames Seo  * @x: The pointer to check.
254d744ce9SJames Seo  *
264d744ce9SJames Seo  * Like IS_ERR(), but does not generate a compiler warning if result is unused.
274d744ce9SJames Seo  */
28aa00edc1SLinus Torvalds #define IS_ERR_VALUE(x) unlikely((unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO)
2907ab67c8SLinus Torvalds 
304d744ce9SJames Seo /**
314d744ce9SJames Seo  * ERR_PTR - Create an error pointer.
324d744ce9SJames Seo  * @error: A negative error code.
334d744ce9SJames Seo  *
344d744ce9SJames Seo  * Encodes @error into a pointer value. Users should consider the result
354d744ce9SJames Seo  * opaque and not assume anything about how the error is encoded.
364d744ce9SJames Seo  *
374d744ce9SJames Seo  * Return: A pointer with @error encoded within its value.
384d744ce9SJames 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 
44a759e37fSUros Bizjak /* Return the pointer in the percpu address space. */
45a759e37fSUros Bizjak #define ERR_PTR_PCPU(error) ((void __percpu *)(unsigned long)ERR_PTR(error))
46a759e37fSUros Bizjak 
47*18311a76SRaag Jadav /* Cast an error pointer to __iomem. */
48*18311a76SRaag Jadav #define IOMEM_ERR_PTR(error) (__force void __iomem *)ERR_PTR(error)
49*18311a76SRaag Jadav 
504d744ce9SJames Seo /**
514d744ce9SJames Seo  * PTR_ERR - Extract the error code from an error pointer.
524d744ce9SJames Seo  * @ptr: An error pointer.
534d744ce9SJames Seo  * Return: The error code within @ptr.
544d744ce9SJames Seo  */
PTR_ERR(__force const void * ptr)55e7152b97SDan Carpenter static inline long __must_check PTR_ERR(__force const void *ptr)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	return (long) ptr;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
60a759e37fSUros Bizjak /* Read an error pointer from the percpu address space. */
61a759e37fSUros Bizjak #define PTR_ERR_PCPU(ptr) (PTR_ERR((const void *)(__force const unsigned long)(ptr)))
62a759e37fSUros Bizjak 
634d744ce9SJames Seo /**
644d744ce9SJames Seo  * IS_ERR - Detect an error pointer.
654d744ce9SJames Seo  * @ptr: The pointer to check.
664d744ce9SJames Seo  * Return: true if @ptr is an error pointer, false otherwise.
674d744ce9SJames Seo  */
IS_ERR(__force const void * ptr)68a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR(__force const void *ptr)
691da177e4SLinus Torvalds {
7007ab67c8SLinus Torvalds 	return IS_ERR_VALUE((unsigned long)ptr);
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds 
73a759e37fSUros Bizjak /* Read an error pointer from the percpu address space. */
74a759e37fSUros Bizjak #define IS_ERR_PCPU(ptr) (IS_ERR((const void *)(__force const unsigned long)(ptr)))
75a759e37fSUros Bizjak 
764d744ce9SJames Seo /**
774d744ce9SJames Seo  * IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
784d744ce9SJames Seo  * @ptr: The pointer to check.
794d744ce9SJames Seo  *
804d744ce9SJames Seo  * Like IS_ERR(), but also returns true for a null pointer.
814d744ce9SJames Seo  */
IS_ERR_OR_NULL(__force const void * ptr)82a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
83603c4ba9SPhil Carmody {
84dfffa587SViresh Kumar 	return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
85603c4ba9SPhil Carmody }
86603c4ba9SPhil Carmody 
87d1bc8e95SDavid Howells /**
88d1bc8e95SDavid Howells  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
89d1bc8e95SDavid Howells  * @ptr: The pointer to cast.
90d1bc8e95SDavid Howells  *
91d1bc8e95SDavid Howells  * Explicitly cast an error-valued pointer to another pointer type in such a
92d1bc8e95SDavid Howells  * way as to make it clear that's what's going on.
93d1bc8e95SDavid Howells  */
ERR_CAST(__force const void * ptr)94e7152b97SDan Carpenter static inline void * __must_check ERR_CAST(__force const void *ptr)
95d1bc8e95SDavid Howells {
96d1bc8e95SDavid Howells 	/* cast away the const */
97d1bc8e95SDavid Howells 	return (void *) ptr;
98d1bc8e95SDavid Howells }
99d1bc8e95SDavid Howells 
1004d744ce9SJames Seo /**
1014d744ce9SJames Seo  * PTR_ERR_OR_ZERO - Extract the error code from a pointer if it has one.
1024d744ce9SJames Seo  * @ptr: A potential error pointer.
1034d744ce9SJames Seo  *
1044d744ce9SJames Seo  * Convenience function that can be used inside a function that returns
1054d744ce9SJames Seo  * an error code to propagate errors received as error pointers.
1064d744ce9SJames Seo  * For example, ``return PTR_ERR_OR_ZERO(ptr);`` replaces:
1074d744ce9SJames Seo  *
1084d744ce9SJames Seo  * .. code-block:: c
1094d744ce9SJames Seo  *
1104d744ce9SJames Seo  *	if (IS_ERR(ptr))
1114d744ce9SJames Seo  *		return PTR_ERR(ptr);
1124d744ce9SJames Seo  *	else
1134d744ce9SJames Seo  *		return 0;
1144d744ce9SJames Seo  *
1154d744ce9SJames Seo  * Return: The error code within @ptr if it is an error pointer; 0 otherwise.
1164d744ce9SJames Seo  */
PTR_ERR_OR_ZERO(__force const void * ptr)1176e8b8726SRusty Russell static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
118fa9ee9c4SUwe Kleine-König {
119fa9ee9c4SUwe Kleine-König 	if (IS_ERR(ptr))
120fa9ee9c4SUwe Kleine-König 		return PTR_ERR(ptr);
121fa9ee9c4SUwe Kleine-König 	else
122fa9ee9c4SUwe Kleine-König 		return 0;
123fa9ee9c4SUwe Kleine-König }
124fa9ee9c4SUwe Kleine-König 
125ebba5f9fSRandy Dunlap #endif
126ebba5f9fSRandy Dunlap 
1271da177e4SLinus Torvalds #endif /* _LINUX_ERR_H */
128