From patchwork Fri Jul 7 04:46:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13304389 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 B59D0EB64D9 for ; Fri, 7 Jul 2023 04:52:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 523A88D0005; Fri, 7 Jul 2023 00:52:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AD578D0001; Fri, 7 Jul 2023 00:52:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 374F78D0005; Fri, 7 Jul 2023 00:52:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 241938D0001 for ; Fri, 7 Jul 2023 00:52:04 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ECDC31C8FF1 for ; Fri, 7 Jul 2023 04:52:03 +0000 (UTC) X-FDA: 80983593726.11.6B3F6D0 Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) by imf15.hostedemail.com (Postfix) with ESMTP id 32BF0A0010 for ; Fri, 7 Jul 2023 04:52:02 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=CHZ1ledA; spf=pass (imf15.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.219.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1688705522; a=rsa-sha256; cv=none; b=LkZ1a+6yM22r2lyHfBbBIgK5+odFDLQsGjRF6wfI2cOGrCGzhbMPEA9U3MJ+ZTTdePwmc0 guXVWfUVBWh1UG/dXLYNVTsv7+SYv3IQYuhMS8NwiFrY6LHIWeOqcJnOAMJiOi8cONKASy izLbepRDRdObWu+97gILIUNPXA0cmVc= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=CHZ1ledA; spf=pass (imf15.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.219.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1688705522; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=FOcqYStjaEQ6e41x7ko6U0Q7unE1KNRPYn3wRQMvZDI=; b=p6xNUgRlmatzz68DVp28R1OD8ADctmYKHxHIE1Y/kmy3rSSLAIhskgqRV3KJH3I7JZbWB+ a5SMzgZnlgyqqj5THliKc6NQyWroN+bAh3+DtqzwjnqF2txnhbysPn6pcnwih5ME6C9+Or BjUrt6wtnWS+4SJ8/MmOwtf5eO0HMAE= Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-bfe6ea01ff5so1592068276.3 for ; Thu, 06 Jul 2023 21:52:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1688705521; x=1691297521; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=FOcqYStjaEQ6e41x7ko6U0Q7unE1KNRPYn3wRQMvZDI=; b=CHZ1ledA9NwOG/pSJSHv/D+w66wHvrAztOFQOqH479zhDguVShOXbLJovgowm66V7+ klOY9hA4elIXXZbhXyFhcMkhZg6lXL/+gUGkHeX77QXCsBCP3B31wiSwgoakmMY1hKYr FYdZEUpDmS+1i6VPtY+eS3Peq1HVVjUWy9lRvRCq69FAyzYwkTLwnbI3PnJPHewlz367 4oIie2F1IfD3tYhwmtKttNxXHO6fEenTBtE5hJSXY3Cwzaoh+WG3CvNxuA+/r1XO/hsY 7NEkwiRU+VU48LhNB59xbu0htXAoLhAwlRXEsagOVlq2XL6kSe7ebB49GrBkas0T6JFi EwUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688705521; x=1691297521; 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=FOcqYStjaEQ6e41x7ko6U0Q7unE1KNRPYn3wRQMvZDI=; b=guxC4UMCqgAgurpStPTmLQQALjQrrIGAaQxyxEcLU8HMQWnbCGwblPV8m+BdPpjIKN t3WOouQMFpUeti7WoP138Ox9p02XLyO72NZbYkl5jH9UvfOGnO4LrQCLeKvTWVCTB68s WauzPTCFHMJ+P659/W97iJJXs5mkwRwnKCNqZ8xXwXkI3Fq7a24IhtBN3vtpK56Ix1rO h2VNXqwj2g0A8jzqtWKBi5HymRj85kKWB+nlIYp/DqDVkkP0SDjVXTUwyQ0omdz6hHjE cWwb0NNfJ1jyvoq+ZVN69He7LzaW5mXE8WcHfuHpxqdNaAT/BE7PG2QJ1wa5D/751MAS GXrQ== X-Gm-Message-State: ABy/qLamdNvdM7WM2z+r94VYXoxcoPjc0qHvyyCstZUDZC4WQLgJfCPM Mzb9gtDYxhCm2VT95fWkamv5iK3yO9GmmVvvcKE= X-Google-Smtp-Source: APBJJlHb8cX+0KMLn+D06cRMztmkT8KQkZHyFAw/d3ujFt0BRPy1YxSFDgU25z1DlZoH715my6Kvhg== X-Received: by 2002:a17:902:7c0a:b0:1b4:65c3:ca68 with SMTP id x10-20020a1709027c0a00b001b465c3ca68mr405138pll.66.1688705217824; Thu, 06 Jul 2023 21:46:57 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.147]) by smtp.gmail.com with ESMTPSA id bc8-20020a170902930800b001ac95be5081sm2193423plb.307.2023.07.06.21.46.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jul 2023 21:46:57 -0700 (PDT) From: Zhongkun He To: minchan@kernel.org, senozhatsky@chromium.org, mhocko@suse.com Cc: david@redhat.com, yosryahmed@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhongkun He Subject: [RFC PATCH 1/2] memcg: Add support for zram object charge Date: Fri, 7 Jul 2023 12:46:49 +0800 Message-Id: <20230707044649.1169155-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 32BF0A0010 X-Stat-Signature: k6yqy86sb1ksyfaef5m1kmg9qnkrtuge X-Rspam-User: X-HE-Tag: 1688705522-457095 X-HE-Meta: U2FsdGVkX1/oCmnDxAca97UTHXOBcJ2Ya8G3FOwn1DwRFPYwKCZEHC5Pjjl3u6ISMF04ukuS8YB70qvVS7Uin4fJcV5hZtkAL8EMgac7kGCVFoJXUJYjqc3TQBl10r1jKaile1PipQr1cuQSzfisIfUYanMigeL/uw/+Lg+sstPO229Q6dyOnR2CE7CWufLj1/1vTUkCqynEYRTzItRswEdIzM9dz/1wlFgnwuCuVGk5lbgr+YeR0JNaO5lbwkDw1pld8s7/KErdlcjnmB9h3LjvFbBIECoRn9NFDM478Y1a+fwctnM+9q3NXbEgqoXYzknej3iPPYcFKwj96uinSOBmI35gAKKNYokAiEQPGRwa8dgZhNyFU+zVNH+/RuE7oaDV2ub9FhRqM7kO4riblOrS6A4B5ejvLzAtR9eVAiHDLlC0VMt+H8p6Nvkd3a3MFX6b1lcyvLknwmAHE8+XvEKy6Q8wLfgjDsGFY6JhP65vp2lxzyFYTA/F4xiH9tOx3+ijwyo9Ligld7uN7eUWRXnZ0/NZ1UXPgKV2U4rTfQLicXK5MjMio3Zor8zkudDXhRtrPQHASH6Aod8hb/sc2q/eJkYZyHOOox4ks6/PMm/izi6naNxgl2p2lKFkqbj+I9AFBgvbjQry7FFMPc/YAS4B7qLG+Q3ClA7mcEMi89dvD/oe6XiGtYsPm6662ljh0nlhjKDeJNP9xExz8od2VQIjz8f9zd54yrjm0KWy1Fye1w4fU1spGClHgAYrWrl1uL0TZJynLx20x+70UsWEQGr84NQa4eYU55W+2fzaPRoBrcgi6xsF2PI+N1HP9dudjPOfOPohw1qVDu8glhrX91Je5KvV/+uVSoiGZlv9rNgra34Nsp2U2cFaLixAvDN5thrbEwqdfmPJ3RzbzIQ5XfoyudJ/2OJgsiT9pyaNETxTh8QSqRbInOb2K1FS0bObzgtZXYOZjHK7KU7FtxE 2FjWvnnP I/bKPKP7EV4FN+L6rIQ7yukRXgpUIiMwZMOdXah28APFruNbD5BYNQHSE17tKbSb1eFgSxpvKgmHmKzSUtOsVQT4wkAgC1ImXCcTsoo8HjROaMheaRwsUdvWlvj/URgRvBjpWXyN7PD5oydPzPjASfMJu7deHQ3ausOum+ynf+3ra99uh9xYduNMV8vAv2pSW8rqqHLkNxZR3pZQNYVNlvlsm5ovhGyCRlPS6fDYj0vPL9oaaX/kZtD1e35poEhR3yCWbxsBxSV6Rx2LCNxAtdu98YhSqFWskGq+wZ0cTMtzCVxAQLSwaVAxlnkUmswx8IxjO0qdTjuc8c45EpyyzkrIC7poh+9hhbFxRIvVQyBDzZUeBGiL8BErKv9QIFUfng5KA7vZgXJZjv2vts5lC5gsjI0rNoldhaze1 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: The compressed RAM is currently charged to kernel, not to any memory cgroup, which is not satisfy our usage scenario. if the memory of a task is limited by memcgroup, it will swap out the memory to zram swap device when the memory is insufficient. In that case, the memory limit will have no effect. So, it should makes sense to charge the compressed RAM to the page's memory cgroup. As we know, zram can be used in two ways, direct and indirect, this patchset can charge memory in both cases. Direct zram usage by process within a cgroup will fail to charge if there is no memory. Indirect zram usage by process within a cgroup via swap in PF_MEMALLOC context, will charge successfully. Signed-off-by: Zhongkun He --- include/linux/memcontrol.h | 10 ++++++++++ mm/memcontrol.c | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5818af8eca5a..716cc08a6527 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1819,6 +1819,8 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, rcu_read_unlock(); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, size_t size); +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size); #else static inline bool mem_cgroup_kmem_disabled(void) { @@ -1880,6 +1882,14 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, { } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, size_t size) +{ + return 0; +} + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ +} #endif /* CONFIG_MEMCG_KMEM */ #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..e1d2c084c132 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3059,6 +3059,7 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) } return objcg; } +EXPORT_SYMBOL(get_obj_cgroup_from_page); static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) { @@ -3409,6 +3410,28 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) refill_obj_stock(objcg, size, true); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return 0; + + /* + * Indirect zram usage in PF_MEMALLOC, charging must succeed. + * Direct zram usage, charging may failed. + */ + return obj_cgroup_charge(objcg, GFP_KERNEL, size); +} +EXPORT_SYMBOL(obj_cgroup_charge_zram); + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return; + + obj_cgroup_uncharge(objcg, size); +} +EXPORT_SYMBOL(obj_cgroup_uncharge_zram); + #endif /* CONFIG_MEMCG_KMEM */ /*