1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
201ca9fd4SJiri Olsa #ifndef __TOOLS_LINUX_ERR_H
301ca9fd4SJiri Olsa #define __TOOLS_LINUX_ERR_H
401ca9fd4SJiri Olsa
501ca9fd4SJiri Olsa #include <linux/compiler.h>
601ca9fd4SJiri Olsa #include <linux/types.h>
701ca9fd4SJiri Olsa
801ca9fd4SJiri Olsa #include <asm/errno.h>
901ca9fd4SJiri Olsa
1001ca9fd4SJiri Olsa /*
1101ca9fd4SJiri Olsa * Original kernel header comment:
1201ca9fd4SJiri Olsa *
1301ca9fd4SJiri Olsa * Kernel pointers have redundant information, so we can use a
1401ca9fd4SJiri Olsa * scheme where we can return either an error code or a normal
1501ca9fd4SJiri Olsa * pointer with the same return value.
1601ca9fd4SJiri Olsa *
1701ca9fd4SJiri Olsa * This should be a per-architecture thing, to allow different
1801ca9fd4SJiri Olsa * error and pointer decisions.
1901ca9fd4SJiri Olsa *
2001ca9fd4SJiri Olsa * Userspace note:
2101ca9fd4SJiri Olsa * The same principle works for userspace, because 'error' pointers
2201ca9fd4SJiri Olsa * fall down to the unused hole far from user space, as described
23*ff61f079SJonathan Corbet * in Documentation/arch/x86/x86_64/mm.rst for x86_64 arch:
2401ca9fd4SJiri Olsa *
2501ca9fd4SJiri Olsa * 0000000000000000 - 00007fffffffffff (=47 bits) user space, different per mm hole caused by [48:63] sign extension
2601ca9fd4SJiri Olsa * ffffffffffe00000 - ffffffffffffffff (=2 MB) unused hole
2701ca9fd4SJiri Olsa *
2801ca9fd4SJiri Olsa * It should be the same case for other architectures, because
2901ca9fd4SJiri Olsa * this code is used in generic kernel code.
3001ca9fd4SJiri Olsa */
3101ca9fd4SJiri Olsa #define MAX_ERRNO 4095
3201ca9fd4SJiri Olsa
3301ca9fd4SJiri Olsa #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
3401ca9fd4SJiri Olsa
ERR_PTR(long error_)3545633a16SJiri Olsa static inline void * __must_check ERR_PTR(long error_)
3601ca9fd4SJiri Olsa {
3745633a16SJiri Olsa return (void *) error_;
3801ca9fd4SJiri Olsa }
3901ca9fd4SJiri Olsa
PTR_ERR(__force const void * ptr)4001ca9fd4SJiri Olsa static inline long __must_check PTR_ERR(__force const void *ptr)
4101ca9fd4SJiri Olsa {
4201ca9fd4SJiri Olsa return (long) ptr;
4301ca9fd4SJiri Olsa }
4401ca9fd4SJiri Olsa
IS_ERR(__force const void * ptr)4501ca9fd4SJiri Olsa static inline bool __must_check IS_ERR(__force const void *ptr)
4601ca9fd4SJiri Olsa {
4701ca9fd4SJiri Olsa return IS_ERR_VALUE((unsigned long)ptr);
4801ca9fd4SJiri Olsa }
4901ca9fd4SJiri Olsa
IS_ERR_OR_NULL(__force const void * ptr)5061d4595eSLevin, Alexander (Sasha Levin) static inline bool __must_check IS_ERR_OR_NULL(__force const void *ptr)
5161d4595eSLevin, Alexander (Sasha Levin) {
5261d4595eSLevin, Alexander (Sasha Levin) return unlikely(!ptr) || IS_ERR_VALUE((unsigned long)ptr);
5361d4595eSLevin, Alexander (Sasha Levin) }
5461d4595eSLevin, Alexander (Sasha Levin)
PTR_ERR_OR_ZERO(__force const void * ptr)5501ab2e91SDing Xiang static inline int __must_check PTR_ERR_OR_ZERO(__force const void *ptr)
5601ab2e91SDing Xiang {
5701ab2e91SDing Xiang if (IS_ERR(ptr))
5801ab2e91SDing Xiang return PTR_ERR(ptr);
5901ab2e91SDing Xiang else
6001ab2e91SDing Xiang return 0;
6101ab2e91SDing Xiang }
6292151b0aSArnaldo Carvalho de Melo
6392151b0aSArnaldo Carvalho de Melo /**
6492151b0aSArnaldo Carvalho de Melo * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
6592151b0aSArnaldo Carvalho de Melo * @ptr: The pointer to cast.
6692151b0aSArnaldo Carvalho de Melo *
6792151b0aSArnaldo Carvalho de Melo * Explicitly cast an error-valued pointer to another pointer type in such a
6892151b0aSArnaldo Carvalho de Melo * way as to make it clear that's what's going on.
6992151b0aSArnaldo Carvalho de Melo */
ERR_CAST(__force const void * ptr)7092151b0aSArnaldo Carvalho de Melo static inline void * __must_check ERR_CAST(__force const void *ptr)
7192151b0aSArnaldo Carvalho de Melo {
7292151b0aSArnaldo Carvalho de Melo /* cast away the const */
7392151b0aSArnaldo Carvalho de Melo return (void *) ptr;
7492151b0aSArnaldo Carvalho de Melo }
7501ca9fd4SJiri Olsa #endif /* _LINUX_ERR_H */
76