From patchwork Wed Oct 30 08:33:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gutierrez Asier X-Patchwork-Id: 13856115 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 2AEECD7497F for ; Wed, 30 Oct 2024 08:33:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15A008D0001; Wed, 30 Oct 2024 04:33:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 046768D0005; Wed, 30 Oct 2024 04:33:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D173C8D0001; Wed, 30 Oct 2024 04:33:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AB2CA8D0006 for ; Wed, 30 Oct 2024 04:33:46 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 59BCC1A0626 for ; Wed, 30 Oct 2024 08:33:46 +0000 (UTC) X-FDA: 82729604958.02.15445F3 Received: from frasgout.his.huawei.com (frasgout.his.huawei.com [185.176.79.56]) by imf04.hostedemail.com (Postfix) with ESMTP id D2ECC4001B for ; Wed, 30 Oct 2024 08:33:11 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of gutierrez.asier@huawei-partners.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=gutierrez.asier@huawei-partners.com; dmarc=pass (policy=quarantine) header.from=huawei-partners.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730277144; 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:in-reply-to:references:references; bh=t0o+aNC7z3dbkzXbeMfyU8X1dkY839Wkm3UIkvnpevg=; b=T8ZvTYS8TXykL+QyCAu/DO06x2S/+L2P4rZo2oe7xlZlR9TAMoV9PLlfEv0vj+VIO1Nb4j X/tdDzn7JemlbzlU6RVnmmRzzxUstXBF2KNpfKUnclbx11d44jC4fbQ4Oxxc+buL0KPaGH TOJTCx1gKSHgP5RICrGDzCYPGT6hreM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.hostedemail.com: domain of gutierrez.asier@huawei-partners.com designates 185.176.79.56 as permitted sender) smtp.mailfrom=gutierrez.asier@huawei-partners.com; dmarc=pass (policy=quarantine) header.from=huawei-partners.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730277144; a=rsa-sha256; cv=none; b=w68YXOWcWxDmj4AyAc1a8qsnTQcWZbHXZFkC+J9hMrn2hujAtnaB+5nEzy0u9Zw1zos7Bs iVnybxjgbNNSUrkfYMuTG9YmuvE7Qt0tFwA+CjfFbhsWdUx0x9v0zpA6MzIrSE22F4jJzf emtF51xEcgXUj6U6RkD52eCc0s2aZFA= Received: from mail.maildlp.com (unknown [172.18.186.216]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4XdgJL0NLLz6GFtv; Wed, 30 Oct 2024 16:28:50 +0800 (CST) Received: from mscpeml500003.china.huawei.com (unknown [7.188.49.51]) by mail.maildlp.com (Postfix) with ESMTPS id D731A140AA7; Wed, 30 Oct 2024 16:33:40 +0800 (CST) Received: from mscphis01197.huawei.com (10.123.65.218) by mscpeml500003.china.huawei.com (7.188.49.51) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.34; Wed, 30 Oct 2024 11:33:40 +0300 From: To: , , , , , , , , , , CC: , , , , , , , , , , , Subject: [RFC PATCH 1/3] mm: Add thp_flags control for cgroup Date: Wed, 30 Oct 2024 16:33:09 +0800 Message-ID: <20241030083311.965933-2-gutierrez.asier@huawei-partners.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241030083311.965933-1-gutierrez.asier@huawei-partners.com> References: <20241030083311.965933-1-gutierrez.asier@huawei-partners.com> MIME-Version: 1.0 X-Originating-IP: [10.123.65.218] X-ClientProxiedBy: mscpeml500003.china.huawei.com (7.188.49.51) To mscpeml500003.china.huawei.com (7.188.49.51) X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: D2ECC4001B X-Stat-Signature: ycrjzbmkcw5x9ohjfps3ye5at35jqsq4 X-HE-Tag: 1730277191-420411 X-HE-Meta: U2FsdGVkX1+qw61j6FVd0RDl6i54KlGbq0JM04aEnUatkTtnG8mAndH2YcjNPWQybZEgLJdmXeFrzktt5HfOnMTiV2hX3uHEvBux/QZ+V6AyarumM2n46h+DoBt06EIJdukGTLDjH/YGJtMeo3PgDFdxmWUNJtSpp3AYbNFroPkp4AKl8FHYOR3jUIDU2wl2l+Rr/xl0VGM57g3MwX2BSvZrHi2ggrg1Q2g5LKPV2+W2O5mxiApOsJzWh12QlLs4CDc09HTD8UrtzqLD5QRkPSBx2zlqub92TF7xEFcVypQlAVQWjA2rSv1kruyj+KgQf5kn16n2w35EKFajhXFvVcgJbom6g5Y0GX5WjCTJOBOXqfHCtBDhKBIXQxEpAYw8vJbBpiaytVcQ35RboqQIm0vUcbZDYzwOi8DHBTUL/i65/YKTeKBy8Q6xt4ST0g/lhTHsR3vlImTb8cpyXr+MhCcMEYcGgGssjefu5+S5iwVed+m0nPeMftjCVf1XDkALFe3LufYdjCVQ3JhCDOjRRebZU+/L2C9ontqD9uMoO7qAFMzP6s9BNevXy4bIoWACjLQw6xWw6IlXqnaTTXtEeSjmxxixQMBT4lH30tMMy3+P8JzS3ZtT7vlbQ7FbZjwJP517V7eQkmBQFJ3IK1iPa80Xz5Ys11ljw3sQLlI/J+7N9nu6DcGcaRVXWW4kmmYwV0RTu6cT1TII2VOlyoZ2fCZdytrxD389yM/gjZZ/BTi3B/lbE0fxwBX2CVpfZmr5xQF1d4yfGaLknZH+6HsuOmX2ynhuWC9OdRcnF61SLpIc+rLTqnDPGgiVqZRI81oD+bLqpkXsf4yQtn6WPJZ2pZyOt5HGQtSxjjUuurFu4qrxsevJSK+ybvjADPGFxuFzrwDvWRgd/ehbzBctYNi0GxpegWOB542PGPFduSKHhz291N2wFtw1e1SssczN31Wy1L08VCBCtoPBbZ2lWDW TJb+NIy5 xLx2rDj+IU0ZwYJHCtiFzvfbta7OKKOVssrEY8mZTrR+prX0nc+SMR7uiym2fRyfsWWj8a0PUiyH+YL4o1J6on4yEn0AAIp+aosMryoNl9Sykt4gNrJXWVlaPvnsiS+r+OLgfsu1TsaafWyie1RGIswBAVLIIigiaTCiXNMWs5t9q8YCgJVLBOF6XLeaZApEpt4m+2TyRXqUxCoQGjVsCEkraHh4aYVlKvAbpx+G8uQV1dn8reAk/OmynQK1cJHs97x40 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: Asier Gutierrez Exposed a new file in memory cgroup called memory.thp_enabled. This file works in the same way and same format as thp settings in /sys/kernel/mm/transparent_hugepage/enabled. The patch allows to read from and write to that file, changing effectively the memory cgroup THP policy. New cgroups will inherit the THP policies from their parents. Signed-off-by: Asier Gutierrez Signed-off-by: Anatoly Stepanov Reviewed-by: Alexander Kozhevnikov --- include/linux/huge_mm.h | 5 +++ include/linux/memcontrol.h | 15 +++++++ mm/huge_memory.c | 71 ++++++++++++++++++++----------- mm/memcontrol.c | 86 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 24 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index e25d9ebfdf89..86c0fb4c0b28 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -53,6 +53,9 @@ enum transparent_hugepage_flag { TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG, }; +#define HUGEPAGE_FLAGS_ENABLED_MASK ((1UL << TRANSPARENT_HUGEPAGE_FLAG) |\ + (1UL << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)) + struct kobject; struct kobj_attribute; @@ -430,6 +433,8 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmdp, struct folio *folio); +int thp_enabled_parse(const char *buf, unsigned long *flags); +const char *thp_enabled_string(unsigned long flags); #else /* CONFIG_TRANSPARENT_HUGEPAGE */ static inline bool folio_test_pmd_mappable(struct folio *folio) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 0e5bf25d324f..87b5fe93e19d 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -315,6 +315,12 @@ struct mem_cgroup { spinlock_t event_list_lock; #endif /* CONFIG_MEMCG_V1 */ +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + unsigned long thp_flags; + unsigned long thp_anon_orders_always; + unsigned long thp_anon_orders_madvise; + unsigned long thp_anon_orders_inherit; +#endif struct mem_cgroup_per_node *nodeinfo[]; }; @@ -1615,6 +1621,15 @@ struct sock; bool mem_cgroup_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, gfp_t gfp_mask); void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages); +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +int memory_thp_enabled_show(struct seq_file *m, void *v); +ssize_t memory_thp_enabled_write(struct kernfs_open_file *of, char *buf, + size_t nbytes, loff_t off); + +int mem_cgroup_thp_flags_update_all(unsigned long flags, unsigned long mask); +unsigned long memcg_get_thp_flags_all(unsigned long mask); +unsigned long memcg_get_thp_flags(struct vm_area_struct *vma); +#endif #ifdef CONFIG_MEMCG extern struct static_key_false memcg_sockets_enabled_key; #define mem_cgroup_sockets_enabled static_branch_unlikely(&memcg_sockets_enabled_key) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 67c86a5d64a6..0fbdd8213443 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -46,6 +46,8 @@ #include "internal.h" #include "swap.h" +#include + #define CREATE_TRACE_POINTS #include @@ -287,21 +289,43 @@ static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, static struct shrinker *huge_zero_page_shrinker; -#ifdef CONFIG_SYSFS -static ssize_t enabled_show(struct kobject *kobj, - struct kobj_attribute *attr, char *buf) +const char *thp_enabled_string(unsigned long flags) { const char *output; - if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags)) + if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &flags)) output = "[always] madvise never"; - else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, - &transparent_hugepage_flags)) + else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &flags)) output = "always [madvise] never"; else output = "always madvise [never]"; - return sysfs_emit(buf, "%s\n", output); + return output; +} + +int thp_enabled_parse(const char *buf, unsigned long *flags) +{ + if (sysfs_streq(buf, "always")) { + clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, flags); + set_bit(TRANSPARENT_HUGEPAGE_FLAG, flags); + } else if (sysfs_streq(buf, "madvise")) { + clear_bit(TRANSPARENT_HUGEPAGE_FLAG, flags); + set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, flags); + } else if (sysfs_streq(buf, "never")) { + clear_bit(TRANSPARENT_HUGEPAGE_FLAG, flags); + clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, flags); + } else + return -EINVAL; + + return 0; +} + +#ifdef CONFIG_SYSFS +static ssize_t enabled_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + unsigned long flags = transparent_hugepage_flags; + return sysfs_emit(buf, "%s\n", thp_enabled_string(flags)); } static ssize_t enabled_store(struct kobject *kobj, @@ -309,24 +333,21 @@ static ssize_t enabled_store(struct kobject *kobj, const char *buf, size_t count) { ssize_t ret = count; + int err; - if (sysfs_streq(buf, "always")) { - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "madvise")) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else if (sysfs_streq(buf, "never")) { - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); - } else - ret = -EINVAL; + ret = thp_enabled_parse(buf, &transparent_hugepage_flags) ? : count; + if (ret <= 0) + goto out; - if (ret > 0) { - int err = start_stop_khugepaged(); - if (err) - ret = err; - } + if (IS_ENABLED(CONFIG_MEMCG) && !mem_cgroup_disabled()) + err = mem_cgroup_thp_flags_update_all(transparent_hugepage_flags, + HUGEPAGE_FLAGS_ENABLED_MASK); + else + err = start_stop_khugepaged(); + + if (err) + ret = err; +out: return ret; } @@ -1036,7 +1057,9 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma) { const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE); - +#ifdef CONFIG_MEMCG + unsigned long transparent_hugepage_flags = memcg_get_thp_flags(vma); +#endif /* Always do synchronous compaction */ if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags)) return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index d563fb515766..2b25c45c85c3 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -970,6 +970,33 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) return memcg; } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +static inline bool memcg_thp_always_enabled(struct mem_cgroup *memcg) +{ + return test_bit(TRANSPARENT_HUGEPAGE_FLAG, &memcg->thp_flags); +} + +static inline bool memcg_thp_madvise_enabled(struct mem_cgroup *memcg) +{ + return test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &memcg->thp_flags); +} + +unsigned long memcg_get_thp_flags(struct vm_area_struct *vma) +{ + unsigned long flags = 0UL; + struct mem_cgroup *memcg = get_mem_cgroup_from_mm(vma->vm_mm); + + if (!memcg) + goto out; + + flags = READ_ONCE(memcg->thp_flags); +out: + if (memcg) + css_put(&memcg->css); + return flags; +} +#endif + /** * mem_cgroup_iter - iterate over memory cgroup hierarchy * @root: hierarchy root @@ -3625,6 +3652,11 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) WRITE_ONCE(memcg->oom_kill_disable, READ_ONCE(parent->oom_kill_disable)); page_counter_init(&memcg->kmem, &parent->kmem); page_counter_init(&memcg->tcpmem, &parent->tcpmem); +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + WRITE_ONCE(memcg->thp_flags, READ_ONCE(parent->thp_flags)); + WRITE_ONCE(memcg->thp_anon_orders_inherit, + READ_ONCE(parent->thp_anon_orders_inherit)); #endif } else { init_memcg_stats(); @@ -3634,6 +3666,17 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) #ifdef CONFIG_MEMCG_V1 page_counter_init(&memcg->kmem, NULL); page_counter_init(&memcg->tcpmem, NULL); +#endif +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + WRITE_ONCE(memcg->thp_flags, +#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS + (1<thp_anon_orders_inherit, BIT(PMD_ORDER)); #endif root_mem_cgroup = memcg; return &memcg->css; @@ -4315,6 +4358,19 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, return nbytes; } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +DEFINE_MUTEX(memcg_thp_flags_mutex); + +int memory_thp_enabled_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + unsigned long flags = READ_ONCE(memcg->thp_flags); + + seq_printf(m, "%s\n", thp_enabled_string(flags)); + return 0; +} +#endif + static struct cftype memory_files[] = { { .name = "current", @@ -4383,6 +4439,12 @@ static struct cftype memory_files[] = { .flags = CFTYPE_NS_DELEGATABLE, .write = memory_reclaim, }, +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + { + .name = "thp_enabled", + .seq_show = memory_thp_enabled_show, + }, +#endif { } /* terminate */ }; @@ -4844,6 +4906,30 @@ void mem_cgroup_uncharge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages) refill_stock(memcg, nr_pages); } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +int mem_cgroup_thp_flags_update_all(unsigned long new_flags, unsigned long mask) +{ + int ret = 0; + struct mem_cgroup *iter, *memcg = root_mem_cgroup; + unsigned long enabled_mask = + (1UL << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG) | + (1UL << TRANSPARENT_HUGEPAGE_FLAG); + + mutex_lock(&memcg_thp_flags_mutex); + enabled_mask &= new_flags; + + for_each_mem_cgroup_tree(iter, memcg) { + unsigned long old_flags = iter->thp_flags; + + iter->thp_flags = (old_flags & ~mask) | new_flags; + } + + mutex_unlock(&memcg_thp_flags_mutex); + return ret; +} + +#endif + static int __init cgroup_memory(char *s) { char *token;