mm/secretmem: Keep locked memory accounting until inode eviction
Subsystems (2)
PATCH 1/1 mm/secretmem: Keep locked memory accounting until inode eviction
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 that the hibernation guard covers retained secret pages. Let iput() handle file-creation failures and tolerate an inode with no private state.
---
On an arm64 QEMU guest with an 8 KiB RLIMIT_MEMLOCK, an O_PATH reference retained two secretmem pages. The baseline dropped the charge to zero and allowed a further one-page fault; the patch kept both pages charged and rejected that fault until the last path reference closed. The Image was built with KASAN and lockdep. Hibernation, other architectures, and long-running concurrency were not tested.
mm/secretmem.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
mm/secretmem.c +18 -13
@@ -185,18 +185,14 @@ static void secretmem_destroy_inode_priv(struct inode *inode) {struct secretmem_inode_state *state = inode->i_private; +if (!state) +return ; +secretmem_unaccount_all_folios (state);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 +212,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 +269,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 +292,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 +305,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 +327,18 @@ 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); +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 +346,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 ; }
Signature · 1 line
2.55.0
Quoted 6 lines · Kim
A secretmem inode can outlive its original file. An
O_PATHdescriptor opened through/proc/self/fdkeeps 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 reuseRLIMIT_MEMLOCKwhile those folios remain allocated.
Quoted 8 lines
Move private-state and locked-memory accounting cleanup to inode eviction, after truncating the page cache. Keep
secretmem_usersactive over the same lifetime so that the hibernation guard covers retained secret pages. Letiput()handle file-creation failures and tolerate an inode with no private state.Fixes 97d34aa65c29 ("mm/secretmem: properly account locked pages")Signed-off-by Baek Kyu, Kim <[email protected]>
Lorenzo, Mike, this problem seems real to me. Can you take a look?
Quoted 101 lines
---
On an arm64 QEMU guest with an 8 KiBRLIMIT_MEMLOCK, anO_PATHreference retained two secretmem pages. The baseline dropped the charge to zero and allowed a further one-page fault; the patch kept both pages charged and rejected that fault until the last path reference closed. The Image was built with KASAN and lockdep. Hibernation, other architectures, and long-running concurrency were not tested.mm/secretmem.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)mm/secretmem.c +18
-13@@ -185,18 +185,14 @@ static void secretmem_destroy_inode_priv(struct inode *inode) {struct secretmem_inode_state *state = inode->i_private; +if (!state) +return ; +secretmem_unaccount_all_folios (state);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 +212,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 +269,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 +292,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 +305,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 +327,18 @@ 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); +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 +346,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 ; }
Signature · 3 lines
Cheers, David
Mike Rapoport
maintainerQuoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Kim
A secretmem inode can outlive its original file. An
O_PATHdescriptor opened through/proc/self/fdkeeps 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 reuseRLIMIT_MEMLOCKwhile those folios remain allocated.
Quoted 5 lines
Quoted 5 lines
Move private-state and locked-memory accounting cleanup to inode eviction, after truncating the page cache. Keep
secretmem_usersactive over the same lifetime so that the hibernation guard covers retained secret pages. Letiput()handle file-creation failures and tolerate an inode with no private state.
I don't like the last part, see below.
Quoted 4 lines
Quoted 2 lines
Fixes 97d34aa65c29 ("mm/secretmem: properly account locked pages")Signed-off-by Baek Kyu, Kim <[email protected]>Lorenzo, Mike, this problem seems real to me. Can you take a look?
Yep, it's real and the fix is correct.
Quoted 19 lines
Quoted 19 lines
---
On an arm64 QEMU guest with an 8 KiBRLIMIT_MEMLOCK, anO_PATHreference retained two secretmem pages. The baseline dropped the charge to zero and allowed a further one-page fault; the patch kept both pages charged and rejected that fault until the last path reference closed. The Image was built with KASAN and lockdep. Hibernation, other architectures, and long-running concurrency were not tested.mm/secretmem.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)mm/secretmem.c +0
-0@@ -185,18 +185,14 @@ static void secretmem_destroy_inode_priv(struct inode *inode) {struct secretmem_inode_state *state = inode->i_private;
Quoted 3 lines
mm/secretmem.c static void secretmem_destroy_inode_priv(struct inode *inode) · line 190
Quoted 3 lines
mm/secretmem.c +3
-0+if (!state) +return ; +
Its really not obvious how we can get here with state == NULL.
And since it is needed only for error path in secretmem_file_create(), I suggest rearranging the initialization so that we never get here with NULL state.
Allocate the state before creating the inode and adjust error handling in secretmem_file_create() so that we always get to iput with valid i_private.
Quoted 30 lines
Quoted 30 lines
mm/secretmem.c +1
-7secretmem_unaccount_all_folios (state);free_uid (state->user);kfree (state); inode->i_private =NULL ; -} -@@ -296,7 +292,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 +305,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 +327,18 @@ SYSCALL_DEFINE1(memfd_secret, unsigned int, flags)return FD_ADD (flags &O_CLOEXEC ,secretmem_file_create (flags)); }
Signature · 2 lines
Sincerely yours, Mike.
Quoted 48 lines · Mike Rapoport
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Kim
A secretmem inode can outlive its original file. An
O_PATHdescriptor opened through/proc/self/fdkeeps 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 reuseRLIMIT_MEMLOCKwhile those folios remain allocated.Quoted 5 lines
Quoted 5 lines
Move private-state and locked-memory accounting cleanup to inode eviction, after truncating the page cache. Keep
secretmem_usersactive over the same lifetime so that the hibernation guard covers retained secret pages. Letiput()handle file-creation failures and tolerate an inode with no private state.I don't like the last part, see below.
Quoted 4 lines
Quoted 2 lines
Fixes 97d34aa65c29 ("mm/secretmem: properly account locked pages")Signed-off-by Baek Kyu, Kim <[email protected]>Lorenzo, Mike, this problem seems real to me. Can you take a look?
Yep, it's real and the fix is correct.
Quoted 19 lines
Quoted 19 lines
---
On an arm64 QEMU guest with an 8 KiBRLIMIT_MEMLOCK, anO_PATHreference retained two secretmem pages. The baseline dropped the charge to zero and allowed a further one-page fault; the patch kept both pages charged and rejected that fault until the last path reference closed. The Image was built with KASAN and lockdep. Hibernation, other architectures, and long-running concurrency were not tested.mm/secretmem.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)mm/secretmem.c +0
-0@@ -185,18 +185,14 @@ static void secretmem_destroy_inode_priv(struct inode *inode) {struct secretmem_inode_state *state = inode->i_private;Quoted 3 lines
mm/secretmem.c static void secretmem_destroy_inode_priv(struct inode *inode) · line 190
Quoted 3 lines
mm/secretmem.c +3
-0+if (!state) +return ; +Its really not obvious how we can get here with state == NULL.
Quoted 5 lines
And since it is needed only for error path in
secretmem_file_create(), I suggest rearranging the initialization so that we never get here with NULL state.
Allocate the state before creating the inode and adjust error handling insecretmem_file_create()so that we always get to iput with validi_private.
Ack, Kim, can you resend (putting me on CC ?)
Signature · 3 lines
Cheers, David
PATCH v2 1/1 mm/secretmem: Keep locked memory accounting until inode eviction
In reply to Baek Kyu, KimA 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 that the hibernation guard covers retained secret pages.
Allocate private state before inode creation and release it if the secure-inode helper fails. Attach it immediately after creation so later failures can use iput() for cleanup. An LSM denial inside the helper can call iput() before i_private is set; skip private cleanup at eviction when i_private is NULL.
On arm64 QEMU with an 8 KiB RLIMIT_MEMLOCK, the original kernel allowed an additional 4 KiB secretmem fault while O_PATH retained two pages. v2 rejected that fault with SIGBUS and allowed it after O_PATH closed. Injected LSM denial and file-allocation failure completed without a crash; file-allocation cleanup returned secretmem_users to zero. KASAN, lockdep, hibernation and long-running concurrency were not tested in this v2 run.
---
Changes in v2:
- Allocate state before inode creation, as Mike Rapoport suggested, and free it if the secure-inode helper fails.
- Guard private-state cleanup in secretmem_evict_inode() because an LSM denial can call iput() before i_private is set.
Link to v1: https://lists.openwall.net/linux-kernel/2026/10/30/1883
mm/secretmem.c | 58 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 30 deletions(-)
mm/secretmem.c +28 -30
@@ -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, };
@@ -264,39 +256,34 @@ static const struct inode_operations secretmem_iops = {static struct vfsmount *secretmem_mnt; -static int secretmem_init_inode_priv (struct inode *inode) -{ -struct secretmem_inode_state *state; - - state =kzalloc_obj (*state); -if (!state) -return -ENOMEM ; - - state->user =get_uid (current_user ()); - inode->i_private = state; -return 0 ; -} -static struct file *secretmem_file_create (unsigned long flags) { +struct secretmem_inode_state *state;struct file *file;struct inode *inode;const char *anon_name ="[secretmem]" ;int err; + state =kzalloc_obj (*state); +if (!state) +return ERR_PTR (-ENOMEM ); + state->user =get_uid (current_user ()); + inode =anon_inode_make_secure_inode (secretmem_mnt->mnt_sb, anon_name,NULL ); -if (IS_ERR (inode)) +if (IS_ERR (inode)) { +free_uid (state->user); +kfree (state);return ERR_CAST (inode); + } - err =secretmem_init_inode_priv (inode); -if (err) - goto err_free_inode; + inode->i_private = state; +atomic_inc (&secretmem_users); file =alloc_file_pseudo (inode, secretmem_mnt,"secretmem" ,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 +296,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 +318,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 filesystem root and failed secure inodes have no private state. */ +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 +339,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 ; }
Mike Rapoport
maintainerHi,
Please don't send vN+1 as a reply to vN next time please.
Quoted 90 lines · Kim
A secretmem inode can outlive its original file. An
O_PATHdescriptor opened through/proc/self/fdkeeps 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 reuseRLIMIT_MEMLOCKwhile those folios remain allocated.Move private-state and locked-memory accounting cleanup to inode eviction, after truncating the page cache. Keep
secretmem_usersactive over the same lifetime so that the hibernation guard covers retained secret pages.Allocate private state before inode creation and release it if the secure-inode helper fails. Attach it immediately after creation so later failures can use
iput()for cleanup. An LSM denial inside the helper can calliput()beforei_privateis set; skip private cleanup at eviction wheni_privateis NULL.On arm64 QEMU with an 8 KiB
RLIMIT_MEMLOCK, the original kernel allowed an additional 4 KiB secretmem fault whileO_PATHretained two pages. v2 rejected that fault with SIGBUS and allowed it afterO_PATHclosed. Injected LSM denial and file-allocation failure completed without a crash; file-allocation cleanup returnedsecretmem_usersto zero. KASAN, lockdep, hibernation and long-running concurrency were not tested in this v2 run.Fixes 97d34aa65c29 ("mm/secretmem: properly account locked pages")Signed-off-by Baek Kyu, Kim <[email protected]>---
Changes in v2:
- Allocate state before inode creation, as Mike Rapoport suggested, and free it if the secure-inode helper fails.
- Guard private-state cleanup insecretmem_evict_inode()because an LSM denial can calliput()beforei_privateis set.Link to v1: https://lists.openwall.net/linux-kernel/2026/10/30/1883
mm/secretmem.c | 58 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 30 deletions(-)mm/secretmem.c +1
-21@@ -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, };@@ -264,39 +256,34 @@ static const struct inode_operations secretmem_iops = {static struct vfsmount *secretmem_mnt; -static int secretmem_init_inode_priv (struct inode *inode) -{ -struct secretmem_inode_state *state; - - state =kzalloc_obj (*state); -if (!state) -return -ENOMEM ; - - state->user =get_uid (current_user ()); - inode->i_private = state; -return 0 ; -} -static struct file *secretmem_file_create (unsigned long flags) { +struct secretmem_inode_state *state;struct file *file;struct inode *inode;const char *anon_name ="[secretmem]" ;int err;
mm/secretmem.c static const struct inode_operations secretmem_iops = { · line 275
Quoted 10 lines
mm/secretmem.c +8
-1+ state =kzalloc_obj (*state); +if (!state) +return ERR_PTR (-ENOMEM ); + state->user =get_uid (current_user ()); + inode =anon_inode_make_secure_inode (secretmem_mnt->mnt_sb, anon_name,NULL ); -if (IS_ERR (inode)) +if (IS_ERR (inode)) { +free_uid (state->user); +kfree (state);
Please don't do cleanups inside if (err), use goto.
See https://docs.kernel.org/process/coding-style.html#centralized-exiting-of-functions
Quoted 5 lines
mm/secretmem.c +1
-2return ERR_CAST (inode); + } - err =secretmem_init_inode_priv (inode); -if (err)
mm/secretmem.c static const struct inode_operations secretmem_iops = { · line 286
Quoted 10 lines
mm/secretmem.c +3
-2- goto err_free_inode; + inode->i_private = state; +atomic_inc (&secretmem_users); file =alloc_file_pseudo (inode, secretmem_mnt,"secretmem" ,O_RDWR |O_LARGEFILE , &secretmem_fops);if (IS_ERR (file)) { err =PTR_ERR (file); - goto err_free_priv; + goto err_free_inode;
I think here we lost free_uid().
mm/secretmem.c static struct file *secretmem_file_create(unsigned long flags) · line 316
Quoted 12 lines
mm/secretmem.c +0
-4}mapping_set_gfp_mask (inode->i_mapping,GFP_USER );@@ -309,11 +296,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);
What prevents moving this between iput() and return err?
Quoted 32 lines
mm/secretmem.c +15
-0err_free_inode:iput (inode);return ERR_PTR (err);@@ -335,6 +318,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 filesystem root and failed secure inodes have no private state. */ +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 +339,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 ; }
Signature · 2 lines
Sincerely yours, Mike.