From patchwork Fri Aug 23 16:27:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Yuan X-Patchwork-Id: 13775537 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 E451CC5321D for ; Fri, 23 Aug 2024 16:27:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D368800B1; Fri, 23 Aug 2024 12:27:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5834C800A4; Fri, 23 Aug 2024 12:27:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 47236800B1; Fri, 23 Aug 2024 12:27:15 -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 26946800A4 for ; Fri, 23 Aug 2024 12:27:15 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C6D9E120DC8 for ; Fri, 23 Aug 2024 16:27:14 +0000 (UTC) X-FDA: 82484039988.05.2EB59B2 Received: from mail-4323.proton.ch (mail-4323.proton.ch [185.70.43.23]) by imf11.hostedemail.com (Postfix) with ESMTP id A9E4B40006 for ; Fri, 23 Aug 2024 16:27:12 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=yhndnzj.com header.s=protonmail header.b=PMK7RTWW; dmarc=pass (policy=quarantine) header.from=yhndnzj.com; spf=pass (imf11.hostedemail.com: domain of me@yhndnzj.com designates 185.70.43.23 as permitted sender) smtp.mailfrom=me@yhndnzj.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724430341; 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=7LV5wuij5UHvq2TzSAXYR/Fp+ylFof5gSswrfbA2tBU=; b=B9efDXSSqCM9GSqY/RuxMk9JC/K/x5JLMp90POxNzL3sX3YTQdU2GhKfuN1s6MAZ1zGjff ui+ier/so5Y/UI2jSLT6241yQuc7WzRSeF+dTgPSAfxhDSp10fWIYnj2xr1hSve/HtsIky vtrlti4AuQIdmZxAn8G3K5bswdZw05c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724430341; a=rsa-sha256; cv=none; b=ibwz/w9QT9y6EmzyAe891hrAHn0lQwTbrJ/DVq8f0F3OqZvbBCERq/d83QukrMXhC1eldJ YRav+jsooMZI7EWLfSX6QGM2wbwpgutau88JvZj7ANTtyNNdwIkXVKljyAMe3QaLmMcnmP E+4EjaxJPSPPPZMdftyGlWwCm4E7d8A= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=yhndnzj.com header.s=protonmail header.b=PMK7RTWW; dmarc=pass (policy=quarantine) header.from=yhndnzj.com; spf=pass (imf11.hostedemail.com: domain of me@yhndnzj.com designates 185.70.43.23 as permitted sender) smtp.mailfrom=me@yhndnzj.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yhndnzj.com; s=protonmail; t=1724430429; x=1724689629; bh=7LV5wuij5UHvq2TzSAXYR/Fp+ylFof5gSswrfbA2tBU=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=PMK7RTWWQ6HcUw0ixQgF0emg4ra1NhSx9KpxtrQMlLZYDWnFBZy0RQO1CwCmjCMQM OnmXPC9rN66VxdZYrhN3DKTzN4R+aIATv5/e9Y+eA3XhN7o/7n/WUm99K54f0Zro2+ n+VEfPQ4vl2CnM4RCh6qFXzfZSJkwrkHfFM46H01QfwvgAPY7GPMn+BMRyzfQR1anh QJtwfmfojLVv+CQ0DKU0Xkf+3/CTbaqJLJP6pygG6H+efzD49m6AfgSu0UjpdvuT+I VwiD9tCmdJVex+RBKf02uM5qASyWyHPvBP38SXprzDIo0iNEc3K29ltQOZ23schN7d zN915e3f1w1Zw== Date: Fri, 23 Aug 2024 16:27:06 +0000 To: linux-kernel@vger.kernel.org From: Mike Yuan Cc: Mike Yuan , linux-mm@kvack.org, cgroups@vger.kernel.org, Nhat Pham , Yosry Ahmed , Johannes Weiner , Andrew Morton , Muchun Song , Shakeel Butt , Roman Gushchin , Michal Hocko , =?utf-8?q?Michal_Koutn=C3=BD?= , stable@vger.kernel.org Subject: [PATCH v3 1/3] mm/memcontrol: respect zswap.writeback setting from parent cg too Message-ID: <20240823162506.12117-1-me@yhndnzj.com> Feedback-ID: 102487535:user:proton X-Pm-Message-ID: 1aeab9fed9e0166d41ca57d11d544d5490bc3859 MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A9E4B40006 X-Stat-Signature: itrruh1xk3h73wkidxt8xn3yduonqa3s X-Rspam-User: X-HE-Tag: 1724430432-524369 X-HE-Meta: U2FsdGVkX19bQPAdXmneqsFT11WsAvMr+6MxIO80Egyi1ew3pTjVOi3Pv9nRmhJ46bKZoVaVITPUPddIVdYtC4ReAsgA2RrkFlzjqzxfSnu93BqAldKoITnrwJ2MlHBrJvEPKw2dY4+MFEGzXXVghMBYmUzlVYLNoFcHL0H3UA+h05q8+BuzEX/poU0EXneVIOnUA9PDA8FFtrfJeRbR//DD6Dm7jymKM98elCLGrsLFiJjjRAk8ctlz16/qGSkPtXLyDq2mTjKYvYRpIEJkAP0t9G2Dq9wFa7pbVMltE9Iqf66aJjFgPYiOHvLGwlNzWSkZ5BEzBX8xuefUSzVjPI+u/sa/iiSs7nGCVzHe8YkTkYDubT9jNxWvbXSICWeB/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HOzyWBL75jkxGvukFxJ6lSEmcCbe5DVPqqzH0M7JhzP7HzdnVNOaT72xtA+2GQ2JS4Y48rUv58QrbMuYyeGdbg+BeIXIlu5nvn/ev/s3yu8s7EbHbyf0olEcjVyAh9n6tDEQ3JqWi2QFnloAnUBPhc= 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: Currently, the behavior of zswap.writeback wrt. the cgroup hierarchy seems a bit odd. Unlike zswap.max, it doesn't honor the value from parent cgroups. This surfaced when people tried to globally disable zswap writeback, i.e. reserve physical swap space only for hibernation [1] - disabling zswap.writeback only for the root cgroup results in subcgroups with zswap.writeback=1 still performing writeback. The inconsistency became more noticeable after I introduced the MemoryZSwapWriteback= systemd unit setting [2] for controlling the knob. The patch assumed that the kernel would enforce the value of parent cgroups. It could probably be workarounded from systemd's side, by going up the slice unit tree and inheriting the value. Yet I think it's more sensible to make it behave consistently with zswap.max and friends. [1] https://wiki.archlinux.org/title/Power_management/Suspend_and_hibernate#Disable_zswap_writeback_to_use_the_swap_space_only_for_hibernation [2] https://github.com/systemd/systemd/pull/31734 Changes in v3: - Additionally drop inheritance of zswap.writeback setting on cgroup creation, which is no longer needed Link to v2: https://lore.kernel.org/linux-kernel/20240816144344.18135-1-me@yhndnzj.com/ Changes in v2: - Actually base on latest tree (is_zswap_enabled() -> zswap_is_enabled()) - Update Documentation/admin-guide/cgroup-v2.rst to reflect the change Link to v1: https://lore.kernel.org/linux-kernel/20240814171800.23558-1-me@yhndnzj.com/ Cc: Nhat Pham Cc: Yosry Ahmed Cc: Johannes Weiner Cc: Andrew Morton Cc: Michal Koutný Fixes: 501a06fe8e4c ("zswap: memcontrol: implement zswap writeback disabling") Cc: Signed-off-by: Mike Yuan Reviewed-by: Nhat Pham Acked-by: Yosry Ahmed --- Documentation/admin-guide/cgroup-v2.rst | 7 ++++--- mm/memcontrol.c | 12 +++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) base-commit: 47ac09b91befbb6a235ab620c32af719f8208399 diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 86311c2907cd..95c18bc17083 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1717,9 +1717,10 @@ The following nested keys are defined. entries fault back in or are written out to disk. memory.zswap.writeback - A read-write single value file. The default value is "1". The - initial value of the root cgroup is 1, and when a new cgroup is - created, it inherits the current value of its parent. + A read-write single value file. The default value is "1". + Note that this setting is hierarchical, i.e. the writeback would be + implicitly disabled for child cgroups if the upper hierarchy + does so. When this is set to 0, all swapping attempts to swapping devices are disabled. This included both zswap writebacks, and swapping due diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f29157288b7d..d563fb515766 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3613,8 +3613,7 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) memcg1_soft_limit_reset(memcg); #ifdef CONFIG_ZSWAP memcg->zswap_max = PAGE_COUNTER_MAX; - WRITE_ONCE(memcg->zswap_writeback, - !parent || READ_ONCE(parent->zswap_writeback)); + WRITE_ONCE(memcg->zswap_writeback, true); #endif page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); if (parent) { @@ -5320,7 +5319,14 @@ void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size) bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) { /* if zswap is disabled, do not block pages going to the swapping device */ - return !zswap_is_enabled() || !memcg || READ_ONCE(memcg->zswap_writeback); + if (!zswap_is_enabled()) + return true; + + for (; memcg; memcg = parent_mem_cgroup(memcg)) + if (!READ_ONCE(memcg->zswap_writeback)) + return false; + + return true; } static u64 zswap_current_read(struct cgroup_subsys_state *css,