public inbox for linux-mm@kvack.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] Modify memfd_luo code
@ 2026-03-23 11:07 Chenghao Duan
  0 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: Chenghao Duan

I found several modifiable points while reading the code. Please review.

v2:
As suggested by Pratyush Yadav, add patch
<mm/memfd: use folio_nr_pages() for shmem inode accounting>.
https://lore.kernel.org/all/2vxzqzpebzi2.fsf@kernel.org/

<mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path>
Same as V1, no logic changes; depends on patch #1 for modifications.

<mm/memfd_luo: remove unnecessary memset in zero-size memfd path>
No modifications have been made.

<mm/memfd_luo: use i_size_write() to set inode size during retrieve>
Add consistency-related descriptions to the commit log.


v1:
https://lore.kernel.org/all/20260319012845.29570-1-duanchenghao@kylinos.cn/

Chenghao Duan (4):
  mm/memfd: use folio_nr_pages() for shmem inode accounting
  mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path
  mm/memfd_luo: remove unnecessary memset in zero-size memfd path
  mm/memfd_luo: use i_size_write() to set inode size during retrieve

 mm/memfd_luo.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

-- 
2.25.1



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

* [PATCH v2 0/4] Modify memfd_luo code
@ 2026-03-23 11:07 Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 1/4] mm/memfd: use folio_nr_pages() for shmem inode accounting Chenghao Duan
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: jianghaoran, Chenghao Duan

I found several modifiable points while reading the code. Please review.

v2:
As suggested by Pratyush Yadav, add patch
<mm/memfd: use folio_nr_pages() for shmem inode accounting>.
https://lore.kernel.org/all/2vxzqzpebzi2.fsf@kernel.org/

<mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path>
Same as V1, no logic changes; depends on patch #1 for modifications.

<mm/memfd_luo: remove unnecessary memset in zero-size memfd path>
No modifications have been made.

<mm/memfd_luo: use i_size_write() to set inode size during retrieve>
Add consistency-related descriptions to the commit log.


v1:
https://lore.kernel.org/all/20260319012845.29570-1-duanchenghao@kylinos.cn/

Chenghao Duan (4):
  mm/memfd: use folio_nr_pages() for shmem inode accounting
  mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path
  mm/memfd_luo: remove unnecessary memset in zero-size memfd path
  mm/memfd_luo: use i_size_write() to set inode size during retrieve

 mm/memfd_luo.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

-- 
2.25.1



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

* [PATCH v2 1/4] mm/memfd: use folio_nr_pages() for shmem inode accounting
  2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
@ 2026-03-23 11:07 ` Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 2/4] mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path Chenghao Duan
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: jianghaoran, Chenghao Duan

memfd_luo_retrieve_folios() called shmem_inode_acct_blocks() and
shmem_recalc_inode() with hardcoded 1 instead of the actual folio
page count.  memfd may use large folios (THP/hugepages), causing
quota/limit under-accounting and incorrect stat output.

Fix by using folio_nr_pages(folio) for both functions.

Issue found by AI review and suggested by Pratyush Yadav <pratyush@kernel.org>.
https://sashiko.dev/#/patchset/20260319012845.29570-1-duanchenghao%40kylinos.cn

Suggested-by: Pratyush Yadav <pratyush@kernel.org>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 mm/memfd_luo.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index b8edb9f981d7..953440994ad2 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -395,6 +395,7 @@ static int memfd_luo_retrieve_folios(struct file *file,
 	struct inode *inode = file_inode(file);
 	struct address_space *mapping = inode->i_mapping;
 	struct folio *folio;
+	long npages;
 	int err = -EIO;
 	long i;
 
@@ -441,14 +442,15 @@ static int memfd_luo_retrieve_folios(struct file *file,
 		if (flags & MEMFD_LUO_FOLIO_DIRTY)
 			folio_mark_dirty(folio);
 
-		err = shmem_inode_acct_blocks(inode, 1);
+		npages = folio_nr_pages(folio);
+		err = shmem_inode_acct_blocks(inode, npages);
 		if (err) {
-			pr_err("shmem: failed to account folio index %ld: %d\n",
-			       i, err);
+			pr_err("shmem: failed to account folio index %ld(%ld pages): %d\n",
+			       i, npages, err);
 			goto unlock_folio;
 		}
 
-		shmem_recalc_inode(inode, 1, 0);
+		shmem_recalc_inode(inode, npages, 0);
 		folio_add_lru(folio);
 		folio_unlock(folio);
 		folio_put(folio);
-- 
2.25.1



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

* [PATCH v2 2/4] mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path
  2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 1/4] mm/memfd: use folio_nr_pages() for shmem inode accounting Chenghao Duan
@ 2026-03-23 11:07 ` Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 3/4] mm/memfd_luo: remove unnecessary memset in zero-size memfd path Chenghao Duan
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: jianghaoran, Chenghao Duan

Move shmem_recalc_inode() out of the loop in memfd_luo_retrieve_folios()
to improve performance when restoring large memfds.

Currently, shmem_recalc_inode() is called for each folio during restore,
which is O(n) expensive operations. This patch collects the number of
successfully added folios and calls shmem_recalc_inode() once after the
loop completes, reducing complexity to O(1).

Additionally, fix the error path to also call shmem_recalc_inode() for
the folios that were successfully added before the error occurred.

Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 mm/memfd_luo.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 953440994ad2..2a01eaff03c2 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -395,7 +395,7 @@ static int memfd_luo_retrieve_folios(struct file *file,
 	struct inode *inode = file_inode(file);
 	struct address_space *mapping = inode->i_mapping;
 	struct folio *folio;
-	long npages;
+	long npages, nr_added_pages = 0;
 	int err = -EIO;
 	long i;
 
@@ -450,12 +450,14 @@ static int memfd_luo_retrieve_folios(struct file *file,
 			goto unlock_folio;
 		}
 
-		shmem_recalc_inode(inode, npages, 0);
+		nr_added_pages += npages;
 		folio_add_lru(folio);
 		folio_unlock(folio);
 		folio_put(folio);
 	}
 
+	shmem_recalc_inode(inode, nr_added_pages, 0);
+
 	return 0;
 
 unlock_folio:
@@ -474,6 +476,8 @@ static int memfd_luo_retrieve_folios(struct file *file,
 			folio_put(folio);
 	}
 
+	shmem_recalc_inode(inode, nr_added_pages, 0);
+
 	return err;
 }
 
-- 
2.25.1



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

* [PATCH v2 3/4] mm/memfd_luo: remove unnecessary memset in zero-size memfd path
  2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 1/4] mm/memfd: use folio_nr_pages() for shmem inode accounting Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 2/4] mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path Chenghao Duan
@ 2026-03-23 11:07 ` Chenghao Duan
  2026-03-23 11:07 ` [PATCH v2 4/4] mm/memfd_luo: use i_size_write() to set inode size during retrieve Chenghao Duan
  2026-03-23 21:09 ` [PATCH v2 0/4] Modify memfd_luo code Andrew Morton
  4 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: jianghaoran, Chenghao Duan

The memset(kho_vmalloc, 0, sizeof(*kho_vmalloc)) call in the zero-size
file handling path is unnecessary because the allocation of the ser
structure already uses the __GFP_ZERO flag, ensuring the memory is
already zero-initialized.

Reviewed-by: Pratyush Yadav <pratyush@kernel.org>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 mm/memfd_luo.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index 2a01eaff03c2..bf827e574bec 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -103,7 +103,6 @@ static int memfd_luo_preserve_folios(struct file *file,
 	if (!size) {
 		*nr_foliosp = 0;
 		*out_folios_ser = NULL;
-		memset(kho_vmalloc, 0, sizeof(*kho_vmalloc));
 		return 0;
 	}
 
-- 
2.25.1



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

* [PATCH v2 4/4] mm/memfd_luo: use i_size_write() to set inode size during retrieve
  2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
                   ` (2 preceding siblings ...)
  2026-03-23 11:07 ` [PATCH v2 3/4] mm/memfd_luo: remove unnecessary memset in zero-size memfd path Chenghao Duan
@ 2026-03-23 11:07 ` Chenghao Duan
  2026-03-23 21:09 ` [PATCH v2 0/4] Modify memfd_luo code Andrew Morton
  4 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-23 11:07 UTC (permalink / raw)
  To: pasha.tatashin, rppt, pratyush, akpm, linux-kernel, linux-mm
  Cc: jianghaoran, Chenghao Duan

Use i_size_write() instead of directly assigning to inode->i_size
when restoring the memfd size in memfd_luo_retrieve(), to keep code
consistency.

No functional change intended.

Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Signed-off-by: Chenghao Duan <duanchenghao@kylinos.cn>
---
 mm/memfd_luo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/memfd_luo.c b/mm/memfd_luo.c
index bf827e574bec..76edf9a3f5b5 100644
--- a/mm/memfd_luo.c
+++ b/mm/memfd_luo.c
@@ -499,7 +499,7 @@ static int memfd_luo_retrieve(struct liveupdate_file_op_args *args)
 	}
 
 	vfs_setpos(file, ser->pos, MAX_LFS_FILESIZE);
-	file->f_inode->i_size = ser->size;
+	i_size_write(file_inode(file), ser->size);
 
 	if (ser->nr_folios) {
 		folios_ser = kho_restore_vmalloc(&ser->folios);
-- 
2.25.1



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

* Re: [PATCH v2 0/4] Modify memfd_luo code
  2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
                   ` (3 preceding siblings ...)
  2026-03-23 11:07 ` [PATCH v2 4/4] mm/memfd_luo: use i_size_write() to set inode size during retrieve Chenghao Duan
@ 2026-03-23 21:09 ` Andrew Morton
  2026-03-24  1:54   ` Chenghao Duan
  4 siblings, 1 reply; 8+ messages in thread
From: Andrew Morton @ 2026-03-23 21:09 UTC (permalink / raw)
  To: Chenghao Duan
  Cc: pasha.tatashin, rppt, pratyush, linux-kernel, linux-mm,
	jianghaoran

On Mon, 23 Mar 2026 19:07:43 +0800 Chenghao Duan <duanchenghao@kylinos.cn> wrote:

> I found several modifiable points while reading the code. Please review.

Thanks, I've updated mm.git to this version,

AI review has raised a few potential issues.  Perhaps they were present
in its review of the v1 patchset.

	https://sashiko.dev/#/patchset/20260323110747.193569-1-duanchenghao@kylinos.cn


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

* Re: [PATCH v2 0/4] Modify memfd_luo code
  2026-03-23 21:09 ` [PATCH v2 0/4] Modify memfd_luo code Andrew Morton
@ 2026-03-24  1:54   ` Chenghao Duan
  0 siblings, 0 replies; 8+ messages in thread
From: Chenghao Duan @ 2026-03-24  1:54 UTC (permalink / raw)
  To: Andrew Morton
  Cc: pasha.tatashin, rppt, pratyush, linux-kernel, linux-mm,
	jianghaoran

On Mon, Mar 23, 2026 at 02:09:38PM -0700, Andrew Morton wrote:
> On Mon, 23 Mar 2026 19:07:43 +0800 Chenghao Duan <duanchenghao@kylinos.cn> wrote:
> 
> > I found several modifiable points while reading the code. Please review.
> 
> Thanks, I've updated mm.git to this version,
> 
> AI review has raised a few potential issues.  Perhaps they were present
> in its review of the v1 patchset.
> 
> 	https://sashiko.dev/#/patchset/20260323110747.193569-1-duanchenghao@kylinos.cn

Sure. My apologies for only addressing Pratyush Yadav's comments in my
reply to the v1 patchset, while overlooking the AI review link mentioned
in the email.

I have realized that the AI raised structural potential issues in the
overall code, not problems specific to the patch itself. I will analyze
the findings from the AI review and attempt to resolve these potential
issues.

If anyone has better solutions or code for the potential issues raised
in the AI review, please feel free to contribute.

BRs
Chenghao


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

end of thread, other threads:[~2026-03-24  1:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-23 11:07 [PATCH v2 0/4] Modify memfd_luo code Chenghao Duan
2026-03-23 11:07 ` [PATCH v2 1/4] mm/memfd: use folio_nr_pages() for shmem inode accounting Chenghao Duan
2026-03-23 11:07 ` [PATCH v2 2/4] mm/memfd_luo: optimize shmem_recalc_inode calls in retrieve path Chenghao Duan
2026-03-23 11:07 ` [PATCH v2 3/4] mm/memfd_luo: remove unnecessary memset in zero-size memfd path Chenghao Duan
2026-03-23 11:07 ` [PATCH v2 4/4] mm/memfd_luo: use i_size_write() to set inode size during retrieve Chenghao Duan
2026-03-23 21:09 ` [PATCH v2 0/4] Modify memfd_luo code Andrew Morton
2026-03-24  1:54   ` Chenghao Duan
  -- strict thread matches above, loose matches on Subject: below --
2026-03-23 11:07 Chenghao Duan

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