From: Kemeng Shi <shikemeng@huaweicloud.com>
To: hughd@google.com, baolin.wang@linux.alibaba.com,
willy@infradead.org, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: [PATCH 4/7] mm: shmem: handle special case of shmem_recalc_inode() in it's caller
Date: Fri, 6 Jun 2025 06:10:34 +0800 [thread overview]
Message-ID: <20250605221037.7872-5-shikemeng@huaweicloud.com> (raw)
In-Reply-To: <20250605221037.7872-1-shikemeng@huaweicloud.com>
The sepcial case in shmem_recalc_inode() is tailored for shmem_writepage().
By raising swapped before nrpages is lowered directly within
shmem_writepage(), we can simplify code of shmem_recalc_inode() and
eliminate the need of executing the special case code for all callers of
shmem_recalc_inode().
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
mm/shmem.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
diff --git a/mm/shmem.c b/mm/shmem.c
index 2349673b239b..9f5e1eccaacb 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -443,15 +443,6 @@ static void shmem_recalc_inode(struct inode *inode, long alloced, long swapped)
info->swapped += swapped;
freed = info->alloced - info->swapped -
READ_ONCE(inode->i_mapping->nrpages);
- /*
- * Special case: whereas normally shmem_recalc_inode() is called
- * after i_mapping->nrpages has already been adjusted (up or down),
- * shmem_writepage() has to raise swapped before nrpages is lowered -
- * to stop a racing shmem_recalc_inode() from thinking that a page has
- * been freed. Compensate here, to avoid the need for a followup call.
- */
- if (swapped > 0)
- freed += swapped;
if (freed > 0)
info->alloced -= freed;
spin_unlock(&info->lock);
@@ -1694,9 +1685,16 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
list_add(&info->swaplist, &shmem_swaplist);
if (!folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN)) {
- shmem_recalc_inode(inode, 0, nr_pages);
+ /*
+ * Raise swapped before nrpages is lowered to stop racing
+ * shmem_recalc_inode() from thinking that a page has been freed.
+ */
+ spin_lock(&info->lock);
+ info->swapped += nr_pages;
+ spin_unlock(&info->lock);
swap_shmem_alloc(folio->swap, nr_pages);
shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap));
+ shmem_recalc_inode(inode, 0, 0);
mutex_unlock(&shmem_swaplist_mutex);
BUG_ON(folio_mapped(folio));
--
2.30.0
next prev parent reply other threads:[~2025-06-05 13:17 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-05 22:10 [PATCH 0/7] Some random fixes and cleanups to shmem Kemeng Shi
2025-06-05 20:02 ` Andrew Morton
2025-06-05 22:10 ` [PATCH 1/7] mm: shmem: correctly pass alloced parameter to shmem_recalc_inode() to avoid WARN_ON() Kemeng Shi
2025-06-05 19:57 ` Andrew Morton
2025-06-06 1:11 ` Kemeng Shi
2025-06-06 1:28 ` Andrew Morton
2025-06-06 2:29 ` Kemeng Shi
2025-06-06 1:31 ` Kemeng Shi
2025-06-07 6:11 ` Baolin Wang
2025-06-09 0:46 ` Kemeng Shi
2025-06-10 1:02 ` Kemeng Shi
2025-06-11 7:29 ` Baolin Wang
2025-06-11 8:38 ` Kemeng Shi
2025-06-05 22:10 ` [PATCH 2/7] mm: shmem: avoid setting error on splited entries in shmem_set_folio_swapin_error() Kemeng Shi
2025-06-07 6:20 ` Baolin Wang
2025-06-09 1:19 ` Kemeng Shi
2025-06-11 7:41 ` Baolin Wang
2025-06-11 9:11 ` Kemeng Shi
2025-06-13 6:56 ` Baolin Wang
2025-06-05 22:10 ` [PATCH 3/7] mm: shmem: avoid missing entries in shmem_undo_range() when entries was splited concurrently Kemeng Shi
2025-06-05 22:10 ` Kemeng Shi [this message]
2025-06-05 22:10 ` [PATCH 5/7] mm: shmem: wrap additional shmem quota related code with CONFIG_TMPFS_QUOTA Kemeng Shi
2025-06-05 22:10 ` [PATCH 6/7] mm: shmem: simplify error flow in thpsize_shmem_enabled_store() Kemeng Shi
2025-06-05 22:10 ` [PATCH 7/7] mm: shmem: eliminate unneeded page counting in shmem_unuse_swap_entries() Kemeng Shi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250605221037.7872-5-shikemeng@huaweicloud.com \
--to=shikemeng@huaweicloud.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=hughd@google.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox