From mboxrd@z Thu Jan 1 00:00:00 1970 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 BC5ADCD1284 for ; Tue, 2 Apr 2024 10:44:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 377566B0095; Tue, 2 Apr 2024 06:44:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 327E16B0099; Tue, 2 Apr 2024 06:44:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 216816B009A; Tue, 2 Apr 2024 06:44:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 036F36B0095 for ; Tue, 2 Apr 2024 06:44:21 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B0689140AF9 for ; Tue, 2 Apr 2024 10:44:21 +0000 (UTC) X-FDA: 81964257522.13.FC48DFC Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 4B78620019 for ; Tue, 2 Apr 2024 10:44:19 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712054659; 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=8jvzg8Ge4fQislkRliJKUdt3CxM/5GoqfgqP7PbIgg4=; b=SRlNfBloeVs32xYN6N0lFg77j1ZCznq7lJdLaMYmiig8RHsHp0EukE9KZMEifBvaxfJ9r3 RmG48v0T3a1tXeASBfDTt3r2BPfymI+nBo3XUYkKZUyxXuDq+BmEHANnUB6JNrfNMHN4PV di3VKtSmCIZH5Y18jJFkIzMT2Le6liY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712054659; a=rsa-sha256; cv=none; b=VESy4sQ3Br1RQF5DoqwcXe8vVmUebxzwQdg/YrcEFg4AHP4E1oAApl695QgRKPGo8+R/Z0 J0LiG9uyFwBSH1T7ETav0qSlk/l9dVOrI9z+f9d144keaRr315R7IRIbZzsnc6k5hJzN16 CFucs4CWxHJAI9LlLVPvBM0r2Zh/HPk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6DBCDFEC; Tue, 2 Apr 2024 03:44:47 -0700 (PDT) Received: from [10.1.38.163] (XHFQ2J9959.cambridge.arm.com [10.1.38.163]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DD5583F766; Tue, 2 Apr 2024 03:44:13 -0700 (PDT) Message-ID: <16ac20eb-41fc-4024-a701-de576ff63c09@arm.com> Date: Tue, 2 Apr 2024 11:44:12 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm: add per-order mTHP alloc_success and alloc_fail counters Content-Language: en-GB To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, linux-mm@kvack.org, willy@infradead.org, david@redhat.com, cerasuolodomenico@gmail.com, kasong@tencent.com, surenb@google.com, v-songbaohua@oppo.com, yosryahmed@google.com, yuzhao@google.com, chrisl@kernel.org, peterx@redhat.com References: <20240328095139.143374-1-21cnbao@gmail.com> From: Ryan Roberts In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4B78620019 X-Rspam-User: X-Stat-Signature: 5n491wkfrz8zd83jgdbb7tszbikeb6xg X-Rspamd-Server: rspam03 X-HE-Tag: 1712054659-203260 X-HE-Meta: U2FsdGVkX1+CKgKjwoLE/uFtCUEWUIRUuSnncncnp0czztFcAoKMimAAvwxeFmVAc/grrefRweBsjY6HaMkVa/Ejp1y9pXTyttp1JQGXRvcSBaamFdGCWWLfbU4LQXUxuhiVTF+Oa1duNs5zcis+MmN1EdMXz4eIX6KoJLq4hzSQ4zs4ZCB6672KPDBoTxBx/bQ14yxpwLEIpdR+Xmz8qY8/xClhh73vlGJZSNZ/4vSh+5mH9OzgFQyC7e0bMvGbdVRk6bumgYhxba5exkQnNQvJI91gHmMtlqMn6J4uPAHLLK7SM0NvGBMAOfBOs0DYqvrTv6Or7hpXeR79iIbB+jVZrFDxTKVb4iml3qKweJx4udI7TWKRe55NZkiIr3eS42v7ROIHZGmBWp6QgpSghtflurH/IHxwdkTHEllqyDCM6TYWHQWpWM7GQ5okebD2GSl90bnH6KgA123NIT3iODGPuA0u3rMAXc3KywYmXbHhSSWHXo06jNgWm2UbwqKI7V/No/iazDUuHsVz8kQeiO0dmpnD++m4tzxBtfH/2pv2h+HlxL6qtfyWQwWinc4dVE1ojGa1wgPV2348YX2DTvtrj4Ku6nCqA7W5ynJhbMXkXkUIc5Z5q1LsVozrU3k3vCK6q34yMl3E7xCWKfquzrn8Li5QV/Vzv118LMG0MrjoOrlWWrBF13nGycNfS0vGq8fgs0+XbAHhzd7X6alAaLUjgtUyd2yrkRzoFY+MyQZgpgyrrd9IsJ7HObL5QVFfs2Ea8yDtAVv5yMXptgQKWy3/Pus84JtB3vfPCgCVYZpMZLZ9uRRdobtpjwn24JDnyIjZW29PDq1Jyk4+9SgWDSxO6x/ThnVt6eQAgyXM0yZuuh7y3U0OJZdNTk/26/CqUlyfeYDx/n5rL8IuElCKUjtmG/+WwG5APgKz9+vIOJQ4xbW2EBUGa1Eet7QDXq9nuSNtZ5yHVcwPBcvM4Ks 9WxpOykm r3l43QAdhHrDytzEbg+Th68jjbUbwx5tAwr1rKeGPjXEVsrfCMnhPOsqwxIjyvSgpXT+TCX7/hNROYUasw+Kx5KMELKCXAszSzWjuz0n63WoklVF3yQA8LKrDh38KgLusPs/hFTGKYV9XgGZVjBUG3I65WsZ+jY4B+L4uFniCv32Eio1Hy60l7XQ9Wg5b55tOSJ9M7ol2ZhbPtS7lW3Iuq65tXwQVTp0SBhyvPAa3Q11n92qKYRUIHH7BYnUflIAbxgfFzWVEAqifNDu47Re+SknHCndcT6J6fS6LTOuvaL3DCtTqDyq9yZGIZFMvlUV+5WjdiulnEBWVrjvIRC82m3+SPkv0MpIlw1L5kmSp9GqPAyNxqGc2bX4pCjFOJTOMFS4FOjoNcVsZT/QMldF2NMIMmw/OyUGGDzXO0ZfkHtBVJ+o= 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: On 02/04/2024 10:40, Barry Song wrote: > On Tue, Apr 2, 2024 at 9:58 PM Ryan Roberts wrote: >> >> On 28/03/2024 09:51, Barry Song wrote: >>> From: Barry Song >>> >>> Profiling a system blindly with mTHP has become challenging due >>> to the lack of visibility into its operations. Presenting the >>> success rate of mTHP allocations appears to be pressing need. >>> >>> Recently, I've been experiencing significant difficulty debugging >>> performance improvements and regressions without these figures. >>> It's crucial for us to understand the true effectiveness of >>> mTHP in real-world scenarios, especially in systems with >>> fragmented memory. >>> >>> This patch sets up the framework for per-order mTHP counters, >>> starting with the introduction of alloc_success and alloc_fail >>> counters. Incorporating additional counters should now be >>> straightforward as well. >>> >>> The initial two unsigned longs for each event are unused, given >>> that order-0 and order-1 are not mTHP. Nonetheless, this refinement >>> improves code clarity. >> >> Overall, this approach looks good to me - thanks for doing this! > > Thank you for reviewing! > >> >>> >>> Signed-off-by: Barry Song >>> --- >>> -v2: >>> * move to sysfs and provide per-order counters; David, Ryan, Willy >>> -v1: >>> https://lore.kernel.org/linux-mm/20240326030103.50678-1-21cnbao@gmail.com/ >>> >>> include/linux/huge_mm.h | 17 +++++++++++++ >>> mm/huge_memory.c | 54 +++++++++++++++++++++++++++++++++++++++++ >>> mm/memory.c | 3 +++ >>> 3 files changed, 74 insertions(+) >>> >>> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h >>> index e896ca4760f6..27fa26a22a8f 100644 >>> --- a/include/linux/huge_mm.h >>> +++ b/include/linux/huge_mm.h >>> @@ -264,6 +264,23 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, >>> enforce_sysfs, orders); >>> } >>> >>> +enum thp_event_item { >>> + THP_ALLOC_SUCCESS, >>> + THP_ALLOC_FAIL, >>> + NR_THP_EVENT_ITEMS >>> +}; >>> + >>> +struct thp_event_state { >>> + unsigned long event[PMD_ORDER + 1][NR_THP_EVENT_ITEMS]; >>> +}; >>> + >>> +DECLARE_PER_CPU(struct thp_event_state, thp_event_states); >>> + >>> +static inline void count_thp_event(int order, enum thp_event_item item) >> >> We previously concluded that we will likely want a "currently allocated THPs" >> counter, which will need both increment and decrement. So perhaps > > I intend to handle this via incremental patches, but it's improbable > to occur this > month due to my packed schedule with multiple tasks. Presently, my top priority > regarding counting is the fallback ratio for allocations. Following > that, it would > likely be the SWAP allocation and fallback ratio to determine if the swapfile is > highly fragmented. Yes indeed. I wasn't suggesting that the extra counters should be added immediately, just that having a view on the eventual need for "instantaneous" counters might influence the name of this function. > >> "thp_event_inc()" (and eventual "thp_event_dec()" are more appropriate? And >> perhaps "event" isn't the right description either since it implies always >> counting upwards (to me at least). Although I guess you have borrowed from the >> vmstat pattern? That file has some instantaneous counters, so how does it decrement? >> > > Counting represents just one aspect of vmstat. In this scenario, the values are > consistently increasing. For those metrics that could fluctuate in > either direction, > an entirely different mechanism is employed. These are typically managed through > global atomic operations. OK, but why do these need to be global atomic? I would have thought we could just define the per-cpu values as signed and allow both inc and dec? Then when you sum them, you get the correct answer. You could have: long stat[PMD_ORDER + 1][NR_THP_EVENT_ITEMS]; thp_stat_inc(int order, enum thp_stat_item item); thp_stat_dec(int order, enum thp_stat_item item); // Wrapper for increment-only events static inline void count_thp_event(int order, enum thp_stat_item item) { thp_stat_inc(order, item); } Just a thought. Perhaps its better to follow the established pattern. > > /* > * Zone and node-based page accounting with per cpu differentials. > */ > extern atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS]; > extern atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS]; > extern atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS]; > > void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long); > void __inc_zone_page_state(struct page *, enum zone_stat_item); > void __dec_zone_page_state(struct page *, enum zone_stat_item); > > void __mod_node_page_state(struct pglist_data *, enum node_stat_item > item, long); > void __inc_node_page_state(struct page *, enum node_stat_item); > void __dec_node_page_state(struct page *, enum node_stat_item); > > static inline void zone_page_state_add(long x, struct zone *zone, > enum zone_stat_item item) > { > atomic_long_add(x, &zone->vm_stat[item]); > atomic_long_add(x, &vm_zone_stat[item]); > } > > Counting is likely most efficiently handled with per-CPU copies and a summation > function to aggregate values from all CPUs. > >>> +{ >>> + this_cpu_inc(thp_event_states.event[order][item]); >> >> Could we declare as "PMD_ORDER - 1" then do [order - 2] here? That would save 16 >> bytes per CPU. (8K in a system with 512 CPUs). Possibly with a order_index() >> helper since the *_show() functions will need this too. > > I actually put some words in commit messages > > "The initial two unsigned longs for each event are unused, given > that order-0 and order-1 are not mTHP. Nonetheless, this refinement > improves code clarity." Yes I saw that. I was just observing that we can have both memory effciency and code clarity fairly easily. Not a strong opinion though. > > However, if conserving memory is a higher priority, I can utilize > PMD_ORDER - 1 in version 3. > >> >>> +} >>> + >>> #define transparent_hugepage_use_zero_page() \ >>> (transparent_hugepage_flags & \ >>> (1<>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>> index 1683de78c313..addd093d8410 100644 >>> --- a/mm/huge_memory.c >>> +++ b/mm/huge_memory.c >>> @@ -526,6 +526,52 @@ static const struct kobj_type thpsize_ktype = { >>> .sysfs_ops = &kobj_sysfs_ops, >>> }; >>> >>> +DEFINE_PER_CPU(struct thp_event_state, thp_event_states) = {{{0}}}; >>> + >>> +static unsigned long sum_thp_events(int order, enum thp_event_item item) >>> +{ >>> + unsigned long sum = 0; >>> + int cpu; >>> + >>> + for_each_online_cpu(cpu) { >>> + struct thp_event_state *this = &per_cpu(thp_event_states, cpu); >>> + >>> + sum += this->event[order][item]; >>> + } >>> + >>> + return sum; >>> +} >>> + >>> +static ssize_t alloc_success_show(struct kobject *kobj, >>> + struct kobj_attribute *attr, char *buf) >>> +{ >>> + int order = to_thpsize(kobj)->order; >>> + >>> + return sysfs_emit(buf, "%lu\n", sum_thp_events(order, THP_ALLOC_SUCCESS)); >>> +} >>> + >>> +static ssize_t alloc_fail_show(struct kobject *kobj, >>> + struct kobj_attribute *attr, char *buf) >>> +{ >>> + int order = to_thpsize(kobj)->order; >>> + >>> + return sysfs_emit(buf, "%lu\n", sum_thp_events(order, THP_ALLOC_FAIL)); >>> +} >>> + >>> +static struct kobj_attribute alloc_success_attr = __ATTR_RO(alloc_success); >>> +static struct kobj_attribute alloc_fail_attr = __ATTR_RO(alloc_fail); >>> + >>> +static struct attribute *stats_attrs[] = { >>> + &alloc_success_attr.attr, >>> + &alloc_fail_attr.attr, >>> + NULL, >>> +}; >>> + >>> +static struct attribute_group stats_attr_group = { >>> + .name = "stats", >>> + .attrs = stats_attrs, >>> +}; >>> + >>> static struct thpsize *thpsize_create(int order, struct kobject *parent) >>> { >>> unsigned long size = (PAGE_SIZE << order) / SZ_1K; >>> @@ -549,6 +595,12 @@ static struct thpsize *thpsize_create(int order, struct kobject *parent) >>> return ERR_PTR(ret); >>> } >>> >>> + ret = sysfs_create_group(&thpsize->kobj, &stats_attr_group); >>> + if (ret) { >>> + kobject_put(&thpsize->kobj); >>> + return ERR_PTR(ret); >>> + } >>> + >>> thpsize->order = order; >>> return thpsize; >>> } >>> @@ -1050,8 +1102,10 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) >>> folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, vma, haddr, true); >>> if (unlikely(!folio)) { >>> count_vm_event(THP_FAULT_FALLBACK); >>> + count_thp_event(HPAGE_PMD_ORDER, THP_ALLOC_FAIL); >>> return VM_FAULT_FALLBACK; >>> } >>> + count_thp_event(HPAGE_PMD_ORDER, THP_ALLOC_SUCCESS); >>> return __do_huge_pmd_anonymous_page(vmf, &folio->page, gfp); >>> } >>> >>> diff --git a/mm/memory.c b/mm/memory.c >>> index 984b138f85b4..c9c1031c2ecb 100644 >>> --- a/mm/memory.c >>> +++ b/mm/memory.c >>> @@ -4365,7 +4365,10 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) >>> } >>> folio_throttle_swaprate(folio, gfp); >>> clear_huge_page(&folio->page, vmf->address, 1 << order); >>> + count_thp_event(order, THP_ALLOC_SUCCESS); >>> return folio; >>> + } else { >> >> Do we need this else, given the last statement in the "if" clause is return? > > I included this "else" statement because we encounter a scenario where > we "goto next" even if allocation succeeds. > > folio = vma_alloc_folio(gfp, order, vma, addr, true); > if (folio) { > if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { > folio_put(folio); > goto next; > } > > After revisiting the code for a second time, I noticed that the SUCCESS > counter is incremented after the "goto" statement, so I realize that I won't > need this "else" clause. > > folio = vma_alloc_folio(gfp, order, vma, addr, true); > if (folio) { > if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { > folio_put(folio); > goto next; > } > folio_throttle_swaprate(folio, gfp); > clear_huge_page(&folio->page, vmf->address, 1 << order); > count_thp_event(order, THP_ALLOC_SUCCESS); > return folio; > > > So, I have two options: > > 1. Move the count_thp_event(order, THP_ALLOC_SUCCESS); statement ahead of > mem_cgroup_charge(folio, vma->vm_mm, gfp) and retain the else clause. > 2. Keep THP_ALLOC_SUCCESS unchanged and remove the else clause. > > Since mem_cgroup_charge() rarely fails, option 2 should suffice. I guess it depends exactly on what the semantics of THP_ALLOC_SUCCESS and THP_ALLOC_FAIL are supposed to be. For me, this makes most sense: while (orders) { addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); folio = vma_alloc_folio(gfp, order, vma, addr, true); if (folio) { if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { folio_put(folio); goto next; } folio_throttle_swaprate(folio, gfp); clear_huge_page(&folio->page, vmf->address, 1 << order); count_thp_event(order, THP_ALLOC_SUCCESS); return folio; } next: count_thp_event(order, THP_ALLOC_FAIL); order = next_order(&orders, order); } But this way you will end up with more THP_ALLOC_FAIL events than page faults. Let's say you have 1M, 512K, 256K and 128K mTHP enabled, and you fail to allocate all except 128K; You will end up with 3 FAILs and 1 SUCCESS for a single fault. Is that a problem? I guess an alternative approach would be to mark SUCCESS per-size. But have a single, global FAIL counter, which just tells you how many faults end up getting a 4K page. (or have that counter in addition to the per-size FAIL counters). It feels a bit odd that you get a per-size FAIL if the allocation fails, but not if the VMA is congested; if the VMA is congested, you will have already filtered out the larger orders and therefore never see the allocation FAIL. What question are you usually trying to answer with these stats? Perhaps I'm making this more complicated than it needs to be... :) > > By the way, why do we proceed to the next order after > mem_cgroup_charge() fails? > I assume mem_cgroup_charge() is still likely to fail even after we move to > the next order? That bit was added by Kefeng Wang, but my understanding is that mem_cgroup_charge() will fail if we are bumping up against the quota limit? So I guess the thinking is we may not be able to allocate 1M but 32K may be fine. > > >> >>> + count_thp_event(order, THP_ALLOC_FAIL); >>> } >>> next: >>> order = next_order(&orders, order); >> >> Thanks, >> Ryan >> > > Thanks > Barry