xref: /linux-6.15/include/linux/err.h (revision dfffa587)
11da177e4SLinus Torvalds #ifndef _LINUX_ERR_H
21da177e4SLinus Torvalds #define _LINUX_ERR_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #include <linux/compiler.h>
5a5ed3ceeSJoe Perches #include <linux/types.h>
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <asm/errno.h>
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * Kernel pointers have redundant information, so we can use a
11a5ed3ceeSJoe Perches  * scheme where we can return either an error code or a normal
121da177e4SLinus Torvalds  * pointer with the same return value.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This should be a per-architecture thing, to allow different
151da177e4SLinus Torvalds  * error and pointer decisions.
161da177e4SLinus Torvalds  */
17fa79837dSRalf Baechle #define MAX_ERRNO	4095
18fa79837dSRalf Baechle 
19ebba5f9fSRandy Dunlap #ifndef __ASSEMBLY__
20ebba5f9fSRandy Dunlap 
21fa79837dSRalf Baechle #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
2207ab67c8SLinus Torvalds 
23e47103b1SJani Nikula static inline void * __must_check ERR_PTR(long error)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds 	return (void *) error;
261da177e4SLinus Torvalds }
271da177e4SLinus Torvalds 
28e7152b97SDan Carpenter static inline long __must_check PTR_ERR(__force const void *ptr)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	return (long) ptr;
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds 
33a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR(__force const void *ptr)
341da177e4SLinus Torvalds {
3507ab67c8SLinus Torvalds 	return IS_ERR_VALUE((unsigned long)ptr);
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
38a5ed3ceeSJoe Perches static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
39603c4ba9SPhil Carmody {
40*dfffa587SViresh Kumar 	return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
41603c4ba9SPhil Carmody }
42603c4ba9SPhil Carmody 
43d1bc8e95SDavid Howells /**
44d1bc8e95SDavid Howells  * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
45d1bc8e95SDavid Howells  * @ptr: The pointer to cast.
46d1bc8e95SDavid Howells  *
47d1bc8e95SDavid Howells  * Explicitly cast an error-valued pointer to another pointer type in such a
48d1bc8e95SDavid Howells  * way as to make it clear that's what's going on.
49d1bc8e95SDavid Howells  */
50e7152b97SDan Carpenter static inline void * __must_check ERR_CAST(__force const void *ptr)
51d1bc8e95SDavid Howells {
52d1bc8e95SDavid Howells 	/* cast away the const */
53d1bc8e95SDavid Howells 	return (void *) ptr;
54d1bc8e95SDavid Howells }
55d1bc8e95SDavid Howells 
566e8b8726SRusty Russell static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
57fa9ee9c4SUwe Kleine-König {
58fa9ee9c4SUwe Kleine-König 	if (IS_ERR(ptr))
59fa9ee9c4SUwe Kleine-König 		return PTR_ERR(ptr);
60fa9ee9c4SUwe Kleine-König 	else
61fa9ee9c4SUwe Kleine-König 		return 0;
62fa9ee9c4SUwe Kleine-König }
63fa9ee9c4SUwe Kleine-König 
646e8b8726SRusty Russell /* Deprecated */
656e8b8726SRusty Russell #define PTR_RET(p) PTR_ERR_OR_ZERO(p)
666e8b8726SRusty Russell 
67ebba5f9fSRandy Dunlap #endif
68ebba5f9fSRandy Dunlap 
691da177e4SLinus Torvalds #endif /* _LINUX_ERR_H */
70