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 128BCE6FE49 for ; Fri, 6 Sep 2024 22:56:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 36D266B007B; Fri, 6 Sep 2024 18:56:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 31C846B0083; Fri, 6 Sep 2024 18:56:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E3D16B0085; Fri, 6 Sep 2024 18:56:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0151F6B007B for ; Fri, 6 Sep 2024 18:56:33 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 7B79E1C5E11 for ; Fri, 6 Sep 2024 22:56:33 +0000 (UTC) X-FDA: 82535824266.01.E9503B6 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) by imf20.hostedemail.com (Postfix) with ESMTP id AB9B21C0002 for ; Fri, 6 Sep 2024 22:56:31 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="aYD/jZpe"; spf=pass (imf20.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=1725663366; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=HiNEXxUXaAya/J/V4msCpoH+GzBrHzq/eQ1BTfNZOyg=; b=YRYEOeKcvatrzmACeZ24/zoUfOr+bdiVrvlBXxv5FHHTGyPnfUMQQpvGWUf8A7VvNB7HM0 q1ipCJ8+ZrIn79NPwv3DQODW7JIb2XHaD71WwcjnprwjvNJ7leU/JcctdWBI5+HwoW9858 A0WS4vMlomsPYOL3ptaYEdYk40KinV8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="aYD/jZpe"; spf=pass (imf20.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725663366; a=rsa-sha256; cv=none; b=U+cbym4psl4VAEUjsN/uSYZoM0Gvh0FQyTAOAPZccAgsz5PFl0wFpZoZMAWGWCtYr4+LL/ nTXNuik4qDhymXjCeES3SXHinaEjwuGI/pzoS+4Izf00sJCTyPIO3/8nnl5snKG4R1T107 I3tZhD1BUTonUIVpMYqBj2SILEeiuVw= Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-a8692bbec79so346650066b.3 for ; Fri, 06 Sep 2024 15:56:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1725663390; x=1726268190; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=HiNEXxUXaAya/J/V4msCpoH+GzBrHzq/eQ1BTfNZOyg=; b=aYD/jZpesDbjEd0hQkO9c9z9LR7s4BLKsksFOEasaNuvy7l+Psp4/ZLU4TGoEA8WgE utjpV1DAlz9+NRHBLS+npD5RvZd2rSnbro/y8oiw3hgxRVeQmZkrro7PgwpokfykeRc+ HsmZAzWtQHMX9tFWBrncGIPgo45oq6VvjLDtp0jq0jyXBUI6bL7q5JxO9mf+/y9MBiqy U9x3B83bOF7noPDBkLvGcT3RGcfkiXX7LO0twC6M6OHsvEbkvMQ/KiXXioPl4Q/1PAnx ypzcFf6nx3YSTTC1nwjRc6trl/Eq4bTj8bOKf9lkudVWc6RBDjISjG0E3QU6Z8NQK62E DB3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725663390; x=1726268190; h=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=HiNEXxUXaAya/J/V4msCpoH+GzBrHzq/eQ1BTfNZOyg=; b=Wkq2lrp+VWMfGljxale1/2izkcn8xXzbVslEkYWvEprxtFu7tNlijbWzF9t0MqTiKD 3MpdO9ppge4Fu18iNstHp9Gn7m8EHQyuuc+WpzgyEPVzksaHBpS+uda0Dg1u3IkN9MVJ JMQmUWxctunPD7YwtydI7zAD4JMaICnoQiCv1RZjCCQoi0SdhLRLTY+5Ivoqdb0o11z4 vh9VMV6Dr+aLJ/OxreGCCk5HlD5gaNPq/agJBymLq35E7deySaLwFgP0I9uUtlXJ+zXi lqnB5NKFC4LjwCoNOzDEfLAcoZuGXnuMXPehqnLt58gpSHzExRz7e9yaa/2VLfeD+qxy qnmQ== X-Forwarded-Encrypted: i=1; AJvYcCVebxvQOFGYvKn+OcA3VSQLHpaRFv4POEYjNlCcJS1pzGZK9/sQ2SoQxducjT6nQzFPMj9CqEcZbw==@kvack.org X-Gm-Message-State: AOJu0YwSvcGPBvpURVl36VyUIORqGcf3FdCLwq1qgU5H0qi66EbhMJ+8 dwB3tJR1931WvkeBVZC5IoF6l5npw2zAP+sdYlZtY4urZ+RLBI7tN0IXn0CnB3qRT5I2yYvEPxg nEttZHOG/Tz1Jgl/vkcBmjS2LHTdx50lr0jgA X-Google-Smtp-Source: AGHT+IHVaYMNUjIyM5zmQ6DYBa7f2LxFvdld0ZeITpKhBft97oz/KkCwl+fmf8DKk1mlfjh3WU/DXvkc9+cnS7WHjpQ= X-Received: by 2002:a17:907:a45:b0:a8a:445f:ac4d with SMTP id a640c23a62f3a-a8a885f48b3mr363423566b.18.1725663389282; Fri, 06 Sep 2024 15:56:29 -0700 (PDT) MIME-Version: 1.0 References: <20240906215842.1603-1-21cnbao@gmail.com> In-Reply-To: <20240906215842.1603-1-21cnbao@gmail.com> From: Yosry Ahmed Date: Fri, 6 Sep 2024 15:55:52 -0700 Message-ID: Subject: Re: [PATCH v8 1/3] mm: Fix swap_read_folio_zeromap() for large folios with partial zeromap To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, hannes@cmpxchg.org, hch@infradead.org, hughd@google.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, minchan@kernel.org, nphamcs@gmail.com, ryan.roberts@arm.com, ryncsn@gmail.com, senozhatsky@chromium.org, shakeel.butt@linux.dev, shy828301@gmail.com, surenb@google.com, usamaarif642@gmail.com, v-songbaohua@oppo.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Stat-Signature: 7muma5mw85jpr1yabzjdwdqu4sf9w68p X-Rspamd-Queue-Id: AB9B21C0002 X-Rspamd-Server: rspam11 X-HE-Tag: 1725663391-403290 X-HE-Meta: U2FsdGVkX1+PZeZnWwLuchvTJOmGlx9x+/71rbK9R+0vT4T2Bb2U80W/pvUOnsrwWo02XfnfoJ+nd21F158bJO7mFuzfKD/3aY+M7WqLHb+mZMeGoHcH60BB1FK2e0lUE3OSlyOxvHn2Frgw4Qm2l0cG6FZmTQTie/8+SNKbz8S6h6lo1nGS3htsDtoaeFWIhSPcpMbJM+sIA8HyONNp0oHrWgSIgC6OL38kNjfZtugjM0WNXgdraGH3muzlcHqba+U0BM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4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [..] > Yep :-) with the above two changes, the patch becomes: > > From 272c04cb758b8062eaa96a52b855ff79c8afdf6a Mon Sep 17 00:00:00 2001 > From: Barry Song > Date: Thu, 5 Sep 2024 11:56:03 +1200 > Subject: [PATCH v8 1/3] mm: Fix swap_read_folio_zeromap() for large folios > with partial zeromap > > 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 > let swap_read_folio_zeromap() return false since we will still > read corrupted data. > > Additionally, the iteration of test_bit() is unnecessary and > can be replaced with bitmap operations, which are more efficient. > > We can adopt the style of swap_pte_batch() and folio_pte_batch() to > introduce swap_zeromap_batch() which seems to provide the greatest > flexibility for the caller. This approach allows the caller to either > check if the zeromap status of all entries is consistent or determine > the number of contiguous entries with the same status. > > 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") > Cc: Usama Arif > Cc: Yosry Ahmed > Signed-off-by: Barry Song Reviewed-by: Yosry Ahmed > --- > mm/page_io.c | 32 +++++++------------------------- > mm/swap.h | 33 +++++++++++++++++++++++++++++++++ > 2 files changed, 40 insertions(+), 25 deletions(-) > > diff --git a/mm/page_io.c b/mm/page_io.c > index 4bc77d1c6bfa..bc1183299a7d 100644 > --- a/mm/page_io.c > +++ b/mm/page_io.c > @@ -226,26 +226,6 @@ static void swap_zeromap_folio_clear(struct folio *folio) > } > } > > -/* > - * 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 = swp_swap_info(folio->swap); > - swp_entry_t entry; > - unsigned int i; > - > - for (i = 0; i < folio_nr_pages(folio); i++) { > - entry = 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,19 +504,21 @@ static void sio_read_complete(struct kiocb *iocb, long ret) > > static bool swap_read_folio_zeromap(struct folio *folio) > { > - unsigned int idx = swap_zeromap_folio_test(folio); > - > - if (idx == 0) > - return false; > + int nr_pages = folio_nr_pages(folio); > + bool is_zeromap; > > /* > * Swapping in a large folio that is partially in the zeromap is not > * currently handled. Return true without marking the folio uptodate 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(swap_zeromap_batch(folio->swap, nr_pages, > + &is_zeromap) != nr_pages)) > return true; > > + if (!is_zeromap) > + return false; > + > folio_zero_range(folio, 0, folio_size(folio)); > folio_mark_uptodate(folio); > return true; > diff --git a/mm/swap.h b/mm/swap.h > index f8711ff82f84..e0397a197620 100644 > --- a/mm/swap.h > +++ b/mm/swap.h > @@ -80,6 +80,32 @@ static inline unsigned int folio_swap_flags(struct folio *folio) > { > return swp_swap_info(folio->swap)->flags; > } > + > +/* > + * Return the count of contiguous swap entries that share the same > + * zeromap status as the starting entry. If is_zeromap is not NULL, > + * it will return the zeromap status of the starting entry. > + */ > +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, > + bool *is_zeromap) > +{ > + struct swap_info_struct *sis = swp_swap_info(entry); > + unsigned long start = swp_offset(entry); > + unsigned long end = start + max_nr; > + bool first_bit; > + > + first_bit = test_bit(start, sis->zeromap); > + if (is_zeromap) > + *is_zeromap = first_bit; > + > + if (max_nr <= 1) > + return max_nr; > + if (first_bit) > + return find_next_zero_bit(sis->zeromap, end, start) - start; > + else > + return find_next_bit(sis->zeromap, end, start) - start; > +} > + > #else /* CONFIG_SWAP */ > struct swap_iocb; > static inline void swap_read_folio(struct folio *folio, struct swap_iocb **plug) > @@ -171,6 +197,13 @@ static inline unsigned int folio_swap_flags(struct folio *folio) > { > return 0; > } > + > +static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr, > + bool *has_zeromap) > +{ > + return 0; > +} > + > #endif /* CONFIG_SWAP */ > > #endif /* _MM_SWAP_H */ > -- > 2.34.1 > > Thanks > Barry >