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 F03C6CD98F8 for ; Wed, 11 Oct 2023 07:57:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 832628D00EF; Wed, 11 Oct 2023 03:57:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E15A8D0002; Wed, 11 Oct 2023 03:57:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D03A8D00EF; Wed, 11 Oct 2023 03:57:10 -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 5E39A8D0002 for ; Wed, 11 Oct 2023 03:57:10 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1F4A2A01FF for ; Wed, 11 Oct 2023 07:57:10 +0000 (UTC) X-FDA: 81332425020.01.8DA0F94 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by imf12.hostedemail.com (Postfix) with ESMTP id 4DC2940019 for ; Wed, 11 Oct 2023 07:57:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=none (imf12.hostedemail.com: domain of houtao@huaweicloud.com has no SPF policy when checking 45.249.212.56) smtp.mailfrom=houtao@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697011027; 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=TJkc3cYpvmzajb4ICfhsmzA/U2ApnNVfb+VsjRWuIkg=; b=jq5ES/Sic5RQmRoYyJYLIpDTgl5ARXaoR3xXy6K+YWKH4lLsHPZn365eUmVByVonMKEgCp 7NA0UINwKKYlHmxl57wUrR0IAkDZV77nKfvczzfiTWAeKiVMJYQkjkTsFwdqAEmYCSo027 sgG/CUaAG//BGo3rGRaYVsPNGn5+hUM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=none (imf12.hostedemail.com: domain of houtao@huaweicloud.com has no SPF policy when checking 45.249.212.56) smtp.mailfrom=houtao@huaweicloud.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697011027; a=rsa-sha256; cv=none; b=D8LbvNWQ/3q5J6CUZmg9bMd4TFTJ0vr1rOR0BUNVNOwoGRXoaokhvhnvy3XuOPu4qoqn7Z HeWag95cr21u9j4hNHyFeS9tTr3soaUFjXSni1q/R2l4eNqJFz0ebcWZzftveoR74ZqWL+ ICOiGpZKjceYt4yI+JARienKQhQ7/kg= Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4S52vq2x5Sz4f3jXc for ; Wed, 11 Oct 2023 14:30:47 +0800 (CST) Received: from [10.174.176.117] (unknown [10.174.176.117]) by APP3 (Coremail) with SMTP id _Ch0CgA3jEsYQSZlxvYMCg--.14676S2; Wed, 11 Oct 2023 14:30:52 +0800 (CST) Subject: Re: [PATCH bpf-next 1/6] mm/percpu.c: introduce alloc_size_percpu() To: Dennis Zhou Cc: bpf@vger.kernel.org, linux-mm@kvack.org, Martin KaFai Lau , Alexei Starovoitov , Andrii Nakryiko , Song Liu , Hao Luo , Yonghong Song , Daniel Borkmann , KP Singh , Stanislav Fomichev , Jiri Olsa , John Fastabend , houtao1@huawei.com, Tejun Heo , Christoph Lameter , Andrew Morton References: <20231007135106.3031284-1-houtao@huaweicloud.com> <20231007135106.3031284-2-houtao@huaweicloud.com> From: Hou Tao Message-ID: <3c00c6d1-da26-19fe-aa39-10c4993c475b@huaweicloud.com> Date: Wed, 11 Oct 2023 14:30:48 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US X-CM-TRANSID:_Ch0CgA3jEsYQSZlxvYMCg--.14676S2 X-Coremail-Antispam: 1UD129KBjvJXoWxJw45XFW7Wry8CFW7ZFyDtrb_yoW5tF1fpF WkW3ZYyF4kXrnrWw1Sqw1jqw1fWws5GFyxJ343GFy5AFnIvr9Fgr1vyrW5uFyrCrn29r12 vFZ0qFs3CFW3X3DanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvab4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7Mxk0xIA0c2IE e2xFo4CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxV Aqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a 6rW5MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6x kF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv 67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyT uYvjxUo0eHDUUUU X-CM-SenderInfo: xkrx3t3r6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 4DC2940019 X-Rspam-User: X-Stat-Signature: 8shg31cr3etqga3zzsawyyh3re33y4cm X-Rspamd-Server: rspam01 X-HE-Tag: 1697011025-854409 X-HE-Meta: U2FsdGVkX18ugfzbeGtX4b7aqvNNgkO0pwDzcABf21gKHRmoTzayfUp1HtolWApt8iMC749qLE1MXoAM+PfJol+lvzX44qL75AYtwLlYxWKDWyRrn+2rETbiC5MJON1mqoaiPuWHIunHfaRA9TPhhjZE5rFJrOiL7IVpWhXXfqxyArc26mehfCeLoCSHlTaW7iEGobxY0NNC9uLXQPAPGGxxOGza2xKsd51uz4icmJ92PjRL16QYXa9TC14HSBfJI0P281MPWVoKrY7M9F22pEZZdUEy/fBMffdwpvZVabtyN/XaZnVqQ0f+KlcxNHdqf0oboh8OCiU237nvsvFsiiP31s7mWOOy/CZ/KmHp4NccvkScmsesu4or8lC+Wi//oGvAjbO0M9KBMnws7F9w7/U5VZaMvCl72IQG1uQ7gscY2I5RG0q4dbaElH13gysekItVXLiFliVpFWCMbkEBUQHg4RF1Mxz0OuPCFypLa3lDHmTd0EqR06kAYnNe/2FvQD+Ij8VfcN+E7QaimUH9U1F+kQeqosuGMG/2dwZhVqrkHx1KQEeu+E8eAVLIONahn8ciVPkRV8YdGGzPAf19UB6q3qfoA5ECPzveOffFWH/YEDW5zS5/Hq+nW+2OxyjGZ/H+bXV+JFbjefQaeg9rpTJMepeqZf2+ha1+PI8A9ujfwHleSr3hSKE/cTUs+ZiCOtiXMxnBZAqisT5X6kMX2YH5O26J0csQlUizyyL+HdhHbL5e8kvCPfQhoPYuu04Uj4xXxi13pIy242M1rP57rSxAvhcP1CljSeFsAnsJgkT+Kt0fkXUif3xnGU8KabFskcD/kkaCxZMUyt6vvW0iAKPN0QQLmhL77iDrzf5nPwJeH/K1fOQw8WDAt00YLMnmkQMXm4ways1bjl2OtOMrXT3f0sLPfaBSuBYpH3SY+znxlh7Wak4X4DrkLlqMivrIe8BWRgyvnp/C3qBzRAF QEeGSqRR 3AlVYCnjsF59NDcogEZP4UOJq+nxaOHWuwTjEYf68WkRVgtO2UrWyN50esSm71Ks2nZ4oyMpapVQ/jrye+9THx3MPmU6f6SC854E7DrT4GT/fqCE+sOIikjKAB9gf1E2kgEBvvs/FwqsxNmAptKScG4Dm2beMDV7kT9TZMe+/h2+5lGmJjJJZBXwimWq95aENEAifXQjhf39Eym4m8h2T6RUKrjgsJ+Qqvx/5ePaw1iZ46Yg4VNm64wryoGhSAa1sKS2OdDVLQYOWonFtOFuHPC3OpgxdQMUYoEmcv549d0BJvqiqE7iAaOVSeF1F1nqF+EL86ygJGLXBbE0= 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: Hi, On 10/9/2023 6:32 AM, Dennis Zhou wrote: > Hello, > > On Sat, Oct 07, 2023 at 09:51:01PM +0800, Hou Tao wrote: >> From: Hou Tao >> >> Introduce alloc_size_percpu() to get the size of the dynamic per-cpu >> area. It will be used by bpf memory allocator in the following patches. >> BPF memory allocator maintains multiple per-cpu area caches for multiple >> area sizes and it needs the size of dynamic per-cpu area to select the >> corresponding cache when bpf program frees the dynamic per-cpu area. >> >> Signed-off-by: Hou Tao >> --- >> include/linux/percpu.h | 1 + >> mm/percpu.c | 29 +++++++++++++++++++++++++++++ >> 2 files changed, 30 insertions(+) >> >> diff --git a/include/linux/percpu.h b/include/linux/percpu.h >> index 68fac2e7cbe6..d140d9d79567 100644 >> --- a/include/linux/percpu.h >> +++ b/include/linux/percpu.h >> @@ -132,6 +132,7 @@ extern void __init setup_per_cpu_areas(void); >> extern void __percpu *__alloc_percpu_gfp(size_t size, size_t align, gfp_t gfp) __alloc_size(1); >> extern void __percpu *__alloc_percpu(size_t size, size_t align) __alloc_size(1); >> extern void free_percpu(void __percpu *__pdata); >> +extern size_t alloc_size_percpu(void __percpu *__pdata); >> >> DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) >> >> diff --git a/mm/percpu.c b/mm/percpu.c >> index 7b40b3963f10..f541cfc3cb2d 100644 >> --- a/mm/percpu.c >> +++ b/mm/percpu.c >> @@ -2244,6 +2244,35 @@ static void pcpu_balance_workfn(struct work_struct *work) >> mutex_unlock(&pcpu_alloc_mutex); >> } >> >> +/** >> + * alloc_size_percpu - the size of the dynamic percpu area > Can we name this pcpu_alloc_size(). A few other functions are > exposed under pcpu_* so it's a bit easier to keep track of. Will do in v2. > >> + * @ptr: pointer to the dynamic percpu area >> + * >> + * Return the size of the dynamic percpu area @ptr. >> + * >> + * RETURNS: >> + * The size of the dynamic percpu area. >> + * >> + * CONTEXT: >> + * Can be called from atomic context. >> + */ >> +size_t alloc_size_percpu(void __percpu *ptr) >> +{ >> + struct pcpu_chunk *chunk; >> + int bit_off, end; >> + void *addr; >> + >> + if (!ptr) >> + return 0; >> + >> + addr = __pcpu_ptr_to_addr(ptr); >> + /* No pcpu_lock here: ptr has not been freed, so chunk is still alive */ > Now that percpu variables are floating around more commonly, I think we > or I need to add more validation guards so it's easier to > debug bogus/stale pointers. Potentially like a static_key or Kconfig so > that we take the lock and `test_bit()`. Before the patch, it seems that free_percpu() is the only user of __pcpu_ptr_to_addr(ptr). I can move the invocation of both __pcpu_ptr_to_addr() and pcpu_chunk_addr_search() into a common helper if you are OK with it. And I think it will ease the work of adding validation guard on the per-cpu pointer. > >> + chunk = pcpu_chunk_addr_search(addr); >> + bit_off = (addr - chunk->base_addr) / PCPU_MIN_ALLOC_SIZE; >> + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), bit_off + 1); > Nit: can you please reflow `bit_off + 1` to the next line. I know we > dropped the line requirement, but percpu.c almost completely still > follows it. Will do in v2. > >> + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; >> +} >> + >> /** >> * free_percpu - free percpu area >> * @ptr: pointer to area to free >> -- >> 2.29.2 >> > Thanks, > Dennis