From patchwork Fri Aug 4 07:57:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13341437 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 6A639C001DF for ; Fri, 4 Aug 2023 07:57:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFE8F8D0010; Fri, 4 Aug 2023 03:57:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DAEE08D0002; Fri, 4 Aug 2023 03:57:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C760A8D0010; Fri, 4 Aug 2023 03:57:47 -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 B7AB08D0002 for ; Fri, 4 Aug 2023 03:57:47 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 886F7C13B1 for ; Fri, 4 Aug 2023 07:57:47 +0000 (UTC) X-FDA: 81085668174.19.46F179A Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf08.hostedemail.com (Postfix) with ESMTP id D21B316000B for ; Fri, 4 Aug 2023 07:57:45 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cyFCTzgx; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf08.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691135865; 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=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=AkglUEdmfY5cpUdcEG00VfSSza5YUI3kj2K4uun1DWoHkTlubsKQ8iR5XDNJxDVBUWpP41 lMsAy0yI2TcQAyZAqoC+jxuztjXXc0vPzmdAwnlw1BdfW5IwPOHEOsizztWkGKOZRYs6O9 51O3HJMVbonYU4XjAOAIBuDNk4XmmNI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cyFCTzgx; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf08.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.210.172 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691135865; a=rsa-sha256; cv=none; b=7NnGFUkoeubBlMjt6mjgrE1pS8y4WJae9jjNreFbDq/wCp+S3tKQwhFnYjGHFpQ47+mn4V qtFTRkY7CPTXD4CMPSb1I8SEFeXOgL6c0jxQYlGKp9uIWWDJr2MdUOmGCf4JEAjss+sbt4 suxy++UX24mGBb2ox15rFw5sO0QQ7Sk= Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-686f8614ce5so1694184b3a.3 for ; Fri, 04 Aug 2023 00:57:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1691135864; x=1691740664; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=cyFCTzgxWtiVhk0GN4aVnu4QKoUOxtDJ285NYT/WdtIMtJDdEURpJTFHP7/r6bXjV6 L8P0q1S1aQVL0XZ83Y2GvLfNChQwxSAp23v6Z0Ggk6ewzwUZVUEJq+dP04Xd5aXcBWMO rwkKID50qG8Uedkg0KM6CqS+CltvwQomEMNoDZXFxdvxXrlgXEe2WCJrStu6gaOTpVg+ 6COTIYjMyWD2l0W45DWG3YrkIqUyIFSzZEODU4n52f54yIoB7fP+LVFOLh2zO3ZvfJ38 kiL+DUNaBlV/kRc3tJrORJMZb7dOjqVRomaj4xKOPC5hpw9jIuTWV35DIcjhV7bt0EH3 ULSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691135864; x=1691740664; 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=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=QLYdivwjLdJN+dsWznLnsIr60ZNaqIZ9Z8zZDoO17F7Q/TMv17kyygU/t9cydPV4St VgtyCwMWPa90PvmXzj52M0DOL0qns5aLRZMfiRYme74yZbmQXMgDXvf2i5FPPl7L8FGi XL3VwscGZBIJzlZgUOco5qnpRuX0mcbQrzhUEeGvp7S7aTo62oY8X/6C7OEPfnpdZAqO +8t/QDPnJEPQI6STWOdIqm2/f6IJmFBrcgY0iabBtlYyS+qiTitjbgI/stVNBrGSiew/ 9TTAf5cGyvYgafM+lD7UopGyTZkxhC4iP0hrifZ2B8TFCFH9SkY469iTSV4unXwLOVBE 4BEg== X-Gm-Message-State: AOJu0YxVGdfEqH9MQqTmUhYD8m914ePFMRiUpSGheoZuVvJ5x8E8ySYB q/J7xwdVv1e4WwlNxOE35UifFQ== X-Google-Smtp-Source: AGHT+IEINJci+rjslbHv2EkTaLQ/k76JUU6HiPJtghJgD8x05vOGcKrfPI4DZeP83cVwWdid5ntRbg== X-Received: by 2002:a05:6a00:1a4b:b0:687:7b0a:fae4 with SMTP id h11-20020a056a001a4b00b006877b0afae4mr1578686pfv.0.1691135864729; Fri, 04 Aug 2023 00:57:44 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id x10-20020a056a00270a00b0068796c58441sm1016718pfv.60.2023.08.04.00.57.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 00:57:44 -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 RESEND v2 1/2] memcg: Add support for zram object charge Date: Fri, 4 Aug 2023 15:57:36 +0800 Message-Id: <20230804075736.207995-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: D21B316000B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: sa8fpeojaczsjg4jomzqstmjuy67piih X-HE-Tag: 1691135865-878196 X-HE-Meta: U2FsdGVkX19POsfcKPpccheu54wvzuAQZGFjrqM842bxfVola7oh/YTZY4herrpiZa9mnes5221w2gAxOTQYJw/UPfVUwZIne339XzaIQ+MmWUi13bAdyP46Hr+o+OGjvmaoohpeiyUD38vevzaoZDV7wipHiLUgA8WtkMMGqCySX6FkcbSIghSM34eGX70gjELEhrFqR1+wHoD/JzY2b9Skl8Ghnbaikz96BycmqQI5+Gji44CCoUpqnJkAXnhuHUKFdwg7Sw83q73Lz/MUTiNdV6QA9sz7nAMIXTsmWTWtLF/pKVXNPMKXBSq1y03joIRr27BYBRPuLB2ktId3XCxfH4OMt1KpQIn+GZfpvhzMP62iuzX3xhfuP9Y7/WJnFRUbsKQ/XALHKLGndq/SwZT3VFCL4AB4/AuHIMMazFvX662sge5g2ogNCKOhNJ497PHVLsQ3ncOtH4NDKE2oidQ12o81Ob8gksYtdzsxMtxKRLb4+HRYvFNu5f/j2tJ8DBS8mJDH6HdEQVCT+ZTBcRh3YPOw7xFyMYYeOLJgCJ3NVX5eeRVvruQ6AUQP6zYOXeTeXtIsVjtj1kYAuGIvHRFbqzMHyvg4g1bKzlzQSgGbN/u33vTNhqbWWlVvNHghdRN9KWZdhKh+G9m+ZyGUYxij+lsiz0xrHh7XzDd5EjYtdPq3HtFuZGKyP6xjKF+7SKhdApwOoAcYjr/sU/WOFJEkP9WQejH9gDqIBMk4ET/E45ZWQlKEDUsKWeQtGicjrZqxfHzh67Kc4W8LoXpUUOg0sTExflY850tbFMaF6B7qxYlfpFELZbRU2fq4DZ7dn+OovL5vIFOnVWXA8IAo9NQKf9142c6Ug23fiSPK1yan2l25OM/LLMh/o3KDWr260++7N4JKV/dhJ+lOY2XjofnEZs6voBhp9cVqv4AWFkygX8nhX4sTTbeuxp3nraUl7YOAplkp1NsjJ2XxRVd v/shcRdm yGVxfXUOklap5SZhPaFQm8Cf0RZj9uVI7Sevww0GTLJ0He06hR2VGm/0Wm5gyxH9rI2RJ6T06FJNUJ/F+qNAvSLlqgYl7Hq5LBH+Vl7GjL3+f1H50A4uo1tWr34pNgbvB2mutseQIsx/P+jsduScBSaWJ9oJEcbtQf3mbyMuZrqD9q3lDfea+nFoefl38eqbY6VSe2Up4V9cxqvn1BR3rr4FGMRxBbc5qRrgSnDyKWZsGMBM891hvTbXu1st7Essx7KAtnplOhojs4AcHTOMWncniu0JgGhmfcdDya87s/Wj3OmeEVo0TzN9GDQuuleoizwWEqs6uWVZaKVYPxYaQAb4cUo0jFdFCe569P7UZfSTre7trHRqxDBsOLZjbPlilokhZqY24K7ZJHWfchuvlvooLqpdIZ6ZnNNS+ 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(ZRAM) is currently charged to kernel, not to any memory cgroup, which can escape their cgroup memory containment 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 hard limit will be invalidated. 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 | 12 ++++++++++++ mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5818af8eca5a..24bac877bc83 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1819,6 +1819,9 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, rcu_read_unlock(); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + 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 +1883,15 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, { } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + 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..118544acf895 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,29 @@ 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, gfp_t gfp, + 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, 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 */ /*