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 668EDCD4F22 for ; Thu, 5 Sep 2024 00:29:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CAEB46B027C; Wed, 4 Sep 2024 20:29:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5C306B027E; Wed, 4 Sep 2024 20:29:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD7326B027F; Wed, 4 Sep 2024 20:29:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 88F6E6B027C for ; Wed, 4 Sep 2024 20:29:42 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 383A71A0728 for ; Thu, 5 Sep 2024 00:29:42 +0000 (UTC) X-FDA: 82528801404.21.1480E07 Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) by imf22.hostedemail.com (Postfix) with ESMTP id 59CB7C0007 for ; Thu, 5 Sep 2024 00:29:39 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hjGRz2Ew; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.160.51 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725496131; 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=mEFd6qhlBAEcOUAlKTFgbmkZei5HvQ6w0Ywk2W8QABk=; b=M1hHf556YuP8zlfxVgkNW/crmGDSmaAuin6yG9Fw67sAErWUV+wXUb7b/0/RU3FOOF9G1z g0AT7XmCZTOyiXXHJ/+64BnW2gRbnG6bE1RZ9NLqvO0riBEV8X1S8asRXzIMQKHKf4MgDY WeW6CqVQpkEZP6S/eR2znTPSMyR1ZpA= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hjGRz2Ew; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.160.51 as permitted sender) smtp.mailfrom=21cnbao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725496131; a=rsa-sha256; cv=none; b=JOT8qHWy2DiDf4osPCPM3TXmgk6XKQoKgCzb9KhYkGlJL+dNWu7FQZNPlh8ZFLaF/0PTl7 pPTsjEfvQCBOXoaVgSkuF6j1MwIEF2uO9stCJGOMyoA0YDEziRztLqA1FJQiRvn3SSFGdb kBtL68nZasmiI1CGJjGqzzDGab2C0co= Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-277fa3de06fso173342fac.0 for ; Wed, 04 Sep 2024 17:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725496178; x=1726100978; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mEFd6qhlBAEcOUAlKTFgbmkZei5HvQ6w0Ywk2W8QABk=; b=hjGRz2EwYkB8AypDrNtUm4s97VUlunsTybpOhFp/aRnBVXb82bRdGIuWRjF7VsbSmI PPQ4HPpSvzoQPR7wGjNaqXa0Fh8tli9h4tqj6IQr/Ol3CU0Ys3umcg70nhqe4OErHKEK Lk4ie8emZaN+gvwN2cwiZmuMCokkx7maRB4KXx9eXiUBxb4WTW9RynItfiN24w3fq/JM A75OoS4BLKEJTLlSA4lHbYuMxLBZMXRHznk6G7XD2MvN4Lo3JhFqkCCDd+h9zD9C3AeQ P31oV2dDu66bHwYiFefi7GnC8DlqXl/ZfgTXN7VWy2SkpW3qCNX0UBvCllfRoXAvbfWE SJAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725496178; x=1726100978; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mEFd6qhlBAEcOUAlKTFgbmkZei5HvQ6w0Ywk2W8QABk=; b=K7KNAO+33b+ooWX+zeLdS+qZdKwXZo8NeDFovEmKXdW/uVggO48/Coz1hgE0oMD6VO gaCd9Z46f9VVBq1gtuYbqfsAzPvkETui7Mhg5w2xlcVs9FowGUJqPeeFTkqZCXlITFqG xLDAH2O4/ldHA0sGCspZJHSO4ewEm3TpI9L1BCbmWn4bAZauIaFthzDmewq4BDbOCp4/ sVCepaODpIou5/1bfh3iadFtTwlpBkQpYiDcw4x0Jd6V2OX3KdgT2aIQBphyn+31L1sh RqJydm+Z2oQt3B/kKDzkpQQd1nlF6nzRufqL8asfFju3qIO1HGLPtCzUsG0Ay+fuD/kx b6Ag== X-Forwarded-Encrypted: i=1; AJvYcCW+o0KqjVWeWKqu4IU35cW9B36AEa5EZlSbtsTbrAIAWesId988OVH3Pyy30b5QfhuCnB6XNXYeXw==@kvack.org X-Gm-Message-State: AOJu0YxxdPSoe9v4BQShzJUs4RBvh6hY+Xnazi2Da2NIood3Apsu/BPG 9xxXOG8mBa557UNCIImiJYD/+rS0BQvhbTGxirYn7ebysHmbWsrR X-Google-Smtp-Source: AGHT+IEp+9r8rGdfg/De7m0n3NCcboQiamR2fz1tciWoya/v/bNrW9VY5cdwhq+54PSHOLRYulVeVw== X-Received: by 2002:a05:6870:eca7:b0:277:e94b:779c with SMTP id 586e51a60fabf-27810b95fe3mr11015166fac.19.1725496178163; Wed, 04 Sep 2024 17:29:38 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:fdcc:5b39:8249:b563]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7d4fbd852c1sm2258446a12.7.2024.09.04.17.29.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 04 Sep 2024 17:29:37 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: yosryahmed@google.com Cc: 21cnbao@gmail.com, 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 Subject: Re: [PATCH v4 1/2] mm: store zero pages to be swapped out in a bitmap Date: Thu, 5 Sep 2024 12:29:26 +1200 Message-Id: <20240905002926.1055-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 59CB7C0007 X-Stat-Signature: yu3yowzpyx83hdig7i3w735x9fo4cmiy X-HE-Tag: 1725496179-826133 X-HE-Meta: U2FsdGVkX1+EnoK4YFteisUMWpQFoTNYGzTy51a/aAMhcsFb6v2rEfjlAQJRj8Y2IFMMznr1wuPmvbmU6y76ksNXHGRCBSakh2Zab95338WlXOGC4fmvoO25iKqCSk+g1ZnzgMsGI24RCbHsVFjweX8DRDafEaqazOhcAP901OWR0NqnHYmR0m35FfK37f0AIfnRymsEHs0P3+cijU0zDOV0dtonldnut1+c+OmytUi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BcK6q7gpI21iHKcHIB9NzX9HATbvmndUqZqKZcuFcwRe64nupTAnGjWeeGAlqdw8e5bztvF1PnLYMdx9J0I/NBVVDNW61Ta4mCCiFkX0WiGwZhJDJW8shC4fVT+aFfUw5KMODMUsMmSr3IvqyerOHIgAEI3SETIpAqqG+jB67fM/FfQU= 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, Sep 5, 2024 at 11:57 AM 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? >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 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 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 *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,9 +504,10 @@ 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); + unsigned int nr_pages = folio_nr_pages(folio); + zeromap_stat_t stat = swap_zeromap_entries_check(folio->swap, nr_pages); - if (idx == 0) + if (stat == SWAP_ZEROMAP_NON) return false; /* @@ -534,7 +515,7 @@ static bool swap_read_folio_zeromap(struct folio *folio) * 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(stat == 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 folio *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 entry, int nr) +{ + struct swap_info_struct *sis = swp_swap_info(entry); + unsigned long start = swp_offset(entry); + unsigned long end = start + nr; + + if (find_next_bit(sis->zeromap, end, start) == end) + return SWAP_ZEROMAP_NON; + if (find_next_zero_bit(sis->zeromap, end, start) == 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 folio *folio) { return 0; } + +static inline zeromap_stat_t swap_zeromap_entries_check(swp_entry_t entry, int nr) +{ + return SWAP_ZEROMAP_NONE; +} #endif /* CONFIG_SWAP */ #endif /* _MM_SWAP_H */ -- 2.34.1