The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] tmpfs: reset the memory policy on remount with mpol=default
@ 2026-11-27 23:00 Yifan Feng
  2026-11-28  0:10 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Yifan Feng @ 2026-11-27 23:00 UTC (permalink / raw)
  To: Hugh Dickins, David Hildenbrand, Andrew Morton
  Cc: Baolin Wang, Greg Thelen, linux-mm, linux-kernel, Yifan Feng

mpol_parse_str() returns a NULL policy for "default", and
shmem_reconfigure() takes a NULL ctx->mpol to mean that no mpol option
was given.  So a remount with mpol=default succeeds but keeps the old
policy:

  # mount -t tmpfs -o mpol=bind:1 none /mnt
  # mount -o remount,mpol=default /mnt
  # grep /mnt /proc/mounts
  none /mnt tmpfs rw,relatime,mpol=bind:1 0 0

Record that an mpol option was parsed, and replace the policy whenever
one was.

Fixes: 5f00110f7273 ("tmpfs: fix use-after-free of mempolicy object")
Signed-off-by: Yifan Feng <evanfengff@gmail.com>
---
Tested in a 2-node NUMA QEMU guest: after remount,mpol=default new files are allocated on the node of the writing task, and a remount without mpol still keeps the policy. KASAN and kmemleak report nothing over 1500 remounts.
Found and written with the help of an AI coding assistant.

 mm/shmem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/shmem.c b/mm/shmem.c
index a5713109c843..48d1eec43007 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -134,6 +134,7 @@ struct shmem_options {
 #define SHMEM_SEEN_HUGE 4
 #define SHMEM_SEEN_INUMS 8
 #define SHMEM_SEEN_QUOTA 16
+#define SHMEM_SEEN_MPOL 32
 };
 
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -4905,6 +4906,7 @@ static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
 			ctx->mpol = NULL;
 			if (mpol_parse_str(param->string, &ctx->mpol))
 				goto bad_value;
+			ctx->seen |= SHMEM_SEEN_MPOL;
 			break;
 		}
 		goto unsupported_parameter;
@@ -5117,7 +5119,7 @@ static int shmem_reconfigure(struct fs_context *fc)
 	/*
 	 * Preserve previous mempolicy unless mpol remount option was specified.
 	 */
-	if (ctx->mpol) {
+	if (ctx->seen & SHMEM_SEEN_MPOL) {
 		mpol = sbinfo->mpol;
 		sbinfo->mpol = ctx->mpol;	/* transfers initial ref */
 		ctx->mpol = NULL;

base-commit: 822ba3a89c7896b26e4beb70e2ebffb881924dfe
-- 
2.39.3 (Apple Git-146)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tmpfs: reset the memory policy on remount with mpol=default
  2026-11-27 23:00 [PATCH] tmpfs: reset the memory policy on remount with mpol=default Yifan Feng
@ 2026-11-28  0:10 ` Andrew Morton
  2026-11-28 19:03   ` Yifan Feng
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-11-28  0:10 UTC (permalink / raw)
  To: Yifan Feng
  Cc: Hugh Dickins, David Hildenbrand, Baolin Wang, Greg Thelen,
	linux-mm, linux-kernel

On Fri, 27 Nov 2026 15:00:15 -0800 Yifan Feng <evanfengff@gmail.com> wrote:

> mpol_parse_str() returns a NULL policy for "default", and
> shmem_reconfigure() takes a NULL ctx->mpol to mean that no mpol option
> was given.  So a remount with mpol=default succeeds but keeps the old
> policy:
> 
>   # mount -t tmpfs -o mpol=bind:1 none /mnt
>   # mount -o remount,mpol=default /mnt
>   # grep /mnt /proc/mounts
>   none /mnt tmpfs rw,relatime,mpol=bind:1 0 0
> 
> Record that an mpol option was parsed, and replace the policy whenever
> one was.

Oh.

So aiui, remounting with mpol=default "succeeds", but new file
policies still get the old mempolicy instead of using the "process
allocation policy"?

hp2:/usr/src/mm> egrep -r "mempolicy|mpol" tools/testing/selftests/mm
tools/testing/selftests/mm/mkdirty.c:#include <linux/mempolicy.h>
hp2:/usr/src/mm> 

Oh.

> Fixes: 5f00110f7273 ("tmpfs: fix use-after-free of mempolicy object")

+cc:stable.  Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tmpfs: reset the memory policy on remount with mpol=default
  2026-11-28  0:10 ` Andrew Morton
@ 2026-11-28 19:03   ` Yifan Feng
  2026-11-28 19:33     ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Yifan Feng @ 2026-11-28 19:03 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Hugh Dickins, David Hildenbrand, Baolin Wang, Greg Thelen,
	linux-mm, linux-kernel

On Fri, 27 Nov 2026 16:10:49 -0800 Andrew Morton <akpm@linux-foundation.org> wrote:

> So aiui, remounting with mpol=default "succeeds", but new file
> policies still get the old mempolicy instead of using the "process
> allocation policy"?

Yes. The remount returns 0, /proc/mounts keeps the old mpol=, and
files created after it are still allocated by the old policy. In a
2-node guest, after mount mpol=interleave:0-1 and remount,mpol=default,
a new 32 MB file was still 16 MB on each node. With the patch it is
all on the node of the writing task.

> hp2:/usr/src/mm> egrep -r "mempolicy|mpol" tools/testing/selftests/mm

I can write a selftest for the tmpfs mpol= option if that is welcome.

Thanks,
Yifan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] tmpfs: reset the memory policy on remount with mpol=default
  2026-11-28 19:03   ` Yifan Feng
@ 2026-11-28 19:33     ` Andrew Morton
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-11-28 19:33 UTC (permalink / raw)
  To: Yifan Feng
  Cc: Hugh Dickins, David Hildenbrand, Baolin Wang, Greg Thelen,
	linux-mm, linux-kernel

On Sat, 28 Nov 2026 11:03:02 -0800 Yifan Feng <evanfengff@gmail.com> wrote:

> > hp2:/usr/src/mm> egrep -r "mempolicy|mpol" tools/testing/selftests/mm
> 
> I can write a selftest for the tmpfs mpol= option if that is welcome.

That sounds useful, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-28 19:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 23:00 [PATCH] tmpfs: reset the memory policy on remount with mpol=default Yifan Feng
2026-11-28  0:10 ` Andrew Morton
2026-11-28 19:03   ` Yifan Feng
2026-11-28 19:33     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox