From mboxrd@z Thu Jan 1 00:00:00 1970 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 13413C3DA64 for ; Wed, 31 Jul 2024 10:23:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B1CC6B0088; Wed, 31 Jul 2024 06:23:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 861A56B0089; Wed, 31 Jul 2024 06:23:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7025E6B008A; Wed, 31 Jul 2024 06:23:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4945F6B0088 for ; Wed, 31 Jul 2024 06:23:26 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BEE7E8022E for ; Wed, 31 Jul 2024 10:23:25 +0000 (UTC) X-FDA: 82399660770.21.9CD2337 Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) by imf12.hostedemail.com (Postfix) with ESMTP id EBCAE40023 for ; Wed, 31 Jul 2024 10:23:23 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aOAM0oUl; spf=pass (imf12.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.46 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=1722421360; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BFmQVShAHj9eLaCuGgxCH1jsKTl/l8rQteiLrzZiBsw=; b=Gp9qHZ3Mnynjumum41Fh0zg3lvy8EOz4fNe+V8mtzSblVeJGE3wDz4Kq/e3j6S+PxZJNAj UjXSRDGiMXpT8P/u0P/FOuSZf0N/11MRFTFLchM04WUxyy+0F1qemXoqiR3hq4WPKR2z/9 v8GuVXkIH2cb0XG/g+seqn/X4L2j5Io= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=aOAM0oUl; spf=pass (imf12.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.46 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=1722421360; a=rsa-sha256; cv=none; b=CIRae2Assaub7YJViNrTO6llaafw/GUPabIdgBhhHniecMkgqf8Jmiea16KBYPWLU5/boR ja+AaOIrZ/8jY7W8EjQ7IeBHlay0mbtIHt1NmXsQMOc0teGehB9FAm86I+Uom5z9f1n1ob 8OZFBXd+3kgJD8ul5++Zo4EHCm+a7UM= Received: by mail-ua1-f46.google.com with SMTP id a1e0cc1a2514c-810177d1760so1845533241.2 for ; Wed, 31 Jul 2024 03:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722421403; x=1723026203; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=BFmQVShAHj9eLaCuGgxCH1jsKTl/l8rQteiLrzZiBsw=; b=aOAM0oUl+zJxDzlUPu3Qd6CapgwW4Ig1OjTNhKthnTQCFS4BNjDaDLGIJFTJUCpewl CL2fol/8p52JHQ9gzLaW6lVqHe+nY728PwZxe8p91/Mu00WzKy6s0TAg82CZ3UC4yF6/ 8EppuRZo+IZu/0yQkb6VHL9lBICH1RIVBcrh9hwxMw2H0eI5vYjN9o6OFreTlfjxv998 s/f0wsWiqc9HMfEgNeZBpQf+UZTyCfP3q7AvzIyh2O/wpV3S7NCeLKDl8O1YTFzkmu0S haCvh2qfmPO/+ciSGL+NfXy6Zv4qYJ6DDtbujOJ+pWfjAIpNDeKYgFEZGQYs0SMJ+cNK 3XhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722421403; x=1723026203; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BFmQVShAHj9eLaCuGgxCH1jsKTl/l8rQteiLrzZiBsw=; b=vhRUJsftzLRRrmDM1xZXWmkMYY6dbYfLzYQnsfQFP1+Uxmom01Ev2+37tf75GPdZjZ WQiji0hK3ELCqZ+03qXN/OmYLghSYbNOF6dcdBzDE27PrFMmxtLO50JtYtDCAzf0d1eF EN3gRs7i8q6fh1YEucUx2XGmv7zyJjEfoqjta3zKwwKrfKivf5x6H+3G+c3rlZ6aMNcm DZKbNpci3jRSIZIp62JvK8YTg+UfaCLAqkaTIKjclhrTij3XiKyVpbf0BHSk5DEdU2Gu NMC3NxhFU5qW898J6FA0b4NsajfenUJIJvFGK7psaQT7z4FWpjikKvPl3TcYFr17dHGf +G9A== X-Forwarded-Encrypted: i=1; AJvYcCWTt9tsoSgffkAuowExgy3b7pwzq4/JumdXKWELX4xLHee2KjIgXglJrSiUjnDhZ6tbK82brdlCA0oA5TWw6EXtfeA= X-Gm-Message-State: AOJu0YxVVFi5x4HFds2ClRALaih834J91E0EJSdzsH7rVcdCJ4ql62WM 7l9JpDGn4yXUr4yg7DUrnHYLxbkBUTFfyUQAJPzShNVMR4X2Zqy+uwpB68NU4mEL3XRSLf6aG46 jgifCEIXnbPYmBt2mLh0ZXG/5soI= X-Google-Smtp-Source: AGHT+IE6T6qCBdXInX5r6zrKwIKTZBwCXC2KCf/joU6AbvNPxLssLAwURAjVzgEBk9dEYD2w3BWFMh5S5GrmYLWKmEM= X-Received: by 2002:a05:6102:38ce:b0:492:9394:d2e with SMTP id ada2fe7eead31-493fa1c7700mr16224459137.16.1722421402781; Wed, 31 Jul 2024 03:23:22 -0700 (PDT) MIME-Version: 1.0 References: <20240730071339.107447-1-21cnbao@gmail.com> <20240730071339.107447-2-21cnbao@gmail.com> <874j86oubf.fsf@yhuang6-desk2.ccr.corp.intel.com> <87zfpynf2r.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <87zfpynf2r.fsf@yhuang6-desk2.ccr.corp.intel.com> From: Barry Song <21cnbao@gmail.com> Date: Wed, 31 Jul 2024 18:23:11 +0800 Message-ID: Subject: Re: [PATCH 1/1] mm: swap: add nr argument in swapcache_prepare and swapcache_clear to support large folios To: "Huang, Ying" Cc: akpm@linux-foundation.org, linux-mm@kvack.org, 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Stat-Signature: 883dnou3fmumyk9wpjw6sby4d1w1n7gn X-Rspam-User: X-Rspamd-Queue-Id: EBCAE40023 X-Rspamd-Server: rspam02 X-HE-Tag: 1722421403-680263 X-HE-Meta: U2FsdGVkX19KPzAQJFLovn8OWyKCnQuIdVPRgV9J0lQEhPONST1yKtSMDn0U4jXBVFVOZOzS2c7aRt5aUcpXWgQaK45qrYLhaPFAJ/Y+d1QMagFKjJ2hJhYzwOr/MxUYr+PB8oz7xnVVYY2TpLlB8rUNsZcCbB+p0qJKOrqLcWuxSQKLVJlgpyUkmd1sl1MnyUz8dZPsraGFILVEkgUh69QpgaD4Y+wbIqdQ/fGu8DaN9GECamSkdIq2GJrMfkVndxwTuGc/IfZY3j8VNhvLVgwEH8dvTc8niG0oyyw7cSWU1t4Ky5V7kNVBRU7NIv02uzsYLK/zxjXWtHTwIar/kNDpuhFefQd6rlq2NKJq/YRRw8ow9sIhjMrLFjvdtpWjdcWECeqbeqTJdMV+L0oxUHRNmg98UkLz77M4c0SkxoWI7Ushgp9HEAzERVvVQUH5J4y9o70D/CHaOnpRpW6k1wlO1N/ui/2sA1X7uodK4Yo1wc+7k9N+RlsfGrVHqmrYwr9TdGLn1ihd+PZ2tpd2qOqIQs7NwdAU8kSP0BnLez/r6UFB1utGjY1n1gXYMy/2/arPc1SoeaQv8uKIf7X5x4L1gzUm29GzJi7cc69fD+bW8WJRif2Y7vjLvuCNwX6QlEQRgOElxblTLpXHnsVbQdRPwcXIEF6hm2jG7bLyyWxN4nu7EdEDH1oGEqEfcnwJnBdo+b/0E9cOPTwVGI9XW10301+qwzOml6l3PgyMGFZIxEmV+O/AF9QjqBNioXHqjfbLRoW7/zmNOPIl41B4g1SctFPv6CvO802kQ5H2VCfhMBn1xvI9MekuKiNVWSoEFtB2r+L+2uwYs2gIQaqG3H5JGzeIqwA0V5mEFsg+5wHxZvDmrAqrOKazXtHDgYpWEjVE4/M2E7FTba6TbJ4VPNPJCFRoJay0odh9oHiNW9XpdNFTV/nya1OciWybBE/etyrFD/2tUli00nZgB6m fOl3gd8Q 8LBsO5LLXOOtXBVtPopMdUNQsFR3sca18OYuh3aihBBC4rIHYk9jO84CtziX7MA5KCOBN3mR039O3ecs7uouN4xBuhbp/SuqJqJDrHth27JAenUS75iY2QktZJ/fuT/qudgOb2xyapnLvsJyFHpkIFRoB10p9AcVXvcVrrCZFCpqFR/ZQZfW8R2ZX5Cgyjb2kDMUkGnPGuCgQxUMO3A5VBOM3E2h6SOy7nKmlMxxw4mHkhKf+Ui/RYpFwQqUfg/BAkeOF6Sn24PDmD4jYLOzcMUFuPDiLr8sLgLqJCfvzuVkSUGq18PPGPbrrZSV3O6+Igq4d355kRkV+AktXeZ/wQZ3fZTqA99r5MNs9ZIgEZA9igqH3BvDsWXl/QW98RbW//Gg2T5brkRY6OAJKjgGw5SMfIatlX/A5GiUIo5vU7yK33Qpe2f2Zcvp2k9z3mpmDIcOfw15g4vSmzB84zLi4FJQJgUXdS8h4Xv9sjPA64YMTlYJcqYkMte7BoA== 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: On Wed, Jul 31, 2024 at 4:28=E2=80=AFPM Huang, Ying = wrote: > > Barry Song <21cnbao@gmail.com> writes: > > > On Wed, Jul 31, 2024 at 4:14=E2=80=AFPM Huang, Ying wrote: > >> > >> Hi, Barry, > >> > >> Barry Song <21cnbao@gmail.com> writes: > >> > >> > From: Barry Song > >> > > >> > Right now, swapcache_prepare() and swapcache_clear() supports one en= try > >> > only, to support large folios, we need to handle multiple swap entri= es. > >> > > >> > To optimize stack usage, we iterate twice in __swap_duplicate(): the > >> > first time to verify that all entries are valid, and the second time > >> > to apply the modifications to the entries. > >> > > >> > Currently, we're using nr=3D1 for the existing users. > >> > > >> > Reviewed-by: Baolin Wang > >> > Signed-off-by: Barry Song > >> > --- > >> > include/linux/swap.h | 4 +- > >> > mm/memory.c | 6 +-- > >> > mm/swap.h | 5 ++- > >> > mm/swap_state.c | 2 +- > >> > mm/swapfile.c | 101 +++++++++++++++++++++++++---------------= --- > >> > 5 files changed, 68 insertions(+), 50 deletions(-) > >> > > >> > diff --git a/include/linux/swap.h b/include/linux/swap.h > >> > index ba7ea95d1c57..5b920fa2315b 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(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(swp_entry_t swp, int nr) > >> > { > >> > return 0; > >> > } > >> > diff --git a/mm/memory.c b/mm/memory.c > >> > index 833d2cad6eb2..b8675617a5e3 100644 > >> > --- a/mm/memory.c > >> > +++ b/mm/memory.c > >> > @@ -4081,7 +4081,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > >> > * reusing the same entry. It's undetectable a= s > >> > * pte_same() returns true due to entry reuse. > >> > */ > >> > - if (swapcache_prepare(entry)) { > >> > + if (swapcache_prepare(entry, 1)) { > >> > /* Relax a bit to prevent rapid repeat= ed page faults */ > >> > schedule_timeout_uninterruptible(1); > >> > goto out; > >> > @@ -4387,7 +4387,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > >> > out: > >> > /* Clear the swap cache pin for direct swapin after PTL unlock= */ > >> > if (need_clear_cache) > >> > - swapcache_clear(si, entry); > >> > + swapcache_clear(si, entry, 1); > >> > if (si) > >> > put_swap_device(si); > >> > return ret; > >> > @@ -4403,7 +4403,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > >> > folio_put(swapcache); > >> > } > >> > if (need_clear_cache) > >> > - swapcache_clear(si, entry); > >> > + swapcache_clear(si, entry, 1); > >> > if (si) > >> > put_swap_device(si); > >> > return ret; > >> > diff --git a/mm/swap.h b/mm/swap.h > >> > index baa1fa946b34..7c6330561d84 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(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 *mappin= g, > >> > @@ -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(struct swap_info_struct *si, swp= _entry_t entry, int nr) > >> > { > >> > } > >> > > >> > @@ -172,4 +172,5 @@ static inline unsigned int folio_swap_flags(stru= ct folio *folio) > >> > return 0; > >> > } > >> > #endif /* CONFIG_SWAP */ > >> > + > >> > >> NITPICK: Is it necessary to add a blank line here? But I don't think = a > >> new version is necessary if this is the only change needed. > > > > No need to add a blank line; it was probably a mistake I made in Vim. > > > >> > >> > #endif /* _MM_SWAP_H */ > >> > diff --git a/mm/swap_state.c b/mm/swap_state.c > >> > index a1726e49a5eb..b06f2a054f5a 100644 > >> > --- a/mm/swap_state.c > >> > +++ b/mm/swap_state.c > >> > @@ -477,7 +477,7 @@ struct folio *__read_swap_cache_async(swp_entry_= t entry, gfp_t gfp_mask, > >> > /* > >> > * Swap entry may have been freed since our caller obs= erved it. > >> > */ > >> > - err =3D swapcache_prepare(entry); > >> > + err =3D swapcache_prepare(entry, 1); > >> > if (!err) > >> > break; > >> > > >> > diff --git a/mm/swapfile.c b/mm/swapfile.c > >> > index 5f73a8553371..757d38a86f56 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 cou= nt. > >> > + * Verify that nr swap entries are valid and increment their swap m= ap counts. > >> > * > >> > * Returns error code in following case. > >> > * - success -> 0 > >> > @@ -3373,60 +3373,77 @@ 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(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 =3D swp_swap_info(entry); > >> > > >> > offset =3D swp_offset(entry); > >> > + VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); > >> > ci =3D lock_cluster_or_swap_info(p, offset); > >> > > >> > - count =3D p->swap_map[offset]; > >> > + err =3D 0; > >> > + for (i =3D 0; i < nr; i++) { > >> > + count =3D 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) =3D=3D SWAP_MAP_BAD)) { > >> > - err =3D -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 l= ock 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; > >> > + has_cache =3D count & SWAP_HAS_CACHE; > >> > + count &=3D ~SWAP_HAS_CACHE; > >> > > >> > - if (usage =3D=3D SWAP_HAS_CACHE) { > >> > + if (usage =3D=3D 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 els= e added cache */ > >> > + err =3D -EEXIST; > >> > + else /* no users re= maining */ > >> > + err =3D -ENOENT; > >> > > >> > - /* set SWAP_HAS_CACHE if there is no cache and entry i= s 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) { > >> > > >> > - } else if (count || has_cache) { > >> > + if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) > >> > + continue; > >> > + else if ((count & ~COUNT_CONTINUED) > SWAP_MAP= _MAX) > >> > + err =3D -EINVAL; > >> > + else if (swap_count_continued(p, offset + i, c= ount)) > >> > + continue; > >> > >> IIUC, this will make the change to swap map directly instead of > >> verification. If the verification failed for some entry later, the > >> count will be wrong? Or I missed something? > > > > To avoid using a bitmap or a larger stack, we actually verify during > > the first iteration. > > This ensures that by the second iteration, we can safely commit the > > modification. > > > > I actually put some words in the changelog :-) > > > > To optimize stack usage, we iterate twice in __swap_duplicate(): the > > first time to verify that all entries are valid, and the second time > > to apply the modifications to the entries. > > Yes, I have seen it and I think that it is a good strategy. > > But, IIUC, swap_count_continued() will change the higher bits of the > swap_map instead of verifying. Or, my understanding is wrong? > Ying, your understanding is 100% correct. but the code also has nothing broken. we didn't extend swap_duplicate() to have argument nr, so all users which can set usage=3D1 will definitely have nr=3D1. int swap_duplicate(swp_entry_t entry) { int err =3D 0; while (!err && __swap_duplicate(entry, 1, 1) =3D=3D -ENOMEM) err =3D add_swap_count_continuation(entry, GFP_ATOMIC); return err; } Maybe I can add a VM_WARN_ON to warn those people who might want to extend swap_duplicate()? in that case, things could be quite tricky. --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3386,6 +3386,7 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) offset =3D swp_offset(entry); VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); + VM_WARN_ON(usage =3D=3D 1 && nr > 1); ci =3D lock_cluster_or_swap_info(p, offset); err =3D 0; > -- > Best Regards, > Huang, Ying > > >> > >> > + else > >> > + err =3D -ENOMEM; > >> > + } else > >> > + err =3D -ENOENT; /* unused sw= ap entry */ > >> > > >> > - if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) > >> > + if (err) > >> > + goto unlock_out; > >> > + } > >> > + > >> > + for (i =3D 0; i < nr; i++) { > >> > + count =3D p->swap_map[offset + i]; > >> > + has_cache =3D count & SWAP_HAS_CACHE; > >> > + count &=3D ~SWAP_HAS_CACHE; > >> > + > >> > + if (usage =3D=3D SWAP_HAS_CACHE) > >> > + has_cache =3D SWAP_HAS_CACHE; > >> > + else 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= */ > >> > + count =3D 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); > >> > @@ -3439,7 +3456,7 @@ static int __swap_duplicate(swp_entry_t entry,= unsigned char usage) > >> > */ > >> > void swap_shmem_alloc(swp_entry_t entry) > >> > { > >> > - __swap_duplicate(entry, SWAP_MAP_SHMEM); > >> > + __swap_duplicate(entry, SWAP_MAP_SHMEM, 1); > >> > } > >> > > >> > /* > >> > @@ -3453,29 +3470,29 @@ int swap_duplicate(swp_entry_t entry) > >> > { > >> > int err =3D 0; > >> > > >> > - while (!err && __swap_duplicate(entry, 1) =3D=3D -ENOMEM) > >> > + while (!err && __swap_duplicate(entry, 1, 1) =3D=3D -ENOMEM) > >> > err =3D add_swap_count_continuation(entry, GFP_ATOMIC)= ; > >> > return err; > >> > } > >> > > >> > /* > >> > - * @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(swp_entry_t entry, int nr) > >> > { > >> > - return __swap_duplicate(entry, SWAP_HAS_CACHE); > >> > + return __swap_duplicate(entry, SWAP_HAS_CACHE, nr); > >> > } > >> > > >> > -void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry= ) > >> > +void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry= , int nr) > >> > { > >> > unsigned long offset =3D 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) > >> > >> -- > >> Best Regards, > >> Huang, Ying > > > > Thanks > > Barry