From nobody Wed Feb 11 10:00:07 2026 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 163D9745E7 for ; Tue, 21 May 2024 11:03:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289423; cv=none; b=e1u/AlW6ZpnOE2n3AaDH5WMVZOJjBrKgQehiORt+b/bvCFVC3T6NzqQfU2TqW6cARZFGjaBrVSeWd1SNN2Po4msZOwD9W5UagQXxc4A+p6fkv7RRFa2U45vjOX3nmCihsHuITFuluuVQCpDwb/WJjmXye5yfybFUDdWy6JHDeKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289423; c=relaxed/simple; bh=rc2tOhLkwkX3ybDr66V8237pfMfld4Tlo9Wq9wRfzIk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tF7aMfd8EYYhmz4jF8xUzyRXGaJJ8Kx9CfAW0Ort78qQGPfZlM41zz7ve6EuAwMZmjcYzYryTas9khawoZa9ZwZZDr0nl0Qr2PxdOBVlZPI7U48gjyH2Pw8MybX8kLrVEjMpeKhBwAbfEopmB73jHdt6XcDtsHTsaWBR77MjHEQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=nzMjLmy3; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="nzMjLmy3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289414; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=jRAtHbKubpnpADzITDXZT1b01GiilShGna80PhKpWSg=; b=nzMjLmy3ZA00agnVzDjG7nRemJ2J8K1qo4r4yJKo11Qfu19C9apclBzd40rzTaPT2yAezGz2vfwbDH0BOb+qpjpeDP6f0oN0pcSJW615OhhuvP1NauobnslPGlGjbJt57/NMlghbBRlRWT6kPh5tCBlcQMFwqeEZzx8/U4WrYDk= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R111e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067110;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xeTAt_1716289410; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xeTAt_1716289410) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:31 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/8] mm: fix shmem swapout statistic Date: Tue, 21 May 2024 19:03:11 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As we know, shmem not only supports the sharing of anonymous pages, but also the RAM-based temporary filesystem. Therefore, shmem swapouts should not be marked as anonymous swapout statistics. Fix it by adding folio_test_anon(). Fixes: d0f048ac39f6 ("mm: add per-order mTHP anon_swpout and anon_swpout_fa= llback counters") Signed-off-by: Baolin Wang --- mm/page_io.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/page_io.c b/mm/page_io.c index 46c603dddf04..b181b81f39e3 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -217,7 +217,9 @@ static inline void count_swpout_vm_event(struct folio *= folio) count_memcg_folio_events(folio, THP_SWPOUT, 1); count_vm_event(THP_SWPOUT); } - count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_SWPOUT); + + if (folio_test_anon(folio)) + count_mthp_stat(folio_order(folio), MTHP_STAT_ANON_SWPOUT); #endif count_vm_events(PSWPOUT, folio_nr_pages(folio)); } --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-110.freemail.mail.aliyun.com (out30-110.freemail.mail.aliyun.com [115.124.30.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7AFC1F947 for ; Tue, 21 May 2024 11:03:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.110 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289420; cv=none; b=nMURNTo5z+T4bNTnexca4KGXg1Ytrkg4Hy9gkC1Hfu90NwxV014b4tUkTjLqCmbyvWMIEEsSAEUWQYdfS4llWQt3SMAntRtcOnl6DdoH/UiZ1KvDHmEL7IUG52vjXZ2SsPl+/4ytZyduK8VA3XKYZb6rOKZI3xRgw0ZI8Gc8hxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289420; c=relaxed/simple; bh=h22zJbPRxyQFlNc4ugoehr8HE78sCotSnHOiJ3gEnU4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KU1oWZun7a/xnmUrxaa54XtUXg/0gdDE1PUsnhxgnX6X244yKlkKThgDHSZDE6AdciHqirjKwVQ/4Xr4seFPZl177fONh3g9+k9fF59h5rUgqNZUV1RkCELK/iIf/TYoCrplP1xQ1o6bjBDZv0kW1xDDEjcmUHpw1QCiut/Tq1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=lz3wmb9h; arc=none smtp.client-ip=115.124.30.110 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="lz3wmb9h" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289414; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=emwz+iI9NQ7GxFXPBlPImuiv2PaM3+Taa8lvRwiumnM=; b=lz3wmb9hM7NQm3xqYf8LPwVLZmpLvXu3uuGv0pvPh9RnGTU8FhSIHjQeHOmBJyr7IWGr9cAraPwBnK7LvvMbuxJNxcDCh0XiuLweDjD+LUCfhZ1XFVja4BLLP45UjsWr04fBiQEtCodoi18gUa21KVDxK1jeTH4cGU24+EPeKfg= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033022160150;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xl94l_1716289411; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xl94l_1716289411) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:33 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/8] mm: vmscan: add validation before spliting shmem large folio Date: Tue, 21 May 2024 19:03:12 +0800 Message-Id: <5c919e5002739d2f396918d11bde5711d4ae8182.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add swap available space validation before spliting shmem large folio to avoid redundant split, since we can not write shmem folio to the swap device in this case. Signed-off-by: Baolin Wang --- mm/vmscan.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/vmscan.c b/mm/vmscan.c index 6981a71c8ef0..bf11c0cbf12e 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1252,6 +1252,14 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_list, } } else if (folio_test_swapbacked(folio) && folio_test_large(folio)) { + + /* + * Do not split shmem folio if no swap memory + * available. + */ + if (!total_swap_pages) + goto activate_locked; + /* Split shmem folio */ if (split_folio_to_list(folio, folio_list)) goto keep_locked; --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69DEE44384 for ; Tue, 21 May 2024 11:03:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.119 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289421; cv=none; b=B9Fia6MXdJaitoP/XGIXwpcM26vpEmza7FxQ/66cBbOPJC95XVByS3pmI4inVT5CznCCt187vqIAoeLmR/MoAbmrQCEftqEwsQm4bs4LXFOl0i59xvvSt486iji7j8JeAWa+8RZM1fw7mIBiyIynmpWOSsbGrcQeOLT/KL69GzM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289421; c=relaxed/simple; bh=joGBV1HzsxEZPHmpZZMSyuQ/Mwl/EwRqzN4M/M0mgsk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bb3gtr5e/h4/Ww9LHYsMHuXWlZ3bbT5eA9MWjnaJ+w2wGY0TfqZwF5NdFFi8iyND6iGkUDzVJ+PIhN/iJAhBcNCP63F1EEJ3hILGqfwq3sDFFvJv+H1XtR2ikdZh0W+Kep0iuYTgodHtXQxisGEao5DvER1RGVrkmBlakUjEgnw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=QqaMVMxk; arc=none smtp.client-ip=115.124.30.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="QqaMVMxk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289416; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=AnBwKR9CDgtaWp21L3C4wGJsMstG019ckRvWwRbPXDM=; b=QqaMVMxkO39baTbt3EFevek7dyw2Du373ayknRz2U6NGfFkPoppI+sT+Vo17XfRqfZYGONl4Ug32Ot3vzD/bW2GBCz9ozGGuyYuHGuzLl/ISlhLj1d3vMmIynNhNEWB+WAgDUUi0tscqPg3NkRuNDKgpAP+3eB+ugQKwUJCCv+E= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R751e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033068173054;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xkOjU_1716289413; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xkOjU_1716289413) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:34 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/8] mm: swap: extend swap_shmem_alloc() to support batch SWAP_MAP_SHMEM flag setting Date: Tue, 21 May 2024 19:03:13 +0800 Message-Id: <3ded3d39f19250ed4f16a42f22121b5f2c2dfb5c.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support shmem large folio swap operations, add a new parameter to swap_shmem_alloc() that allows batch SWAP_MAP_SHMEM flag setting for shmem swap entries. While we are at it, using folio_nr_pages() to get the number of pages of the folio as a preparation. Signed-off-by: Baolin Wang --- include/linux/swap.h | 4 +- mm/shmem.c | 6 ++- mm/swapfile.c | 98 +++++++++++++++++++++++--------------------- 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 48131b869a4d..78922922abbd 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -479,7 +479,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t en= try); extern swp_entry_t get_swap_page_of_type(int); extern int get_swap_pages(int n, swp_entry_t swp_entries[], int order); extern int add_swap_count_continuation(swp_entry_t, gfp_t); -extern void swap_shmem_alloc(swp_entry_t); +extern void swap_shmem_alloc(swp_entry_t, int); extern int swap_duplicate(swp_entry_t); extern int swapcache_prepare(swp_entry_t); extern void swap_free_nr(swp_entry_t entry, int nr_pages); @@ -546,7 +546,7 @@ static inline int add_swap_count_continuation(swp_entry= _t swp, gfp_t gfp_mask) return 0; } =20 -static inline void swap_shmem_alloc(swp_entry_t swp) +static inline void swap_shmem_alloc(swp_entry_t swp, int nr) { } =20 diff --git a/mm/shmem.c b/mm/shmem.c index fd2cb2e73a21..daab124c3e61 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1433,6 +1433,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) struct shmem_sb_info *sbinfo =3D SHMEM_SB(inode->i_sb); swp_entry_t swap; pgoff_t index; + int nr_pages; =20 /* * Our capabilities prevent regular writeback or sync from ever calling @@ -1465,6 +1466,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) } =20 index =3D folio->index; + nr_pages =3D folio_nr_pages(folio); =20 /* * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC @@ -1517,8 +1519,8 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) if (add_to_swap_cache(folio, swap, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, NULL) =3D=3D 0) { - shmem_recalc_inode(inode, 0, 1); - swap_shmem_alloc(swap); + shmem_recalc_inode(inode, 0, nr_pages); + swap_shmem_alloc(swap, nr_pages); shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); =20 mutex_unlock(&shmem_swaplist_mutex); diff --git a/mm/swapfile.c b/mm/swapfile.c index 99e701620562..2f23b87ddcb3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3387,62 +3387,58 @@ void si_swapinfo(struct sysinfo *val) * - swap-cache reference is requested but the entry is not used. -> ENOENT * - swap-mapped reference requested but needs continued swap count. -> EN= OMEM */ -static int __swap_duplicate(swp_entry_t entry, unsigned char usage) +static int __swap_duplicate(struct swap_info_struct *p, unsigned long offs= et, + int nr, unsigned char usage) { - struct swap_info_struct *p; struct swap_cluster_info *ci; - unsigned long offset; unsigned char count; unsigned char has_cache; - int err; + int err, i; =20 - p =3D swp_swap_info(entry); - - offset =3D swp_offset(entry); ci =3D lock_cluster_or_swap_info(p, offset); =20 - count =3D p->swap_map[offset]; - - /* - * swapin_readahead() doesn't check if a swap entry is valid, so the - * swap entry could be SWAP_MAP_BAD. Check here with lock held. - */ - if (unlikely(swap_count(count) =3D=3D SWAP_MAP_BAD)) { - err =3D -ENOENT; - goto unlock_out; - } - - has_cache =3D count & SWAP_HAS_CACHE; - count &=3D ~SWAP_HAS_CACHE; - err =3D 0; - - if (usage =3D=3D SWAP_HAS_CACHE) { + for (i =3D 0; i < nr; i++) { + count =3D p->swap_map[offset + i]; =20 - /* set SWAP_HAS_CACHE if there is no cache and entry is used */ - if (!has_cache && count) - has_cache =3D SWAP_HAS_CACHE; - else if (has_cache) /* someone else added cache */ - err =3D -EEXIST; - else /* no users remaining */ + /* + * swapin_readahead() doesn't check if a swap entry is valid, so the + * swap entry could be SWAP_MAP_BAD. Check here with lock held. + */ + if (unlikely(swap_count(count) =3D=3D SWAP_MAP_BAD)) { err =3D -ENOENT; + break; + } =20 - } else if (count || has_cache) { + has_cache =3D count & SWAP_HAS_CACHE; + count &=3D ~SWAP_HAS_CACHE; + err =3D 0; + + if (usage =3D=3D SWAP_HAS_CACHE) { + /* set SWAP_HAS_CACHE if there is no cache and entry is used */ + if (!has_cache && count) + has_cache =3D SWAP_HAS_CACHE; + else if (has_cache) /* someone else added cache */ + err =3D -EEXIST; + else /* no users remaining */ + err =3D -ENOENT; + } else if (count || has_cache) { + if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + count +=3D usage; + else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) + err =3D -EINVAL; + else if (swap_count_continued(p, offset + i, count)) + count =3D COUNT_CONTINUED; + else + err =3D -ENOMEM; + } else + err =3D -ENOENT; /* unused swap entry */ =20 - if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) - count +=3D usage; - else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) - err =3D -EINVAL; - else if (swap_count_continued(p, offset, count)) - count =3D COUNT_CONTINUED; - else - err =3D -ENOMEM; - } else - err =3D -ENOENT; /* unused swap entry */ + if (err) + break; =20 - if (!err) - WRITE_ONCE(p->swap_map[offset], count | has_cache); + WRITE_ONCE(p->swap_map[offset + i], count | has_cache); + } =20 -unlock_out: unlock_cluster_or_swap_info(p, ci); return err; } @@ -3451,9 +3447,12 @@ static int __swap_duplicate(swp_entry_t entry, unsig= ned char usage) * Help swapoff by noting that swap entry belongs to shmem/tmpfs * (in which case its reference count is never incremented). */ -void swap_shmem_alloc(swp_entry_t entry) +void swap_shmem_alloc(swp_entry_t entry, int nr) { - __swap_duplicate(entry, SWAP_MAP_SHMEM); + struct swap_info_struct *p =3D swp_swap_info(entry); + unsigned long offset =3D swp_offset(entry); + + __swap_duplicate(p, offset, nr, SWAP_MAP_SHMEM); } =20 /* @@ -3465,9 +3464,11 @@ void swap_shmem_alloc(swp_entry_t entry) */ int swap_duplicate(swp_entry_t entry) { + struct swap_info_struct *p =3D swp_swap_info(entry); + unsigned long offset =3D swp_offset(entry); int err =3D 0; =20 - while (!err && __swap_duplicate(entry, 1) =3D=3D -ENOMEM) + while (!err && __swap_duplicate(p, offset, 1, 1) =3D=3D -ENOMEM) err =3D add_swap_count_continuation(entry, GFP_ATOMIC); return err; } @@ -3482,7 +3483,10 @@ int swap_duplicate(swp_entry_t entry) */ int swapcache_prepare(swp_entry_t entry) { - return __swap_duplicate(entry, SWAP_HAS_CACHE); + struct swap_info_struct *p =3D swp_swap_info(entry); + unsigned long offset =3D swp_offset(entry); + + return __swap_duplicate(p, offset, 1, SWAP_HAS_CACHE); } =20 void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F140B770E1 for ; Tue, 21 May 2024 11:03:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289426; cv=none; b=jyq30i0Vd7YfaREH8u8FILMhZsgt3Pda+rRTSpTJe0Sx/h9PoWNAvTTKJwIll+kzf1VjuU/0qGvfPyToyX3bxl3YH/xpvEBnixDuETkNSPO8c+WZ71G+GODcVcam1VcAWLLP7DkzzbDbqbie1v83K+fSgN4l87M3IpftWCEKd88= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289426; c=relaxed/simple; bh=x55X4hb+WDGyX98I9cJOxVIp/SSeNLmmTlFy+aZV4Jo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S/NLvAU1Ood10InBiVVTA7BpKkY42inIKZbZno8AhB+b+oS34MNpLXU2dZavMuqAV4xDJErR47z6qKuHsO1lT6ur66BA3F0t5UmYCSN6Ff6SS30lgiA3HZ0yS94iyOOvyi3tVYRYgWMnEk8VZsz4vCSJ1jvbj8kj2a9MSF7RQRw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=a0abYcVE; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="a0abYcVE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289417; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=ArTvZKlaOes4XoFtzrKMEwLUH8CHPrOqCRPSPeGMt/g=; b=a0abYcVElqGnhHB+0BarctgCnQwfetl3e4cTU2SDMdZg6QXiM3aff4nrNph168S8acADzcyo7xn/8G4ZLvhYctlWrNMdKetxd6kr96VgLnCFf5jjrqcrrspsbk4ewqh9OQuVcYFhvn5Z70qdgDnppgNdXGWqMk4KYkCj/NTA6KY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xeTCm_1716289415; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xeTCm_1716289415) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:36 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 4/8] mm: shmem: support large folio allocation for shmem_replace_folio() Date: Tue, 21 May 2024 19:03:14 +0800 Message-Id: <73d93136d8469658715388878c947e8952164c53.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support large folio swapin for shmem in the following patches, add large folio allocation for the new replacement folio in shmem_replace_folio= (), as well as updating statistics using the number of pages in the folio. Signed-off-by: Baolin Wang --- mm/shmem.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index daab124c3e61..74821a7031b8 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1901,8 +1901,7 @@ static int shmem_replace_folio(struct folio **foliop,= gfp_t gfp, * limit chance of success by further cpuset and node constraints. */ gfp &=3D ~GFP_CONSTRAINT_MASK; - VM_BUG_ON_FOLIO(folio_test_large(old), old); - new =3D shmem_alloc_folio(gfp, info, index); + new =3D shmem_alloc_hugefolio(gfp, info, index, folio_order(old)); if (!new) return -ENOMEM; =20 @@ -1923,11 +1922,13 @@ static int shmem_replace_folio(struct folio **folio= p, gfp_t gfp, xa_lock_irq(&swap_mapping->i_pages); error =3D shmem_replace_entry(swap_mapping, swap_index, old, new); if (!error) { + int nr_pages =3D folio_nr_pages(old); + mem_cgroup_migrate(old, new); - __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1); - __lruvec_stat_mod_folio(new, NR_SHMEM, 1); - __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1); - __lruvec_stat_mod_folio(old, NR_SHMEM, -1); + __lruvec_stat_mod_folio(new, NR_FILE_PAGES, nr_pages); + __lruvec_stat_mod_folio(new, NR_SHMEM, nr_pages); + __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -nr_pages); + __lruvec_stat_mod_folio(old, NR_SHMEM, -nr_pages); } xa_unlock_irq(&swap_mapping->i_pages); =20 --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FB62612EB for ; Tue, 21 May 2024 11:03:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.119 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289423; cv=none; b=Wzb9fNTbJHeK1q3fsP5/mh6V7ioi8AJi/yJtSWslJM48px9BjOL/Il3dqEaCuKcRE7ds6X9DEjzbNeo2KRLGM5ffPfjyCEnG8txOJfsUNf/LkHI+jFpdojAvBqzGIZhCUinHce3EsgwBCAFyGU6ONtCJkWofdqi6sVoQ88YlCn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289423; c=relaxed/simple; bh=QktWnmZza0+8gvOpDIgJiQ9JxnbnzLBik8mUH7+XnKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QRl3RX6E0KvV8m5l3/gWwYrGw6ERdxaxqMsducs3IMsuI3ywj+Qeu5ieJQvInOSuaMm4vV0+IcO/fHkH3W9/HjEyG4KZLy+v93w+BbLKJUkXuKkUrX4wsPKHjz+t4LA6K6m0VBU+MMgGRI1uhTnxmtOBgOqbZAs7BkfKglsnIzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=oVY+upUy; arc=none smtp.client-ip=115.124.30.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="oVY+upUy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289419; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=eLWM5LDYn4qmAcvClqJJz7Ry6ODVE2l8ecGnrwuiQN0=; b=oVY+upUyau3AQlXbvwgwJy9gBbrTKIB2NAPxsZlcumizQeO0ndU8eaqP9jB7AlXnTaLq8n1TFWXbA0y4ezsBb0BnZw0xAat0wdSTXX5eHwWeGlQGzdBxehKlA2v0SRDpqZhLOG93CSVGys6ziT/166ghuf8Z6WcD3acwgLqVjFU= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R261e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067111;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xeTDd_1716289416; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xeTDd_1716289416) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:37 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 5/8] mm: shmem: extend shmem_partial_swap_usage() to support large folio swap Date: Tue, 21 May 2024 19:03:15 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support shmem large folio swapout in the following patches, using xa_get_order() to get the order of the swap entry to calculate the swap usage of shmem. Signed-off-by: Baolin Wang --- mm/shmem.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 74821a7031b8..bc099e8b9952 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -865,13 +865,16 @@ unsigned long shmem_partial_swap_usage(struct address= _space *mapping, struct page *page; unsigned long swapped =3D 0; unsigned long max =3D end - 1; + int order; =20 rcu_read_lock(); xas_for_each(&xas, page, max) { if (xas_retry(&xas, page)) continue; - if (xa_is_value(page)) - swapped++; + if (xa_is_value(page)) { + order =3D xa_get_order(xas.xa, xas.xa_index); + swapped +=3D 1 << order; + } if (xas.xa_index =3D=3D max) break; if (need_resched()) { --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65495763E2 for ; Tue, 21 May 2024 11:03:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289427; cv=none; b=lkc9TniMm9Th7JY8P6VWknyyXGkR0m6/PVIZboCI1WTGuYoW07TEP/P98ydzO9xGKF3EKlpdz1ul1v6u6t/I/R3tioOSAc+anjp84sEeUm8ZHczo5Vmviymqub7cne6sGE2udq5bp0uWxo5Nc8Sc2N6UDp90/aFY73fn4Vevz0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289427; c=relaxed/simple; bh=JbpnkhBzrR/4hgYIwKsVc5Yaw6wzRpHSnBdM3hIWcD4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d38B84JGcFMng8+NuxLyfuDaNM+XE68FnWLa/XzFBgY07IrfvO9JfoTFDqjSNk5qv8NUh1Tbi2Ldlr7K2CxvScCjg0rCRZTr/YzKUpYXM8WzXK8tXq6lFV2QsNhaLZYqZMa17wEVQneTe1VU/Cxz6LrA/6d6s9NB4FhiOYaRuvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=KddY3vsg; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="KddY3vsg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289422; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=RUK8yowxerwNkroF7zPanFQGgOdBeDeZsuZ9hXwq2Jg=; b=KddY3vsgh3ZyQny2pUAsR9BB+dk6jyCRv+BewdrEmAOlNwU8g94yc4AZDoijXwF63L7u9VpCYZBPEsUwECE520qFbwvqAsE9fmL4vfVx0pt9ULm7vNMvFtNf+ygls7ZxsBeBfi1t8EcJiGasKaTrpr0fjmlZ+3NETiWrpwvV8GI= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067112;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xkOmJ_1716289418; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xkOmJ_1716289418) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:39 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 6/8] mm: add new 'orders' parameter for find_get_entries() and find_lock_entries() Date: Tue, 21 May 2024 19:03:16 +0800 Message-Id: <88b28bb5929ff8dd23278974138c85c2226e333d.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the following patches, shmem will support the swap out of large folios, which means the shmem mappings may contain large order swap entries, so an 'orders' array is added for find_get_entries() and find_lock_entries() to obtain the order size of shmem swap entries, which will help in the release of shmem large folio swap entries. Signed-off-by: Baolin Wang --- mm/filemap.c | 27 +++++++++++++++++++++++++-- mm/internal.h | 4 ++-- mm/shmem.c | 17 +++++++++-------- mm/truncate.c | 8 ++++---- 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/mm/filemap.c b/mm/filemap.c index ec273b00ce5f..9d8544df5e4a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2036,14 +2036,24 @@ static inline struct folio *find_get_entry(struct x= a_state *xas, pgoff_t max, * Return: The number of entries which were found. */ unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, - pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices) + pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices, + int *orders) { XA_STATE(xas, &mapping->i_pages, *start); struct folio *folio; + int order; =20 rcu_read_lock(); while ((folio =3D find_get_entry(&xas, end, XA_PRESENT)) !=3D NULL) { indices[fbatch->nr] =3D xas.xa_index; + if (orders) { + if (!xa_is_value(folio)) + order =3D folio_order(folio); + else + order =3D xa_get_order(xas.xa, xas.xa_index); + + orders[fbatch->nr] =3D order; + } if (!folio_batch_add(fbatch, folio)) break; } @@ -2056,6 +2066,8 @@ unsigned find_get_entries(struct address_space *mappi= ng, pgoff_t *start, folio =3D fbatch->folios[idx]; if (!xa_is_value(folio)) nr =3D folio_nr_pages(folio); + else if (orders) + nr =3D 1 << orders[idx]; *start =3D indices[idx] + nr; } return folio_batch_count(fbatch); @@ -2082,10 +2094,12 @@ unsigned find_get_entries(struct address_space *map= ping, pgoff_t *start, * Return: The number of entries which were found. */ unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, - pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices) + pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices, + int *orders) { XA_STATE(xas, &mapping->i_pages, *start); struct folio *folio; + int order; =20 rcu_read_lock(); while ((folio =3D find_get_entry(&xas, end, XA_PRESENT))) { @@ -2099,9 +2113,16 @@ unsigned find_lock_entries(struct address_space *map= ping, pgoff_t *start, if (folio->mapping !=3D mapping || folio_test_writeback(folio)) goto unlock; + if (orders) + order =3D folio_order(folio); VM_BUG_ON_FOLIO(!folio_contains(folio, xas.xa_index), folio); + } else if (orders) { + order =3D xa_get_order(xas.xa, xas.xa_index); } + + if (orders) + orders[fbatch->nr] =3D order; indices[fbatch->nr] =3D xas.xa_index; if (!folio_batch_add(fbatch, folio)) break; @@ -2120,6 +2141,8 @@ unsigned find_lock_entries(struct address_space *mapp= ing, pgoff_t *start, folio =3D fbatch->folios[idx]; if (!xa_is_value(folio)) nr =3D folio_nr_pages(folio); + else if (orders) + nr =3D 1 << orders[idx]; *start =3D indices[idx] + nr; } return folio_batch_count(fbatch); diff --git a/mm/internal.h b/mm/internal.h index 17b0a1824948..755df223cd3a 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -339,9 +339,9 @@ static inline void force_page_cache_readahead(struct ad= dress_space *mapping, } =20 unsigned find_lock_entries(struct address_space *mapping, pgoff_t *start, - pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); + pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices, int *orders); unsigned find_get_entries(struct address_space *mapping, pgoff_t *start, - pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices); + pgoff_t end, struct folio_batch *fbatch, pgoff_t *indices, int *orders); void filemap_free_folio(struct address_space *mapping, struct folio *folio= ); int truncate_inode_folio(struct address_space *mapping, struct folio *foli= o); bool truncate_inode_partial_folio(struct folio *folio, loff_t start, diff --git a/mm/shmem.c b/mm/shmem.c index bc099e8b9952..b3e39d9cf42c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -840,14 +840,14 @@ static void shmem_delete_from_page_cache(struct folio= *folio, void *radswap) * Remove swap entry from page cache, free the swap and its page cache. */ static int shmem_free_swap(struct address_space *mapping, - pgoff_t index, void *radswap) + pgoff_t index, void *radswap, int order) { void *old; =20 old =3D xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); if (old !=3D radswap) return -ENOENT; - free_swap_and_cache(radix_to_swp_entry(radswap)); + free_swap_and_cache_nr(radix_to_swp_entry(radswap), 1 << order); return 0; } =20 @@ -981,6 +981,7 @@ static void shmem_undo_range(struct inode *inode, loff_= t lstart, loff_t lend, pgoff_t end =3D (lend + 1) >> PAGE_SHIFT; struct folio_batch fbatch; pgoff_t indices[PAGEVEC_SIZE]; + int orders[PAGEVEC_SIZE]; struct folio *folio; bool same_folio; long nr_swaps_freed =3D 0; @@ -996,15 +997,15 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, loff_t lend, folio_batch_init(&fbatch); index =3D start; while (index < end && find_lock_entries(mapping, &index, end - 1, - &fbatch, indices)) { + &fbatch, indices, orders)) { for (i =3D 0; i < folio_batch_count(&fbatch); i++) { folio =3D fbatch.folios[i]; =20 if (xa_is_value(folio)) { if (unfalloc) continue; - nr_swaps_freed +=3D !shmem_free_swap(mapping, - indices[i], folio); + if (!shmem_free_swap(mapping, indices[i], folio, orders[i])) + nr_swaps_freed +=3D 1 << orders[i]; continue; } =20 @@ -1058,7 +1059,7 @@ static void shmem_undo_range(struct inode *inode, lof= f_t lstart, loff_t lend, cond_resched(); =20 if (!find_get_entries(mapping, &index, end - 1, &fbatch, - indices)) { + indices, orders)) { /* If all gone or hole-punch or unfalloc, we're done */ if (index =3D=3D start || end !=3D -1) break; @@ -1072,12 +1073,12 @@ static void shmem_undo_range(struct inode *inode, l= off_t lstart, loff_t lend, if (xa_is_value(folio)) { if (unfalloc) continue; - if (shmem_free_swap(mapping, indices[i], folio)) { + if (shmem_free_swap(mapping, indices[i], folio, orders[i])) { /* Swap was replaced by page: retry */ index =3D indices[i]; break; } - nr_swaps_freed++; + nr_swaps_freed +=3D 1 << orders[i]; continue; } =20 diff --git a/mm/truncate.c b/mm/truncate.c index e99085bf3d34..514834045bc8 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -352,7 +352,7 @@ void truncate_inode_pages_range(struct address_space *m= apping, folio_batch_init(&fbatch); index =3D start; while (index < end && find_lock_entries(mapping, &index, end - 1, - &fbatch, indices)) { + &fbatch, indices, NULL)) { truncate_folio_batch_exceptionals(mapping, &fbatch, indices); for (i =3D 0; i < folio_batch_count(&fbatch); i++) truncate_cleanup_folio(fbatch.folios[i]); @@ -392,7 +392,7 @@ void truncate_inode_pages_range(struct address_space *m= apping, while (index < end) { cond_resched(); if (!find_get_entries(mapping, &index, end - 1, &fbatch, - indices)) { + indices, NULL)) { /* If all gone from start onwards, we're done */ if (index =3D=3D start) break; @@ -496,7 +496,7 @@ unsigned long mapping_try_invalidate(struct address_spa= ce *mapping, int i; =20 folio_batch_init(&fbatch); - while (find_lock_entries(mapping, &index, end, &fbatch, indices)) { + while (find_lock_entries(mapping, &index, end, &fbatch, indices, NULL)) { for (i =3D 0; i < folio_batch_count(&fbatch); i++) { struct folio *folio =3D fbatch.folios[i]; =20 @@ -622,7 +622,7 @@ int invalidate_inode_pages2_range(struct address_space = *mapping, =20 folio_batch_init(&fbatch); index =3D start; - while (find_get_entries(mapping, &index, end, &fbatch, indices)) { + while (find_get_entries(mapping, &index, end, &fbatch, indices, NULL)) { for (i =3D 0; i < folio_batch_count(&fbatch); i++) { struct folio *folio =3D fbatch.folios[i]; =20 --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D1FC770F3 for ; Tue, 21 May 2024 11:03:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.101 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289427; cv=none; b=dgC7KlT1aqnaGw1uL4py1opqInU4Hu1IqD3VLHdvGbuAxGRon9afJBuiNDf5jB3qQJbOd70l/k2wD/GgSw4bz8w0UNk4akzy5gtVuKygiitWBK4rY+wbZ8XrAmWSpFNWxE2cAHSbccDghaqEgiGOrKdCC8J+xJrGu2+uKkLQZCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289427; c=relaxed/simple; bh=CkVLodKJ9ucWBi7mpVP2iJelihrLRHWS7bUyzqopUjM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vDLJ40h+i2eq07gOOgQD4zd2mAmzxnRjnbH/VyFW77bXMyYuRhOfdQo2QAAfRcuOFo6FB4Js3dyUPw8oHHFULgTwdAEaLR3ZhVY9bo5sXLKpFY/36xgD1HPdZj6zg6UxMUohRHfT7DPDh3zo5T+ZzqTFF1T6GUFdZKLgLNn4R50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=fUJdX6hW; arc=none smtp.client-ip=115.124.30.101 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="fUJdX6hW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289423; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/V3DnBye2LjO7u5ouBgBU8nPrUqi/2Egdsz3zKACdwM=; b=fUJdX6hWqHjDussoPyT9DN3O/eSP1VyI5mUQHx5keTV89BRSE3o3E2RJsTSshV+b+JIGO0raC3TK6naifFHJpE+xUlla0YCYIdAlHHTmULJb9TCfZLgw+IdgmZpxUg6Xxo+TSVJ7llXpTFTP6/n/176rElMjaulhKZkoNGCxt34= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R931e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067113;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xl985_1716289419; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xl985_1716289419) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:41 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 7/8] mm: shmem: use swap_free_nr() to free shmem swap entries Date: Tue, 21 May 2024 19:03:17 +0800 Message-Id: <90fda314a9e966374fe837ae3ebd74708380479d.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" As a preparation for supporting shmem large folio swapout, use swap_free_nr= () to free some continuous swap entries of the shmem large folio when the large folio was swapped in from the swap cache. Signed-off-by: Baolin Wang --- mm/shmem.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index b3e39d9cf42c..fdc71e14916c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1962,6 +1962,7 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, struct address_space *mapping =3D inode->i_mapping; swp_entry_t swapin_error; void *old; + int nr_pages; =20 swapin_error =3D make_poisoned_swp_entry(); old =3D xa_cmpxchg_irq(&mapping->i_pages, index, @@ -1970,6 +1971,7 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, if (old !=3D swp_to_radix_entry(swap)) return; =20 + nr_pages =3D folio_nr_pages(folio); folio_wait_writeback(folio); delete_from_swap_cache(folio); /* @@ -1977,8 +1979,8 @@ static void shmem_set_folio_swapin_error(struct inode= *inode, pgoff_t index, * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) * in shmem_evict_inode(). */ - shmem_recalc_inode(inode, -1, -1); - swap_free(swap); + shmem_recalc_inode(inode, -nr_pages, -nr_pages); + swap_free_nr(swap, nr_pages); } =20 /* @@ -1997,7 +1999,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, struct swap_info_struct *si; struct folio *folio =3D NULL; swp_entry_t swap; - int error; + int error, nr_pages; =20 VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); swap =3D radix_to_swp_entry(*foliop); @@ -2044,6 +2046,7 @@ static int shmem_swapin_folio(struct inode *inode, pg= off_t index, goto failed; } folio_wait_writeback(folio); + nr_pages =3D folio_nr_pages(folio); =20 /* * Some architectures may have to restore extra metadata to the @@ -2062,14 +2065,14 @@ static int shmem_swapin_folio(struct inode *inode, = pgoff_t index, if (error) goto failed; =20 - shmem_recalc_inode(inode, 0, -1); + shmem_recalc_inode(inode, 0, -nr_pages); =20 if (sgp =3D=3D SGP_WRITE) folio_mark_accessed(folio); =20 delete_from_swap_cache(folio); folio_mark_dirty(folio); - swap_free(swap); + swap_free_nr(swap, nr_pages); put_swap_device(si); =20 *foliop =3D folio; --=20 2.39.3 From nobody Wed Feb 11 10:00:07 2026 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D863479B9D for ; Tue, 21 May 2024 11:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289433; cv=none; b=cr0H+VLJdIsQYMhWcN9KjG9hfR+vfP87/4KVqA0bI/uVebuxPJY9s1FbGfOfhysWFdGRH2vg9A9NbcKvVQp5jN3aQYj4hi6VDQGI6mq/FH8pkBReNXq3GClob5CxRAs9NE/tLDqlLheHv3QmTpEV6BHAiRuZeFjBcZy+XPNTyVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716289433; c=relaxed/simple; bh=gsjyHEcCKLsatizaYh93zXZYrs2WIQaycqvPWmajK+I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RK+nNBY5oF+BmZbTF1xlwHDxntdILSNLL7Fvae0ZUfn6U7xubykbn9BhDBF1yYSHeyUZ6CsuVwfQ1T4+fU/AQpc1VAR5cF48ELaOO2XJOJ8REjgoyXTzSXLqxst5m2QWiohQWQZBWFCvaLUlM0b8YsjhsVcsuJGoorqqrt4jyFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=JSbbCpxS; arc=none smtp.client-ip=115.124.30.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="JSbbCpxS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1716289424; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=3ZBjHevxDMMp9OTvEWxh5sKqkBiotmw21gIhPQdkVy8=; b=JSbbCpxSuHCmM8c8wQVf7vJ92cG/QxZ9CgqZn8gfNjkl1dkF+01Sx0t1M9gEimdaz/cssXeJpAmw+ZMx9yede5bRvO9tEThPVavcjSG6um/Kt7J7zxhOoFk/YhXXH2wXEuFfm0wf/lMDZgu6zO65tXQohpf0OVhk/2reNUSXYHo= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R551e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067110;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=17;SR=0;TI=SMTPD_---0W6xl98k_1716289421; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W6xl98k_1716289421) by smtp.aliyun-inc.com; Tue, 21 May 2024 19:03:42 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, ioworker0@gmail.com, hrisl@kernel.org, p.raghav@samsung.com, da.gomez@samsung.com, wangkefeng.wang@huawei.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, shy828301@gmail.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 8/8] mm: shmem: support large folio swap out Date: Tue, 21 May 2024 19:03:18 +0800 Message-Id: <1f50ac5f9dfa69c3c7cc57440eae2b1728178cca.1716285099.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Shmem will support large folio allocation [1] [2] to get a better performan= ce, however, the memory reclaim still splits the precious large folios when try= ing to swap out shmem, which may lead to the memory fragmentation issue and can= not take advantage of the large folio for shmeme. Moreover, the swap code already supports for swapping out large folio witho= ut split, hence this patch set supports the large folio swap out for shmem. Note the i915_gem_shmem driver still need to be split when swapping, thus add a new flag 'split_large_folio' for writeback_control to indicate spliti= ng the large folio. [1] https://lore.kernel.org/all/cover.1715571279.git.baolin.wang@linux.alib= aba.com/ [2] https://lore.kernel.org/all/20240515055719.32577-1-da.gomez@samsung.com/ Signed-off-by: Baolin Wang --- drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 1 + include/linux/writeback.h | 1 + mm/shmem.c | 3 +-- mm/vmscan.c | 14 ++++++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c b/drivers/gpu/drm/i9= 15/gem/i915_gem_shmem.c index 38b72d86560f..968274be14ef 100644 --- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c +++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c @@ -308,6 +308,7 @@ void __shmem_writeback(size_t size, struct address_spac= e *mapping) .range_start =3D 0, .range_end =3D LLONG_MAX, .for_reclaim =3D 1, + .split_large_folio =3D 1, }; unsigned long i; =20 diff --git a/include/linux/writeback.h b/include/linux/writeback.h index 112d806ddbe4..6f2599244ae0 100644 --- a/include/linux/writeback.h +++ b/include/linux/writeback.h @@ -63,6 +63,7 @@ struct writeback_control { unsigned range_cyclic:1; /* range_start is cyclic */ unsigned for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ unsigned unpinned_netfs_wb:1; /* Cleared I_PINNING_NETFS_WB */ + unsigned split_large_folio:1; /* Split large folio for shmem writeback */ =20 /* * When writeback IOs are bounced through async layers, only the diff --git a/mm/shmem.c b/mm/shmem.c index fdc71e14916c..6645169aa913 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -776,7 +776,6 @@ static int shmem_add_to_page_cache(struct folio *folio, VM_BUG_ON_FOLIO(index !=3D round_down(index, nr), folio); VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); - VM_BUG_ON(expected && folio_test_large(folio)); =20 folio_ref_add(folio, nr); folio->mapping =3D mapping; @@ -1460,7 +1459,7 @@ static int shmem_writepage(struct page *page, struct = writeback_control *wbc) * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, * and its shmem_writeback() needs them to be split when swapping. */ - if (folio_test_large(folio)) { + if (wbc->split_large_folio && folio_test_large(folio)) { /* Ensure the subpages are still dirty */ folio_test_set_dirty(folio); if (split_huge_page(page) < 0) diff --git a/mm/vmscan.c b/mm/vmscan.c index bf11c0cbf12e..856286e84d62 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1260,8 +1260,12 @@ static unsigned int shrink_folio_list(struct list_he= ad *folio_list, if (!total_swap_pages) goto activate_locked; =20 - /* Split shmem folio */ - if (split_folio_to_list(folio, folio_list)) + /* + * Only split shmem folio when CONFIG_THP_SWAP + * is not enabled. + */ + if (!IS_ENABLED(CONFIG_THP_SWAP) && + split_folio_to_list(folio, folio_list)) goto keep_locked; } =20 @@ -1363,10 +1367,16 @@ static unsigned int shrink_folio_list(struct list_h= ead *folio_list, * starts and then write it out here. */ try_to_unmap_flush_dirty(); +try_pageout: switch (pageout(folio, mapping, &plug)) { case PAGE_KEEP: goto keep_locked; case PAGE_ACTIVATE: + if (shmem_mapping(mapping) && folio_test_large(folio) && + !split_folio_to_list(folio, folio_list)) { + nr_pages =3D 1; + goto try_pageout; + } goto activate_locked; case PAGE_SUCCESS: stat->nr_pageout +=3D nr_pages; --=20 2.39.3