Skip to contentSkip to conversation

asm-generic: replace ________addr with __UNIQUE_ID(addr)

PATCH In review Min-Hsun Chang 3 participants 12 messages Mar 7 – Apr 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asm-generic: replace ________addr with __UNIQUE_ID(addr)

The macro __set_fixmap_offset() uses a hardcoded identifier ________addr, which can lead to variable name shadowing if a caller happens to use the same name in its scope.

Following the pattern in commit 44238e82670e ("rcu: Replace ________p1 and _________p1 with __UNIQUE_ID(rcu)") and commit 589a9785ee3a ("min/max:
remove sparse warnings when they're nested"), replace the hardcoded identifier with __UNIQUE_ID(addr).

Since __UNIQUE_ID() must be expanded once to remain consistent across declaration, assignment, and return within the statement expression, introduce a nested helper macro ___set_fixmap_offset.

---
 include/asm-generic/fixmap.h | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
include/asm-generic/fixmap.h +10 -6
index 29cab7947980..2d1e9a2c6a3b 100644
@@ -16,6 +16,7 @@
 #define __ASM_GENERIC_FIXMAP_H
 
 #include <linux/bug.h>
+#include <linux/compiler.h>
 #include <linux/mm_types.h>
 
 #define __fix_to_virt(x)	(FIXADDR_TOP - ((x) << PAGE_SHIFT))
@@ -71,14 +72,17 @@ static inline unsigned long virt_to_fix(const unsigned long vaddr)
 #endif
 
 /* Return a pointer with offset calculated */
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
+#define ___set_fixmap_offset(idx, phys, flags, uniq)            \
+({                                                              \
+	unsigned long uniq;                                         \
+	__set_fixmap(idx, phys, flags);                             \
+	uniq = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));       \
+	uniq;                                                       \
 })
 
+#define __set_fixmap_offset(idx, phys, flags)                   \
+	___set_fixmap_offset(idx, phys, flags, __UNIQUE_ID(addr))
+
 #define set_fixmap_offset(idx, phys) \
 	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)
 
Signature · 1 line
2.50.1
#1 lore raw

Hi Arnd,

I'm sending a gentle ping regarding this patch to replace the hardcoded identifier in __set_fixmap_offset with __UNIQUE_ID(addr).

Please let me know if any changes are needed.

Thanks.

Min-Hsun Chang

#2 lore raw
In reply to Min-Hsun Chang
Quoted 4 lines · On Sat, 7 Mar 2026 17:21:19 +0800 Min-Hsun Chang <[email protected]> wrote:

The macro __set_fixmap_offset() uses a hardcoded identifier ________addr, which can lead to variable name shadowing if a caller happens to use the same name in its scope.

Quoted 8 lines

Following the pattern in commit 44238e82670e ("rcu: Replace ________p1 and _________p1 with __UNIQUE_ID(rcu)") and commit 589a9785ee3a ("min/max:
remove sparse warnings when they're nested"), replace the hardcoded identifier with __UNIQUE_ID(addr).

Since __UNIQUE_ID() must be expanded once to remain consistent across declaration, assignment, and return within the statement expression, introduce a nested helper macro ___set_fixmap_offset.

I'm not sure that is really relevant here - nested use is probably impossible.
It is almost certainly enough to prefix locals with an leading _ and an abbreviation of the macro name.

But see below.

Quoted 24 lines
---
 include/asm-generic/fixmap.h | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
include/asm-generic/fixmap.h +1 -2
index 29cab7947980..2d1e9a2c6a3b 100644
@@ -16,6 +16,7 @@
 #define __ASM_GENERIC_FIXMAP_H
 
 #include <linux/bug.h>
+#include <linux/compiler.h>
 #include <linux/mm_types.h>
 
 #define __fix_to_virt(x)	(FIXADDR_TOP - ((x) << PAGE_SHIFT))
@@ -71,14 +72,17 @@ static inline unsigned long virt_to_fix(const unsigned long vaddr)
 #endif
 
 /* Return a pointer with offset calculated */
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\

include/asm-generic/fixmap.h static inline unsigned long virt_to_fix(const unsigned long vaddr) · line 80

Quoted 10 lines
include/asm-generic/fixmap.h +6 -4
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
+#define ___set_fixmap_offset(idx, phys, flags, uniq)            \
+({                                                              \
+	unsigned long uniq;                                         \
+	__set_fixmap(idx, phys, flags);                             \
+	uniq = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));       \
+	uniq;                                                       \

You don't need a variable to hold the result at all.

The real problem with this define is that both idx and phys are expanded twice.
So you either need to copy those to locals or use a static inline function - which should be ok here because the types are fixed. (Assuming neither __set_fixmap() or fix_to_virt() are #defines that modify their arguments.)

David

Quoted 8 lines
include/asm-generic/fixmap.h +3 -0
 })
 
+#define __set_fixmap_offset(idx, phys, flags)                   \
+	___set_fixmap_offset(idx, phys, flags, __UNIQUE_ID(addr))
+
 #define set_fixmap_offset(idx, phys) \
 	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)
 
#3 lore raw

PATCH v2 1/1 asm-generic: convert __set_fixmap_offset() to static inline

The current macro implementation of __set_fixmap_offset() uses a hardcoded identifier ________addr within a statement expression. This can lead to variable shadowing and potential naming conflicts.

Furthermore, the macro expands its idx and phys arguments twice. This double evaluation is dangerous if arguments with side effects (e.g., i++) are passed to the macro.

Convert the macro to a static inline function as suggested by David. This improves type safety, avoids shadowing, and ensures each argument is evaluated exactly once.

---
Changes in v2:
- Convert the macro to a static inline function instead of just renaming the local variable (suggested by David Laight).
- This addresses both variable shadowing and double evaluation
issues pointed out during v1 review.
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 include/asm-generic/fixmap.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
include/asm-generic/fixmap.h +7 -7
index 29cab7947980..541b8db35af2 100644
@@ -71,13 +71,13 @@ static inline unsigned long virt_to_fix(const unsigned long vaddr)
 #endif
 
 /* Return a pointer with offset calculated */
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
-})
+static inline unsigned long __set_fixmap_offset(unsigned int idx,
+						phys_addr_t phys,
+						pgprot_t flags)
+{
+	__set_fixmap(idx, phys, flags);
+	return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
+}
 
 #define set_fixmap_offset(idx, phys) \
 	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)
Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to David Laight
Quoted 15 lines · On Sun, 22 Mar 2026 14:40:32 +0000 David Laight <[email protected]>…

Review

Quoted 12 lines
(quoted diff) +6 -6
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
+#define ___set_fixmap_offset(idx, phys, flags, uniq)            \
+({                                                              \
+	unsigned long uniq;                                         \
+	__set_fixmap(idx, phys, flags);                             \
+	uniq = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));       \
+	uniq;                                                       \

You don't need a variable to hold the result at all.

Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

What's wrong with the below?

/* Return a pointer with offset calculated */
static inline unsigned long
__set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)

{
	__set_fixmap(idx, phys, flags);
	return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
}

static inline unsigned long
set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys)

{
	return __set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL);
}

/*
* Some hardware wants to get fixmapped without caching. */
static inline void
set_fixmap_nocache(enum fixed_addresses idx, phys_addr_t phys)

{
	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE);
}

static inline void
set_fixmap_offset_nocache(enum fixed_addresses idx, phys_addr_t phys)

{
	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE);
}

I'll toss the below into mm.git, shall send it to Arnd if nothing blows up.

From: Andrew Morton <[email protected]>
Subject: include/asm-generic/fixmap.h: reimplement nasty macros in C
Date: Sat Apr 25 01:42:28 PM PDT 2026

Min-Hsun Chang reports[1] "the macro __set_fixmap_offset() uses a hardcoded identifier ________addr, which can lead to variable name shadowing if a caller happens to use the same name in its scope."

As is usual with macro messes, the answer is to reimplement everything in C.

---

 include/asm-generic/fixmap.h |   34 ++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
include/asm-generic/fixmap.h renamed from include/asm-generic/fixmap.h~a +21 -13
@@ -71,25 +71,33 @@ static inline unsigned long virt_to_fix(
 #endif
 
 /* Return a pointer with offset calculated */
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
-})
+static inline unsigned long
+__set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
+{
+	__set_fixmap(idx, phys, flags);
+	return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
+}
 
-#define set_fixmap_offset(idx, phys) \
-	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)
+static inline unsigned long
+set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys)
+{
+	return __set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL);
+}
 
 /*
  * Some hardware wants to get fixmapped without caching.
  */
-#define set_fixmap_nocache(idx, phys) \
-	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE)
+static inline void
+set_fixmap_nocache(enum fixed_addresses idx, phys_addr_t phys)
+{
+	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE);
+}
 
-#define set_fixmap_offset_nocache(idx, phys) \
-	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE)
+static inline void
+set_fixmap_offset_nocache(enum fixed_addresses idx, phys_addr_t phys)
+{
+	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE);
+}
 
 /*
  * Some fixmaps are for IO

_

#5 lore raw
Quoted 17 lines · On Sat, 25 Apr 2026 13:57:37 -0700 Andrew Morton <[email protected]>…
Quoted 15 lines · On Sun, 22 Mar 2026 14:40:32 +0000 David Laight <[email protected]>…

include/asm-generic/fixmap.h static inline unsigned long virt_to_fix(

Quoted 12 lines
include/asm-generic/fixmap.h renamed from include/asm-generic/fixmap.h~a +6 -6
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
+#define ___set_fixmap_offset(idx, phys, flags, uniq)            \
+({                                                              \
+	unsigned long uniq;                                         \
+	__set_fixmap(idx, phys, flags);                             \
+	uniq = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));       \
+	uniq;                                                       \  

You don't need a variable to hold the result at all.

Quoted 5 lines
Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

Sometimes #defines generate better code because they are expanded earlier, and sometimes you want type-agnostic 'functions'.
But neither is true here.

But I think I'd go for 'always_inline'.
Sometimes the compilers make silly decisions.

David

Quoted 111 lines

What's wrong with the below?

/* Return a pointer with offset calculated */
static inline unsigned long
__set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)

{
	__set_fixmap(idx, phys, flags);
	return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
}

static inline unsigned long
set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys)

{
	return __set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL);
}

/*
* Some hardware wants to get fixmapped without caching. */
static inline void
set_fixmap_nocache(enum fixed_addresses idx, phys_addr_t phys)

{
	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE);
}

static inline void
set_fixmap_offset_nocache(enum fixed_addresses idx, phys_addr_t phys)

{
	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE);
}

I'll toss the below into mm.git, shall send it to Arnd if nothing blows up.

From: Andrew Morton <[email protected]>
Subject: include/asm-generic/fixmap.h: reimplement nasty macros in C
Date: Sat Apr 25 01:42:28 PM PDT 2026

Min-Hsun Chang reports[1] "the macro __set_fixmap_offset() uses a hardcoded identifier ________addr, which can lead to variable name shadowing if a caller happens to use the same name in its scope."

As is usual with macro messes, the answer is to reimplement everything in C.

---

 include/asm-generic/fixmap.h |   34 ++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
include/asm-generic/fixmap.h renamed from include/asm-generic/fixmap.h~a +21 -13
@@ -71,25 +71,33 @@ static inline unsigned long virt_to_fix(
 #endif
 
 /* Return a pointer with offset calculated */
-#define __set_fixmap_offset(idx, phys, flags)				\
-({									\
-	unsigned long ________addr;					\
-	__set_fixmap(idx, phys, flags);					\
-	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
-	________addr;							\
-})
+static inline unsigned long
+__set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys, pgprot_t flags)
+{
+	__set_fixmap(idx, phys, flags);
+	return fix_to_virt(idx) + (phys & (PAGE_SIZE - 1));
+}
 
-#define set_fixmap_offset(idx, phys) \
-	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)
+static inline unsigned long
+set_fixmap_offset(enum fixed_addresses idx, phys_addr_t phys)
+{
+	return __set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL);
+}
 
 /*
  * Some hardware wants to get fixmapped without caching.
  */
-#define set_fixmap_nocache(idx, phys) \
-	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE)
+static inline void
+set_fixmap_nocache(enum fixed_addresses idx, phys_addr_t phys)
+{
+	__set_fixmap(idx, phys, FIXMAP_PAGE_NOCACHE);
+}
 
-#define set_fixmap_offset_nocache(idx, phys) \
-	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE)
+static inline void
+set_fixmap_offset_nocache(enum fixed_addresses idx, phys_addr_t phys)
+{
+	__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NOCACHE);
+}
 
 /*
  * Some fixmaps are for IO

_

maintainer
#6 lore raw
Quoted 6 lines · On Sat, 25 Apr 2026 23:01:34 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

Quoted 6 lines

Sometimes #defines generate better code because they are expanded earlier, and sometimes you want type-agnostic 'functions'.
But neither is true here.

But I think I'd go for 'always_inline'.
Sometimes the compilers make silly decisions.

Gee, if `static inline' misbehaves then we have big problems!

What's special about the fixmap code anyway? It's not exactly fastpath. Perhaps this stuff can simply be uninlined.

Pet peeves:

We use too many macros.

We inline too much stuff.

#7 lore raw
Quoted 12 lines · On Sat, 25 Apr 2026 15:12:40 -0700 Andrew Morton <[email protected]>…
Quoted 10 lines · On Sat, 25 Apr 2026 23:01:34 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

Sometimes #defines generate better code because they are expanded earlier, and sometimes you want type-agnostic 'functions'.
But neither is true here.

Quoted 7 lines
Quoted 2 lines

But I think I'd go for 'always_inline'.
Sometimes the compilers make silly decisions.

Gee, if `static inline' misbehaves then we have big problems!

What's special about the fixmap code anyway? It's not exactly fastpath. Perhaps this stuff can simply be uninlined.

Some of the inlines are trivial - just adding an extra parameter. But this set would be simpler if the last function (__native_set_fixmap() for x86) returned (address & ~PAGE_MASK)

Quoted 4 lines

Pet peeves:

We use too many macros.

Indeed, I get fed up of looking things up to find they are trivial. Some of the 'helpers' do nothing for core readability. Even things like BIT() can have unexpected consequences.

Not to say I haven't got the pre-processor to do odd things in the past. But it is usually trying to avoid having to keep multiple definitions aligned with each other.

Quoted 1 line

We inline too much stuff.

True - don't look at what strlcpy() can generate.
The inline code should just get the constants from the compiler and then call the appropriate function.

As does the compiler.
pixpaper_panel_hw_init() repeatedly calls two static functions that contain sleeps. They all get inlined bloating the code size and exploding the stack when clang separately allocates all the buffers in the called functions.

David

maintainer
#8 lore raw
Quoted 18 lines · On Sun, 26 Apr 2026 11:49:38 +0100 David Laight <[email protected]>…
Quoted 15 lines · On Sat, 25 Apr 2026 15:12:40 -0700 Andrew Morton <[email protected]>…
Quoted 12 lines · On Sat, 25 Apr 2026 23:01:34 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

Sometimes #defines generate better code because they are expanded earlier, and sometimes you want type-agnostic 'functions'.
But neither is true here.

But I think I'd go for 'always_inline'.
Sometimes the compilers make silly decisions.

Quoted 8 lines
Quoted 4 lines

Gee, if `static inline' misbehaves then we have big problems!

What's special about the fixmap code anyway? It's not exactly fastpath. Perhaps this stuff can simply be uninlined.

Some of the inlines are trivial - just adding an extra parameter. But this set would be simpler if the last function (__native_set_fixmap() for x86) returned (address & ~PAGE_MASK)

I don't understand this?

Quoted 8 lines
Quoted 4 lines

Pet peeves:

We use too many macros.

Indeed, I get fed up of looking things up to find they are trivial. Some of the 'helpers' do nothing for core readability. Even things like BIT() can have unexpected consequences.

Sure.

Quoted 9 lines

Not to say I haven't got the pre-processor to do odd things in the past. But it is usually trying to avoid having to keep multiple definitions aligned with each other.

Quoted 1 line

We inline too much stuff.

True - don't look at what strlcpy() can generate.
The inline code should just get the constants from the compiler and then call the appropriate function.

I can't actually find an in-kernel strlcpy()?

Quoted 5 lines

As does the compiler.
pixpaper_panel_hw_init() repeatedly calls two static functions that contain sleeps. They all get inlined bloating the code size and exploding the stack when clang separately allocates all the buffers in the called functions.

Poor thing. I guess compiler developers are more focused on userspace code and aren't especially concerned about kernel's particular requirements. And that's understandable - it's down to us to persuade them to add options to permit us to tune the compiler behavior.

#9 lore raw
Quoted 22 lines · On Sun, 26 Apr 2026 04:09:25 -0700 Andrew Morton <[email protected]>…
Quoted 20 lines · On Sun, 26 Apr 2026 11:49:38 +0100 David Laight <[email protected]>…
Quoted 17 lines · On Sat, 25 Apr 2026 15:12:40 -0700 Andrew Morton <[email protected]>…
Quoted 6 lines · On Sat, 25 Apr 2026 23:01:34 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 2 lines

The real problem with this define is that both idx and phys are expanded twice.

The real problem with this define is that it's a define. Why oh why do we keep doing this to ourselves?

Quoted 6 lines

Sometimes #defines generate better code because they are expanded earlier, and sometimes you want type-agnostic 'functions'.
But neither is true here.

But I think I'd go for 'always_inline'.
Sometimes the compilers make silly decisions.

Gee, if `static inline' misbehaves then we have big problems!

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

What's special about the fixmap code anyway? It's not exactly fastpath. Perhaps this stuff can simply be uninlined.

Some of the inlines are trivial - just adding an extra parameter. But this set would be simpler if the last function (__native_set_fixmap() for x86) returned (address & ~PAGE_MASK)

I don't understand this?

I found the following:

#define set_fixmap_offset(idx, phys) \
__set_fixmap_offset(idx, phys, FIXMAP_PAGE_NORMAL)

#define __set_fixmap_offset(idx, phys, flags) \

({									\
	unsigned long ________addr;					\
	__set_fixmap(idx, phys, flags);					\
	________addr = fix_to_virt(idx) + ((phys) & (PAGE_SIZE - 1));	\
	________addr;							\
})

static inline void __set_fixmap(enum fixed_addresses idx,
phys_addr_t phys, pgprot_t flags)

{
	native_set_fixmap(idx, phys, flags);
}

void native_set_fixmap(unsigned /* enum fixed_addresses */ idx,
phys_addr_t phys, pgprot_t flags)

{
	/* Sanitize 'prot' against any unsupported bits: */
	pgprot_val(flags) &= __default_kernel_pte_mask;

	__native_set_fixmap(idx, pfn_pte(phys >> PAGE_SHIFT, flags));
}

void __native_set_fixmap(enum fixed_addresses idx, pte_t pte)

{
	unsigned long address = __fix_to_virt(idx);
#ifdef CONFIG_X86_64
       /*
	* Ensure that the static initial page tables are covering the
	* fixmap completely.
	*/
	BUILD_BUG_ON(__end_of_permanent_fixed_addresses >
		     (FIXMAP_PMD_NUM * PTRS_PER_PTE));

#endif

	if (idx >= __end_of_fixed_addresses) {
		BUG();
		return;
	}
	set_pte_vaddr(address, pte);
	fixmaps_set++;
}

Doesn't seem to hard to arrange to return 'address + (phys & ~PAGE_SHIFT)' from __set_fixmap().
(Other architectures will vary, but I suspect they are similar.)

...

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

We inline too much stuff.

True - don't look at what strlcpy() can generate.
The inline code should just get the constants from the compiler and then call the appropriate function.

I can't actually find an in-kernel strlcpy()?

That is because I meant strscpy() :-)

In particular https://elixir.bootlin.com/linux/v7.0/source/include/linux/fortify-string.h#L275
Particularly once it gets as far as calling strnlen().

Quoted 11 lines
Quoted 6 lines

As does the compiler.
pixpaper_panel_hw_init() repeatedly calls two static functions that contain sleeps. They all get inlined bloating the code size and exploding the stack when clang separately allocates all the buffers in the called functions.

Poor thing. I guess compiler developers are more focused on userspace code and aren't especially concerned about kernel's particular requirements. And that's understandable - it's down to us to persuade them to add options to permit us to tune the compiler behavior.

That would be wrong for for userspace as well. Inlining and loop unrolling decisions don't seem to allow for the cost of reading code from memory into the I-cache. (Loop unrolling is so 1980s)

My suspicion is that very few writes of user code ever look at the generated code.
So the compiler developers go their own way optimising things that improve some benchmarks but make more normal programs run a bit slower.
Of course, modern cpu are fast enough that it doesn't matter what is generated for most code.
(end rant)

David

maintainer
#10 lore raw
Quoted 10 lines · On Sun, 26 Apr 2026 18:34:20 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 3 lines

True - don't look at what strlcpy() can generate.
The inline code should just get the constants from the compiler and then call the appropriate function.

I can't actually find an in-kernel strlcpy()?

That is because I meant strscpy() :-)

In particular https://elixir.bootlin.com/linux/v7.0/source/include/linux/fortify-string.h#L275
Particularly once it gets as far as calling strnlen().

Oh geeze, why did we do that. This is the kernel - string functions aren't fastpath! Someone please move all that gunk into lib/string.c!

We'll still need some macro if we are to retain the (imo silly) omit-arg3-if-its-an-array trick.

#11 lore raw
Quoted 8 lines · On Sun, 26 Apr 2026 11:09:32 -0700 Andrew Morton <[email protected]>…
Quoted 6 lines · On Sun, 26 Apr 2026 18:34:20 +0100 David Laight <[email protected]>…
Quoted 5 lines
Quoted 3 lines

True - don't look at what strlcpy() can generate.
The inline code should just get the constants from the compiler and then call the appropriate function.

I can't actually find an in-kernel strlcpy()?

Quoted 7 lines
Quoted 4 lines

That is because I meant strscpy() :-)

In particular https://elixir.bootlin.com/linux/v7.0/source/include/linux/fortify-string.h#L275
Particularly once it gets as far as calling strnlen().

Oh geeze, why did we do that. This is the kernel - string functions aren't fastpath! Someone please move all that gunk into lib/string.c!

It might be worth selecting memcpy() for fixed length copies that fit. Especially since that can get inlined to something smaller than the call.

Quoted 2 lines

We'll still need some macro if we are to retain the (imo silly) omit-arg3-if-its-an-array trick.

Try: strscpy(tgt, src, len, spare_argument)
I'm pretty sure it compiles fine.

David