From patchwork Tue May 21 11:03:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13669305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5391C25B74 for ; Tue, 21 May 2024 11:03:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 411486B009E; Tue, 21 May 2024 07:03:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3995F6B009F; Tue, 21 May 2024 07:03:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EBDB6B00A0; Tue, 21 May 2024 07:03:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id EF2E56B009F for ; Tue, 21 May 2024 07:03:46 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 45232121176 for ; Tue, 21 May 2024 11:03:46 +0000 (UTC) X-FDA: 82142117652.21.CBBCCF5 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) by imf17.hostedemail.com (Postfix) with ESMTP id D149E40027 for ; Tue, 21 May 2024 11:03:41 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=QqaMVMxk; spf=pass (imf17.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1716289423; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AnBwKR9CDgtaWp21L3C4wGJsMstG019ckRvWwRbPXDM=; b=4ey8Qw9vMUETZ6zrjtxo6HaiqQdB1NCon57zocNNndUKCvVOuXrS6HxQXIv/V15UGee3Eh awoPB/gjbiyLp8uNCRBPx0WdTAfx+tSCQq8VL94b+xCmgqBe6fzjy0HhXVs81/rkMQlTHv syXk8izF93/VsEkbGwFKsisQoPJOagY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=QqaMVMxk; spf=pass (imf17.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.99 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716289423; a=rsa-sha256; cv=none; b=RBLS/0o4z5LmYiuDOWpQSuDP2CLdFhteTcYNctKUWYWC9l+XnWCRMDIPVMsK+ikKCr7SmI sLfcmEBUyIVdRNt0ncq/PV1jQ+4xYfdbU29CbznoX9za/RmDoDnmxT36EOWxk+d4O+2t9K rpNZf5O5VurSe5SnCwoDfr5N+tkM3l8= 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: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D149E40027 X-Stat-Signature: yhdrtha9dcrsmnjpn5dqycpxeaeodn6i X-HE-Tag: 1716289421-263960 X-HE-Meta: U2FsdGVkX1/5cKff7GC15cPr4lWzNVAev5gqPEtqf6Gjr+PnSHjHtK5kBfubbnHBPgvkRF7aqHJGeyAASVokbRauGN6+9z0CwlgZrxsntZg41TTq8ys9Qmz7ltd+HUHvMApBBxvtNgqWziPscaffYGxyzL4LzTw2AkNMnq21G+ecid1hbWmrtWV0ZNnKN8+pSzmVJEDcE87uJekscc770tWB/epem8GuRXA3ARH+5Bu6PDjmmAnp/wNaSIWa4Hm4tkcK1Tu4NKQx6m7EeNAVwO5C24KQBSKhXJ3RDH1K51rqE8yyx0bacSnDft/xv6V1LmcSCzH47ZcKTvG+xP3BMs3dP1jfKzSfYxzZ35ZW2PR33f0CY1qfTp+a4OZDmJTJzZeBrF5W+mICU/4bNc496G5C4n4+Iq0PmRxNpc4ByLVWjbbfs2EcEtvsjwdpR1DrnU2ydSmn4/D6396j16rbzMa1PqcxMUSUuRfmY9MRjPYF404iCmIriUFVOp7slL6te5N6rJMFM03iVhaQHvpjw7YaJXMKknqTLXXmSKMgGWxYPXvo7Y0mepybuj92xVayYXsZv0f0niNUrenrnrwTaBSt8jutBReDIlEHE/5RL05Wywnb6KFgQeXrA1hGP0qlClLwb6ZDJrODVuwicZEMg1n9K8FCvUsB+LqAoV8N7JtZQq0nAeJ767/lJrbECPJ4znZSca+Xc3x5WRap5DcaojP4bQqsjrpNphM4Sy76roKUGtiE1Jgs3gtUXg4kQIqJIJ94uBi0EYNpkbbd1c/qQbsQbcRO6RYebhu3RmmnHFqJU+dx0CXklhhW6vGZRp0CaTgVYhciP9T47HiMD++fxBcveJjedwx/S1ID59lcyZDjD3nwYecYEVrtYdFRBScyWuVnlQwfP9g9EsHucOir1/VtTCz/IjVH6ayIYG8d3tGOKB7KAsr6PJMi2of7c0K9QiPwLtR8EYc14M87YT1 EaixD6rj eyOZz0akqjd/rnvWO8yfW+mwtR4BrPmYpPPL6khDnR5V/jU/HL6nRU78sb/l6Bzm20DzZ3MXvpg4Qctp1bmsIaq5paQaSX9OC+oZn5W5I2e+LSZY7qVYjU+NYJFKayllsvvv0hjLoOnP8NhqniaP7xm935L+076x1nYCGGBnh9S3nCw1pRrbQ9xYM22JIQv3xlXegcqOQ09d5h0uO4ywRnMzw7aoPx+VSUfC7geQr9CCc0aceS3kdlPN1yFch2mg6mTjwFIovUPIcYS2Jw256vPN4nPt3yp369QMW X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 entry); 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; } -static inline void swap_shmem_alloc(swp_entry_t swp) +static inline void swap_shmem_alloc(swp_entry_t swp, int nr) { } 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 = SHMEM_SB(inode->i_sb); swp_entry_t swap; pgoff_t index; + int nr_pages; /* * 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) } index = folio->index; + nr_pages = folio_nr_pages(folio); /* * 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) == 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)); 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. -> ENOMEM */ -static int __swap_duplicate(swp_entry_t entry, unsigned char usage) +static int __swap_duplicate(struct swap_info_struct *p, unsigned long offset, + 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; - p = swp_swap_info(entry); - - offset = swp_offset(entry); ci = lock_cluster_or_swap_info(p, offset); - count = 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) == SWAP_MAP_BAD)) { - err = -ENOENT; - goto unlock_out; - } - - has_cache = count & SWAP_HAS_CACHE; - count &= ~SWAP_HAS_CACHE; - err = 0; - - if (usage == SWAP_HAS_CACHE) { + for (i = 0; i < nr; i++) { + count = p->swap_map[offset + i]; - /* set SWAP_HAS_CACHE if there is no cache and entry is used */ - if (!has_cache && count) - has_cache = SWAP_HAS_CACHE; - else if (has_cache) /* someone else added cache */ - err = -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) == SWAP_MAP_BAD)) { err = -ENOENT; + break; + } - } else if (count || has_cache) { + has_cache = count & SWAP_HAS_CACHE; + count &= ~SWAP_HAS_CACHE; + err = 0; + + if (usage == SWAP_HAS_CACHE) { + /* set SWAP_HAS_CACHE if there is no cache and entry is used */ + if (!has_cache && count) + has_cache = SWAP_HAS_CACHE; + else if (has_cache) /* someone else added cache */ + err = -EEXIST; + else /* no users remaining */ + err = -ENOENT; + } else if (count || has_cache) { + if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + count += usage; + else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) + err = -EINVAL; + else if (swap_count_continued(p, offset + i, count)) + count = COUNT_CONTINUED; + else + err = -ENOMEM; + } else + err = -ENOENT; /* unused swap entry */ - if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) - count += usage; - else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) - err = -EINVAL; - else if (swap_count_continued(p, offset, count)) - count = COUNT_CONTINUED; - else - err = -ENOMEM; - } else - err = -ENOENT; /* unused swap entry */ + if (err) + break; - if (!err) - WRITE_ONCE(p->swap_map[offset], count | has_cache); + WRITE_ONCE(p->swap_map[offset + i], count | has_cache); + } -unlock_out: unlock_cluster_or_swap_info(p, ci); return err; } @@ -3451,9 +3447,12 @@ static int __swap_duplicate(swp_entry_t entry, unsigned 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 = swp_swap_info(entry); + unsigned long offset = swp_offset(entry); + + __swap_duplicate(p, offset, nr, SWAP_MAP_SHMEM); } /* @@ -3465,9 +3464,11 @@ void swap_shmem_alloc(swp_entry_t entry) */ int swap_duplicate(swp_entry_t entry) { + struct swap_info_struct *p = swp_swap_info(entry); + unsigned long offset = swp_offset(entry); int err = 0; - while (!err && __swap_duplicate(entry, 1) == -ENOMEM) + while (!err && __swap_duplicate(p, offset, 1, 1) == -ENOMEM) err = 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 = swp_swap_info(entry); + unsigned long offset = swp_offset(entry); + + return __swap_duplicate(p, offset, 1, SWAP_HAS_CACHE); } void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry)