[PATCH 1/2] err.h: Add ERR_PTR_PCPU(), PTR_ERR_PCPU() and IS_ERR_PCPU() functions

From: Uros Bizjak
Date: Tue Aug 13 2024 - 13:25:58 EST


Add ERR_PTR_PCPU(), PTR_ERR_PCPU() and IS_ERR_PCPU() functions that
operate on pointers in the percpu address space.

These functions remove the need for (__force void *) function
argument casts (to avoid sparse -Wcast-from-as warnings).

The patch will also avoid future build errors due to pointer address
space mismatch with enabled strict percpu address space checks.

Signed-off-by: Uros Bizjak <ubizjak@xxxxxxxxx>
Cc: Catalin Marinas <catalin.marinas@xxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
---
include/linux/err.h | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)

diff --git a/include/linux/err.h b/include/linux/err.h
index b5d9bb2a2349..997fd6fe1d0c 100644
--- a/include/linux/err.h
+++ b/include/linux/err.h
@@ -6,6 +6,7 @@
#include <linux/types.h>

#include <asm/errno.h>
+#include <asm/percpu.h>

/*
* Kernel pointers have redundant information, so we can use a
@@ -41,6 +42,22 @@ static inline void * __must_check ERR_PTR(long error)
return (void *) error;
}

+/**
+ * ERR_PTR_PCPU - Create an error pointer in the percpu address space.
+ * @error: A negative error code.
+ *
+ * Encodes @error into a pointer value in the percpu address space.
+ * Users should consider the result opaque and not assume anything
+ * about how the error is encoded.
+ *
+ * Return: A pointer in the percpu address space with @error encoded
+ * within its value.
+ */
+static inline void __percpu * __must_check ERR_PTR_PCPU(long error)
+{
+ return (void __percpu *) error;
+}
+
/**
* PTR_ERR - Extract the error code from an error pointer.
* @ptr: An error pointer.
@@ -51,6 +68,17 @@ static inline long __must_check PTR_ERR(__force const void *ptr)
return (long) ptr;
}

+/**
+ * PTR_ERR_PCPU - Extract the error code from an error pointer in the
+ * percpu address space.
+ * @ptr: An error pointer in the percpu address space.
+ * Return: The error code within @ptr.
+ */
+static inline long __must_check PTR_ERR_PCPU(const void __percpu *ptr)
+{
+ return (__force long) ptr;
+}
+
/**
* IS_ERR - Detect an error pointer.
* @ptr: The pointer to check.
@@ -61,6 +89,16 @@ static inline bool __must_check IS_ERR(__force const void *ptr)
return IS_ERR_VALUE((unsigned long)ptr);
}

+/**
+ * IS_ERR_PCPU - Detect an error pointer in the percpu address space.
+ * @ptr: The pointer in the percpu address space to check.
+ * Return: true if @ptr is an error pointer, false otherwise.
+ */
+static inline bool __must_check IS_ERR_PCPU(const void __percpu *ptr)
+{
+ return IS_ERR_VALUE((__force unsigned long)ptr);
+}
+
/**
* IS_ERR_OR_NULL - Detect an error pointer or a null pointer.
* @ptr: The pointer to check.
--
2.42.0