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 C7AA3D3E783 for ; Wed, 6 Nov 2024 02:52:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 619ED6B00A4; Tue, 5 Nov 2024 21:52:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C9F16B00A5; Tue, 5 Nov 2024 21:52:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 491246B00A6; Tue, 5 Nov 2024 21:52:50 -0500 (EST) 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 2A1226B00A4 for ; Tue, 5 Nov 2024 21:52:50 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C20EC81439 for ; Wed, 6 Nov 2024 02:52:49 +0000 (UTC) X-FDA: 82754147364.29.BE5C15A Received: from out-182.mta0.migadu.com (out-182.mta0.migadu.com [91.218.175.182]) by imf17.hostedemail.com (Postfix) with ESMTP id D743840026 for ; Wed, 6 Nov 2024 02:52:21 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="SYR4/KZl"; spf=pass (imf17.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.182 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730861345; 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=cV6dJd0Z+fIOMfEtL/0r1shAJgGhenmAKhtEa57be4M=; b=VlHb8kumXc6eGMDEaEacmDm4/8VVjXg2BeGiEQR0plaLCz0kdPyr/GrkAW+aVXNztVCWEQ nnDoHqh4yUEdAPlA4pZVLbrot1BJG8ANqpPwNgPXRzJ6QF3DjUJ+GLFqE4NGDZpPMci9rX A8DU+JA99IFDTI7ZUY2gYcT/ADadxnY= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="SYR4/KZl"; spf=pass (imf17.hostedemail.com: domain of chengming.zhou@linux.dev designates 91.218.175.182 as permitted sender) smtp.mailfrom=chengming.zhou@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730861345; a=rsa-sha256; cv=none; b=o/Bnuyfb04SdW7JO2o3iK2STtml6gwdA5IeGpx4S6FJEjvN5ul2UoLt0kLJ++dVfDhTh5Y 5Ep0vVYfH9AJCfS3bn1izvubksCtkuhIgJ+Cpx9/tHDrSouL6NYeVB8XO74mS10rNEuHER sO7DAqiYx5Lf0N3y6eDUzVlUzqpWAJg= Message-ID: <067039c2-5656-40b5-ab6e-a2325e3a9f4e@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1730861565; h=from:from: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; bh=cV6dJd0Z+fIOMfEtL/0r1shAJgGhenmAKhtEa57be4M=; b=SYR4/KZl6gWuWteE8cAhyqv6HsCgyVyi3J3r7yHvpMpHr3cxllwbDEPnQPsyOrqApfsCx+ X+iaQPBMwg90YM+t6A9J6HgspPJWRQvedfNtn2Tvvqg4kw1F/PytCKmYztrR/8qqsQYT+/ NZShQBOxYg5pqsZn6P89vV+uOVjegDY= Date: Wed, 6 Nov 2024 10:52:31 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v3] mm: count zeromap read and set for swapout and swapin To: Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Barry Song , Nhat Pham , Usama Arif , Yosry Ahmed , Hailong Liu , Johannes Weiner , David Hildenbrand , Hugh Dickins , Matthew Wilcox , Shakeel Butt , Andi Kleen , Baolin Wang , Chris Li , "Huang, Ying" , Kairui Song , Ryan Roberts References: <20241105211934.5083-1-21cnbao@gmail.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Chengming Zhou In-Reply-To: <20241105211934.5083-1-21cnbao@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D743840026 X-Stat-Signature: exxpewpph1xx71bksaecpxz4n8sckycz X-Rspam-User: X-HE-Tag: 1730861541-520574 X-HE-Meta: U2FsdGVkX18wKyNO9/kF7CbFhYjDtL7bp6Aeg8MMdVKgf4GMqEQ64rqnu9GtNmwoHKaeeqMUDsSwH4qfmZhkRCrCmCW5uiOyygZbC4pb4XenkoS+l1qaXayAsx9w7skPF81GMiOnn1fb4ddHLbfnz2NofTiZn2mz1KV6afKEquCEnHcHOPBtQ17OyAmXY1iW1EkjQntJICkk7yOhn8aUc2v2LYMmf+2qJjvc5KFH/DPL61ybM/76f8W0PnkRMu2M4Ul9hdwYbdprTHvwhXeJLu7XMZ+HomCE9WjfGrTbAHJvPi+Ql2kvpfqn4QcL+gfxaN+DdPwDUo/YyX19hETnYo8IAN6mnTkhRDPEb+/vl5ksYR2bbPmU5A54x+9G0R8CWLnSu4rLcnjyQUyjn0IztV/L/XqUgXgVw10ZEZqVCMMcniOzl7eKLU5Whws8mZUjimxrJvr2GVLdY6a9a96En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: 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 2024/11/6 05:19, Barry Song wrote: > From: Barry Song > > When the proportion of folios from the zeromap is small, missing their > accounting may not significantly impact profiling. However, it’s easy > to construct a scenario where this becomes an issue—for example, > allocating 1 GB of memory, writing zeros from userspace, followed by > MADV_PAGEOUT, and then swapping it back in. In this case, the swap-out > and swap-in counts seem to vanish into a black hole, potentially > causing semantic ambiguity. > > On the other hand, Usama reported that zero-filled pages can exceed 10% in > workloads utilizing zswap, while Hailong noted that some app in Android > have more than 6% zero-filled pages. Before commit 0ca0c24e3211 ("mm: store > zero pages to be swapped out in a bitmap"), both zswap and zRAM implemented > similar optimizations, leading to these optimized-out pages being counted > in either zswap or zRAM counters (with pswpin/pswpout also increasing for > zRAM). With zeromap functioning prior to both zswap and zRAM, userspace > will no longer detect these swap-out and swap-in actions. > > We have three ways to address this: > > 1. Introduce a dedicated counter specifically for the zeromap. > 2. Use pswpin/pswpout accounting, treating the zero map as a standard > backend. This approach aligns with zRAM's current handling of > same-page fills at the device level. However, it would mean losing > the optimized-out page counters previously available in zRAM and > would not align with systems using zswap. Additionally, as noted by > Nhat Pham, pswpin/pswpout counters apply only to I/O done directly > to the backend device. > 3. Count zeromap pages under zswap, aligning with system behavior when > zswap is enabled. However, this would not be consistent with zRAM, > nor would it align with systems lacking both zswap and zRAM. > > Given the complications with options 2 and 3, this patch selects > option 1. > > We can find these counters from /proc/vmstat (counters for the whole > system) and memcg's memory.stat (counters for the interested memcg). > > For example: > > $ grep -E 'swpin_zero|swpout_zero' /proc/vmstat > swpin_zero 1648 > swpout_zero 33536 > > $ grep -E 'swpin_zero|swpout_zero' /sys/fs/cgroup/system.slice/memory.stat > swpin_zero 3905 > swpout_zero 3985 > > This patch does not address any specific zeromap bug, but the missing > swpout and swpin counts for zero-filled pages can be highly confusing > and may mislead user-space agents that rely on changes in these counters > as indicators. Therefore, we add a Fixes tag to encourage the inclusion > of this counter in any kernel versions with zeromap. > > Fixes: 0ca0c24e3211 ("mm: store zero pages to be swapped out in a bitmap") > Reviewed-by: Nhat Pham > Cc: Usama Arif > Cc: Chengming Zhou > Cc: Yosry Ahmed > Cc: Hailong Liu > Cc: Johannes Weiner > Cc: David Hildenbrand > Cc: Hugh Dickins > Cc: Matthew Wilcox (Oracle) > Cc: Shakeel Butt > Cc: Andi Kleen > Cc: Baolin Wang > Cc: Chris Li > Cc: "Huang, Ying" > Cc: Kairui Song > Cc: Ryan Roberts > Signed-off-by: Barry Song Looks good to me! Reviewed-by: Chengming Zhou Thanks. > --- > -v3: > * collected Nhat's reviewed-by, thanks! > * refine doc per Usama and David, thanks! > * refine changelog > > Documentation/admin-guide/cgroup-v2.rst | 9 +++++++++ > include/linux/vm_event_item.h | 2 ++ > mm/memcontrol.c | 4 ++++ > mm/page_io.c | 16 ++++++++++++++++ > mm/vmstat.c | 2 ++ > 5 files changed, 33 insertions(+) > > diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst > index db3799f1483e..13736a94edfd 100644 > --- a/Documentation/admin-guide/cgroup-v2.rst > +++ b/Documentation/admin-guide/cgroup-v2.rst > @@ -1599,6 +1599,15 @@ The following nested keys are defined. > pglazyfreed (npn) > Amount of reclaimed lazyfree pages > > + swpin_zero > + Number of pages swapped into memory and filled with zero, where I/O > + was optimized out because the page content was detected to be zero > + during swapout. > + > + swpout_zero > + Number of zero-filled pages swapped out with I/O skipped due to the > + content being detected as zero. > + > zswpin > Number of pages moved in to memory from zswap. > > diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h > index aed952d04132..f70d0958095c 100644 > --- a/include/linux/vm_event_item.h > +++ b/include/linux/vm_event_item.h > @@ -134,6 +134,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, > #ifdef CONFIG_SWAP > SWAP_RA, > SWAP_RA_HIT, > + SWPIN_ZERO, > + SWPOUT_ZERO, > #ifdef CONFIG_KSM > KSM_SWPIN_COPY, > #endif > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 5e44d6e7591e..7b3503d12aaf 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -441,6 +441,10 @@ static const unsigned int memcg_vm_event_stat[] = { > PGDEACTIVATE, > PGLAZYFREE, > PGLAZYFREED, > +#ifdef CONFIG_SWAP > + SWPIN_ZERO, > + SWPOUT_ZERO, > +#endif > #ifdef CONFIG_ZSWAP > ZSWPIN, > ZSWPOUT, > diff --git a/mm/page_io.c b/mm/page_io.c > index 5d9b6e6cf96c..4b4ea8e49cf6 100644 > --- a/mm/page_io.c > +++ b/mm/page_io.c > @@ -204,7 +204,9 @@ static bool is_folio_zero_filled(struct folio *folio) > > static void swap_zeromap_folio_set(struct folio *folio) > { > + struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio); > struct swap_info_struct *sis = swp_swap_info(folio->swap); > + int nr_pages = folio_nr_pages(folio); > swp_entry_t entry; > unsigned int i; > > @@ -212,6 +214,12 @@ static void swap_zeromap_folio_set(struct folio *folio) > entry = page_swap_entry(folio_page(folio, i)); > set_bit(swp_offset(entry), sis->zeromap); > } > + > + count_vm_events(SWPOUT_ZERO, nr_pages); > + if (objcg) { > + count_objcg_events(objcg, SWPOUT_ZERO, nr_pages); > + obj_cgroup_put(objcg); > + } > } > > static void swap_zeromap_folio_clear(struct folio *folio) > @@ -507,6 +515,7 @@ static void sio_read_complete(struct kiocb *iocb, long ret) > static bool swap_read_folio_zeromap(struct folio *folio) > { > int nr_pages = folio_nr_pages(folio); > + struct obj_cgroup *objcg; > bool is_zeromap; > > /* > @@ -521,6 +530,13 @@ static bool swap_read_folio_zeromap(struct folio *folio) > if (!is_zeromap) > return false; > > + objcg = get_obj_cgroup_from_folio(folio); > + count_vm_events(SWPIN_ZERO, nr_pages); > + if (objcg) { > + count_objcg_events(objcg, SWPIN_ZERO, nr_pages); > + obj_cgroup_put(objcg); > + } > + > folio_zero_range(folio, 0, folio_size(folio)); > folio_mark_uptodate(folio); > return true; > diff --git a/mm/vmstat.c b/mm/vmstat.c > index 22a294556b58..c8ef7352f9ed 100644 > --- a/mm/vmstat.c > +++ b/mm/vmstat.c > @@ -1418,6 +1418,8 @@ const char * const vmstat_text[] = { > #ifdef CONFIG_SWAP > "swap_ra", > "swap_ra_hit", > + "swpin_zero", > + "swpout_zero", > #ifdef CONFIG_KSM > "ksm_swpin_copy", > #endif