* [PATCH v1 0/2] kernel.h: Move out a couple of macros and constants
@ 2025-03-24 10:50 Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 1/2] kernel.h: Move READ/WRITE definitions to <linux/types.h> Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 2/2] kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h Andy Shevchenko
0 siblings, 2 replies; 3+ messages in thread
From: Andy Shevchenko @ 2025-03-24 10:50 UTC (permalink / raw)
To: Andy Shevchenko, Alexandru Ardelean, linux-kernel; +Cc: Andrew Morton
The kernel.h hosts a couple of macros and constants that may have been put
to a better place. Do that. Also add missing documentation.
No functional changes intended.
Andy Shevchenko (1):
kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h
Ingo Molnar (1):
kernel.h: Move READ/WRITE definitions to <linux/types.h>
include/linux/kernel.h | 14 +-------
include/linux/types.h | 4 +++
include/linux/util_macros.h | 66 +++++++++++++++++++++++++++++++++++++
3 files changed, 71 insertions(+), 13 deletions(-)
--
2.47.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v1 1/2] kernel.h: Move READ/WRITE definitions to <linux/types.h>
2025-03-24 10:50 [PATCH v1 0/2] kernel.h: Move out a couple of macros and constants Andy Shevchenko
@ 2025-03-24 10:50 ` Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 2/2] kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h Andy Shevchenko
1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2025-03-24 10:50 UTC (permalink / raw)
To: Andy Shevchenko, Alexandru Ardelean, linux-kernel
Cc: Andrew Morton, Ingo Molnar
From: Ingo Molnar <mingo@kernel.org>
Headers shouldn't be forced to include <linux/kernel.h> just to
gain these simple constants.
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
include/linux/kernel.h | 4 ----
include/linux/types.h | 4 ++++
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index be2e8c0a187e..01bb0fac3667 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -41,10 +41,6 @@
#define STACK_MAGIC 0xdeadbeef
-/* generic data direction definitions */
-#define READ 0
-#define WRITE 1
-
#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
#define u64_to_user_ptr(x) ( \
diff --git a/include/linux/types.h b/include/linux/types.h
index 49b79c8bb1a9..6dfdb8e8e4c3 100644
--- a/include/linux/types.h
+++ b/include/linux/types.h
@@ -136,6 +136,10 @@ typedef s64 ktime_t;
typedef u64 sector_t;
typedef u64 blkcnt_t;
+/* generic data direction definitions */
+#define READ 0
+#define WRITE 1
+
/*
* The type of an index into the pagecache.
*/
--
2.47.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v1 2/2] kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h
2025-03-24 10:50 [PATCH v1 0/2] kernel.h: Move out a couple of macros and constants Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 1/2] kernel.h: Move READ/WRITE definitions to <linux/types.h> Andy Shevchenko
@ 2025-03-24 10:50 ` Andy Shevchenko
1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2025-03-24 10:50 UTC (permalink / raw)
To: Andy Shevchenko, Alexandru Ardelean, linux-kernel; +Cc: Andrew Morton
While the natural choice of PTR_IF() is kconfig.h, the latter is
too broad to include C code and actually the macro was moved out
from there in the past. But kernel.h is neither a good choice for
that. Move it to util_macros.h. Do the same for u64_to_user_ptr().
While moving, add necessary documentation.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
include/linux/kernel.h | 10 +-----
include/linux/util_macros.h | 66 +++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+), 9 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 01bb0fac3667..1cce1f6410a9 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -33,6 +33,7 @@
#include <linux/sprintf.h>
#include <linux/static_call_types.h>
#include <linux/instruction_pointer.h>
+#include <linux/util_macros.h>
#include <linux/wordpart.h>
#include <asm/byteorder.h>
@@ -41,15 +42,6 @@
#define STACK_MAGIC 0xdeadbeef
-#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
-
-#define u64_to_user_ptr(x) ( \
-{ \
- typecheck(u64, (x)); \
- (void __user *)(uintptr_t)(x); \
-} \
-)
-
struct completion;
struct user;
diff --git a/include/linux/util_macros.h b/include/linux/util_macros.h
index 3b570b765b75..7b64fb597f85 100644
--- a/include/linux/util_macros.h
+++ b/include/linux/util_macros.h
@@ -79,6 +79,72 @@
(__fc_i); \
})
+/**
+ * PTR_IF - evaluate to @ptr if @cond is true, or to NULL otherwise.
+ * @cond: A conditional, usually in a form of IS_ENABLED(CONFIG_FOO)
+ * @ptr: A pointer to assign if @cond is true.
+ *
+ * PTR_IF(IS_ENABLED(CONFIG_FOO), ptr) evaluates to @ptr if CONFIG_FOO is set
+ * to 'y' or 'm', or to NULL otherwise. The (ptr) argument must be a pointer.
+ *
+ * The macro can be very useful to help compiler dropping dead code.
+ *
+ * For instance, consider the following::
+ *
+ * #ifdef CONFIG_FOO_SUSPEND
+ * static int foo_suspend(struct device *dev)
+ * {
+ * ...
+ * }
+ * #endif
+ *
+ * static struct pm_ops foo_ops = {
+ * #ifdef CONFIG_FOO_SUSPEND
+ * .suspend = foo_suspend,
+ * #endif
+ * };
+ *
+ * While this works, the foo_suspend() macro is compiled conditionally,
+ * only when CONFIG_FOO_SUSPEND is set. This is problematic, as there could
+ * be a build bug in this function, we wouldn't have a way to know unless
+ * the configuration option is set.
+ *
+ * An alternative is to declare foo_suspend() always, but mark it
+ * as __maybe_unused. This works, but the __maybe_unused attribute
+ * is required to instruct the compiler that the function may not
+ * be referenced anywhere, and is safe to remove without making
+ * a fuss about it. This makes the programmer responsible for tagging
+ * the functions that can be garbage-collected.
+ *
+ * With the macro it is possible to write the following:
+ *
+ * static int foo_suspend(struct device *dev)
+ * {
+ * ...
+ * }
+ *
+ * static struct pm_ops foo_ops = {
+ * .suspend = PTR_IF(IS_ENABLED(CONFIG_FOO_SUSPEND), foo_suspend),
+ * };
+ *
+ * The foo_suspend() function will now be automatically dropped by the
+ * compiler, and it does not require any specific attribute.
+ */
+#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
+
+/**
+ * to_user_ptr - cast a pointer passed as u64 from user space to void __user *
+ * @x: The u64 value from user space, usually via IOCTL
+ *
+ * to_user_ptr() simply casts a pointer passed as u64 from user space to void
+ * __user * correctly. Using this lets us get rid of all the tiresome casts.
+ */
+#define u64_to_user_ptr(x) \
+({ \
+ typecheck(u64, (x)); \
+ (void __user *)(uintptr_t)(x); \
+})
+
/**
* is_insidevar - check if the @ptr points inside the @var memory range.
* @ptr: the pointer to a memory address.
--
2.47.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-03-24 10:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 10:50 [PATCH v1 0/2] kernel.h: Move out a couple of macros and constants Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 1/2] kernel.h: Move READ/WRITE definitions to <linux/types.h> Andy Shevchenko
2025-03-24 10:50 ` [PATCH v1 2/2] kernel.h: Move PTR_IF() and u64_to_user_ptr() to util_macros.h Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).