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 D0440CD4F5B for ; Thu, 5 Sep 2024 07:39:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 380306B040E; Thu, 5 Sep 2024 03:39:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 308F46B0415; Thu, 5 Sep 2024 03:39:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 183B36B041B; Thu, 5 Sep 2024 03:39:24 -0400 (EDT) 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 E8BD56B040E for ; Thu, 5 Sep 2024 03:39:23 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 8BE44A1A21 for ; Thu, 5 Sep 2024 07:39:23 +0000 (UTC) X-FDA: 82529884206.05.42085A8 Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by imf26.hostedemail.com (Postfix) with ESMTP id AEA5C140015 for ; Thu, 5 Sep 2024 07:39:21 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZWpuZB1f; spf=pass (imf26.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.43 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=1725521865; 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=QDy1iJqXHORqufKOt/lM2FthAPTJ7F0XYzMDKso6N5Q=; b=WGTTa+WGpDHcqwnhH/Ut8OSrMPZ08F8RH48tHV3YsKsXGnmDVetzkD94/elmp7i5LnY1gl i+md5mcy1pKeb2PiUlCW9JTgf2sGtc6iUwWhpokIIW5+DHlAefhKE1trzXRdPakGeeosbO 1HAYIifBu7Tk4Yj3Y83cfP65nrwrlI8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725521865; a=rsa-sha256; cv=none; b=kLNjXAoTEVA6tCIXINZaDyxMxgcrC359e/Xn7XJMAPIFDyAtgDZ0CxiuWKCw1HoRCsDIXV Muq7y9hYWy0h+dp3Y1GEp+MMJZdJxT8OkNRPqalzDcTS2hOnltiG16wG0rzt6KiFmZ/+Xh hstS/Qa00DYTOw2YZH58CU/ZtGFJLAc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZWpuZB1f; spf=pass (imf26.hostedemail.com: domain of yosryahmed@google.com designates 209.85.208.43 as permitted sender) smtp.mailfrom=yosryahmed@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5c2561e8041so562429a12.2 for ; Thu, 05 Sep 2024 00:39:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725521960; x=1726126760; 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=QDy1iJqXHORqufKOt/lM2FthAPTJ7F0XYzMDKso6N5Q=; b=ZWpuZB1fECm5a77VPoKFXli8R/1lJEKc9yOhKWCv68ZK1QM/xYUbjem9TLig63axb3 iZAwvn4WRrOMS7ymSDYbFjCMsfV8SDTNdwtQj7IwXQfVJf+LcKTz9teMeGsIsVs/nWmJ hK65v87Y8F/el8pdaNPrkuplfP6uop3B8LcfLq5I4BKhaciEDKcLWJYO6X2jxNZUequA evnhwjDqfcpX+i+iS7y+qu+R9xkZ1P+HBv0maHvoyAxexus0fqyoHurBoLwKP3ME/FgX Z6JpfjAKviF1DhzXLnh+Jy3VTPiBkbCY486/F5qTqUbKLqWys9jhT/HK960x5wn94vLW 3pFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725521960; x=1726126760; 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=QDy1iJqXHORqufKOt/lM2FthAPTJ7F0XYzMDKso6N5Q=; b=UwTgkER+df9wOrbAnEJpdBIjfKFXNbaGROLyZ0ihcg0R84vCo/QQwg1dRPyDG5KDdC GQWkCr/5rcmPGGA7b7oC8X1fBewVcsLOLXOUUXvkh/Tss0+dRoTJ9DZhmgb2Xsq7uFmv nr4bCh8voJMygF3gLFH/SVYtJfuQBklqetIGPyU7Ul2EVqJA44DifNUp/4XeXfXqtHVm z4ljHjcbE0KcpWN1MIzmPzj6krmBLEKNV3+IE+MNWtB7/MBPr0lRLI3ejeo9rbRwHQ4i UipTIXI59Jh0oU1MFiG/m5CtGKmTrnSJUVXcaK744zd4Sa02cYKbNEZuSGnypzsQGgKG FbZw== X-Forwarded-Encrypted: i=1; AJvYcCXUMjpdRJuuggfcBu9zXAnGGrrnK2CPWNPS1xOxvr4gxckKkLKRm0DS/qcxC3gKzVeZbty1909vng==@kvack.org X-Gm-Message-State: AOJu0YxfDuljaGQIJ30NS6vExAdWerp9EmOz4+bQ+GnSF9NfPg38upxe FIfoO5o5ofDV671Y4Dx29CBpbSGG5PPT2d8GGmCHoWq+lOdiyvbQC7WylrvjjQ4Er5Wi78TGkzs q7zfud3NpLZX8w3c8XAw/KAJydB+tMonV498h X-Google-Smtp-Source: AGHT+IEK71SW+38TwgBUkw+J1TEOWXLzwIJkR7hNQmXRs0b8u9HsgCN5rqc3JAoiaK/4NLi7jKy7E7geKpnm9TnHbfA= X-Received: by 2002:a17:907:12cd:b0:a89:f171:8dc0 with SMTP id a640c23a62f3a-a89f171926fmr697246566b.11.1725521959655; Thu, 05 Sep 2024 00:39:19 -0700 (PDT) MIME-Version: 1.0 References: <20240905002926.1055-1-21cnbao@gmail.com> In-Reply-To: <20240905002926.1055-1-21cnbao@gmail.com> From: Yosry Ahmed Date: Thu, 5 Sep 2024 00:38:43 -0700 Message-ID: Subject: Re: [PATCH v4 1/2] mm: store zero pages to be swapped out in a bitmap To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, chengming.zhou@linux.dev, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hughd@google.com, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, nphamcs@gmail.com, shakeel.butt@linux.dev, usamaarif642@gmail.com, willy@infradead.org, ying.huang@intel.com, Barry Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: AEA5C140015 X-Stat-Signature: ewwbkbfz87auodxckghuzdficwejpumr X-HE-Tag: 1725521961-497828 X-HE-Meta: U2FsdGVkX1/8pwvvgRmvTKZheihiI1Z7eqeDwz40QYooTLdf5relWhNzM2NlZVhTrm2g4Be9qP6hFG42Godk+xsF85Z5qMXs1BnLA32QR0txwp8B3Ozh2D/y3h5L0p8dmjTldVNWz3TODATj3+bh7nIl7nIHhOZJv6tY7Etekk+FilIMU8RxzDWsOLga3rn+EaIRsu5s/cvsPOSPyanemnAuh0LuvfhEtq28iMR4wF9mVzPPXCjEEn+xEk3NlOABsuSzwdYSCDB2N0ZFE/C/VmuV/AQhim2gK7IC1fQvViqDRLFuRqlCYySgTWOB0kioNo/QojJHm6L99rnZoppYMKg3b3dhYENUGKfSN/etZtXgEPCcILO9ojpxHgCZyQ2cZdz8WEX2gKZ7lZFsTw3q1gqvfxrwsk6wE+sgUC7EQQra6tNETlYOWIuWj7BrwSYnlSBVyeFyq8d/ec/3GswqLb3o4obQCz4J4bhf0q+iSqrH1H7MGJv1BfvigKGO8gJbXrFDe9wTYnvoR6gL4MeRVGuUiEm7iMeSZQSjE+5j/ulG1zBlnQWOZYMepaO6zTF4Psjn8VlE5nFi/bt3WCHqyOLR13hOIb6z4YB6LPeXxtLsoZiw1H4aj2LVrbhtb1QQXAvmUETqCSnqKTP3s5rjFSyLX6sRj52dQcw/x6NMra2NWk+ni8qNxcoFTcxVe8ZMhEj5RQ7xnDNH+nl06xcgrdLKOR0er6talm0Tysy7FNwDooXa6KihWK3VNq92E7+k0GxRajJvIXzjeIq7I8i0Iudha6V3RMYg5IsyCU1MbDJtBzEQqQBj/Kui+Hha4yxAyWTXC70Z0JJ1hDux2iZ9qn2XMHmNweMFtTF9l1zqv1SK7JjdnpggbFt3Y22dhQfHECVH/nd2q3PuOolJhPA8upeG/gFofXl0ptfYtZ1Q3DI5LtoWZYCRitewr/epS9frI4KcTrcPslvOG64u6zS c3BEYlxA iJp6cDTfGI5EiSyVUT1Vr7LDNxpMUjfWKdB0Bu2RjdflXZQy0KHOiNv2nULWIgrnMMvwu96oamGkY96LBc6Ex+EW/JLAZutjhjonHSOMQlGXHWl9SSFYxGlyFGIeu3u7i1xyG7ZJckUFWNhgS4lWILsgoTUZffoRseexMk0xhDk2NAZuTbH1uVUMjdg39BNLbJO13rcGoVVLRjdMz1vGtRboUZpJE7fkqqkbRokoOnJTzNp2oqcHxMV/8X2U/hnpnYdFU104NPT3ljPM= 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, Sep 4, 2024 at 5:29=E2=80=AFPM Barry Song <21cnbao@gmail.com> wrote= : > > On Thu, Sep 5, 2024 at 11:57=E2=80=AFAM Yosry Ahmed wrote: > > > > [..] > > > well. i feel i have a much cheaper way to implement this, which > > > can entirely iteration even in your original code: > > > > > > +/* > > > + * Return the number of entries which are zero-filled according to > > > + * swap_info_struct->zeromap. It isn't precise if the return value > > > + * is 1 for nr > 1. In this case, it means entries have inconsistent > > > + * zeromap. > > > + */ > > > +static inline unsigned int swap_zeromap_entries_count(swp_entry_t > > > entry, int nr) > > > > FWIW I am not really a fan of the count() function not returning an > > actual count. I think an enum with three states is more appropriate > > here, and renaming the function to swap_zeromap_entries_check() or > > similar. > > > > Make sense to me, what about the below? LGTM from a high level, but I didn't look closely at the bitmap interface usage. I am specifically unsure if we can pass 'end' as the size, but it seems like it should be fine. I will let Usama take a look. Thanks! > > From 24228a1e8426b8b05711a5efcfaae70abeb012c4 Mon Sep 17 00:00:00 2001 > From: Barry Song > Date: Thu, 5 Sep 2024 11:56:03 +1200 > Subject: [PATCH] mm: fix handling zero for large folios with partial zero= map > > There could be a corner case where the first entry is non-zeromap, > but a subsequent entry is zeromap. In this case, we should not > return false. > > Additionally, the iteration of test_bit() is unnecessary and > can be replaced with bitmap operations, which are more efficient. > > Since swap_read_folio() can't handle reading a large folio that's > partially zeromap and partially non-zeromap, we've moved the code > to mm/swap.h so that others, like those working on swap-in, can > access it. > > Fixes: 0ca0c24e3211 ("mm: store zero pages to be swapped out in a bitmap"= ) > Signed-off-by: Barry Song > --- > mm/page_io.c | 27 ++++----------------------- > mm/swap.h | 29 +++++++++++++++++++++++++++++ > 2 files changed, 33 insertions(+), 23 deletions(-) > > diff --git a/mm/page_io.c b/mm/page_io.c > index 4bc77d1c6bfa..46907c9dd20b 100644 > --- a/mm/page_io.c > +++ b/mm/page_io.c > @@ -226,26 +226,6 @@ static void swap_zeromap_folio_clear(struct folio *f= olio) > } > } > > -/* > - * Return the index of the first subpage which is not zero-filled > - * according to swap_info_struct->zeromap. > - * If all pages are zero-filled according to zeromap, it will return > - * folio_nr_pages(folio). > - */ > -static unsigned int swap_zeromap_folio_test(struct folio *folio) > -{ > - struct swap_info_struct *sis =3D swp_swap_info(folio->swap); > - swp_entry_t entry; > - unsigned int i; > - > - for (i =3D 0; i < folio_nr_pages(folio); i++) { > - entry =3D page_swap_entry(folio_page(folio, i)); > - if (!test_bit(swp_offset(entry), sis->zeromap)) > - return i; > - } > - return i; > -} > - > /* > * We may have stale swap cache pages in memory: notice > * them here and get rid of the unnecessary final write. > @@ -524,9 +504,10 @@ static void sio_read_complete(struct kiocb *iocb, lo= ng ret) > > static bool swap_read_folio_zeromap(struct folio *folio) > { > - unsigned int idx =3D swap_zeromap_folio_test(folio); > + unsigned int nr_pages =3D folio_nr_pages(folio); > + zeromap_stat_t stat =3D swap_zeromap_entries_check(folio->swap, n= r_pages); > > - if (idx =3D=3D 0) > + if (stat =3D=3D SWAP_ZEROMAP_NON) > return false; > > /* > @@ -534,7 +515,7 @@ static bool swap_read_folio_zeromap(struct folio *fol= io) > * currently handled. Return true without marking the folio uptod= ate so > * that an IO error is emitted (e.g. do_swap_page() will sigbus). > */ > - if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) > + if (WARN_ON_ONCE(stat =3D=3D SWAP_ZEROMAP_PARTIAL)) > return true; > > folio_zero_range(folio, 0, folio_size(folio)); > diff --git a/mm/swap.h b/mm/swap.h > index f8711ff82f84..f8e3fa061c1d 100644 > --- a/mm/swap.h > +++ b/mm/swap.h > @@ -4,6 +4,12 @@ > > struct mempolicy; > > +typedef enum { > + SWAP_ZEROMAP_NON, > + SWAP_ZEROMAP_FULL, > + SWAP_ZEROMAP_PARTIAL > +} zeromap_stat_t; > + > #ifdef CONFIG_SWAP > #include /* for swp_offset */ > #include /* for bio_end_io_t */ > @@ -80,6 +86,24 @@ static inline unsigned int folio_swap_flags(struct fol= io *folio) > { > return swp_swap_info(folio->swap)->flags; > } > + > +/* > + * Check if nr entries are all zeromap, non-zeromap or partially zeromap > + */ > +static inline zeromap_stat_t swap_zeromap_entries_check(swp_entry_t entr= y, int nr) > +{ > + struct swap_info_struct *sis =3D swp_swap_info(entry); > + unsigned long start =3D swp_offset(entry); > + unsigned long end =3D start + nr; > + > + if (find_next_bit(sis->zeromap, end, start) =3D=3D end) > + return SWAP_ZEROMAP_NON; > + if (find_next_zero_bit(sis->zeromap, end, start) =3D=3D end) > + return SWAP_ZEROMAP_FULL; > + > + return SWAP_ZEROMAP_PARTIAL; > +} > + > #else /* CONFIG_SWAP */ > struct swap_iocb; > static inline void swap_read_folio(struct folio *folio, struct swap_iocb= **plug) > @@ -171,6 +195,11 @@ static inline unsigned int folio_swap_flags(struct f= olio *folio) > { > return 0; > } > + > +static inline zeromap_stat_t swap_zeromap_entries_check(swp_entry_t entr= y, int nr) > +{ > + return SWAP_ZEROMAP_NONE; > +} > #endif /* CONFIG_SWAP */ > > #endif /* _MM_SWAP_H */ > -- > 2.34.1 > >