From patchwork Fri Jul 26 09:46:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13742537 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 8A6DFC3DA7F for ; Fri, 26 Jul 2024 09:47:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 028526B0099; Fri, 26 Jul 2024 05:47:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF2646B009C; Fri, 26 Jul 2024 05:47:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D444F6B009E; Fri, 26 Jul 2024 05:47:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AD60C6B0099 for ; Fri, 26 Jul 2024 05:47:13 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4DDFE1217BF for ; Fri, 26 Jul 2024 09:47:13 +0000 (UTC) X-FDA: 82381425546.07.A03F0D5 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by imf25.hostedemail.com (Postfix) with ESMTP id 685B8A000D for ; Fri, 26 Jul 2024 09:47:11 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Q1dvEpZX; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721987229; 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=QemnTZhx46SI6yTUVPx+f2+oh78G0V4lHylINHFTYuU=; b=utiHwH7qMbsTcwfAo7hikK4kbmSI4HSmb2PoqDOEZGH142ckeYLUi/osm5VsaY8y/I31a6 q4PH+wBJat3miw2Pm2l++AmpXTN/3nM43tzEUw6fB75UbA6uaJjAsCL06B6vz+OgVGSiOK 8LRZuxlM5jGBt2dpqyMXdcYncVuh5PI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Q1dvEpZX; spf=pass (imf25.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.215.173 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721987229; a=rsa-sha256; cv=none; b=w1L0AFeIiC0uP76Vk78y8ICsaC3MIbbyowfI6lKyIc2QY//C7nMlRGDH2UvlZRGZZHk2EK E4tF2TiMjo1ACcAztulXIguBGj0fissViNmUWtag154A1dlZBeWksIE2p8rTYr5bSC5Tfw TtWrlMAh6pA2rf8M883AKtPGnWhkw9k= Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-7aa7703cf08so638350a12.2 for ; Fri, 26 Jul 2024 02:47:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987230; x=1722592030; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QemnTZhx46SI6yTUVPx+f2+oh78G0V4lHylINHFTYuU=; b=Q1dvEpZXMKB7crCPwuU9tf5331Yxzjm3IZ9rHCe9w+6hxQ9qv3+BbiDFv9bT+mMycg wBykcOZdMqqetgYV4xxgpX2Ayp4dHBVzG43YRM4v7SkOzPVroDvnFGwJJrHGnVf9vBRx QEa0a1Sf+RRrlkvbmr8KE2/VnZqP/SALdOPf8+i20bIRqjEVs6q8lXziX+Sjv929pWdC SJ6wVHWKLk9Vsuvyis8IzGYc7Yr0W0/UvqETJ6wYJ+Vd+WQrEhfQzga/EU0YxLQHNfzj lV6yODOGPqsrt1Irm0fCSMH60Mabv1o/TkWuc1UjqaXSwmiuOEvwsQkQg3LAW0tvYJLp 1kvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987230; x=1722592030; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QemnTZhx46SI6yTUVPx+f2+oh78G0V4lHylINHFTYuU=; b=Q/+orHfI7hMUFfk+DIUU4pIqTHCY1jjfu851fehiNtAKokmP5wx6/iCsEy/QV+usJr 4jhr7IKYSG1OuT3fsHTpNbY3lI5+O8Z26AOI8A3J/FItNUCsqen4du7fILhb62e/GdlP sc0C6J4frv8yLWEjS/6ufjhtE4O0QlMBzS4B0xuVd+L3bVjRtrCU1po5eceOiFZ7pX+8 K+LWbPQ6DvCYfNFo+7r2dDY2S5ad2T8XubOq+7HUmDsxQzp4ndBNUZcVjMS1FYoBEvaU ZAg4ZuZBcSaEx4c1aSHEUvYth7ec8tAue0lPoYJzxnRxvAZVdP1sZlNDi+9HBNut5Ttr 5CvQ== X-Forwarded-Encrypted: i=1; AJvYcCV3lGf6yoeq+aFBLfd2SOVwuGWy69x2fhtbfhOqr98Ard/qztjAlUDoZAQVzT4mDlR4sIPrOsZ+oeNMjk6qbU4U+OQ= X-Gm-Message-State: AOJu0Yxa6d7tcwVoa/oE1X2+iLc1AuUBLZlEgcMEz1rSlLSLn+P8bGXZ /LooWCpukADwWm8zlNl+95GW8fhk4Itl2XyCGJamIhrvo2XPqKlY X-Google-Smtp-Source: AGHT+IEuYL/V6q+/Rf3zyV7tTxuHUrsd/jgokfNn9VNFhVU1hmhRuwtOEWzWwCDmdE70qZ7N+f0uTQ== X-Received: by 2002:a05:6a21:390:b0:1c4:23f0:9665 with SMTP id adf61e73a8af0-1c472aaeb54mr6667769637.29.1721987229972; Fri, 26 Jul 2024 02:47:09 -0700 (PDT) Received: from localhost.localdomain ([2407:7000:8942:5500:aaa1:59ff:fe57:eb97]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7d15e98sm28127455ad.99.2024.07.26.02.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:47:09 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: ying.huang@intel.com, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, yosryahmed@google.com Subject: [PATCH v5 1/4] mm: swap: introduce swapcache_prepare_nr and swapcache_clear_nr for large folios swap-in Date: Fri, 26 Jul 2024 21:46:15 +1200 Message-Id: <20240726094618.401593-2-21cnbao@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240726094618.401593-1-21cnbao@gmail.com> References: <20240726094618.401593-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 8mnaw56k7ahfy3ais46u7n8mypesnj7c X-Rspamd-Queue-Id: 685B8A000D X-Rspamd-Server: rspam11 X-HE-Tag: 1721987231-710986 X-HE-Meta: U2FsdGVkX19yGXr+BjYvdIsDI5l9pX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2YRFwHI/CRCVGAHIg5gJizi3Kgl4vYHTJvIScgZrj5uguj1B9ZBJolwkbX4NXZR8ez3fXd7xNg4ckYZLSz8RqqQEwfwzrpyXpAXECVoEYtLVfWDS712YlFdYw+7IHkLgsHke7FlkZtqW+B/XNUFYp+t3009ZbjyzexwZq2aVZ82N5IbSsDMUGpXwCyC2chqQkx2OTrhPfhyPmTTid0sQ3G6W78CpBxDyKc8GQqK5CQpDf8Ub82+hhq34TJ4c1PxtUpm/CyBfZv3NZJsUAFUswdbc8GgAhtiRHfOdt1Z+MyJaLI4NhJUlIQL52ByaksuYmzBA0wRtSE+/nR24bEAkpJKsjNkXNN65ehhrsaAZtX7skdzai2ZbNyAv 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: From: Barry Song Commit 13ddaf26be32 ("mm/swap: fix race when skipping swapcache") supports one entry only, to support large folio swap-in, we need to handle multiple swap entries. To optimize stack usage, we iterate twice in __swap_duplicate_nr(): the first time to verify that all entries are valid, and the second time to apply the modifications to the entries. Signed-off-by: Barry Song Reviewed-by: Baolin Wang --- include/linux/swap.h | 9 +++- mm/swap.h | 10 ++++- mm/swapfile.c | 102 ++++++++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 44 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ba7ea95d1c57..f1b28fd04533 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -480,7 +480,7 @@ 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 int swap_duplicate(swp_entry_t); -extern int swapcache_prepare(swp_entry_t); +extern int swapcache_prepare_nr(swp_entry_t entry, int nr); extern void swap_free_nr(swp_entry_t entry, int nr_pages); extern void swapcache_free_entries(swp_entry_t *entries, int n); extern void free_swap_and_cache_nr(swp_entry_t entry, int nr); @@ -554,7 +554,7 @@ static inline int swap_duplicate(swp_entry_t swp) return 0; } -static inline int swapcache_prepare(swp_entry_t swp) +static inline int swapcache_prepare_nr(swp_entry_t swp, int nr) { return 0; } @@ -612,6 +612,11 @@ static inline void swap_free(swp_entry_t entry) swap_free_nr(entry, 1); } +static inline int swapcache_prepare(swp_entry_t entry) +{ + return swapcache_prepare_nr(entry, 1); +} + #ifdef CONFIG_MEMCG static inline int mem_cgroup_swappiness(struct mem_cgroup *memcg) { diff --git a/mm/swap.h b/mm/swap.h index baa1fa946b34..81ff7eb0be9c 100644 --- a/mm/swap.h +++ b/mm/swap.h @@ -59,7 +59,7 @@ void __delete_from_swap_cache(struct folio *folio, void delete_from_swap_cache(struct folio *folio); void clear_shadow_from_swap_cache(int type, unsigned long begin, unsigned long end); -void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry); +void swapcache_clear_nr(struct swap_info_struct *si, swp_entry_t entry, int nr); struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr); struct folio *filemap_get_incore_folio(struct address_space *mapping, @@ -120,7 +120,7 @@ static inline int swap_writepage(struct page *p, struct writeback_control *wbc) return 0; } -static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +static inline void swapcache_clear_nr(struct swap_info_struct *si, swp_entry_t entry, int nr) { } @@ -172,4 +172,10 @@ static inline unsigned int folio_swap_flags(struct folio *folio) return 0; } #endif /* CONFIG_SWAP */ + +static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +{ + swapcache_clear_nr(si, entry, 1); +} + #endif /* _MM_SWAP_H */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 5f73a8553371..e688e46f1c62 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3363,7 +3363,7 @@ void si_swapinfo(struct sysinfo *val) } /* - * Verify that a swap entry is valid and increment its swap map count. + * Verify that nr swap entries are valid and increment their swap map counts. * * Returns error code in following case. * - success -> 0 @@ -3373,66 +3373,88 @@ 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_nr(swp_entry_t entry, unsigned char usage, int nr) { 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); + VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); ci = lock_cluster_or_swap_info(p, offset); - count = p->swap_map[offset]; + err = 0; + for (i = 0; i < nr; i++) { + count = p->swap_map[offset + i]; - /* - * 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; - } + /* + * 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; + has_cache = count & SWAP_HAS_CACHE; + count &= ~SWAP_HAS_CACHE; - if (usage == SWAP_HAS_CACHE) { + if (usage == SWAP_HAS_CACHE) { + /* set SWAP_HAS_CACHE if there is no cache and entry is used */ + if (!has_cache && count) + continue; + else if (has_cache) /* someone else added cache */ + err = -EEXIST; + else /* no users remaining */ + err = -ENOENT; - /* 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) { - } else if (count || has_cache) { + if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + continue; + else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) + err = -EINVAL; + else if (swap_count_continued(p, offset + i, count)) + continue; + else + err = -ENOMEM; + } else + err = -ENOENT; /* unused swap entry */ - if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) + if (err) + goto unlock_out; + } + + for (i = 0; i < nr; i++) { + count = p->swap_map[offset + i]; + has_cache = count & SWAP_HAS_CACHE; + count &= ~SWAP_HAS_CACHE; + + if (usage == SWAP_HAS_CACHE) + has_cache = SWAP_HAS_CACHE; + else 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 */ + count = COUNT_CONTINUED; - 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; } +static int __swap_duplicate(swp_entry_t entry, unsigned char usage) +{ + return __swap_duplicate_nr(entry, usage, 1); +} + /* * Help swapoff by noting that swap entry belongs to shmem/tmpfs * (in which case its reference count is never incremented). @@ -3459,23 +3481,23 @@ int swap_duplicate(swp_entry_t entry) } /* - * @entry: swap entry for which we allocate swap cache. + * @entry: first swap entry from which we allocate nr swap cache. * - * Called when allocating swap cache for existing swap entry, + * Called when allocating swap cache for existing swap entries, * This can return error codes. Returns 0 at success. * -EEXIST means there is a swap cache. * Note: return code is different from swap_duplicate(). */ -int swapcache_prepare(swp_entry_t entry) +int swapcache_prepare_nr(swp_entry_t entry, int nr) { - return __swap_duplicate(entry, SWAP_HAS_CACHE); + return __swap_duplicate_nr(entry, SWAP_HAS_CACHE, nr); } -void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +void swapcache_clear_nr(struct swap_info_struct *si, swp_entry_t entry, int nr) { unsigned long offset = swp_offset(entry); - cluster_swap_free_nr(si, offset, 1, SWAP_HAS_CACHE); + cluster_swap_free_nr(si, offset, nr, SWAP_HAS_CACHE); } struct swap_info_struct *swp_swap_info(swp_entry_t entry)