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 C95E3C5475B for ; Thu, 29 Feb 2024 00:52:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3CB376B00A6; Wed, 28 Feb 2024 19:52:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 37A826B00A8; Wed, 28 Feb 2024 19:52:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 242976B00A9; Wed, 28 Feb 2024 19:52:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 16B166B00A6 for ; Wed, 28 Feb 2024 19:52:27 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D4B61121229 for ; Thu, 29 Feb 2024 00:52:26 +0000 (UTC) X-FDA: 81843015492.20.F10BC0C Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by imf28.hostedemail.com (Postfix) with ESMTP id F06DCC0014 for ; Thu, 29 Feb 2024 00:52:24 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3vafhnjB; spf=pass (imf28.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709167945; 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=JOoFnCufx7taaiCSkKihrCSXYV9CGIUvZlyJqjbocTk=; b=oRiNvGyafuO5zhv3efYUE8Xl7hcFf4M1nk+QukEYLQnbF8WRAuQGCF5xtdsvcLmm3OWQod H5njZI2pVhVM3vXBvBYhJ4CJ4j0qIazcB5/tOJT9iGaappsbSJMi9Pgvtksb8v/WA262pv F8PnDEHX1VGiW64zdwphSA5V/TX3JYo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709167945; a=rsa-sha256; cv=none; b=AX4IfUn+L3lq5VAAigdx7T1zmFJAH7XG4agqHbrg04EB+vVFqNnH5hkB74eKJr7WJf+cbc EB4u/o09OtdwqPxNn4+/J8RvKm0Es4hd1F0XVdW/huMY60uF3cl5rDogoPT/yikP3i5USg vFjnODJzq/F8auBBqWdMHVwYHUqZI30= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3vafhnjB; spf=pass (imf28.hostedemail.com: domain of yosryahmed@google.com designates 209.85.218.53 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a2f22bfb4e6so68625966b.0 for ; Wed, 28 Feb 2024 16:52:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709167943; x=1709772743; 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=JOoFnCufx7taaiCSkKihrCSXYV9CGIUvZlyJqjbocTk=; b=3vafhnjBb77jKTPKExNvG1uuUay9HWGBS+X+uRjH3io7uxZzRMzL+RMhBXH/byHahN ULhkTMvYskWivN7BfP5MWGQ0BVSdNVRABVPxieFhWniRU08Q+9OH8FCq6eIDQbFwWCji dd7f6QPm+MHfH37TPoiqdhVUwHY9q3SV/1f3iiF/5P9uBvIW8JPelefIjbAtvvmVwEBH PscMask83DKonlHWMOYW+3+LwCUr9oxOtj47i9yv6u3Qpw6+IrS/PrexJrGEQJdVPTGU 7zQfGEv2u5l/AtxrAlhBuzVE8WvX6jPw3SDTsX+Wtz+Ob+C3yW/suxwog6FxalMR72g/ ZPgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709167943; x=1709772743; 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=JOoFnCufx7taaiCSkKihrCSXYV9CGIUvZlyJqjbocTk=; b=Ti9bo87xF9UGXv47GcQQ6B+4MI06Lgd8c2ratDq8zXJMCv2ypMP10V+lO9Sx/fhy2h hJ/Cq/kb5VFU72M2QE2YUkQxhLWtDz3drXC8/ryvPuxY5Qk8XfAxkgpVaGwev0x13/cc w/TvOIzNvCTm8CRqQ/c9VreysGbcZzeC0ODOC8haSBTdok3jGvepQ8Ej/N96rIM7Ib5j pHFBCySVY8ywmbn32q7H+r/zBkZN0NOwAPAjdioi9IX6kYinU4Ia3a5LNAWYe8YvvPzo vEk6HM8OpkX+8rvlX+wmnXjLztdxp0LMT28DvP3veFGowNHoOhjB7bjvCOx6vqfJtNSn qp+g== X-Forwarded-Encrypted: i=1; AJvYcCXagfHcO5pg/yNLkQfQlKhXMMwi86WHzQAPcBv9Siv9QAFooLvT3ZycGtJ1TIsw+yqUomALQZs48F2Mb14JQZBBYHA= X-Gm-Message-State: AOJu0YwiCrG2LhDWyBgS7gZVpiE5tMYueaWsFpQz5xhkbt3Y2GEpQFTP hoUtG34iqLHep1Fblg7lPxiWQLVDA0uG0G5BuJBzcKO/616BsOzdXSrMbvoD1TaorQQbbfD0q75 1wvWQ4ucRorpZUP4b9fqYj0pBli3HBEehj/cV X-Google-Smtp-Source: AGHT+IEPA4ZTo8LTkRBSWlvTNJeoKH3nUD5wkqOYyabq6B7/WHJkyx0NFuHjRttWmpSWiJ+8mr+rCudvqOHhtPP1YwI= X-Received: by 2002:a17:906:508:b0:a3e:b442:cfdd with SMTP id j8-20020a170906050800b00a3eb442cfddmr324683eja.17.1709167943213; Wed, 28 Feb 2024 16:52:23 -0800 (PST) MIME-Version: 1.0 References: <20240229003753.134193-1-21cnbao@gmail.com> <20240229003753.134193-5-21cnbao@gmail.com> In-Reply-To: <20240229003753.134193-5-21cnbao@gmail.com> From: Yosry Ahmed Date: Wed, 28 Feb 2024 16:51:45 -0800 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: Barry Song <21cnbao@gmail.com> 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-Stat-Signature: d7pa949jc5rwxha86ybjpkngmso3itbf X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: F06DCC0014 X-Rspam-User: X-HE-Tag: 1709167944-567041 X-HE-Meta: U2FsdGVkX19FutY1mbOMzyrVv4Q84H+moDglqhu3cfLkpSmDqxXal/bIBWPVRDPZXe07meF5d9+zglFzGLbcKLnK0Rb6kBQdWAcF+ipjOIXkg8YN9RIkLb9d/z7luTRCH4Cl/MTL1N5DcLvDm+bZg1dO8NtmeRe4bmczcrT9QivKST4+A4wyDUC8Do3J3agrpUTdpIHzoRavlcv51nkx1fcQ5UeBDM7uaeIUTTJ1uWJ7DB19V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S2XcvKXdlZTZtxx+TAgOiudWt0YO1zLEkfzoOSyH0mal7PqCpB0T5BHS6Lb+DrUvtzXbWZ3STaE2lqTTK24lp8KyGiCInbb9JbSKqXg8tM4Am0daxG9DWUEVhjESdDJAQefZmZcSNT+9QNZm/zUEfC0wdsCCSGgeILS7SonSu+2GxTObVvVsSLyFJtC4CPRQ16GauPPiM8enteNgp3C9V49e12VGwrQl1/+4fVMphS/2THlRE0OrgeiIhdFxxK0inh+asEtrDTGldKTm8Hbex5auKPkd5F11PA84ERX4EnYGp7DBQv74v4P5DbvO3JU1S/1ZevmbULACSryjcJF/acLSXdFHT5D4ag/DYgVfVa3kdgTae9bofL6HCOZS5lZwKhkeznO+gLGHg== 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, Feb 28, 2024 at 4:39=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrot= e: > > From: Barry Song > > Commit 13ddaf26be32 ("mm/swap: fix race when skipping swapcache") support= s > one entry only, to support large folio swap-in, we need to handle multipl= e > 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, g= fp_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 co= unt. > * > * 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. -> ENO= ENT > * - 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 char = 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]; I am not closely following this series, but a couple of things caught my ey= es. Is this reasonable for stack usage? > + 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, 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; > - } > + 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 u= sed */ > - if (!has_cache && count) > - has_cache =3D SWAP_HAS_CACHE; > - else if (has_cache) /* someone else added cac= he */ > - err =3D -EEXIST; > - else /* no users remaining */ > + /* > + * swapin_readahead() doesn't check if a swap entry is va= lid, so the > + * swap entry could be SWAP_MAP_BAD. Check here with lock= 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? > > - } 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 en= try is used */ > + if (!has_cache[i] && count[i]) > + has_cache[i] =3D SWAP_HAS_CACHE; > + else if (has_cache[i]) /* someone else a= dded cache */ > + err =3D -EEXIST; > + else /* no users remai= ning */ > + err =3D -ENOENT; > + } else if (count[i] || has_cache[i]) { > + > + if ((count[i] & ~COUNT_CONTINUED) < SWAP_MAP_MAX) > + count[i] +=3D usage; > + else if ((count[i] & ~COUNT_CONTINUED) > SWAP_MAP= _MAX) > + err =3D -EINVAL; > + else if (swap_count_continued(p, offset + i, coun= t[i])) > + count[i] =3D COUNT_CONTINUED; > + else > + err =3D -ENOMEM; > + } else > + err =3D -ENOENT; /* unused swap = entry */ > + } > > + if (!err) { > + for (i =3D 0; i < nr; i++) > + WRITE_ONCE(p->swap_map[offset + i], count[i] | ha= s_cache[i]); > + } > unlock_out: > unlock_cluster_or_swap_info(p, ci); > return err; > }