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 C673EC04E69 for ; Wed, 16 Aug 2023 13:12:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C9EA8D0036; Wed, 16 Aug 2023 09:12:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0536E8D0001; Wed, 16 Aug 2023 09:12:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E35AE8D0036; Wed, 16 Aug 2023 09:12:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D2CCC8D0001 for ; Wed, 16 Aug 2023 09:12:25 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9A08E120F38 for ; Wed, 16 Aug 2023 13:12:25 +0000 (UTC) X-FDA: 81130006650.20.C2ACF06 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf18.hostedemail.com (Postfix) with ESMTP id 133621C00E2 for ; Wed, 16 Aug 2023 13:11:24 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="fsm/Z/ak"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf18.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692191486; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7y+FGla4ZqarJpsY9rL6fJ+tM9ztKi60WDdpJ1g+IXE=; b=jGuebmlufXK6igoaaA7tiAk2RgOWlsTGzJ8d5B5z3cieUic0CyTsL8QJ4M+NxAkNpOKSa3 JMDFT2floKcqho82gCd1g1tB6ZZ1mMfiWv15lPjKlt1gHn7CeyrRWROD0+VVbSZms2WiOT Ix5JPbT3vHkFkbH95l/ryWAUZWUWRns= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="fsm/Z/ak"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf18.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692191486; a=rsa-sha256; cv=none; b=VdRVLtxYU91vByJe6scprK73XEDPsVRSBfMnNAaemeG1fn29sFJFTstlY8Mvj8WImO9KWF 1KYbzTRqeL+YksPDgpIc4RcQWJvZeCi7fspy832ODd+Yfh+kirAjR5TB7rIuJJG/iuOeE+ 1zGvn1ebaW+KbH93K6HQeeT22o7XkVs= Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-564cd28d48dso3884955a12.0 for ; Wed, 16 Aug 2023 06:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1692191483; x=1692796283; h=content-transfer-encoding:in-reply-to:from:references:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=7y+FGla4ZqarJpsY9rL6fJ+tM9ztKi60WDdpJ1g+IXE=; b=fsm/Z/ak+zuFz6543kEjTWDmVmFDFGkCxSiQA9p+6ruCdw9Uo+s8f1m7XMOiWar8XQ 91ONLABndc8fHMjvnH0f4jsYoMKNEWwOMBEnQ0oLsy7+UBeHNSfF0vry2NcMfcdJTrTS vm9o3WQio1gP8LRejs/Cu7K61qUbL/LZFAdrWJhkilDgW/mnRwiEExx6ZFgrtL6lj8Q2 6Q/qaVGZNetg4dlHPuE6C5H4mFlcQW4G9+VItj7Ciai/tukYrLB4cFyCbDmQNtVUc2lf zSyw1XjyzVJlhMO/Mk9qY07LD2nDCkKTCXBHfZ2bntqv84f4VM6qS9E1+MJS2ioAT/Ev c+5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692191483; x=1692796283; h=content-transfer-encoding:in-reply-to:from:references:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=7y+FGla4ZqarJpsY9rL6fJ+tM9ztKi60WDdpJ1g+IXE=; b=Gxo9tScKw+ZcIDFMj+KRm8g3Mfw9tOtDXEkL0nHwWygGtkdtlUP3Wz0pmTUrYjxzPR RyVYPxcSEfpTc9BCvvJWvuuvTOWz+tLmxEqWp7AKxxcY59WQDL3nQae+Ls6xqwbM8IQe Sb015LAMATQAcMNvrTOwMdL0wF9SmQVewj2IAltUOcjbJAoYRynqkQw1UFCcAUY2kw00 N9p28puzkjptZHlkXKCb2YQhaGTgP4WFCkjAV6o8bGcm6zLk4exknQQ+ypMC7MVaZgB8 ivWTldX/O5enTlXn1wtqK169jGLiXw+Joq0pd+F6n4IFTEAJZo7r4l7GPxBt3SJrV2D8 rIUA== X-Gm-Message-State: AOJu0Yw8cHOHS8RlPttspAUb2h2fgI+xMISOWd7qDOF2MYvBuPAgR6wa CX6FFWmq01c6qwuhRMkA3eAVhA== X-Google-Smtp-Source: AGHT+IEhtIhiYveh9oLegEn5CX8U4HyvOiK88j2387qNBXnQtj2zYvVJZn9ewmGPBGrq9IlVrtu1Xw== X-Received: by 2002:a17:90b:1e07:b0:268:fb85:3b2 with SMTP id pg7-20020a17090b1e0700b00268fb8503b2mr1246851pjb.7.1692191483350; Wed, 16 Aug 2023 06:11:23 -0700 (PDT) Received: from [10.254.252.111] ([139.177.225.249]) by smtp.gmail.com with ESMTPSA id u10-20020a17090ae00a00b0026b3f76a063sm7261968pjy.44.2023.08.16.06.11.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Aug 2023 06:11:23 -0700 (PDT) Message-ID: <6babc4c1-0f0f-f0b1-1d45-311448af8d70@bytedance.com> Date: Wed, 16 Aug 2023 21:11:15 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH 06/11] maple_tree: Introduce mas_replace_entry() to directly replace an entry To: "Liam R. Howlett" , Peng Zhang , 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 References: <20230726080916.17454-1-zhangpeng.00@bytedance.com> <20230726080916.17454-7-zhangpeng.00@bytedance.com> <20230726160843.hpl4razxiikqbuxy@revolver> <20aab1af-c183-db94-90d7-5e5425e3fd80@bytedance.com> <20230731164854.vbndc2z2mqpw53in@revolver> From: Peng Zhang In-Reply-To: <20230731164854.vbndc2z2mqpw53in@revolver> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 133621C00E2 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: tg7zzxtgifobcziy8pke3mma3oe4hem1 X-HE-Tag: 1692191484-730655 X-HE-Meta: U2FsdGVkX18b38Oowqn2QvYhEICcZWQEd83M4/m1QeSYD6SXHAM+h+tPOc1cg7UHHrOFhL+cbkYR2Zjdn0BedKja82eNXR7ZGHcT6DvTVGvaxkMxyJ6Yucd2J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ErudpsJKMKDKyOZ2ElhKOW8KZlNHCC+aXrkC1K472pSl17fteZKbYduzW1N4ctkBY4XHmLVMa2BwsZTIrMFfSzgU6LEhSnDqtEt5NgmtC0kbRb4Ecqgz1l7MGL/MDEGzPpXYyzz8+VKuEGE07jWzWTHHUB+2r/ObJ8grbigJu0xNL7CGosXDSU+rjyFAuKL0e2YFj/tCt7NZ4jsAyqWIKOtJrIwdUn9bnF4N6OBHakRONL+6fEB+yWHWkQFbOBOQuaW6Ab 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: 在 2023/8/1 00:48, Liam R. Howlett 写道: > * Peng Zhang [230731 08:39]: >> >> >> 在 2023/7/27 00:08, Liam R. Howlett 写道: >>> * Peng Zhang [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 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. 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 >>>> --- >>>> 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 >>>> + 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 >>>>