From patchwork Tue Oct 3 00:18:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13406630 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 9AE7AE776DB for ; Tue, 3 Oct 2023 00:18:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37A578D0050; Mon, 2 Oct 2023 20:18:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 301A28D0003; Mon, 2 Oct 2023 20:18:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 154638D0050; Mon, 2 Oct 2023 20:18:34 -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 017F98D0003 for ; Mon, 2 Oct 2023 20:18:33 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D6A491C9B37 for ; Tue, 3 Oct 2023 00:18:33 +0000 (UTC) X-FDA: 81302238906.29.E3046A9 Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) by imf06.hostedemail.com (Postfix) with ESMTP id 050C8180002 for ; Tue, 3 Oct 2023 00:18:31 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YAYbDG1W; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.160.52 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696292312; 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:in-reply-to:references:references:dkim-signature; bh=S/ArhJOrTFxsFSzyEEFASkcwnYkX8juRHkl0KQuu84A=; b=VTX8pykqCeC/XBCSVqIsw8cWT/bdV/ecmZMZNHoZfWLnyS+03sb0MEErAan+L+iXuTFcBX 3KTZxc0oM2ym18gaFFRIv+zsLBZUtkFrrv+OdfujQ11VFStY50hFCFoK2Bg1LxAPbRumlt IwOQt/kn45/T12TggvXsOMW+SMZTWSI= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YAYbDG1W; spf=pass (imf06.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.160.52 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696292312; a=rsa-sha256; cv=none; b=UVHd8qiafr6t0Rc6fhR0h9zDmwNKGNLB+ir5OtF8z4R8dxLUtjBK15DQlhYB8XnJH0Joii jEMaLa4p0Zriwt4pw1nVpvthSTYiF2TC3Tlpxxi2OADXJPLZFJPQaH8Sy5+n6Se8KoiFpn h7N8HaMo14FV00dp5kRpkwL2+kJ9A1I= Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-1dce0f9e222so225837fac.0 for ; Mon, 02 Oct 2023 17:18:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696292311; x=1696897111; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=S/ArhJOrTFxsFSzyEEFASkcwnYkX8juRHkl0KQuu84A=; b=YAYbDG1W6v8EZSf4iZ7hUSwpDQ0TRTY0exkRJaGc9L7htwc99SAbHgJZpAtkiK+w+6 zh7scpuv894iFy+YJMPcRREZD6cxc1Q9PImqJFFspuB0hk8j04qI0r5h9bZCrxxuK7W7 I6RzAwPO4K4vA+RvTQqoLDfY8r0SzLS8cicfbShslnaTFNCsbnUEInyjC68N9L+nHNgZ OHGcJC8czypqNU9mr1y7m/1wr1XMxw1Bh75nAoFAR/qqV0r4ev3SdxGQbSpjtpI4U8Af w2sXgUzsmoV8BP1G1INIzQl2H1m/5iaX8+5xcoW69wctWHUxC9OtzcnJ89xhdeIvZIN7 VuCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696292311; x=1696897111; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=S/ArhJOrTFxsFSzyEEFASkcwnYkX8juRHkl0KQuu84A=; b=wSVSBeQuwGnNK5P/riLFb4r2QY5tN9LS1Uclqf8hxIW1Tb4KgN+pnfu+O17wk7pKp+ Yi1xsFL6EBkjDaOo3gg5SzyR4ScsMjtvkDSgx457zWsVmv9DqM8esjoZf9HwFkY4W9Nx USucmFKnwX7sxDEKsMmEWpEDvyuOnOHLbmY2B7+0knsbzfN4UJY7c4ofnIp+X1LGAzKx sV6UFAO7sn5rImUkfXi5beLJE31jeAt7mo+J4BiesvjbHLyDAeWrSfw9Z/ELVMQVJnuJ VMwSn3OWBGortxv+iUztRS2VLtJ88AAX+iCUukV5NKo2OIu8EDMMIL7hhLSO68grMWW0 bGvQ== X-Gm-Message-State: AOJu0YxXCC0M9zTLZ8wWxshexM+N4sE1QBl0QKNNrZgs1Lr4r9A6AzW1 Q2x3WdK3+O3thuXDuN0yd64= X-Google-Smtp-Source: AGHT+IGeZQJRLurhTr3HaqjP/5G/e4x1v6LKbD/D7agOZxG0/HlpHolysCG6h0wgbFAUW9N/6GU6NQ== X-Received: by 2002:a05:6870:14d0:b0:1d1:3c89:a7e5 with SMTP id l16-20020a05687014d000b001d13c89a7e5mr14709954oab.51.1696292310943; Mon, 02 Oct 2023 17:18:30 -0700 (PDT) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id q24-20020a637518000000b00578e9751b88sm52212pgc.1.2023.10.02.17.18.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 17:18:30 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: riel@surriel.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, tj@kernel.org, lizefan.x@bytedance.com, shuah@kernel.org, mike.kravetz@oracle.com, yosryahmed@google.com, fvdl@google.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: [PATCH v3 1/3] memcontrol: add helpers for hugetlb memcg accounting Date: Mon, 2 Oct 2023 17:18:26 -0700 Message-Id: <20231003001828.2554080-2-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003001828.2554080-1-nphamcs@gmail.com> References: <20231003001828.2554080-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 050C8180002 X-Rspam-User: X-Stat-Signature: g673dcajacd8x6uu3w4y5wmhhyzh6zrq X-Rspamd-Server: rspam01 X-HE-Tag: 1696292311-138611 X-HE-Meta: U2FsdGVkX1+KdY4HzJjSfnevHpcd06NA0n9T5DOgJ3MQMnOb8g9ZbkszDQlqtWy1dk8eVj1cTe38njVBFDAqaGbVzvQev8jgX0GWxjIwcNTnOZwIY8yc1DPmc6BrtbDWLLBPN3DzU+QylbpVpOTOR2NQ+e291OdoPY/TnTgOwxNbuOkr058/IeZmDTn6Zo8wbAjeTl947JseP5dEmQrYZF+4e5yN9UWcC1+MnAYCB+VLugOuJ33DEVB6FvIYSam9+4r+F6XzfsbnCmOFcec3WBy//xUMSfkzNCpSMN21FYxHkbbbJzo6a0dWr2y7189znKZM4tupFhr6CXNk6PZtObNNUq8E1CFAekWkBiYO5/3iT22vSqxzyGtQ+4Y37TUwEnX+yt4RpaarhzKQCuFveJciYBfELas8wkUu+wWnmJ5IEKTe3Luwqpa8R6h53nQ664QDoOtZGRiKqpuoI1ozeW5liXjTzzOP7kv1WxSGQQbBVpAGaaPgQB4TbxfRLO/ys0ypwP8CFEPbe/pmHF/OSy6aLEzAp2n21+jdRNoQV9EG7LE4EPXRMyQW857LB8V0odZkCLfJ8pJywxXGgCcy3y1Sb5RAAV5ac18dnN/mF7UxCRB46m6WdrQhXasq/CdNJAKtfB2f98workSSZ6SFRYuwH4MreA09glfz0/mVl0BOMwwKYRC7UhfbGShzAgPpmpO76iJ1XJLTgZrdAbRJh+btIuR807ihc36wYAqGZ52HJObMMtIjJ8LbOUSyaKoPnWewyFyz3AByBJbJGhOQoylLdK48DF0bAT41m6Kjrps1vhu5Vl6BqybECwiYScl9D0lCLnfB4qVVGAxWaaApBfoNnbzPI899VgXs+uF4StAs2418cjDBi8hj3Skp8ZuRlUITJmJ7h0S8zSO8g6iCj0AycTsH3c9C9SlsgwmlByp1hreYYNzRtzUXL8v91HJWfhXrUzYM8tntrwfUzM6 1ieH9I/r q0vaXORjYmUmzhGskIzdKfkmRiFYtstNDBee0tnjWEV2ivZ1ap94H6K3HmdAErNv29idLzzjbxRz+ip4Td/zKUDOxcofG6CREUsIc36t8/Dn2eEv+Znjxf7a+bTuMLZ3nw9SBCHAJn4pQZW6guk/fdvgQJW9HXonc2OsHk5LP3dWJVu80in+mKGEqy4yGWn9steZuvYc/FsFODrVzV3q/FhYWnkh/DGZFhK8eMq/gJOOyFGushP5d93rlHiH2nkPOT4IpNdrpptxuiA6yORBH/0NtNvxGF3G6J/a0EOI7+7/HS25C/4gTyLYY8S+SN794Ux2FJNNJDzXKuIqIb6PHD993G5Tg007+EQ1RhwvY0GYNTh81Yom15gr6CYxz9HFcf2e6M7zi1xavabq1VD8RXr1Tnua8amjdH28IDBfKpxoqoZ8= 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: This patch exposes charge committing and cancelling as parts of the memory controller interface. These functionalities are useful when the try_charge() and commit_charge() stages have to be separated by other actions in between (which can fail). One such example is the new hugetlb accounting behavior in the following patch. The patch also adds a helper function to obtain a reference to the current task's memcg. Signed-off-by: Nhat Pham Acked-by: Michal Hocko Acked-by: Johannes Weiner --- include/linux/memcontrol.h | 21 ++++++++++++++ mm/memcontrol.c | 59 ++++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index e0cfab58ab71..42bf7e9b1a2f 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -653,6 +653,8 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target, page_counter_read(&memcg->memory); } +void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg); + int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp); /** @@ -704,6 +706,8 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list) __mem_cgroup_uncharge_list(page_list); } +void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages); + void mem_cgroup_migrate(struct folio *old, struct folio *new); /** @@ -760,6 +764,8 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p); struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm); +struct mem_cgroup *get_mem_cgroup_from_current(void); + struct lruvec *folio_lruvec_lock(struct folio *folio); struct lruvec *folio_lruvec_lock_irq(struct folio *folio); struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio, @@ -1245,6 +1251,11 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target, return false; } +static inline void mem_cgroup_commit_charge(struct folio *folio, + struct mem_cgroup *memcg) +{ +} + static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp) { @@ -1269,6 +1280,11 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list) { } +static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, + unsigned int nr_pages) +{ +} + static inline void mem_cgroup_migrate(struct folio *old, struct folio *new) { } @@ -1306,6 +1322,11 @@ static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) return NULL; } +static inline struct mem_cgroup *get_mem_cgroup_from_current(void) +{ + return NULL; +} + static inline struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css) { diff --git a/mm/memcontrol.c b/mm/memcontrol.c index d1a322a75172..0219befeae38 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1086,6 +1086,27 @@ struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm) } EXPORT_SYMBOL(get_mem_cgroup_from_mm); +/** + * get_mem_cgroup_from_current - Obtain a reference on current task's memcg. + */ +struct mem_cgroup *get_mem_cgroup_from_current(void) +{ + struct mem_cgroup *memcg; + + if (mem_cgroup_disabled()) + return NULL; + +again: + rcu_read_lock(); + memcg = mem_cgroup_from_task(current); + if (!css_tryget(&memcg->css)) { + rcu_read_unlock(); + goto again; + } + rcu_read_unlock(); + return memcg; +} + static __always_inline bool memcg_kmem_bypass(void) { /* Allow remote memcg charging from any context. */ @@ -2873,7 +2894,12 @@ static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, return try_charge_memcg(memcg, gfp_mask, nr_pages); } -static inline void cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) +/** + * mem_cgroup_cancel_charge() - cancel an uncommitted try_charge() call. + * @memcg: memcg previously charged. + * @nr_pages: number of pages previously charged. + */ +void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, unsigned int nr_pages) { if (mem_cgroup_is_root(memcg)) return; @@ -2898,6 +2924,22 @@ static void commit_charge(struct folio *folio, struct mem_cgroup *memcg) folio->memcg_data = (unsigned long)memcg; } +/** + * mem_cgroup_commit_charge - commit a previously successful try_charge(). + * @folio: folio to commit the charge to. + * @memcg: memcg previously charged. + */ +void mem_cgroup_commit_charge(struct folio *folio, struct mem_cgroup *memcg) +{ + css_get(&memcg->css); + commit_charge(folio, memcg); + + local_irq_disable(); + mem_cgroup_charge_statistics(memcg, folio_nr_pages(folio)); + memcg_check_events(memcg, folio_nid(folio)); + local_irq_enable(); +} + #ifdef CONFIG_MEMCG_KMEM /* * The allocated objcg pointers array is not accounted directly. @@ -6105,7 +6147,7 @@ static void __mem_cgroup_clear_mc(void) /* we must uncharge all the leftover precharges from mc.to */ if (mc.precharge) { - cancel_charge(mc.to, mc.precharge); + mem_cgroup_cancel_charge(mc.to, mc.precharge); mc.precharge = 0; } /* @@ -6113,7 +6155,7 @@ static void __mem_cgroup_clear_mc(void) * we must uncharge here. */ if (mc.moved_charge) { - cancel_charge(mc.from, mc.moved_charge); + mem_cgroup_cancel_charge(mc.from, mc.moved_charge); mc.moved_charge = 0; } /* we must fixup refcnts and charges */ @@ -7020,20 +7062,13 @@ void mem_cgroup_calculate_protection(struct mem_cgroup *root, static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg, gfp_t gfp) { - long nr_pages = folio_nr_pages(folio); int ret; - ret = try_charge(memcg, gfp, nr_pages); + ret = try_charge(memcg, gfp, folio_nr_pages(folio)); if (ret) goto out; - css_get(&memcg->css); - commit_charge(folio, memcg); - - local_irq_disable(); - mem_cgroup_charge_statistics(memcg, nr_pages); - memcg_check_events(memcg, folio_nid(folio)); - local_irq_enable(); + mem_cgroup_commit_charge(folio, memcg); out: return ret; } From patchwork Tue Oct 3 00:18:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13406631 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 F2E99E776DA for ; Tue, 3 Oct 2023 00:18:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 303EC8D0051; Mon, 2 Oct 2023 20:18:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B4678D0003; Mon, 2 Oct 2023 20:18:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 091338D0051; Mon, 2 Oct 2023 20:18:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E70CD8D0003 for ; Mon, 2 Oct 2023 20:18:34 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BB52E140123 for ; Tue, 3 Oct 2023 00:18:34 +0000 (UTC) X-FDA: 81302238948.20.F145173 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) by imf15.hostedemail.com (Postfix) with ESMTP id 06EE5A000C for ; Tue, 3 Oct 2023 00:18:32 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GlZVdX7V; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696292313; 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:in-reply-to:references:references:dkim-signature; bh=ljlf1C6cemVWRMa2u3+4If4eJjY7Mwf2KpBD+q2vND4=; b=GyNRUhW59eJKAov4jyqaPEgRmMPo90RzbpAxWrsKnAeLO6RQ8g4kTADEcwbp6qEGUy7/vl aX+dRt9Ugluqwht4JbPC342HVNl/BbKd4l2eWhCqrXq+0tgqlmLq8BwZA3QsRP1cY8jweI g4KOHhB9zG68a30lHXMKHShEhz/7w/w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696292313; a=rsa-sha256; cv=none; b=g9f58wAzczJ9n2rPM3IiXde4ZtlieoTfwjxvEIRAqgo1M+HtTpfr/ZMq54qSH5BN70Im1/ /75gyj28w2GzJ+0suoXQBVS/Fa0vn7jX4umrk8L+P4dM83DI+UldsiuN9l7+cQb+DsQEU5 9k9iQyYsKJbhHOgkvBf4lCEkr3/9NSM= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=GlZVdX7V; spf=pass (imf15.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.128.180 as permitted sender) smtp.mailfrom=nphamcs@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-59f6441215dso4573427b3.2 for ; Mon, 02 Oct 2023 17:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696292312; x=1696897112; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ljlf1C6cemVWRMa2u3+4If4eJjY7Mwf2KpBD+q2vND4=; b=GlZVdX7Vr4SVTmHI4Vt8DTUeQl55I7VB6v2p1poOSM1zTtgZW0k9vrx60GNanNAEJL wmHv0IdcxHUzgodeMkHKfvjYdYIgpLpwbPMSlYvWkH4a7p71SxaEaTO5kqA0NvugWu9K K9yQC/McV8KWZmAhdJ49udesCiPzCzd0u0wcWcWdUxhv876GKvbnoRCfosNk3PfkryIU a0wDolLUDmNTU9tMMFRgxnu3j3IrmWSZskzuhqLdHSwcyxqSLstTEY8FFYFdIU2+Iiiv 9mhRRjUJAGvQ4lg5bnjYa2Q5/HFevXf3YzOuVYydAQld97+p8BYRl6XU1ihfMukc8FA+ IMCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696292312; x=1696897112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ljlf1C6cemVWRMa2u3+4If4eJjY7Mwf2KpBD+q2vND4=; b=RQv0Jz7wTyJp35ckzrAqDZ1UkMKlgCkQjDh370E31+jFoQV4iPEz3tUHXBdeaXoLAj mYXmGhlIrCXBUq7ZYBrpHL9q1TuXZEa8E8+88pSRWcSL6FAg4B8TJG1W8MbIXpn7X72p H4fihiytaYWJ4kwzY1MzHDYKN7qkMvP7SWm3kX55mIqRosEuKzSEQTmq1ER7Id75w/8C Ei4Q01cnJ7J/3IaoccmZsXgBJt48izcpxcz3yMz8o80PbjNQW69rPIozD5a57C/P9pmH 2Ynsqe76kIZXXYoN8qr3WmaEIMbkCGjBcw+H6KODukRKCplRAUOmzrthPgFrFYvZs6X5 WQ0A== X-Gm-Message-State: AOJu0Yyk9T1kdBt8ljozD7ceNewNKLfs0/oAkjZobbnqm6yWX3RG7pPT DSD737Y4NAZ53bWurCNsvDw= X-Google-Smtp-Source: AGHT+IHKZ2t5Y+qhgEWn4ftJqbQ0/DCHUPMUTJhv+VodPGYm/UEFAikE60ER66YrSWH+pyTsNpZdDw== X-Received: by 2002:a0d:f146:0:b0:589:fad6:c17c with SMTP id a67-20020a0df146000000b00589fad6c17cmr13490093ywf.45.1696292311958; Mon, 02 Oct 2023 17:18:31 -0700 (PDT) Received: from localhost (fwdproxy-prn-015.fbsv.net. [2a03:2880:ff:f::face:b00c]) by smtp.gmail.com with ESMTPSA id a15-20020a17090a70cf00b00276bde3b8cesm6793326pjm.15.2023.10.02.17.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 17:18:31 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: riel@surriel.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, tj@kernel.org, lizefan.x@bytedance.com, shuah@kernel.org, mike.kravetz@oracle.com, yosryahmed@google.com, fvdl@google.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: [PATCH v3 2/3] hugetlb: memcg: account hugetlb-backed memory in memory controller Date: Mon, 2 Oct 2023 17:18:27 -0700 Message-Id: <20231003001828.2554080-3-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003001828.2554080-1-nphamcs@gmail.com> References: <20231003001828.2554080-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 06EE5A000C X-Rspam-User: X-Stat-Signature: xddm3birm7agoqjreynkjhz8eofu5kfe X-Rspamd-Server: rspam03 X-HE-Tag: 1696292312-643790 X-HE-Meta: U2FsdGVkX18IrYUoQ1fwdS/ENlEk/66k7GDNspO6dS7W8xqzN3UUPQEaoFZ5IVsp9U9xdPCIvGFZowBVh8hr/dLaD0zy+LAeyoAUcqHZFrryNplBaeIeHkoc4F6phiIDyldFfBAHU1P5gsuF7qe7ImE/YfRFTrVgjEhYYiVh0b3AsvwBANGPA42fQ7LDCZe6FzMeyan0vXKa1cKQ89wLPVRMA8KZIH4BAgbLwrYKKta/ccGn66/GWOnrUAjT2zswedVP99I99o/y+NQMuSfYMWqqhxbUzi9DzJM9GoJecFHLAPqHlyGpGSpuwqlnR/YhivcaxJC6SU+eEes9f3OrKmzSOA60UBB7fDyi/X6qws51lurpsMehjmQhSmUSy395glsPp+wyNk3hYXUoBVDZFa6T/EFQiygO2tLza9IrKU6/4br2mCaWE9nWRObe7wAH5pv2lVWtv05ddiZosXGubyjMIpbUj/A0x27+TjweSbSVn6VhwLLfs76bMLb+vpaGiT5hWu9tdn/zxc153DrfeXhlRhl3fo7gu3XiJQTvkT2WNNDLokUlKP0rpcS4sIZytpWDAYEV9oaNb0dJKzMwz3Ekht8RVqry8TErmn0eSpKyvxQIpEohn5EPci3S1Q/arZrWAfP2dWc+yAYoZeLikYMIpViWoNhNjZD5clbVr7kuPvjnsFTFSKBF4uvFKTnSp43hCQzsfmF8FMn9TsXoPi+WioQECn3WnejlnvaiS73coqt9mmNrhvy33KpPcPxj3yO41i561jwyqHSczQBaE3LywVnDAS0s3ZOnlHECpocknk2Ovtfe/yp1ZQsClEoL/59RRi0X6lih4/eoV3NWQLqRrhU7H9kAgj/MrbTs+0ZHcOmjXb3+7v4A18O9qj2Rv4p8RMbshQnIBtb3t8OxUSTgDKOi+TePk6IzMTcc8y5Y/b+Whn4QxTAm+kA1I5IMq781qY7ukqG2pXHPsBa XJNxOhsz jDmBeCLhdfG436k+vSciagmfB2j7zIoWB9C71A9+wYm4ZFCG5kZv6C9nusvtd7YIxQEGPrIUKXMnClSHjPYYL5rZ90+ARZOlgFPj3UdPqoUOP7FmwX1qT2atbzLM0Usc+hPGuz46LX64LUAQ2esTRFvokJyZJLXQg9uBWFI+8OXfvvUwesxM6L6WPCiSTDiGHTmuKK9pdkWGxMP2lWfUDvg83YJAUzvpUOsuu1Da6wWwA0gcaRlTphBKxm5nbdYAb9+NU6/0tdjoUG3w3Ts96L7QkNfzyITuJ0UXRziPh+GfM7REV3uOiCNrIwPK3p96c/OIEyCV2p4wZdrEt2nKwIzsb5FoolarliR8ebZIP2SVVmclylDy3Y/shdNBle72YBviqdxdHgudXCNFOn5qg4RvyChs4HfwWEv2nzSeEW8C0D6b5dP1M0qSJ7g== 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: Currently, hugetlb memory usage is not acounted for in the memory controller, which could lead to memory overprotection for cgroups with hugetlb-backed memory. This has been observed in our production system. For instance, here is one of our usecases: suppose there are two 32G containers. The machine is booted with hugetlb_cma=6G, and each container may or may not use up to 3 gigantic page, depending on the workload within it. The rest is anon, cache, slab, etc. We can set the hugetlb cgroup limit of each cgroup to 3G to enforce hugetlb fairness. But it is very difficult to configure memory.max to keep overall consumption, including anon, cache, slab etc. fair. What we have had to resort to is to constantly poll hugetlb usage and readjust memory.max. Similar procedure is done to other memory limits (memory.low for e.g). However, this is rather cumbersome and buggy. Furthermore, when there is a delay in memory limits correction, (for e.g when hugetlb usage changes within consecutive runs of the userspace agent), the system could be in an over/underprotected state. This patch rectifies this issue by charging the memcg when the hugetlb folio is utilized, and uncharging when the folio is freed (analogous to the hugetlb controller). Note that we do not charge when the folio is allocated to the hugetlb pool, because at this point it is not owned by any memcg. Some caveats to consider: * This feature is only available on cgroup v2. * There is no hugetlb pool management involved in the memory controller. As stated above, hugetlb folios are only charged towards the memory controller when it is used. Host overcommit management has to consider it when configuring hard limits. * Failure to charge towards the memcg results in SIGBUS. This could happen even if the hugetlb pool still has pages (but the cgroup limit is hit and reclaim attempt fails). * When this feature is enabled, hugetlb pages contribute to memory reclaim protection. low, min limits tuning must take into account hugetlb memory. * Hugetlb pages utilized while this option is not selected will not be tracked by the memory controller (even if cgroup v2 is remounted later on). Signed-off-by: Nhat Pham Acked-by: Johannes Weiner --- Documentation/admin-guide/cgroup-v2.rst | 29 ++++++++++++++++++++ include/linux/cgroup-defs.h | 5 ++++ include/linux/memcontrol.h | 9 +++++++ kernel/cgroup/cgroup.c | 15 ++++++++++- mm/hugetlb.c | 35 ++++++++++++++++++++----- mm/memcontrol.c | 35 +++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 622a7f28db1f..606b2e0eac4b 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -210,6 +210,35 @@ cgroup v2 currently supports the following mount options. relying on the original semantics (e.g. specifying bogusly high 'bypass' protection values at higher tree levels). + memory_hugetlb_accounting + Count HugeTLB memory usage towards the cgroup's overall + memory usage for the memory controller (for the purpose of + statistics reporting and memory protetion). This is a new + behavior that could regress existing setups, so it must be + explicitly opted in with this mount option. + + A few caveats to keep in mind: + + * There is no HugeTLB pool management involved in the memory + controller. The pre-allocated pool does not belong to anyone. + Specifically, when a new HugeTLB folio is allocated to + the pool, it is not accounted for from the perspective of the + memory controller. It is only charged to a cgroup when it is + actually used (for e.g at page fault time). Host memory + overcommit management has to consider this when configuring + hard limits. In general, HugeTLB pool management should be + done via other mechanisms (such as the HugeTLB controller). + * Failure to charge a HugeTLB folio to the memory controller + results in SIGBUS. This could happen even if the HugeTLB pool + still has pages available (but the cgroup limit is hit and + reclaim attempt fails). + * Charging HugeTLB memory towards the memory controller affects + memory protection and reclaim dynamics. Any userspace tuning + (of low, min limits for e.g) needs to take this into account. + * HugeTLB pages utilized while this option is not selected + will not be tracked by the memory controller (even if cgroup + v2 is remounted later on). + Organizing Processes and Threads -------------------------------- diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index f1b3151ac30b..8641f4320c98 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -115,6 +115,11 @@ enum { * Enable recursive subtree protection */ CGRP_ROOT_MEMORY_RECURSIVE_PROT = (1 << 18), + + /* + * Enable hugetlb accounting for the memory controller. + */ + CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING = (1 << 19), }; /* cftype->flags */ diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 42bf7e9b1a2f..a827e2129790 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -679,6 +679,9 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, return __mem_cgroup_charge(folio, mm, gfp); } +int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, + long nr_pages); + int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, gfp_t gfp, swp_entry_t entry); void mem_cgroup_swapin_uncharge_swap(swp_entry_t entry); @@ -1262,6 +1265,12 @@ static inline int mem_cgroup_charge(struct folio *folio, return 0; } +static inline int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, + gfp_t gfp, long nr_pages) +{ + return 0; +} + static inline int mem_cgroup_swapin_charge_folio(struct folio *folio, struct mm_struct *mm, gfp_t gfp, swp_entry_t entry) { diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 1fb7f562289d..f11488b18ceb 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1902,6 +1902,7 @@ enum cgroup2_param { Opt_favordynmods, Opt_memory_localevents, Opt_memory_recursiveprot, + Opt_memory_hugetlb_accounting, nr__cgroup2_params }; @@ -1910,6 +1911,7 @@ static const struct fs_parameter_spec cgroup2_fs_parameters[] = { fsparam_flag("favordynmods", Opt_favordynmods), fsparam_flag("memory_localevents", Opt_memory_localevents), fsparam_flag("memory_recursiveprot", Opt_memory_recursiveprot), + fsparam_flag("memory_hugetlb_accounting", Opt_memory_hugetlb_accounting), {} }; @@ -1936,6 +1938,9 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param case Opt_memory_recursiveprot: ctx->flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; return 0; + case Opt_memory_hugetlb_accounting: + ctx->flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; + return 0; } return -EINVAL; } @@ -1960,6 +1965,11 @@ static void apply_cgroup_root_flags(unsigned int root_flags) cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_RECURSIVE_PROT; else cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; + + if (root_flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) + cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; + else + cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING; } } @@ -1973,6 +1983,8 @@ static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root seq_puts(seq, ",memory_localevents"); if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT) seq_puts(seq, ",memory_recursiveprot"); + if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING) + seq_puts(seq, ",memory_hugetlb_accounting"); return 0; } @@ -7050,7 +7062,8 @@ static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr, "nsdelegate\n" "favordynmods\n" "memory_localevents\n" - "memory_recursiveprot\n"); + "memory_recursiveprot\n" + "memory_hugetlb_accounting\n"); } static struct kobj_attribute cgroup_features_attr = __ATTR_RO(features); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index de220e3ff8be..74472e911b0a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1902,6 +1902,7 @@ void free_huge_folio(struct folio *folio) pages_per_huge_page(h), folio); hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), pages_per_huge_page(h), folio); + mem_cgroup_uncharge(folio); if (restore_reserve) h->resv_huge_pages++; @@ -3009,11 +3010,20 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, struct hugepage_subpool *spool = subpool_vma(vma); struct hstate *h = hstate_vma(vma); struct folio *folio; - long map_chg, map_commit; + long map_chg, map_commit, nr_pages = pages_per_huge_page(h); long gbl_chg; - int ret, idx; + int memcg_charge_ret, ret, idx; struct hugetlb_cgroup *h_cg = NULL; + struct mem_cgroup *memcg; bool deferred_reserve; + gfp_t gfp = htlb_alloc_mask(h) | __GFP_RETRY_MAYFAIL; + + memcg = get_mem_cgroup_from_current(); + memcg_charge_ret = mem_cgroup_hugetlb_try_charge(memcg, gfp, nr_pages); + if (memcg_charge_ret == -ENOMEM) { + mem_cgroup_put(memcg); + return ERR_PTR(-ENOMEM); + } idx = hstate_index(h); /* @@ -3022,8 +3032,12 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, * code of zero indicates a reservation exists (no change). */ map_chg = gbl_chg = vma_needs_reservation(h, vma, addr); - if (map_chg < 0) + if (map_chg < 0) { + if (!memcg_charge_ret) + mem_cgroup_cancel_charge(memcg, nr_pages); + mem_cgroup_put(memcg); return ERR_PTR(-ENOMEM); + } /* * Processes that did not create the mapping will have no @@ -3034,10 +3048,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, */ if (map_chg || avoid_reserve) { gbl_chg = hugepage_subpool_get_pages(spool, 1); - if (gbl_chg < 0) { - vma_end_reservation(h, vma, addr); - return ERR_PTR(-ENOSPC); - } + if (gbl_chg < 0) + goto out_end_reservation; /* * Even though there was no reservation in the region/reserve @@ -3119,6 +3131,11 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), pages_per_huge_page(h), folio); } + + if (!memcg_charge_ret) + mem_cgroup_commit_charge(folio, memcg); + mem_cgroup_put(memcg); + return folio; out_uncharge_cgroup: @@ -3130,7 +3147,11 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, out_subpool_put: if (map_chg || avoid_reserve) hugepage_subpool_put_pages(spool, 1); +out_end_reservation: vma_end_reservation(h, vma, addr); + if (!memcg_charge_ret) + mem_cgroup_cancel_charge(memcg, nr_pages); + mem_cgroup_put(memcg); return ERR_PTR(-ENOSPC); } diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0219befeae38..6660684f6f97 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -7085,6 +7085,41 @@ int __mem_cgroup_charge(struct folio *folio, struct mm_struct *mm, gfp_t gfp) return ret; } +/** + * mem_cgroup_hugetlb_try_charge - try to charge the memcg for a hugetlb folio + * @memcg: memcg to charge. + * @gfp: reclaim mode. + * @nr_pages: number of pages to charge. + * + * This function is called when allocating a huge page folio to determine if + * the memcg has the capacity for it. It does not commit the charge yet, + * as the hugetlb folio itself has not been obtained from the hugetlb pool. + * + * Once we have obtained the hugetlb folio, we can call + * mem_cgroup_commit_charge() to commit the charge. If we fail to obtain the + * folio, we should instead call mem_cgroup_cancel_charge() to undo the effect + * of try_charge(). + * + * Returns 0 on success. Otherwise, an error code is returned. + */ +int mem_cgroup_hugetlb_try_charge(struct mem_cgroup *memcg, gfp_t gfp, + long nr_pages) +{ + /* + * If hugetlb memcg charging is not enabled, do not fail hugetlb allocation, + * but do not attempt to commit charge later (or cancel on error) either. + */ + if (mem_cgroup_disabled() || !memcg || + !cgroup_subsys_on_dfl(memory_cgrp_subsys) || + !(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING)) + return -EOPNOTSUPP; + + if (try_charge(memcg, gfp, nr_pages)) + return -ENOMEM; + + return 0; +} + /** * mem_cgroup_swapin_charge_folio - Charge a newly allocated folio for swapin. * @folio: folio to charge. From patchwork Tue Oct 3 00:18:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nhat Pham X-Patchwork-Id: 13406632 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 3ECC9E776DE for ; Tue, 3 Oct 2023 00:18:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A617F8D0052; Mon, 2 Oct 2023 20:18:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E03B8D0003; Mon, 2 Oct 2023 20:18:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 796798D0052; Mon, 2 Oct 2023 20:18:36 -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 5C6F38D0003 for ; Mon, 2 Oct 2023 20:18:36 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 38C05B372D for ; Tue, 3 Oct 2023 00:18:36 +0000 (UTC) X-FDA: 81302239032.09.23F0298 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf11.hostedemail.com (Postfix) with ESMTP id 63FE94000E for ; Tue, 3 Oct 2023 00:18:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TqZsISiW; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696292314; 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:in-reply-to:references:references:dkim-signature; bh=vY+Soyh4ezPzEsiTwAjxm3PLfRjJVUJ94CEs1TUSu9g=; b=pP3/3aw8RjMrwRwnrG7WrlUGzEgQ9tLNRuubFyplQwXKQBouaiRwikCWmbjSi1/m5PPyjo M/jqEdZhokWN0b0I1T0h7MuZ4TivPdRb6S77PTd8vNo3ptf55tK6GbQm6qGyVEr9VkLdzl TXnN5KPFMMF7jBRyOgDACMZNL8fjYC0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=TqZsISiW; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of nphamcs@gmail.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=nphamcs@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696292314; a=rsa-sha256; cv=none; b=gKeXeRDQp4mySF/6GAxjU8KTuho9SfvHd2I+qKWC64O/IiQ//2t3C/NqrQyyHphBRsIqiY C2ldvFNzS1nkp7hGkQpmgkIZm5pZFQosqBNsez9IElC/QY0V3P8NhJfuTjPaUOOhJEYQ0o DzF1c38NGqauzYgqPJBswnryJEZeJkM= Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-690f7bf73ddso254043b3a.2 for ; Mon, 02 Oct 2023 17:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696292313; x=1696897113; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vY+Soyh4ezPzEsiTwAjxm3PLfRjJVUJ94CEs1TUSu9g=; b=TqZsISiW0tEGy9MufOM9n7btxjtkXUjbJ8KzWPjf6/C0ygFY+CkfdY3CsG3Te9yDKt YRIiYh7YS0rjGwr6LvNEJEiWIyS7wPjwU4atzrRQysZfJH9sxo4Q2xRevZi6VzkOLbEZ i3SNu5J7RuDAO2alRDJdiixSuW/C/C2f0jfrG67c0eo/QHW30Ei5PRgMDowkNOgG0S+W NjnqSNuJ+hNp03IAHK7Q9gljuo7TxEEcfKKxWFjQzCyB+VhPJpfr6bz63v+Utwb/Wgmk t2eW+DSfQMJuLOcZW6Qre8jnh6R85VRxzOW6hN6Db6tQkYf9t2E9X7grHJ1lypP7iZHn tD9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696292313; x=1696897113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vY+Soyh4ezPzEsiTwAjxm3PLfRjJVUJ94CEs1TUSu9g=; b=no1O6A/tNezVYtMgRp5b1iBwqOi3CShqgFg8/VPLdqDn9XmjcEtujjh+6khkAoNqC3 ZN7C3TKx6KY4PHzNsGcyHn77Y2hcUaZ5x9/UsaERUIqhVbhCC5p9/sUQOCKdU49/FFpA Lbty3EEItHldCQEby2BtxnAzxddty3/V967X+yVk6WtoDYjqQ01eWMlKaQfY85p2s9ho 5vqe7gyIV6K80+PBqnYkeNVAxTqQlmrxuDFD1ywQcDe0b7tmoccATb22lbhMDZ+nuOao 7pSZTZ+rzGuYJ119+8xkfHDx9tBeoGNplKQz2QNDt0h7PAyqNvO5O4RQc7wq/JphCsKi dosA== X-Gm-Message-State: AOJu0Yw7yYbgps+s2DjxCfr0T0RWnyujy/CrRnJuNKsfwEh+G7rAbnDj WwBqhHmDKCtyqDPL7SN7zBM= X-Google-Smtp-Source: AGHT+IEmhPXWxliXuOtMzjnYVzhxuPmmze2DXVfJcRKH51ghReKXZNZYRTeU37cNrXhyi0RIQd0ChA== X-Received: by 2002:a05:6a20:258d:b0:161:27c5:9c41 with SMTP id k13-20020a056a20258d00b0016127c59c41mr12322478pzd.28.1696292313120; Mon, 02 Oct 2023 17:18:33 -0700 (PDT) Received: from localhost (fwdproxy-prn-010.fbsv.net. [2a03:2880:ff:a::face:b00c]) by smtp.gmail.com with ESMTPSA id c1-20020aa78801000000b0068fda1db80bsm84015pfo.75.2023.10.02.17.18.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 17:18:32 -0700 (PDT) From: Nhat Pham To: akpm@linux-foundation.org Cc: riel@surriel.com, hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev, shakeelb@google.com, muchun.song@linux.dev, tj@kernel.org, lizefan.x@bytedance.com, shuah@kernel.org, mike.kravetz@oracle.com, yosryahmed@google.com, fvdl@google.com, linux-mm@kvack.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org Subject: [PATCH v3 3/3] selftests: add a selftest to verify hugetlb usage in memcg Date: Mon, 2 Oct 2023 17:18:28 -0700 Message-Id: <20231003001828.2554080-4-nphamcs@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231003001828.2554080-1-nphamcs@gmail.com> References: <20231003001828.2554080-1-nphamcs@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: skppzcw53z6wpqzprxmn4knuj8kcroj9 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 63FE94000E X-HE-Tag: 1696292314-352973 X-HE-Meta: U2FsdGVkX189zN/IKaKGi6uov+ZJ9CytXKB9ennzDFIqHvmIpElf8A2THkiOaUfa0kZ58PkC6pi6SIVDR4FCvDFeNGoKH8ob12ah2OoGPTBpEZrUq/LY74kBQ5gjtYc67a3gKnMZEN1G+e5zi58PkYqEeNF6Pth1ChJTRX8Y7EUh9ajvimWJoiajIHbG9tGBlXVwcf/qJwbCvy9q2wee8ACl/F8d5FzSOHpkcUMRkTVpF57WXHrY0n3rmlS4Q0jdJldeq5WYJGh/+jTz3k4taZ2jLEAkmivU9iR9UxuvYPHy3sjEE0nKHIfSbjGEA6Qq99cBYht4885rDCR92NTKI0xiNsEoFuqAATynC83qqOqSlAVGU9KRMR1UazZo2VAstCHRJP/ZCa5Rg3HoqzYCmVDXIslD8zZglaHSxxj0EKxhGtMpSuPdq5QD7RGqpgOC6LqqL2j8WYhZBIBTcPteMGuDnFDdKvt+1bDCBlQaFcNs6lqgBJ1BNd8uHlthh8YdnKmQ7PMQ65Od6ORr2EOHkIi/qV0ZpfZRITpVx5NVqZFF8BLSrLOIMzyh/LPjkAYPlF5az2gm5QSKDSScLXWID1BUbJutLvaXozCkr9N+Y39Q9CImGM8WXFertQ0ZySJsBQ2DVrIgnOuSRewZED/8AIDqtOZSL3pcGn4DoEHofTfQ4j+5AbtWA8LU7RVJ0zM/idodD9o/uh6/PUSqGqqCX14luc/n7tDXmDLAOM+qH8+Mzpf82MHS8KU3uWvCo1H9vu4lmllf9MFtVlfZvM9+mJirCGzicyolRtaNXnfdmH3/70gNlc9x7RLtQ3L9UHbHDCnZ6f8YrNzaJrET9lPWwImUWpibP/diHVTBGXNo9XCjm/lDdkIG+kfpDWrFQi4PYWdyQFUB2t/lyu6H2syQOXBwm/YkLjCBJupHIRE+FMiZHQ2G97B8kv2p2E40bG0aFfVq3BFtw4tf8uhBm6Z 6ti9cBfs T/Zfk8pFnR7MloL9mt0ddU1LqfQ5l1s2Y1VRE5hoQYfYB/wSDDCotbWj8osdcxHsW7w2OqtuJ78cWxp45gNY/oAmGbgzHuLGxlvTYOaX9bY69trVdZckK3KOGS6/g7ApaBD3oMxXsaet662VeaSYFMLwRm4Yeq99utlKURbRMa5BzFY43XMscFsoOEuBnN2wIx4aF5KjEAF2/KWJibHDtJ7iNXAKM1IdVynvAlvrKWaabJj+ugfIUQN0srtSDQ9561IJT4snbDp7DPF1H/MSFerndtdkl/LckPpY0KN+f5x0gZ+Sv9n5W6RUsqYLDDqjOdvnCplp7btIe5MVPPyHV6bldoPa4XG7m7VSdijXYO/D4tXY3WASDddfFqfvc4qI+DgYlMRFUZTDSMLlr4IBhWjOPIzUTCmHag9FrGCerfdgpimvA7L93iHziM2mrjx/B9RtpWI3y2gbMZMyA3UxdaVnkjI7XssHqewORGwLxy/85+Tc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.419965, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This patch add a new kselftest to demonstrate and verify the new hugetlb memcg accounting behavior. Signed-off-by: Nhat Pham --- MAINTAINERS | 2 + tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + .../selftests/cgroup/test_hugetlb_memcg.c | 234 ++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_hugetlb_memcg.c diff --git a/MAINTAINERS b/MAINTAINERS index bf0f54c24f81..ce9f40bcc2ba 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5269,6 +5269,7 @@ S: Maintained F: mm/memcontrol.c F: mm/swap_cgroup.c F: tools/testing/selftests/cgroup/memcg_protection.m +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c @@ -9652,6 +9653,7 @@ F: include/linux/hugetlb.h F: mm/hugetlb.c F: mm/hugetlb_vmemmap.c F: mm/hugetlb_vmemmap.h +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c HVA ST MEDIA DRIVER M: Jean-Christophe Trotin diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/selftests/cgroup/.gitignore index af8c3f30b9c1..2732e0b29271 100644 --- a/tools/testing/selftests/cgroup/.gitignore +++ b/tools/testing/selftests/cgroup/.gitignore @@ -7,4 +7,5 @@ test_kill test_cpu test_cpuset test_zswap +test_hugetlb_memcg wait_inotify diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile index c27f05f6ce9b..00b441928909 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -14,6 +14,7 @@ TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_cpu TEST_GEN_PROGS += test_cpuset TEST_GEN_PROGS += test_zswap +TEST_GEN_PROGS += test_hugetlb_memcg LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h @@ -27,3 +28,4 @@ $(OUTPUT)/test_kill: cgroup_util.c $(OUTPUT)/test_cpu: cgroup_util.c $(OUTPUT)/test_cpuset: cgroup_util.c $(OUTPUT)/test_zswap: cgroup_util.c +$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_hugetlb_memcg.c b/tools/testing/selftests/cgroup/test_hugetlb_memcg.c new file mode 100644 index 000000000000..f0fefeb4cc24 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_hugetlb_memcg.c @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" +#include "cgroup_util.h" + +#define ADDR ((void *)(0x0UL)) +#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB) +/* mapping 8 MBs == 4 hugepages */ +#define LENGTH (8UL*1024*1024) +#define PROTECTION (PROT_READ | PROT_WRITE) + +/* borrowed from mm/hmm-tests.c */ +static long get_hugepage_size(void) +{ + int fd; + char buf[2048]; + int len; + char *p, *q, *path = "/proc/meminfo", *tag = "Hugepagesize:"; + long val; + + fd = open(path, O_RDONLY); + if (fd < 0) { + /* Error opening the file */ + return -1; + } + + len = read(fd, buf, sizeof(buf)); + close(fd); + if (len < 0) { + /* Error in reading the file */ + return -1; + } + if (len == sizeof(buf)) { + /* Error file is too large */ + return -1; + } + buf[len] = '\0'; + + /* Search for a tag if provided */ + if (tag) { + p = strstr(buf, tag); + if (!p) + return -1; /* looks like the line we want isn't there */ + p += strlen(tag); + } else + p = buf; + + val = strtol(p, &q, 0); + if (*q != ' ') { + /* Error parsing the file */ + return -1; + } + + return val; +} + +static int set_file(const char *path, long value) +{ + FILE *file; + int ret; + + file = fopen(path, "w"); + if (!file) + return -1; + ret = fprintf(file, "%ld\n", value); + fclose(file); + return ret; +} + +static int set_nr_hugepages(long value) +{ + return set_file("/proc/sys/vm/nr_hugepages", value); +} + +static unsigned int check_first(char *addr) +{ + return *(unsigned int *)addr; +} + +static void write_data(char *addr) +{ + unsigned long i; + + for (i = 0; i < LENGTH; i++) + *(addr + i) = (char)i; +} + +static int hugetlb_test_program(const char *cgroup, void *arg) +{ + char *test_group = (char *)arg; + void *addr; + long old_current, expected_current, current; + int ret = EXIT_FAILURE; + + old_current = cg_read_long(test_group, "memory.current"); + set_nr_hugepages(20); + current = cg_read_long(test_group, "memory.current"); + if (current - old_current >= MB(2)) { + ksft_print_msg( + "setting nr_hugepages should not increase hugepage usage.\n"); + ksft_print_msg("before: %ld, after: %ld\n", old_current, current); + return EXIT_FAILURE; + } + + addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0); + if (addr == MAP_FAILED) { + ksft_print_msg("fail to mmap.\n"); + return EXIT_FAILURE; + } + current = cg_read_long(test_group, "memory.current"); + if (current - old_current >= MB(2)) { + ksft_print_msg("mmap should not increase hugepage usage.\n"); + ksft_print_msg("before: %ld, after: %ld\n", old_current, current); + goto out_failed_munmap; + } + old_current = current; + + /* read the first page */ + check_first(addr); + expected_current = old_current + MB(2); + current = cg_read_long(test_group, "memory.current"); + if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should increase by around 2MB.\n"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + goto out_failed_munmap; + } + + /* write to the whole range */ + write_data(addr); + current = cg_read_long(test_group, "memory.current"); + expected_current = old_current + MB(8); + if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should increase by around 8MB.\n"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + goto out_failed_munmap; + } + + /* unmap the whole range */ + munmap(addr, LENGTH); + current = cg_read_long(test_group, "memory.current"); + expected_current = old_current; + if (!values_close(expected_current, current, 5)) { + ksft_print_msg("memory usage should go back down.\n"); + ksft_print_msg( + "expected memory: %ld, actual memory: %ld\n", + expected_current, current); + return ret; + } + + ret = EXIT_SUCCESS; + return ret; + +out_failed_munmap: + munmap(addr, LENGTH); + return ret; +} + +static int test_hugetlb_memcg(char *root) +{ + int ret = KSFT_FAIL; + char *test_group; + + test_group = cg_name(root, "hugetlb_memcg_test"); + if (!test_group || cg_create(test_group)) { + ksft_print_msg("fail to create cgroup.\n"); + goto out; + } + + if (cg_write(test_group, "memory.max", "100M")) { + ksft_print_msg("fail to set cgroup memory limit.\n"); + goto out; + } + + /* disable swap */ + if (cg_write(test_group, "memory.swap.max", "0")) { + ksft_print_msg("fail to disable swap.\n"); + goto out; + } + + if (!cg_run(test_group, hugetlb_test_program, (void *)test_group)) + ret = KSFT_PASS; +out: + cg_destroy(test_group); + free(test_group); + return ret; +} + +int main(int argc, char **argv) +{ + char root[PATH_MAX]; + int ret = EXIT_SUCCESS, has_memory_hugetlb_acc; + + has_memory_hugetlb_acc = proc_mount_contains("memory_hugetlb_accounting"); + if (has_memory_hugetlb_acc < 0) + ksft_exit_skip("Failed to query cgroup mount option\n"); + else if (!has_memory_hugetlb_acc) + ksft_exit_skip("memory hugetlb accounting is disabled\n"); + + /* Unit is kB! */ + if (get_hugepage_size() != 2048) { + ksft_print_msg("test_hugetlb_memcg requires 2MB hugepages\n"); + ksft_test_result_skip("test_hugetlb_memcg\n"); + return ret; + } + + if (cg_find_unified_root(root, sizeof(root))) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + switch (test_hugetlb_memcg(root)) { + case KSFT_PASS: + ksft_test_result_pass("test_hugetlb_memcg\n"); + break; + case KSFT_SKIP: + ksft_test_result_skip("test_hugetlb_memcg\n"); + break; + default: + ret = EXIT_FAILURE; + ksft_test_result_fail("test_hugetlb_memcg\n"); + break; + } + + return ret; +}