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 A7702C5475B for ; Thu, 29 Feb 2024 00:59:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F9866B0083; Wed, 28 Feb 2024 19:59:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A88E6B0089; Wed, 28 Feb 2024 19:59:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24A14940007; Wed, 28 Feb 2024 19:59:11 -0500 (EST) 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 134D96B0083 for ; Wed, 28 Feb 2024 19:59:11 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id DF5CE12122E for ; Thu, 29 Feb 2024 00:59:10 +0000 (UTC) X-FDA: 81843032460.29.07185D0 Received: from mail-ua1-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) by imf15.hostedemail.com (Postfix) with ESMTP id 203F1A0006 for ; Thu, 29 Feb 2024 00:59:08 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VSHiwfAy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709168349; 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=Gv+KMrAQlYOZozl9mrP8VK8+7Pz7cjDfY39oHMS3ofY=; b=udzmvS6fOsZB2pB5+6eRg4T/FDMjd8NjSYAntYqv2tokX3bPtbokP7gi3f8lKT8Vr1Caf3 si/eqmvqenv4vYITJ+wkIDF5kFWth5Ur5hmnZx9ELjidD6DozDvr4Pegb4lpen7cgxP0FX yCZEitl9pJ7Rxh3Gi96smr3LTAD9jCU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VSHiwfAy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.51 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709168349; a=rsa-sha256; cv=none; b=1Z4YjyHJsTxklPC4u0AgBc8Q1wCWe/iXJpDkLt/iTT2NJDr5+657FElEyw9KkqXWKV2umg Vbpcd0hE1ScfwL9ZkXF/fQK7S2wLR/2Obdql8pXh4yVzTeI+WOtLF8R9IQ/3H4o9MpqmwE 8DV+uje4J9yW/nqIbOUaEGgxCA+fWaE= Received: by mail-ua1-f51.google.com with SMTP id a1e0cc1a2514c-7d2e21181c1so216301241.3 for ; Wed, 28 Feb 2024 16:59:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709168348; x=1709773148; 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=Gv+KMrAQlYOZozl9mrP8VK8+7Pz7cjDfY39oHMS3ofY=; b=VSHiwfAyhhLQUKqLFcneOpszcfEp6/y+SQU/PlM00T+yBNbM/SFd2dq1KGsclxzbEJ /U0GZ2QgpESeb0RW6FHltoO/jI5Eoc4C8Pa2Hsx9WUFXl1vieQexsgaDrZGQKr68DqAD g4k1liK7j/EV3iZ8HhlGD0353YUQFTpjDCB2N8C3wfDygCD5K5KLpA2vQHsD902ZQg6W OIIc4MAAoTxRqmnqfb/DqAciHENjhFihbn0gzap0wSPZC4GoaIHObeIN4gnTrGA6acAP Ib37lGfV4Dsq5EdXCcMciHwkBAbTyuBvnp+4RWuv7tNCXJuMW91otMIfVouukUsfkXiq 9Ojw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709168348; x=1709773148; 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=Gv+KMrAQlYOZozl9mrP8VK8+7Pz7cjDfY39oHMS3ofY=; b=ZmjIeTePwcSq0SeS08dPRZ2qJHf26RzYuCbc2B2Bd/FYyT50xMWaYTDAoyqD/LCfqb c5XYT9hJ/O7Hd+vm2bC5XFug2QV/7wJf1YaOC27hx/Uz7AIQVMp5VhPzxbczN6dWkgZS l4cg3JLOKzozJ9ckh/mDdEyb6dj4YoZsrxLihmZaRYmJvQR/6qKf2MdGxdKX4nlUJggG 5d3TXIdyklIncAIOKA1Tpj+sKq+691uASGnLL1mlyNRDspzYXn5Wd+mAl3E/GlTj1s/g mxMe/dk1QJPX4jV5U0Jd3nQcxSQLLr9eq184VUsMP9kIwrynoJYCFfGiaVuH+2QDBzEt YRzQ== X-Forwarded-Encrypted: i=1; AJvYcCU47NLNIvGFCNT2IjDfsmLW48xbuEwN8d432r+/67YmhKFFmb3k9ayDRqBMTesF+gmmM+XerJGn9gY1FAZNLmVc05s= X-Gm-Message-State: AOJu0YxGkeF5fFmgDEG0tJ+TUnKqLKz+QDK0RkvplQkvil1lyRyuXUUr QtGzHU4RwisMxQBpEUgW3kkSQ7hTYm3WW8ji1s9WWuTJK8Ms2EMUSbVpb3kvV37mytWo2hqxoxJ MTh1yzH+Hb+4pbo1ZIB0lcp8KRQQ= X-Google-Smtp-Source: AGHT+IHn0jtq8H+NDlrbKbvnqI/YW6xgiJtfwzojgOktMlfGLJN90CLVG9NBK1hOyE7w+li7Dbb3ZG9ewD33w6S+Cqo= X-Received: by 2002:a05:6102:a48:b0:470:3a99:f3c with SMTP id i8-20020a0561020a4800b004703a990f3cmr556272vss.23.1709168348192; Wed, 28 Feb 2024 16:59:08 -0800 (PST) MIME-Version: 1.0 References: <20240229003753.134193-1-21cnbao@gmail.com> <20240229003753.134193-5-21cnbao@gmail.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Thu, 29 Feb 2024 13:58:56 +1300 Message-ID: Subject: Re: [PATCH RFC v2 4/5] mm: swap: introduce swapcache_prepare_nr and swapcache_clear_nr for large folios swap-in To: Yosry Ahmed Cc: akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org, ryan.roberts@arm.com, chrisl@kernel.org, linux-kernel@vger.kernel.org, mhocko@suse.com, shy828301@gmail.com, steven.price@arm.com, surenb@google.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com, kasong@tencent.com, nphamcs@gmail.com, chengming.zhou@linux.dev, hannes@cmpxchg.org, linux-arm-kernel@lists.infradead.org, Barry Song , Hugh Dickins , Minchan Kim , SeongJae Park Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 203F1A0006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: auoxo9tut1nnsh81sene6d7thbu5jb6e X-HE-Tag: 1709168348-926015 X-HE-Meta: U2FsdGVkX19dOlMnyS/Hardg4oGjf/rHw4FBcdvXDGtHMScSZO0HVwtWU7IL0FKk4bSADNFyFU2TyruISX4YLxxGHOkgi95TdW7OeLzjOkE110pvUxdUImmCDmHqEcjsPDMt8olIhledSkBXm8hswgWPlbdLT1zMcq9ZU1geqhVyftUjk8IYHL211adT65O5JvqQ33fW9mzwkMnPBPW0SVYPqNEK8fBUM1z/73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x3xG/xWoSCR9VA9ilHIfnl0HMGWw2E+T/OERP10a5sgf9F4wb/2fEh1cUImH2ur59nQTw8VNjYB0GEdm9BFc/fQsJm25/JG8Q+/GZyngECOCliE6FfbP0AjrMD7FaJ3fZfAX5X1wlP0PSIq47K940fUjcCavKzO9GxUp2pix0f334yQgC8rRCvDV+IkuhKhE2uV/GWxLVU25XjzKP+qPtkBtKr3GIMnfez22dn9nEWdJVrZocgJZX0DFXkZ2lzsbLLDWtiny3/eTxOTMuXFQ+T6jMTtYjC+VO6T1TP3BfEfyQx6klrvaHGHM0IxFWiLNEKEuCr1SFwdK6XiaXPLrBYLkXLOvhaIrJKBPDlbTs+e4d38d+7sNwwCoEVgX5ktkf1CToJAZ8NjlikW3nG1ddFHP6mhpPTb5h2W2oVs8C4DJhmr4DeGPp2lw7PudNpOwpmzOw0iATe2L0yJ0XH5qikb/vrMRhi4jS0FVh0ahIN5xRELbg9ypgSDNUVil/VO7G/iTXeg1tgttJW621Q31ruVc+hEqA== 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 Thu, Feb 29, 2024 at 1:52=E2=80=AFPM Yosry Ahmed = wrote: > > On Wed, Feb 28, 2024 at 4:39=E2=80=AFPM Barry Song <21cnbao@gmail.com> wr= ote: > > > > From: Barry Song > > > > Commit 13ddaf26be32 ("mm/swap: fix race when skipping swapcache") suppo= rts > > one entry only, to support large folio swap-in, we need to handle multi= ple > > swap entries. > > > > Cc: Kairui Song > > Cc: "Huang, Ying" > > Cc: Yu Zhao > > Cc: David Hildenbrand > > Cc: Chris Li > > Cc: Hugh Dickins > > Cc: Johannes Weiner > > Cc: Matthew Wilcox (Oracle) > > Cc: Michal Hocko > > Cc: Minchan Kim > > Cc: Yosry Ahmed > > Cc: Yu Zhao > > Cc: SeongJae Park > > Signed-off-by: Barry Song > > --- > > include/linux/swap.h | 1 + > > mm/swap.h | 1 + > > mm/swapfile.c | 117 ++++++++++++++++++++++++++----------------- > > 3 files changed, 72 insertions(+), 47 deletions(-) > > > > diff --git a/include/linux/swap.h b/include/linux/swap.h > > index b3581c976e5f..2691c739d9a4 100644 > > --- a/include/linux/swap.h > > +++ b/include/linux/swap.h > > @@ -480,6 +480,7 @@ 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, int nr); > > extern void swap_free(swp_entry_t); > > extern void swap_nr_free(swp_entry_t entry, int nr_pages); > > extern void swapcache_free_entries(swp_entry_t *entries, int n); > > diff --git a/mm/swap.h b/mm/swap.h > > index fc2f6ade7f80..1cec991efcda 100644 > > --- a/mm/swap.h > > +++ b/mm/swap.h > > @@ -42,6 +42,7 @@ 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, > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > index c0c058ee7b69..c8c8b6dbaeda 100644 > > --- a/mm/swapfile.c > > +++ b/mm/swapfile.c > > @@ -3308,7 +3308,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 = count. > > * > > * Returns error code in following case. > > * - success -> 0 > > @@ -3318,66 +3318,73 @@ void si_swapinfo(struct sysinfo *val) > > * - swap-cache reference is requested but the entry is not used. -> E= NOENT > > * - 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, int nr, unsigned cha= r usage) > > { > > struct swap_info_struct *p; > > struct swap_cluster_info *ci; > > unsigned long offset; > > - unsigned char count; > > - unsigned char has_cache; > > - int err; > > + unsigned char count[SWAPFILE_CLUSTER]; > > + unsigned char has_cache[SWAPFILE_CLUSTER]; > Hi Yosry, Thanks for reviewing! > I am not closely following this series, but a couple of things caught my = eyes. > > Is this reasonable for stack usage? SWAPFILE_CLUSTER is not huge. typically 512 or 256. #ifdef CONFIG_THP_SWAP #define SWAPFILE_CLUSTER HPAGE_PMD_NR #define swap_entry_size(size) (size) #else #define SWAPFILE_CLUSTER 256 If this is still a concern, I can move it to a bitmap. > > > + int err, i; > > > > p =3D swp_swap_info(entry); > > > > offset =3D swp_offset(entry); > > ci =3D lock_cluster_or_swap_info(p, offset); > > > > - count =3D p->swap_map[offset]; > > - > > - /* > > - * swapin_readahead() doesn't check if a swap entry is valid, s= o 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; > > - } > > + for (i =3D 0; i < nr; i++) { > > + count[i] =3D p->swap_map[offset + i]; > > > > - 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 c= ache */ > > - 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 lo= ck held. > > + */ > > + if (unlikely(swap_count(count[i]) =3D=3D SWAP_MAP_BAD))= { > > err =3D -ENOENT; > > + goto unlock_out; > > + } > > Here we immediately exit if there is an error, but we don't below, we > just keep overwriting the error every iteration as far as I can tell. > Also, it doesn't seem like we do any cleanups if we hit an error > halfway through. Should we undo previously updated swap entries, or am > I missing something here? we are safely immediately exiting because we don't change swap_map till we finish all checks. while all checks are done, we write them by WRITE_ONCE(p->swap_map[offset + i], count[i] | has_cache[i]); at the end. > > > > > - } 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, count)) > > - count =3D COUNT_CONTINUED; > > - else > > - err =3D -ENOMEM; > > - } else > > - err =3D -ENOENT; /* unused swap entry = */ > > - > > - if (!err) > > - WRITE_ONCE(p->swap_map[offset], count | has_cache); > > + has_cache[i] =3D count[i] & SWAP_HAS_CACHE; > > + count[i] &=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[i] && count[i]) > > + has_cache[i] =3D SWAP_HAS_CACHE; > > + else if (has_cache[i]) /* someone else= added cache */ > > + err =3D -EEXIST; > > + else /* no users rem= aining */ > > + err =3D -ENOENT; > > + } else if (count[i] || has_cache[i]) { > > + > > + if ((count[i] & ~COUNT_CONTINUED) < SWAP_MAP_MA= X) > > + count[i] +=3D usage; > > + else if ((count[i] & ~COUNT_CONTINUED) > SWAP_M= AP_MAX) > > + err =3D -EINVAL; > > + else if (swap_count_continued(p, offset + i, co= unt[i])) > > + count[i] =3D COUNT_CONTINUED; > > + else > > + err =3D -ENOMEM; > > + } else > > + err =3D -ENOENT; /* unused swa= p entry */ > > + } > > > > + if (!err) { > > + for (i =3D 0; i < nr; i++) > > + WRITE_ONCE(p->swap_map[offset + i], count[i] | = has_cache[i]); Here is the place where we really write data. Before that, we only touched temp variables. > > + } > > unlock_out: > > unlock_cluster_or_swap_info(p, ci); > > return err; > > } thanks Barry