Skip to contentSkip to conversation

mm/rmap: add CONFIG_MM_ID guard for folio_test_large_maybe_mapped_shared()

PATCH RESEND In review Lance Yang 4 participants 10 messages Apr 18, 2025 – Apr 22, 2025

Subsystems (2)
lore
Share & tools
lore

Lance Yang

author
#0 lore raw

PATCH RESEND v2 1/1 mm/rmap: add CONFIG_MM_ID guard for folio_test_large_maybe_mapped_shared()

From: Lance Yang <[email protected]>

To prevent folio_test_large_maybe_mapped_shared() from being used without CONFIG_MM_ID, we add a compile-time check rather than wrapping it in '#ifdef', avoiding even more #ifdef in callers that already use IS_ENABLED(CONFIG_MM_ID).

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of '#ifdef' mess.

---
v1 -> v2:
* Update the changelog, suggested by Andrew and David
* https://lore.kernel.org/linux-mm/[email protected]

 include/linux/page-flags.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/page-flags.h +2 -0
index d3909cb1e576..6bd9b9043976 100644
@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)
 
 static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)
 {
+	/* This function should never be called without CONFIG_MM_ID enabled. */
+	BUILD_BUG_ON(!IS_ENABLED(CONFIG_MM_ID));
 	return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids);
 }
 #undef PF_ANY
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 9 lines · On Fri, 18 Apr 2025 23:22:28 +0800 Lance Yang <[email protected]> wrote:

From: Lance Yang <[email protected]>

To prevent folio_test_large_maybe_mapped_shared() from being used without CONFIG_MM_ID, we add a compile-time check rather than wrapping it in '#ifdef', avoiding even more #ifdef in callers that already use IS_ENABLED(CONFIG_MM_ID).

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of '#ifdef' mess.

I dunno, this just seems really whacky.

include/linux/page-flags.h static inline int folio_has_private(const struct folio *folio) · line 1235

Quoted 7 lines
include/linux/page-flags.h +1 -0
@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)
 
 static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)
 {
+	/* This function should never be called without CONFIG_MM_ID enabled. */

A correcter comment would be "This function should never be compiled without CONFIG_MM_ID enabled".

Which lets the cat out of the bag. Why the heck is it being compiled with CONFIG_MM_ID=n?? We have tools to prevent that.

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It should have no callers, right? If the linker ends up complaining then something went wrong.

#2 lore raw
Quoted 18 lines · Andrew Morton
Quoted 15 lines · On Fri, 18 Apr 2025 23:22:28 +0800 Lance Yang <[email protected]> wrote:

From: Lance Yang <[email protected]>

To prevent folio_test_large_maybe_mapped_shared() from being used without

CONFIG_MM_ID, we add a compile-time check rather than wrapping it in

'#ifdef', avoiding even more #ifdef in callers that already use

IS_ENABLED(CONFIG_MM_ID).

Quoted 6 lines
Quoted 4 lines

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of

'#ifdef' mess.

I dunno, this just seems really whacky.

I'd hope David could leave some comments on that.

Quoted 12 lines
Quoted 12 lines

--- a/include/linux/page-flags.h

+++ b/include/linux/page-flags.h

@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)

static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)

Quoted 8 lines
Quoted 4 lines

{

+ /* This function should never be called without CONFIG_MM_ID enabled. */

A correcter comment would be "This function should never be compiled

without CONFIG_MM_ID enabled".

Yes, that is more exact ;)

Quoted 10 lines

Which lets the cat out of the bag. Why the heck is it being compiled

with CONFIG_MM_ID=n?? We have tools to prevent that.

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared() is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

If we do, with CONFIG_TRANSPARENT_HUGEPAGE=N, we'll hit compilation errors like:

./include/linux/mm.h: In function ‘folio_maybe_mapped_shared’:
./include/linux/mm.h:2337:16: error: implicit declaration of function ‘folio_test_large_maybe_mapped_shared’; did you mean ‘folio_maybe_mapped_shared’? [-Werror=implicit-function-declaration]

 2337 |         return folio_test_large_maybe_mapped_shared(folio);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                folio_maybe_mapped_shared

cc1: some warnings being treated as errors

Thanks,
Lance

Quoted 1 line
#3 lore raw
Quoted 110 lines · Lance Yang
Quoted 39 lines · Andrew Morton

On Fri, 18 Apr 2025 23:22:28 +0800 Lance Yang <[email protected]> wrote:

From: Lance Yang <[email protected]>

To prevent folio_test_large_maybe_mapped_shared() from being used without

CONFIG_MM_ID, we add a compile-time check rather than wrapping it in

'#ifdef', avoiding even more #ifdef in callers that already use

IS_ENABLED(CONFIG_MM_ID).

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of

Quoted 8 lines

'#ifdef' mess.

I dunno, this just seems really whacky.

I'd hope David could leave some comments on that.

Quoted 31 lines

--- a/include/linux/page-flags.h

+++ b/include/linux/page-flags.h

@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)

static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)

{

+ /* This function should never be called without CONFIG_MM_ID enabled. */

Quoted 6 lines

A correcter comment would be "This function should never be compiled

without CONFIG_MM_ID enabled".

Yes, that is more exact ;)

Quoted 17 lines

Which lets the cat out of the bag. Why the heck is it being compiled

with CONFIG_MM_ID=n?? We have tools to prevent that.

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

Quoted 6 lines
Quoted 2 lines

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared()

is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

static inline bool folio_maybe_mapped_shared(struct folio *folio)

{
        [...]
        if (!IS_ENABLED(CONFIG_MM_ID))
                return true;
        [...]
        return folio_test_large_maybe_mapped_shared(folio);
}

folio_maybe_mapped_shared() is always available - doesn't depend on CONFIG_MM_ID or CONFIG_TRANSPARENT_HUGEPAGE.

Thanks,
Lance

Quoted 22 lines

If we do, with CONFIG_TRANSPARENT_HUGEPAGE=N, we'll hit compilation errors like:

./include/linux/mm.h: In function ‘folio_maybe_mapped_shared’:

./include/linux/mm.h:2337:16: error: implicit declaration of function ‘folio_test_large_maybe_mapped_shared’; did you mean ‘folio_maybe_mapped_shared’? [-Werror=implicit-function-declaration]

2337 | return folio_test_large_maybe_mapped_shared(folio);

| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| folio_maybe_mapped_shared

cc1: some warnings being treated as errors

Thanks,

Lance

Quoted 3 lines
#4 lore raw
Quoted 130 lines · Lance Yang
Quoted 110 lines · Lance Yang
Quoted 39 lines · Andrew Morton

On Fri, 18 Apr 2025 23:22:28 +0800 Lance Yang <[email protected]> wrote:

From: Lance Yang <[email protected]>

To prevent folio_test_large_maybe_mapped_shared() from being used without

CONFIG_MM_ID, we add a compile-time check rather than wrapping it in

'#ifdef', avoiding even more #ifdef in callers that already use

IS_ENABLED(CONFIG_MM_ID).

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of

Quoted 8 lines

'#ifdef' mess.

I dunno, this just seems really whacky.

I'd hope David could leave some comments on that.

Quoted 31 lines

--- a/include/linux/page-flags.h

+++ b/include/linux/page-flags.h

@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)

static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)

  {

+ /* This function should never be called without CONFIG_MM_ID enabled. */

Quoted 6 lines

A correcter comment would be "This function should never be compiled

without CONFIG_MM_ID enabled".

Yes, that is more exact ;)

Quoted 17 lines

Which lets the cat out of the bag. Why the heck is it being compiled

with CONFIG_MM_ID=n?? We have tools to prevent that.

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

Quoted 6 lines
Quoted 2 lines

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared()

is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

static inline bool folio_maybe_mapped_shared(struct folio *folio)

{
         [...]
         if (!IS_ENABLED(CONFIG_MM_ID))
                 return true;
         [...]
         return folio_test_large_maybe_mapped_shared(folio);
}
Quoted 2 lines

folio_maybe_mapped_shared() is always available - doesn't depend on CONFIG_MM_ID or CONFIG_TRANSPARENT_HUGEPAGE.

We could #ifdef in folio_maybe_mapped_shared(), which I find rather suboptimal ...

or simply inline it into the 4 callers.

That might be the best approach, given that only selected user should be using the low-level primitive and everybody else should be using folio_maybe_mapped_shared().

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw
Quoted 144 lines · David Hildenbrand
Quoted 141 lines · Lance Yang
Quoted 5 lines · Lance Yang
Quoted 1 line
Quoted 1 line

April 21, 2025 at 7:29 AM, "Andrew Morton" <[email protected]> wrote:

Quoted 3 lines · On Fri, 18 Apr 2025 23:22:28 +0800 Lance Yang <[email protected]> wrote:
Quoted 3 lines
Quoted 3 lines

From: Lance Yang <[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

To prevent folio_test_large_maybe_mapped_shared() from being used without

Quoted 1 line
Quoted 1 line
Quoted 1 line

CONFIG_MM_ID, we add a compile-time check rather than wrapping it in

Quoted 1 line
Quoted 1 line
Quoted 1 line

'#ifdef', avoiding even more #ifdef in callers that already use

Quoted 1 line
Quoted 1 line
Quoted 1 line

IS_ENABLED(CONFIG_MM_ID).

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Also, we used plenty of IS_ENABLED() on purpose to keep the code free of

Quoted 1 line
Quoted 1 line
Quoted 1 line

'#ifdef' mess.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

I dunno, this just seems really whacky.

Quoted 3 lines

I'd hope David could leave some comments on that.

--- a/include/linux/page-flags.h

Quoted 1 line
Quoted 1 line
Quoted 1 line

+++ b/include/linux/page-flags.h

Quoted 1 line
Quoted 1 line
Quoted 1 line

@@ -1232,6 +1232,8 @@ static inline int folio_has_private(const struct folio *folio)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)

Quoted 1 line
Quoted 1 line
Quoted 1 line

{

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ /* This function should never be called without CONFIG_MM_ID enabled. */

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

A correcter comment would be "This function should never be compiled

Quoted 1 line
Quoted 1 line
Quoted 1 line

without CONFIG_MM_ID enabled".

Quoted 3 lines

Yes, that is more exact ;)

Which lets the cat out of the bag. Why the heck is it being compiled

Quoted 1 line
Quoted 1 line
Quoted 1 line

with CONFIG_MM_ID=n?? We have tools to prevent that.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

Quoted 1 line
Quoted 1 line
Quoted 1 line

should have no callers, right? If the linker ends up complaining then

Quoted 1 line
Quoted 1 line
Quoted 1 line

something went wrong.

Quoted 5 lines

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared()

is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

static inline bool folio_maybe_mapped_shared(struct folio *folio)

{

[...]

if (!IS_ENABLED(CONFIG_MM_ID))

return true;

[...]

return folio_test_large_maybe_mapped_shared(folio);

}

folio_maybe_mapped_shared() is always available - doesn't depend on

Quoted 8 lines
Quoted 2 lines

CONFIG_MM_ID or CONFIG_TRANSPARENT_HUGEPAGE.

We could #ifdef in folio_maybe_mapped_shared(), which I find rather suboptimal ...

or simply inline it into the 4 callers.

That might be the best approach, given that only selected user should be using the low-level primitive and everybody else should be using folio_maybe_mapped_shared().

Yep, I tend to prefer the second approach as well, given there are only four callers. Andrew, wdyt?

Thanks,
Lance

Quoted 5 lines

-- Cheers,

David / dhildenb

maintainer
#6 lore raw
In reply to Lance Yang
Quoted 9 lines · On Mon, 21 Apr 2025 05:13:03 +0000 "Lance Yang" <[email protected]> wrote:
Quoted 5 lines

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared() is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

Quoted 8 lines

If we do, with CONFIG_TRANSPARENT_HUGEPAGE=N, we'll hit compilation errors like:

./include/linux/mm.h: In function ‘folio_maybe_mapped_shared’:
./include/linux/mm.h:2337:16: error: implicit declaration of function ‘folio_test_large_maybe_mapped_shared’; did you mean ‘folio_maybe_mapped_shared’? [-Werror=implicit-function-declaration]

 2337 |         return folio_test_large_maybe_mapped_shared(folio);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                folio_maybe_mapped_shared

cc1: some warnings being treated as errors

That's OK - provide a declaration of folio_maybe_mapped_shared() but no definition. So the compiled-out code can be compiled and the linker will confirm that it's never actually called.

#7 lore raw
Quoted 36 lines · Andrew Morton
Quoted 33 lines · On Mon, 21 Apr 2025 05:13:03 +0000 "Lance Yang" <[email protected]> wrote:

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared()

is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

If we do, with CONFIG_TRANSPARENT_HUGEPAGE=N, we'll hit compilation errors like:

./include/linux/mm.h: In function ‘folio_maybe_mapped_shared’:

./include/linux/mm.h:2337:16: error: implicit declaration of function ‘folio_test_large_maybe_mapped_shared’; did you mean ‘folio_maybe_mapped_shared’? [-Werror=implicit-function-declaration]

2337 | return folio_test_large_maybe_mapped_shared(folio);

| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| folio_maybe_mapped_shared

Quoted 9 lines
Quoted 2 lines

cc1: some warnings being treated as errors

That's OK - provide a declaration of folio_maybe_mapped_shared() but no

definition. So the compiled-out code can be compiled and the linker

will confirm that it's never actually called.

Got it, that works as well ;)

So if David is cool with it, I'll send out the new version like this:

include/linux/page-flags.h +5 -0
index d3909cb1e576..a762e4b4eab4 100644
@@ -1230,10 +1230,15 @@ static inline int folio_has_private(const struct folio *folio)
 	return !!(folio->flags & PAGE_FLAGS_PRIVATE);
 }
 
+#ifdef CONFIG_MM_ID
 static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)
 {
 	return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids);
 }
+#else
+bool folio_test_large_maybe_mapped_shared(const struct folio *folio);
+#endif
+
 

Thanks,
Lance

#8 lore raw
Quoted 60 lines · Lance Yang
Quoted 36 lines · Andrew Morton
Quoted 33 lines · On Mon, 21 Apr 2025 05:13:03 +0000 "Lance Yang" <[email protected]> wrote:

Can we just slap "#ifdef CONFIG_MM_ID" around the whole function? It

should have no callers, right? If the linker ends up complaining then

something went wrong.

The reason we can't simply add #ifdef CONFIG_MM_ID around folio_test_large_maybe_mapped_shared()

is because its caller folio_maybe_mapped_shared() relies on IS_ENABLED(CONFIG_MM_ID).

If we do, with CONFIG_TRANSPARENT_HUGEPAGE=N, we'll hit compilation errors like:

./include/linux/mm.h: In function ‘folio_maybe_mapped_shared’:

./include/linux/mm.h:2337:16: error: implicit declaration of function ‘folio_test_large_maybe_mapped_shared’; did you mean ‘folio_maybe_mapped_shared’? [-Werror=implicit-function-declaration]

  2337 | return folio_test_large_maybe_mapped_shared(folio);

| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

| folio_maybe_mapped_shared

Quoted 9 lines
Quoted 2 lines

cc1: some warnings being treated as errors

That's OK - provide a declaration of folio_maybe_mapped_shared() but no

definition. So the compiled-out code can be compiled and the linker

will confirm that it's never actually called.

Got it, that works as well ;)

So if David is cool with it, I'll send out the new version like this:

include/linux/page-flags.h +0 -0
index d3909cb1e576..a762e4b4eab4 100644
@@ -1230,10 +1230,15 @@ static inline int folio_has_private(const struct folio *folio)
  	return !!(folio->flags & PAGE_FLAGS_PRIVATE);

include/linux/page-flags.h static inline int folio_has_private(const struct folio *folio) · line 1240

Quoted 10 lines
include/linux/page-flags.h +4 -0
  }
 
+#ifdef CONFIG_MM_ID
  static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)
  {
  	return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids);
  }
+#else
+bool folio_test_large_maybe_mapped_shared(const struct folio *folio);
+#endif

Fine with me. At this point, I do prefer inlining the function, though.

Signature · 3 lines
Cheers,

David / dhildenb
maintainer
#9 lore raw
Quoted 6 lines · On Tue, 22 Apr 2025 09:00:54 +0200 David Hildenbrand <[email protected]> wrote:
Quoted 6 lines
include/linux/page-flags.h +0 -0
@@ -1230,10 +1230,15 @@ static inline int folio_has_private(const struct folio *folio)
  	return !!(folio->flags & PAGE_FLAGS_PRIVATE);
  }
 
Quoted 10 lines

include/linux/page-flags.h static inline int folio_has_private(const struct folio *folio) · line 1240

Quoted 8 lines
include/linux/page-flags.h +4 -0
+#ifdef CONFIG_MM_ID
  static inline bool folio_test_large_maybe_mapped_shared(const struct folio *folio)
  {
  	return test_bit(FOLIO_MM_IDS_SHARED_BITNUM, &folio->_mm_ids);
  }
+#else
+bool folio_test_large_maybe_mapped_shared(const struct folio *folio);
+#endif

Fine with me. At this point, I do prefer inlining the function, though.

The above has the advantage that if the compiler unintendedly emits a call to folio_test_large_maybe_mapped_shared(), the link will fail.

I do think a little comment which explains this trick is needed.