All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction
@ 2026-11-30  3:20 Baek Kyu, Kim
  2026-11-30  6:56 ` Mike Rapoport
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Baek Kyu, Kim @ 2026-11-30  3:20 UTC (permalink / raw)
  To: Andrew Morton, Mike Rapoport
  Cc: Baek Kyu, Kim, David Hildenbrand, Lorenzo Stoakes, linux-mm,
	linux-kernel

A secretmem inode can outlive its original file. An O_PATH descriptor
opened through /proc/self/fd keeps the inode and its unevictable folios
alive after the mapping and original descriptor are closed. The file
release path nevertheless drops the locked-memory charge, allowing the
same user to reuse RLIMIT_MEMLOCK while those folios remain allocated.

Move private-state and locked-memory accounting cleanup to inode eviction,
after truncating the page cache. Keep secretmem_users active over the same
lifetime so the hibernation guard covers retained secret pages.

Initialize private state after creating the secure inode. The filesystem
root and an inode evicted before state attachment have no private state,
so skip private cleanup in those cases. Once state is attached, iput()
handles file-creation failures and releases the user reference on eviction.

Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages")
Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>
---
Resend: the previous v4 delivery wrapped the diff; no code changes.

Link to previous v4: https://lore-kernel.gnuweeb.org/lkml/CAGDrb2WMTPt2Aj30TtUPyWn_VtUaaTmpmphERgdWLY-9zY80NA@mail.gmail.com/

Changes in v4:
  - Restore private-state allocation after secure inode creation.
  - Explain why inode eviction can encounter a NULL private state.

Link to v3: https://lore-kernel.gnuweeb.org/linux-mm/179586776810.146060.16432032400935006500.c113-v3@gmail.com/

On arm64 QEMU/KVM, an 8 KiB RLIMIT_MEMLOCK allowed the original kernel
to fault a third 4 KiB secretmem page while O_PATH retained two pages.
This patch rejected that fault until the last O_PATH reference closed.
Injected security-init, state-allocation, and file-allocation failures
returned the expected errors and released attached state at inode eviction.
The hibernation guard remained active while O_PATH retained the inode.
W=1 used KCFLAGS=-Wno-error for unrelated GCC 16 warnings; secretmem.c
had none. KASAN, lockdep, a full hibernation cycle, and long-running
concurrency were not tested.

 mm/secretmem.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/mm/secretmem.c b/mm/secretmem.c
index b3f79ad56..706ef6e1b 100644
--- a/mm/secretmem.c
+++ b/mm/secretmem.c
@@ -189,14 +189,7 @@ static void secretmem_destroy_inode_priv(struct inode *inode)
 	free_uid(state->user);
 	kfree(state);
 	inode->i_private = NULL;
-}
-
-static int secretmem_release(struct inode *inode, struct file *file)
-{
 	atomic_dec(&secretmem_users);
-	secretmem_destroy_inode_priv(inode);
-
-	return 0;
 }
 
 static int secretmem_mmap_prepare(struct vm_area_desc *desc)
@@ -216,7 +209,6 @@ bool vma_is_secretmem(struct vm_area_struct *vma)
 }
 
 static const struct file_operations secretmem_fops = {
-	.release	= secretmem_release,
 	.mmap_prepare	= secretmem_mmap_prepare,
 };
 
@@ -274,6 +266,7 @@ static int secretmem_init_inode_priv(struct inode *inode)
 
 	state->user = get_uid(current_user());
 	inode->i_private = state;
+	atomic_inc(&secretmem_users);
 	return 0;
 }
 
@@ -296,7 +289,7 @@ static struct file *secretmem_file_create(unsigned long flags)
 				 O_RDWR | O_LARGEFILE, &secretmem_fops);
 	if (IS_ERR(file)) {
 		err = PTR_ERR(file);
-		goto err_free_priv;
+		goto err_free_inode;
 	}
 
 	mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
@@ -309,11 +302,7 @@ static struct file *secretmem_file_create(unsigned long flags)
 	inode->i_mode |= S_IFREG;
 	inode->i_size = 0;
 
-	atomic_inc(&secretmem_users);
-
 	return file;
-err_free_priv:
-	secretmem_destroy_inode_priv(inode);
 err_free_inode:
 	iput(inode);
 	return ERR_PTR(err);
@@ -335,6 +324,20 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)
 	return FD_ADD(flags & O_CLOEXEC, secretmem_file_create(flags));
 }
 
+static void secretmem_evict_inode(struct inode *inode)
+{
+	truncate_inode_pages_final(&inode->i_data);
+	clear_inode(inode);
+	/* The root has no state; failed setup can call iput() before attachment. */
+	if (inode->i_private)
+		secretmem_destroy_inode_priv(inode);
+}
+
+static const struct super_operations secretmem_super_ops = {
+	.statfs		= simple_statfs,
+	.evict_inode	= secretmem_evict_inode,
+};
+
 static int secretmem_init_fs_context(struct fs_context *fc)
 {
 	struct pseudo_fs_context *ctx;
@@ -342,6 +345,7 @@ static int secretmem_init_fs_context(struct fs_context *fc)
 	ctx = init_pseudo(fc, SECRETMEM_MAGIC);
 	if (!ctx)
 		return -ENOMEM;
+	ctx->ops = &secretmem_super_ops;
 
 	return 0;
 }
-- 
2.55.0



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

* Re: [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction
  2026-11-30  3:20 [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction Baek Kyu, Kim
@ 2026-11-30  6:56 ` Mike Rapoport
  2026-12-01 13:36 ` Lorenzo Stoakes (ARM)
  2026-12-01 18:58 ` + mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch added to mm-new branch Andrew Morton
  2 siblings, 0 replies; 4+ messages in thread
From: Mike Rapoport @ 2026-11-30  6:56 UTC (permalink / raw)
  To: Baek Kyu, Kim
  Cc: Andrew Morton, David Hildenbrand, Lorenzo Stoakes, linux-mm,
	linux-kernel

On Mon, Nov 30, 2026 at 12:20:21PM +0900, Baek Kyu, Kim wrote:
> A secretmem inode can outlive its original file. An O_PATH descriptor
> opened through /proc/self/fd keeps the inode and its unevictable folios
> alive after the mapping and original descriptor are closed. The file
> release path nevertheless drops the locked-memory charge, allowing the
> same user to reuse RLIMIT_MEMLOCK while those folios remain allocated.
> 
> Move private-state and locked-memory accounting cleanup to inode eviction,
> after truncating the page cache. Keep secretmem_users active over the same
> lifetime so the hibernation guard covers retained secret pages.
> 
> Initialize private state after creating the secure inode. The filesystem
> root and an inode evicted before state attachment have no private state,
> so skip private cleanup in those cases. Once state is attached, iput()
> handles file-creation failures and releases the user reference on eviction.
> 
> Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages")
> Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>

Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>

> ---
>  mm/secretmem.c | 30 +++++++++++++++++-------------
>  1 file changed, 17 insertions(+), 13 deletions(-)

-- 
Sincerely yours,
Mike.

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

* Re: [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction
  2026-11-30  3:20 [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction Baek Kyu, Kim
  2026-11-30  6:56 ` Mike Rapoport
@ 2026-12-01 13:36 ` Lorenzo Stoakes (ARM)
  2026-12-01 18:58 ` + mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch added to mm-new branch Andrew Morton
  2 siblings, 0 replies; 4+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-12-01 13:36 UTC (permalink / raw)
  To: Baek Kyu, Kim
  Cc: Andrew Morton, Mike Rapoport, David Hildenbrand, linux-mm,
	linux-kernel

On Mon, Nov 30, 2026 at 12:20:21PM +0900, Baek Kyu, Kim wrote:
> A secretmem inode can outlive its original file. An O_PATH descriptor
> opened through /proc/self/fd keeps the inode and its unevictable folios
> alive after the mapping and original descriptor are closed. The file
> release path nevertheless drops the locked-memory charge, allowing the
> same user to reuse RLIMIT_MEMLOCK while those folios remain allocated.
>
> Move private-state and locked-memory accounting cleanup to inode eviction,
> after truncating the page cache. Keep secretmem_users active over the same
> lifetime so the hibernation guard covers retained secret pages.
>
> Initialize private state after creating the secure inode. The filesystem
> root and an inode evicted before state attachment have no private state,
> so skip private cleanup in those cases. Once state is attached, iput()
> handles file-creation failures and releases the user reference on eviction.
>
> Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages")
> Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>

Looks reasonable so:

Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> ---
> Resend: the previous v4 delivery wrapped the diff; no code changes.
>
> Link to previous v4: https://lore-kernel.gnuweeb.org/lkml/CAGDrb2WMTPt2Aj30TtUPyWn_VtUaaTmpmphERgdWLY-9zY80NA@mail.gmail.com/
>
> Changes in v4:
>   - Restore private-state allocation after secure inode creation.
>   - Explain why inode eviction can encounter a NULL private state.
>
> Link to v3: https://lore-kernel.gnuweeb.org/linux-mm/179586776810.146060.16432032400935006500.c113-v3@gmail.com/
>
> On arm64 QEMU/KVM, an 8 KiB RLIMIT_MEMLOCK allowed the original kernel
> to fault a third 4 KiB secretmem page while O_PATH retained two pages.
> This patch rejected that fault until the last O_PATH reference closed.
> Injected security-init, state-allocation, and file-allocation failures
> returned the expected errors and released attached state at inode eviction.
> The hibernation guard remained active while O_PATH retained the inode.
> W=1 used KCFLAGS=-Wno-error for unrelated GCC 16 warnings; secretmem.c
> had none. KASAN, lockdep, a full hibernation cycle, and long-running
> concurrency were not tested.
>
>  mm/secretmem.c | 30 +++++++++++++++++-------------
>  1 file changed, 17 insertions(+), 13 deletions(-)
>
> diff --git a/mm/secretmem.c b/mm/secretmem.c
> index b3f79ad56..706ef6e1b 100644
> --- a/mm/secretmem.c
> +++ b/mm/secretmem.c
> @@ -189,14 +189,7 @@ static void secretmem_destroy_inode_priv(struct inode *inode)
>  	free_uid(state->user);
>  	kfree(state);
>  	inode->i_private = NULL;
> -}
> -
> -static int secretmem_release(struct inode *inode, struct file *file)
> -{
>  	atomic_dec(&secretmem_users);
> -	secretmem_destroy_inode_priv(inode);
> -
> -	return 0;
>  }
>
>  static int secretmem_mmap_prepare(struct vm_area_desc *desc)
> @@ -216,7 +209,6 @@ bool vma_is_secretmem(struct vm_area_struct *vma)
>  }
>
>  static const struct file_operations secretmem_fops = {
> -	.release	= secretmem_release,
>  	.mmap_prepare	= secretmem_mmap_prepare,
>  };
>
> @@ -274,6 +266,7 @@ static int secretmem_init_inode_priv(struct inode *inode)
>
>  	state->user = get_uid(current_user());
>  	inode->i_private = state;
> +	atomic_inc(&secretmem_users);
>  	return 0;
>  }
>
> @@ -296,7 +289,7 @@ static struct file *secretmem_file_create(unsigned long flags)
>  				 O_RDWR | O_LARGEFILE, &secretmem_fops);
>  	if (IS_ERR(file)) {
>  		err = PTR_ERR(file);
> -		goto err_free_priv;
> +		goto err_free_inode;
>  	}
>
>  	mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
> @@ -309,11 +302,7 @@ static struct file *secretmem_file_create(unsigned long flags)
>  	inode->i_mode |= S_IFREG;
>  	inode->i_size = 0;
>
> -	atomic_inc(&secretmem_users);
> -
>  	return file;
> -err_free_priv:
> -	secretmem_destroy_inode_priv(inode);
>  err_free_inode:
>  	iput(inode);
>  	return ERR_PTR(err);
> @@ -335,6 +324,20 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)
>  	return FD_ADD(flags & O_CLOEXEC, secretmem_file_create(flags));
>  }
>
> +static void secretmem_evict_inode(struct inode *inode)
> +{
> +	truncate_inode_pages_final(&inode->i_data);
> +	clear_inode(inode);
> +	/* The root has no state; failed setup can call iput() before attachment. */
> +	if (inode->i_private)
> +		secretmem_destroy_inode_priv(inode);
> +}
> +
> +static const struct super_operations secretmem_super_ops = {
> +	.statfs		= simple_statfs,
> +	.evict_inode	= secretmem_evict_inode,
> +};
> +
>  static int secretmem_init_fs_context(struct fs_context *fc)
>  {
>  	struct pseudo_fs_context *ctx;
> @@ -342,6 +345,7 @@ static int secretmem_init_fs_context(struct fs_context *fc)
>  	ctx = init_pseudo(fc, SECRETMEM_MAGIC);
>  	if (!ctx)
>  		return -ENOMEM;
> +	ctx->ops = &secretmem_super_ops;
>
>  	return 0;
>  }
> --
> 2.55.0
>

--
Cheers, Lorenzo

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

* + mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch added to mm-new branch
  2026-11-30  3:20 [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction Baek Kyu, Kim
  2026-11-30  6:56 ` Mike Rapoport
  2026-12-01 13:36 ` Lorenzo Stoakes (ARM)
@ 2026-12-01 18:58 ` Andrew Morton
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2026-12-01 18:58 UTC (permalink / raw)
  To: mm-commits, rppt, ljs, david, pocj8ur4in, akpm


The patch titled
     Subject: mm/secretmem: keep locked memory accounting until inode eviction
has been added to the -mm mm-new branch.  Its filename is
     mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: "Baek Kyu, Kim" <pocj8ur4in@gmail.com>
Subject: mm/secretmem: keep locked memory accounting until inode eviction
Date: Mon, 30 Nov 2026 12:20:21 +0900

A secretmem inode can outlive its original file.  An O_PATH descriptor
opened through /proc/self/fd keeps the inode and its unevictable folios
alive after the mapping and original descriptor are closed.  The file
release path nevertheless drops the locked-memory charge, allowing the
same user to reuse RLIMIT_MEMLOCK while those folios remain allocated.

Move private-state and locked-memory accounting cleanup to inode eviction,
after truncating the page cache.  Keep secretmem_users active over the
same lifetime so the hibernation guard covers retained secret pages.

Initialize private state after creating the secure inode.  The filesystem
root and an inode evicted before state attachment have no private state,
so skip private cleanup in those cases.  Once state is attached, iput()
handles file-creation failures and releases the user reference on
eviction.

Link: https://patch.msgid.link/179600882107.30666.13893484586058179051.c113-v4-resend@gmail.com
Fixes: 97d34aa65c29 ("mm/secretmem: properly account locked pages")
Signed-off-by: Baek Kyu, Kim <pocj8ur4in@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Cc: David Hildenbrand <david@kernel.org>

 mm/secretmem.c |   30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

--- a/mm/secretmem.c~mm-secretmem-keep-locked-memory-accounting-until-inode-eviction
+++ a/mm/secretmem.c
@@ -189,14 +189,7 @@ static void secretmem_destroy_inode_priv
 	free_uid(state->user);
 	kfree(state);
 	inode->i_private = NULL;
-}
-
-static int secretmem_release(struct inode *inode, struct file *file)
-{
 	atomic_dec(&secretmem_users);
-	secretmem_destroy_inode_priv(inode);
-
-	return 0;
 }
 
 static int secretmem_mmap_prepare(struct vm_area_desc *desc)
@@ -216,7 +209,6 @@ bool vma_is_secretmem(struct vm_area_str
 }
 
 static const struct file_operations secretmem_fops = {
-	.release	= secretmem_release,
 	.mmap_prepare	= secretmem_mmap_prepare,
 };
 
@@ -274,6 +266,7 @@ static int secretmem_init_inode_priv(str
 
 	state->user = get_uid(current_user());
 	inode->i_private = state;
+	atomic_inc(&secretmem_users);
 	return 0;
 }
 
@@ -296,7 +289,7 @@ static struct file *secretmem_file_creat
 				 O_RDWR | O_LARGEFILE, &secretmem_fops);
 	if (IS_ERR(file)) {
 		err = PTR_ERR(file);
-		goto err_free_priv;
+		goto err_free_inode;
 	}
 
 	mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
@@ -309,11 +302,7 @@ static struct file *secretmem_file_creat
 	inode->i_mode |= S_IFREG;
 	inode->i_size = 0;
 
-	atomic_inc(&secretmem_users);
-
 	return file;
-err_free_priv:
-	secretmem_destroy_inode_priv(inode);
 err_free_inode:
 	iput(inode);
 	return ERR_PTR(err);
@@ -335,6 +324,20 @@ SYSCALL_DEFINE1(memfd_secret, unsigned i
 	return FD_ADD(flags & O_CLOEXEC, secretmem_file_create(flags));
 }
 
+static void secretmem_evict_inode(struct inode *inode)
+{
+	truncate_inode_pages_final(&inode->i_data);
+	clear_inode(inode);
+	/* The root has no state; failed setup can call iput() before attachment. */
+	if (inode->i_private)
+		secretmem_destroy_inode_priv(inode);
+}
+
+static const struct super_operations secretmem_super_ops = {
+	.statfs		= simple_statfs,
+	.evict_inode	= secretmem_evict_inode,
+};
+
 static int secretmem_init_fs_context(struct fs_context *fc)
 {
 	struct pseudo_fs_context *ctx;
@@ -342,6 +345,7 @@ static int secretmem_init_fs_context(str
 	ctx = init_pseudo(fc, SECRETMEM_MAGIC);
 	if (!ctx)
 		return -ENOMEM;
+	ctx->ops = &secretmem_super_ops;
 
 	return 0;
 }
_

Patches currently in -mm which might be from pocj8ur4in@gmail.com are

mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch


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

end of thread, other threads:[~2026-12-01 18:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  3:20 [PATCH v4 RESEND] mm/secretmem: Keep locked memory accounting until inode eviction Baek Kyu, Kim
2026-11-30  6:56 ` Mike Rapoport
2026-12-01 13:36 ` Lorenzo Stoakes (ARM)
2026-12-01 18:58 ` + mm-secretmem-keep-locked-memory-accounting-until-inode-eviction.patch added to mm-new branch Andrew Morton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.