From: Peng Zhang <zhangpeng.00@bytedance.com>
To: "Liam R. Howlett" <Liam.Howlett@Oracle.com>,
Peng Zhang <zhangpeng.00@bytedance.com>,
avagin@gmail.com, npiggin@gmail.com,
mathieu.desnoyers@efficios.com, peterz@infradead.org,
michael.christie@oracle.com, surenb@google.com,
brauner@kernel.org, willy@infradead.org,
akpm@linux-foundation.org, corbet@lwn.net,
linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-mm@kvack.org, linux-doc@vger.kernel.org
Subject: Re: [PATCH 06/11] maple_tree: Introduce mas_replace_entry() to directly replace an entry
Date: Fri, 18 Aug 2023 17:39:53 +0800 [thread overview]
Message-ID: <51cc7e0c-2fb3-1c40-4cd2-bad15737d616@bytedance.com> (raw)
In-Reply-To: <20230816174017.4imcdnktvyoqcxw6@revolver>
在 2023/8/17 01:40, Liam R. Howlett 写道:
> * Peng Zhang <zhangpeng.00@bytedance.com> [230816 09:11]:
>>
>>
>> 在 2023/8/1 00:48, Liam R. Howlett 写道:
>>> * Peng Zhang <zhangpeng.00@bytedance.com> [230731 08:39]:
>>>>
>>>>
>>>> 在 2023/7/27 00:08, Liam R. Howlett 写道:
>>>>> * Peng Zhang <zhangpeng.00@bytedance.com> [230726 04:10]:
>>>>>> If mas has located a specific entry, it may be need to replace this
>>>>>> entry, so introduce mas_replace_entry() to do this. mas_replace_entry()
>>>>>> will be more efficient than mas_store*() because it doesn't do many
>>>>>> unnecessary checks.
>>>>>>
>>>>>> This function should be inline, but more functions need to be moved to
>>>>>> the header file, so I didn't do it for the time being.
>>>>>
>>>>> I am really nervous having no checks here. I get that this could be
>>>>> used for duplicating the tree more efficiently, but having a function
>>>>> that just swaps a value in is very dangerous - especially since it is
>>>>> decoupled from the tree duplication code.
>>>> I've thought about this, and I feel like this is something the user
>>>> should be guaranteed. If the user is not sure whether to use it,
>>>> mas_store() can be used instead.
>>>
>>> Documentation often isn't up to date and even more rarely read.
>>> mas_replace_entry() does not give a hint of a requirement for a specific
>>> state to the mas. This is not acceptable.
>>>
>>> The description of the function also doesn't say anything about a
>>> requirement of the maple state, just that it replaces an already
>>> existing entry. You have to read the notes to find out that 'mas must
>>> already locate an existing entry'.
>>>
>>>> And we should provide this interface
>>>> because it has better performance.
>>>
>>> How much better is the performance? There's always a trade off but
>>> without numbers, this is hard to justify.
>> I have implemented a new version of this pachset, and I will post it
>> soon.
>>
>> I tested the benefits of mas_replace_entry() in userspace.
>> The test code is attached at the end.
>>
>> Run three times:
>> mas_replace_entry(): 2.7613050s 2.7120030s 2.7274200s
>> mas_store(): 3.8451260s 3.8113200s 3.9334160s
>
> This runtime is too short, we should increase the number of elements or
> loops until it is over 10 seconds. This will make the setup time
> and other variances less significant and we can use the command run time
> as a rough estimate of performance. IIRC 134 was picked for a rough
> estimate of an average task size so maybe increase the loops.
I changed nr_entries to 1000, and the measured numbers are as follows:
mas_replace_entry(): 20.0375820s
mas_store(): 28.6175720s
It can be seen that mas_store() is still nearly 40% slower.
>
> I understand the numbers here are from clock recordings to demonstrate
> the significance of your change.
>
>>
>> Using mas_store() reduces the performance of duplicating VMAs by about
>> 41%.
>>
>> So I think mas_replace_entry() is necessary. We can describe it in more
>> detail in the documentation to prevent users from misusing it.
>
> I think something is necessary for a quicker replacement, yes. I don't
> want to go as far as you did with the lack of checking.
>
>>
>>
>> static noinline void __init bench_forking(struct maple_tree *mt)
>> {
>> struct maple_tree newmt;
>> int i, nr_entries = 134, nr_fork = 80000, ret;
>> void *val;
>> MA_STATE(mas, mt, 0, 0);
>> MA_STATE(newmas, &newmt, 0, 0);
>> clock_t start;
>> clock_t end;
>> double cpu_time_used = 0;
>>
>> for (i = 0; i <= nr_entries; i++)
>> mtree_store_range(mt, i*10, i*10 + 5,
>> xa_mk_value(i), GFP_KERNEL);
>>
>> for (i = 0; i < nr_fork; i++) {
>> mt_set_non_kernel(99999);
>>
>> start = clock();
>> mt_init_flags(&newmt, MT_FLAGS_ALLOC_RANGE);
>> mas_lock(&newmas);
>> mas_lock(&mas);
>> ret = __mt_dup(mt, &newmt, GFP_NOWAIT | __GFP_NOWARN);
>> if (ret) {
>> pr_err("OOM!");
>> BUG_ON(1);
>> }
>>
>> mas_set(&newmas, 0);
>> mas_for_each(&newmas, val, ULONG_MAX) {
>> mas_replace_entry(&newmas, val);
>> }
>>
>> mas_unlock(&mas);
>> mas_unlock(&newmas);
>> end = clock();
>> cpu_time_used += ((double) (end - start));
>>
>> mas_destroy(&newmas);
>> mt_validate(&newmt);
>> mt_set_non_kernel(0);
>> mtree_destroy(&newmt);
>> }
>> printf("time consumption:%.7fs\n", cpu_time_used / CLOCKS_PER_SEC);
>> }
>>
>>
>>>
>>>>>
>>>>>>
>>>>>> Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com>
>>>>>> ---
>>>>>> include/linux/maple_tree.h | 1 +
>>>>>> lib/maple_tree.c | 25 +++++++++++++++++++++++++
>>>>>> 2 files changed, 26 insertions(+)
>>>>>>
>>>>>> diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h
>>>>>> index 229fe78e4c89..a05e9827d761 100644
>>>>>> --- a/include/linux/maple_tree.h
>>>>>> +++ b/include/linux/maple_tree.h
>>>>>> @@ -462,6 +462,7 @@ struct ma_wr_state {
>>>>>> void *mas_walk(struct ma_state *mas);
>>>>>> void *mas_store(struct ma_state *mas, void *entry);
>>>>>> +void mas_replace_entry(struct ma_state *mas, void *entry);
>>>>>> void *mas_erase(struct ma_state *mas);
>>>>>> int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp);
>>>>>> void mas_store_prealloc(struct ma_state *mas, void *entry);
>>>>>> diff --git a/lib/maple_tree.c b/lib/maple_tree.c
>>>>>> index efac6761ae37..d58572666a00 100644
>>>>>> --- a/lib/maple_tree.c
>>>>>> +++ b/lib/maple_tree.c
>>>>>> @@ -5600,6 +5600,31 @@ void *mas_store(struct ma_state *mas, void *entry)
>>>>>> }
>>>>>> EXPORT_SYMBOL_GPL(mas_store);
>>>>>> +/**
>>>>>> + * mas_replace_entry() - Replace an entry that already exists in the maple tree
>>>>>> + * @mas: The maple state
>>>>>> + * @entry: The entry to store
>>>>>> + *
>>>>>> + * Please note that mas must already locate an existing entry, and the new entry
>>>>>> + * must not be NULL. If these two points cannot be guaranteed, please use
>>>>>> + * mas_store*() instead, otherwise it will cause an internal error in the maple
>>>>>> + * tree. This function does not need to allocate memory, so it must succeed.
>>>>>> + */
>>>>>> +void mas_replace_entry(struct ma_state *mas, void *entry)
>>>>>> +{
>>>>>> + void __rcu **slots;
>>>>>> +
>>>>>> +#ifdef CONFIG_DEBUG_MAPLE_TREE
>
> CONFIG_DEBUG_MAPLE_TREE is not necessary, MAS_WRAN_ON() will be compiled
> out if it's not set.
>
>>>>>> + MAS_WARN_ON(mas, !mte_is_leaf(mas->node));
>>>>>> + MAS_WARN_ON(mas, !entry);
>>>>>> + MAS_WARN_ON(mas, mas->offset >= mt_slots[mte_node_type(mas->node)]);
>>>>>> +#endif
>>>>>> +
>>>>>> + slots = ma_slots(mte_to_node(mas->node), mte_node_type(mas->node));
>>>>>> + rcu_assign_pointer(slots[mas->offset], entry);
>>>>>> +}
>>>>>> +EXPORT_SYMBOL_GPL(mas_replace_entry);
>>>>>> +
>>>>>> /**
>>>>>> * mas_store_gfp() - Store a value into the tree.
>>>>>> * @mas: The maple state
>>>>>> --
>>>>>> 2.20.1
>>>>>>
next prev parent reply other threads:[~2023-08-18 9:41 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-26 8:09 [PATCH 00/11] Introduce mt_dup() to improve the performance of fork() Peng Zhang
2023-07-26 8:09 ` [PATCH 01/11] maple_tree: Introduce ma_nonleaf_data_end{_nocheck}() Peng Zhang
2023-07-26 14:58 ` Liam R. Howlett
2023-07-31 9:52 ` Peng Zhang
2023-07-31 16:08 ` Liam R. Howlett
2023-07-26 8:09 ` [PATCH 02/11] maple_tree: Validate MAPLE_ENODE and ma_nonleaf_data_end() Peng Zhang
2023-07-26 8:09 ` [PATCH 03/11] maple_tree: Add some helper functions Peng Zhang
2023-07-26 15:02 ` Liam R. Howlett
2023-07-26 15:08 ` Matthew Wilcox
2023-07-31 11:45 ` Peng Zhang
2023-08-11 17:28 ` Liam R. Howlett
2023-07-31 11:40 ` Peng Zhang
2023-07-26 8:09 ` [PATCH 04/11] maple_tree: Introduce interfaces __mt_dup() and mt_dup() Peng Zhang
2023-07-26 16:03 ` Liam R. Howlett
2023-07-31 12:24 ` Peng Zhang
2023-07-31 16:27 ` Liam R. Howlett
2023-08-16 13:41 ` Peng Zhang
2023-08-16 18:30 ` Liam R. Howlett
2023-08-18 11:53 ` Peng Zhang
2023-08-18 16:13 ` Liam R. Howlett
2023-07-26 8:09 ` [PATCH 05/11] maple_tree: Add test for mt_dup() Peng Zhang
2023-07-26 16:06 ` Liam R. Howlett
2023-07-31 12:32 ` Peng Zhang
2023-07-31 16:41 ` Liam R. Howlett
2023-07-26 8:09 ` [PATCH 06/11] maple_tree: Introduce mas_replace_entry() to directly replace an entry Peng Zhang
2023-07-26 16:08 ` Liam R. Howlett
2023-07-31 12:39 ` Peng Zhang
2023-07-31 16:48 ` Liam R. Howlett
2023-08-16 13:11 ` Peng Zhang
2023-08-16 17:40 ` Liam R. Howlett
2023-08-18 9:39 ` Peng Zhang [this message]
2023-08-18 16:15 ` Liam R. Howlett
2023-07-26 8:09 ` [PATCH 07/11] maple_tree: Update the documentation of maple tree Peng Zhang
2023-07-26 8:09 ` [PATCH 08/11] maple_tree: Skip other tests when BENCH is enabled Peng Zhang
2023-07-26 8:09 ` [PATCH 09/11] maple_tree: Update check_forking() and bench_forking() Peng Zhang
2023-07-26 8:09 ` [PATCH 10/11] MAINTAINERS: Add co-maintainer for maple tree Peng Zhang
2023-07-26 16:39 ` Liam R. Howlett
2023-07-31 12:55 ` Peng Zhang
2023-07-31 20:55 ` Liam R. Howlett
2023-07-26 8:09 ` [PATCH 11/11] fork: Use __mt_dup() to duplicate maple tree in dup_mmap() Peng Zhang
2023-07-26 17:06 ` Liam R. Howlett
2023-07-31 12:59 ` Peng Zhang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=51cc7e0c-2fb3-1c40-4cd2-bad15737d616@bytedance.com \
--to=zhangpeng.00@bytedance.com \
--cc=Liam.Howlett@Oracle.com \
--cc=akpm@linux-foundation.org \
--cc=avagin@gmail.com \
--cc=brauner@kernel.org \
--cc=corbet@lwn.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=michael.christie@oracle.com \
--cc=npiggin@gmail.com \
--cc=peterz@infradead.org \
--cc=surenb@google.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).