From patchwork Tue Nov 5 21:19:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13863546 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 7E78ED3E770 for ; Tue, 5 Nov 2024 21:19:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFC216B0082; Tue, 5 Nov 2024 16:19:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAAFA6B0088; Tue, 5 Nov 2024 16:19:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A72476B008A; Tue, 5 Nov 2024 16:19:51 -0500 (EST) 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 85B1D6B0082 for ; Tue, 5 Nov 2024 16:19:51 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2FD8BC17F0 for ; Tue, 5 Nov 2024 21:19:51 +0000 (UTC) X-FDA: 82753307910.04.1BF60C4 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf26.hostedemail.com (Postfix) with ESMTP id E5B7C140007 for ; Tue, 5 Nov 2024 21:19:23 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Jlxeic4m; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730841530; a=rsa-sha256; cv=none; b=AqgYJU+Jd2mikdHkb9M80YZ9q2ccGX40Xe4+yZjo7MTqY0MVEKQPTPscUh/g+kcEFvVpvv TaAkNV4HbU/XUJCupVIlmUJdYOqhxHQ1bdGU2jTxX8kZMz2UG1MYz5M97IPSoayA7Jc9Ya dBXX+7+A/gOj1J3nNi/yTP4Ki+4qpkY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Jlxeic4m; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730841530; 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: references:dkim-signature; bh=78g/UltacuctUqO/KqSaHCwdizA+7FjwVPdidVsrYh8=; b=pJbF2t7oKPlMaNzSwu8Ffn1cNn5uK4G9R4ZQE2BU2zMJ5azgAsq/7roP+khcdQLYmr8uas d3cJBwDOaZI6eYEe+s1pR6bZ+qe+xjxJGw62z1i4oTgr2+vNzHAJuZGgF9yGBwMRlcvCFb yChEhavVUsvWuvjJcaD7QuFQcvILRGc= Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-723db2798caso2672147b3a.0 for ; Tue, 05 Nov 2024 13:19:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730841588; x=1731446388; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=78g/UltacuctUqO/KqSaHCwdizA+7FjwVPdidVsrYh8=; b=Jlxeic4mtlrRjKC1UFDalrzDiUWP+kTRQQQ488qBm7NV9uhnOlqg5eDZ0jSUceBJT0 Mo4OLljAy+2Cb1TKZ76tLE432RLVOeM1/UymxukYGzaHndV1DaYQ/a9VXoOf/XnAA+Wu 2ZY4RBMTl5/s+dNSxfISHGfu6u41ZT8QUKukuTEYA1BpiEzPhITPI4/guOebaQybzj6p YfkYA+2+BWiE4WgiqXm48xSr0MonBqDREOnScz+nigG6INdLNrQ7M3aeup7oISLNNDfT ufv55H5p5dTIkDyAg8RbfAjb0Dy7s4+JRXmefWWWMTz1HOYWBNbY7RwMMQtC54CapEjH 5yMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730841588; x=1731446388; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=78g/UltacuctUqO/KqSaHCwdizA+7FjwVPdidVsrYh8=; b=XxY30VvwgPpjtXtMyaXmvrj8V86vOooGc1kCnIQNKU4O+g9o/0YvDofwy7mSJ+tmZo 69AixkTLtV6W6VjNr/NqCQN8I2zJJLbpZjnUP5n/qDQvctmg07oXcZM1NtXatfXzvuRZ vgVm1h9YNsdlrKrS22/fuQIgJrNvR8zijWXvKCbX8z2Cxb/oL094HkBl1rHvfFXeSCjG 1K/kdRVzC2bN3eEnqDATRzlMDchiuUyf+M3ajFW3GGhdYK50CsEA08L4gF9/pr3zVyC4 pnfz/hU1GfqzB7mtVXZ1bi81llcgfSc+48EyF3qX7XpLCK6WRgvpOL4Maw0BfrY4Ro+b tvYA== X-Forwarded-Encrypted: i=1; AJvYcCV8lJAhiOE668f8zS5SYSYv8eNYCj2poZFURQbomjLD5tB71umU4gZ1g4I2Lqr9M8Lu7sLqqK8qNg==@kvack.org X-Gm-Message-State: AOJu0YwEUaUhhCldshJuUAJEHhC0U5vGB/NP3/4in+p8JbrVPo+1oUhj ljkVr8xMlbsZpOaxtzi3vNXWYS19q3QMWFemfXoYK2iCpRgKJfFg X-Google-Smtp-Source: AGHT+IHboVYLLbLpYHtd/PM6QhN03QTE5xzXp/O1/frEEs2UDffScaNan1q2/LypjnySfgkoVR8/eA== X-Received: by 2002:a05:6a00:27a0:b0:71d:f510:b791 with SMTP id d2e1a72fcca58-720ab3c6d19mr33962932b3a.12.1730841587791; Tue, 05 Nov 2024 13:19:47 -0800 (PST) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:19bb:246:5891:e8ce]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc2c7ef1sm10162920b3a.103.2024.11.05.13.19.40 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 05 Nov 2024 13:19:47 -0800 (PST) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Barry Song , Nhat Pham , Usama Arif , Chengming Zhou , 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 Subject: [PATCH v3] mm: count zeromap read and set for swapout and swapin Date: Wed, 6 Nov 2024 10:19:34 +1300 Message-Id: <20241105211934.5083-1-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: E5B7C140007 X-Rspamd-Server: rspam01 X-Stat-Signature: witk81iedpiht5pdcwdcrndnesbo7rbg X-HE-Tag: 1730841563-207903 X-HE-Meta: U2FsdGVkX1/ajvoi7BPOcD8m6IHeXPJlHV/KXIkujooyc5CEZJ5+IM15W2onaEYfnvOybQds3g8NX6MuVDugtNgohm0yQKbCgF2x7CxBtwU9a/o5wVg7eCrGMcGkLo8v0Qu+vg3TN8GjK79XxKekYtkzpjaWlWFlaDNNJRIXg8G9QRbifJDrS/dZW/wC542wdUx7mNFKBL4RnmnuWmV8+uzXlfd4ozxhWSDC04OIKp97sVf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snnhdfeIVeHlfCtRHiQweBdwqjujehg4sGP2y+wmXkQbcqjJSqDJnOtnHoQA/lqxjaqM9HZiARJ+caPmihtC7koalJHqDU9lycfKy4M0SFeT4u5uHzxHuWLBtfGb6jJMlx1toBv7bU+Zvy4E2BZonEkkBagWfdnvyuJ3/JIw0D1Wt960J4cWbv9diVk6Np4+djSocVoBQ+Y3iwlHVgqQM0k+0MZ1CKHvSUJ7vOVGOJxz2HfbPgx+VF+PaETmjYywyGuTg6kQchYVw3l6QzKctIWR6yEbFMpg3H+hsPIha4yUkR07hgfFMzPBezk5zmwVYfmMnn/XYQgKIl6WRow6uLmEBlpolWOQSaWf44ngdbSCxD3gELfw0NB82OEKbENqkgUxFTcPEu2fxTqpBTmnty3JM/7YJ9YM5w+7+ZnX2VmDTe3xM/JU5afFRMtNEwFDq5Bzndxjd 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: 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 Reviewed-by: Chengming Zhou Acked-by: Johannes Weiner --- -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