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 ADFE9C5475B for ; Thu, 29 Feb 2024 01:12:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42C326B007E; Wed, 28 Feb 2024 20:12:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DB8D6B0082; Wed, 28 Feb 2024 20:12:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27CE96B0085; Wed, 28 Feb 2024 20:12:21 -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 169566B007E for ; Wed, 28 Feb 2024 20:12:21 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D3A96A01E7 for ; Thu, 29 Feb 2024 01:12:20 +0000 (UTC) X-FDA: 81843065640.03.B094051 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) by imf01.hostedemail.com (Postfix) with ESMTP id C44E040014 for ; Thu, 29 Feb 2024 01:12:18 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cHxnukLU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.47 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709169138; 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=za/f+FD8CJTPCMoqnLZV1TIT7CRykE1Lv1/KdXF4ElQ=; b=HlFYWZbyzePhBnwVTHu6hI58IqL9wSzVsrmsKY/CqbFxbcuQ5nT3kLig5Of/9nhc2skNEK SQXF4xtygV5nvMmOyCTmmbGttsW9TRIR0R02/DoU4Ruh2NeFhtfdBgsvHbIhNmu66TsFBj Oz3d4nxmZYQPLiFp1HcJ1tacWnwb1Fk= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cHxnukLU; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf01.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.47 as permitted sender) smtp.mailfrom=yosryahmed@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709169138; a=rsa-sha256; cv=none; b=6aC9AmyT+k6QXQ/hMy7Vy9EWE3Vkp8KdoZKBJMxeEgfbJWKHlSS2ILSODo12EPdUlXtYgZ yXQyNg1hyctLU8b0uyJBqX6qFij4CR8IZZx31vEuJrLrV8W0HP022+OMFQlgzHdbo1fhHc s7LXwcS1Mi94L8oKyExgyq9aqwFEVjk= Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-55a179f5fa1so679478a12.0 for ; Wed, 28 Feb 2024 17:12:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709169137; x=1709773937; 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=za/f+FD8CJTPCMoqnLZV1TIT7CRykE1Lv1/KdXF4ElQ=; b=cHxnukLUtHH/I8Ev5BdAplUd/VISR/ttUXH2bGwC1xB7j1RXh5aZcX7UIFuLaWFj9u qpytioZP4oJehjUWUPbmWG7hCqDMdBf+euT2RM0BETfNTBkd8cB/qshekmFdzfuikBb5 gVSCku79XgThpRYJyS+TzgLzDIq1eW28eX3tJJff9BTu0AaSOAW9YoWs2ZnotNmOuOhp Vddl/SDZ3gskz+puyPLxsdMhY9HCDAm1RTFQQBUPZ4PmCNJ1wwg8UghG9EySTCA6srXw 4G94tm9bBwLVsY13EI3DZT6oEc+7t/ZTAcWt1X8cfGuC24rqpVI34VAYz5/VDoBoJHod dcSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709169137; x=1709773937; 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=za/f+FD8CJTPCMoqnLZV1TIT7CRykE1Lv1/KdXF4ElQ=; b=lik9TOW6B9yNuwniV1turvkrbRlyWDzV5zefYwZ1mPWu/HJO+GZ5ly5KfzUKiMwCM9 Im25ZpuBhHZhZ03nibPstT8irUCFkkXrBBaQEAA+q6NYcZUzC3RScNTxZCGl3mfqFHiN V2Oa+ytVvztPyPmTRTu4n4LbgssUU77gpmX2USds8k+G5uEfxjyGC4e6Gi9rQQ2aBwET 8uj6nRFWJqJw8m/oo34AQMuHH2jor1gf7b5NMBzgYEoeVR/8wx0jIXR0LUYDYC92DLrM 2/TDzgGr933AHNUMlYNoCVJBA/LHAdpVBF07LjHcqHAUNC/edhgM/Ydsua9CW5i2MhSA T5xw== X-Forwarded-Encrypted: i=1; AJvYcCUhyOw4GFJ7+FXjtkyripDSB+2JoTGEuumouwXaRT5gvl/7w4g+FLWyLW0Yx/apDxwT519Ev/EDHMreki5RLOB+VEo= X-Gm-Message-State: AOJu0YxBX8HIi7HEns/H+xNM/S2Nk0XEVHJbj1l4QVzBd0qZx7jekbRq Fn69JED2aWR2cfTyDWqIwRrJ3Ri6LjVAp3czojTmCcF3FUStfZpKdjP6k9aHCSapBgVkTRY7ItE ZTYnGhVgfNGMCqP14mS4S0xBLYWY85yIxVD8G X-Google-Smtp-Source: AGHT+IEAgL1Bww+5JuEgUxcDBnN9H9F8iZLjpAJi5GB+9ifzlx3C9P2jRNo47k/MGoURNxB9skuSrspX3DXCeGqK5HI= X-Received: by 2002:a17:906:f9cc:b0:a43:adc:7220 with SMTP id lj12-20020a170906f9cc00b00a430adc7220mr349274ejb.8.1709169136943; Wed, 28 Feb 2024 17:12:16 -0800 (PST) MIME-Version: 1.0 References: <20240229003753.134193-1-21cnbao@gmail.com> <20240229003753.134193-5-21cnbao@gmail.com> In-Reply-To: From: Yosry Ahmed Date: Wed, 28 Feb 2024 17:11:40 -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-Rspamd-Queue-Id: C44E040014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: pwx4ifaktow6mmsby7zzzabm71rpf3z6 X-HE-Tag: 1709169138-774748 X-HE-Meta: U2FsdGVkX1/RbDPpAz4D+gRYuhUXaVo4gK4UzKxTAUTRtq2f1MuWYgYVXXfq6dvXliORMjKg8tzeoU4/RCWLJcHsdUnNdsrsrKANGXA0N7K49vARZ6CThAcoogRBDZRj2CywC4uVlw1dbKuXgI+nkFZiVrYf+S0CEweItPPGVpsUY0E8kIt6dm7eSLthuTGUgvvqL7HWmC9itVyhijd/P2/UggTISRYIzbJBU7WCc/W7gF4q+H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VqpOHRwfhHuFiRX4QkHqe8Hfpr1/lTbnV60ce1FEMa3K/fUB7ZkVWX06kPZhSIC0D9UoHRk2q9wuLwI8a69cyVFYWQiI/+yWb9CXp615msbPVLwL5QwHTxee5Y7qCsI5U4uHk4vt3/kAr1YNtV/FjXBENQxHGckW7ReACpcHcC704XNYlnJAlU9SUDRaq3kzkckRLiSnaiIEVI+kyzUn9xrtqDZRcbiqC/HWbimk3kxD+1EQMPjyjYXU1ZkCBFIfJv8Gf3LAB7REhyv2Jv35WctFlnDHCyoycMZbQu+apYHtjhyWk2YBvXLO5y7QzcyVIpu/fBsGsxOBrS5lMnzuieVnQ0/h3xlXGhu4F2uXeJ4lN2OGgCF9Jq7MCEa9t9MQ8yGyPNTULV6QPCDe+mQ== 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:59=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrot= e: > > 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> = wrote: > > > > > > From: Barry Song > > > > > > Commit 13ddaf26be32 ("mm/swap: fix race when skipping swapcache") sup= ports > > > one entry only, to support large folio swap-in, we need to handle mul= tiple > > > 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 ent= ry, 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 coun= t. > > > + * Verify that nr swap entries are valid and increment their swap ma= p 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. ->= 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, int nr, unsigned c= har 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 m= y eyes. > > > > Is this reasonable for stack usage? > > SWAPFILE_CLUSTER is not huge. typically 512 or 256. So that's 1K of stack usage out of 16K total on x86. I think this may be a lot for a single function to use, but perhaps others will disagree. > > #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,= so the > > > - * swap entry could be SWAP_MAP_BAD. Check here with lock hel= d. > > > - */ > > > - 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= cache */ > > > - err =3D -EEXIST; > > > - else /* no users remaining= */ > > > + /* > > > + * swapin_readahead() doesn't check if a swap entry i= s valid, 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? > > 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. I see, but I think we may be overwriting the error from each iteration belo= w? > > > > > > > > > - } 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 entr= y */ > > > - > > > - 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 an= d entry is used */ > > > + if (!has_cache[i] && count[i]) > > > + has_cache[i] =3D SWAP_HAS_CACHE; > > > + else if (has_cache[i]) /* someone el= se added cache */ > > > + err =3D -EEXIST; > > > + else /* no users r= emaining */ > > > + 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, = count[i])) > > > + count[i] =3D COUNT_CONTINUED; > > > + else > > > + err =3D -ENOMEM; > > > + } else > > > + err =3D -ENOENT; /* unused s= wap 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