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 D7FDDE95A67 for ; Sun, 8 Oct 2023 22:32:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FE926B019C; Sun, 8 Oct 2023 18:32:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AE0D6B019F; Sun, 8 Oct 2023 18:32:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 376396B01AA; Sun, 8 Oct 2023 18:32:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2906A6B019C for ; Sun, 8 Oct 2023 18:32:22 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DBE3C1A0117 for ; Sun, 8 Oct 2023 22:32:21 +0000 (UTC) X-FDA: 81323744082.15.89FAA1F Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by imf05.hostedemail.com (Postfix) with ESMTP id 2783B100016 for ; Sun, 8 Oct 2023 22:32:19 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf05.hostedemail.com: domain of dennisszhou@gmail.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=dennisszhou@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696804340; a=rsa-sha256; cv=none; b=wMEScyl5PnAjs19QG972J1wfHic+URiBT2osY2FXKt6zMNTpmKqu07j/5SjrSQoXpzliWp paS/LwfMEAV5W0HwFDiTs8LnSLE4gOpkwU+/D7tqsk91uxY0Be+Ozuq3+JVIeUSsx+QaUU FaZ91I4zHS0EfiTXTnZWiCGHlxgyP2w= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf05.hostedemail.com: domain of dennisszhou@gmail.com designates 209.85.221.43 as permitted sender) smtp.mailfrom=dennisszhou@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696804340; 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: in-reply-to:in-reply-to:references:references; bh=agWXTU2Yij2i3Ap0SjGTJ2Rdepm57nHoh7x5LeUftzY=; b=OsqjLH42YdckTyAlk7DW4Fk8oX8LBxiyZSDwZP8eg7eUITGi6+7MFFzYGXsVFEERFAIpyh 2pThaqfqJ0XJ1YgS0SDNVsf+HDT63eOAbqJkMRrTY4acykpaMjdEXNUORLQFVJDUwuHoEP sUwROrydydaSi/AujFLCQnNKl4u9L4Q= Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3248aa5cf4eso3941153f8f.1 for ; Sun, 08 Oct 2023 15:32:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696804339; x=1697409139; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=agWXTU2Yij2i3Ap0SjGTJ2Rdepm57nHoh7x5LeUftzY=; b=SX27qiLOVIi1QLLW8N+bPwMGTsfeIk8PgBmPjzRSDScj2hrYEHrZDNjfJOWly68Q/O w+77ylJYQzjkEKo+zwhwPmv6mNHsekjZdxik+SQ36I8y/IPevhyjIcpQr1flXSm/zEuI Wld3hYOXOfLIS2qVvH8nDKvWG0k6gzEjUEWaTI4GA5U9QiWeS0xaCkXX9wNK7vUrT98c gU01JqvzVwlzzrKGAWfqPpmZbW41ZnzekwHsInHRopm3873jOtS3pRyWtEVQ2DrsmYYp jSP924sU+J56pD79jne89si9609acsoNR62bU9mcMeYzFlTo3gla/pgf85B1tlvvfi3I 4E2Q== X-Gm-Message-State: AOJu0YwgyDzoAYfUfAG9M5pHQQXiVOUUfytocQmatKqBXJpJudW/YGo7 teurOtJ5AugVzTTJscpkd2Y= X-Google-Smtp-Source: AGHT+IHzup7h6T7NZ16OB3Cn+I4QiSpINsYCf5NAnllIaELngKdXcHcsJ7bPpIosr4ciiDK3tenQ3A== X-Received: by 2002:adf:fa12:0:b0:321:685f:e0a7 with SMTP id m18-20020adffa12000000b00321685fe0a7mr12093445wrr.3.1696804338296; Sun, 08 Oct 2023 15:32:18 -0700 (PDT) Received: from snowbird (host86-164-181-115.range86-164.btcentralplus.com. [86.164.181.115]) by smtp.gmail.com with ESMTPSA id z3-20020a056000110300b0031c6581d55esm7752662wrw.91.2023.10.08.15.32.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 15:32:17 -0700 (PDT) Date: Sun, 8 Oct 2023 15:32:15 -0700 From: Dennis Zhou To: Hou Tao 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 Subject: Re: [PATCH bpf-next 1/6] mm/percpu.c: introduce alloc_size_percpu() Message-ID: References: <20231007135106.3031284-1-houtao@huaweicloud.com> <20231007135106.3031284-2-houtao@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231007135106.3031284-2-houtao@huaweicloud.com> X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 2783B100016 X-Stat-Signature: 8nnsa1bmssew3aks84oepkfgxirw8dag X-Rspam-User: X-HE-Tag: 1696804339-372178 X-HE-Meta: U2FsdGVkX18zfovMeV611XbTVXwf23Spnp/kXuN05Zr6HhYUq82UMJpD13p156xmR7ugmpLaXRz0sVSAOMQtBoiIkXS4IvEGuAdVuGfLP7WLrWyNaXuOQQa3x+2jZvfb7STcoIaI+eTPkc+tKerl4IJtGmbKoZwgC8PcM8iwW7UDqV8r94Ykvuu5IgXn/CjxSeKmjLK/0f4OKg9doLvwu9azOA+7dbkXUevFJVilyjxR3gvxBYNtlRKOV/qUvtg6xu68Pr431To7OF+w9kDOUFWKo4l3ubUcbLXJG8DoupjXK/htlorskc0o38NafaPwJswlNIFdQaGOc3C/8whvFj2f3jNFj7hlhttoxiB2jYA8hbzb4Pa2Mwcs7Stk/NDez5yIkywNZd+CZzLyd5RGMSqwN5oVNDLRIycLyFLCY14Nu84M/j6DeBx1GzwQrX8iSZupqM1bwVEKz2elnp9c3SbMFOoitUquN1pENqptpDtlRakXo/qrXI7HkvYMkwH4lKVSkmKwjIE6E2ue8e/xgOpVkOGDO4k2dSambahf/9cMiu0fnykthI4WgsjANSVyq8lPrTX/7sjlGjPxVc2rDBz44g5J6G36OQfUOjmx8CErTxnT+ScHKR7YUeC4G5U8or9HJSNbz7O0CeC0O66tJR9Ha6izMmiGQQMyVrtTZSJKXGDEYCxKr3Kuf2RLuteXWFAhCX1Pyu7HIS/lj4o3AzY0jEgEs3r2olV6YD0ufi4JrDj0quyScMjA9ZnmzaBlfy86hK+/6W78aPnPc5UzGHTWwQpmMcNbi5vBTpT81XrE56Gw7u2qa67CiUejc+yDw1D/QfvqwNhoeMBxZ/fKNVvb3Hf1IEEljX8S/DtHfi/nO3MQgyUibxqCX5NMUP9JubkVchai9laMAvx2fjbVsVdyH5L/I0nMu/aFr52J2ewE3/HErREsBdMjP4Wc2DTBrrW1t4fM9iLW3sLNROq sgj+U69+ WkzCxhgcj1b4XzJXpYBVpODgjZ/FVzko0lcVLNHrw62QY7zYWbbvWmGWThXFvRRqMfj14wd4FNlEEe394+Kld+OzpoCUpdd7CHyo+32XgN/dtD8/dCndItAGeUM+8F1kd8t/U8xjIWRS280VLJuIsnGUhQk8RXDmQKBn7fHU5L5KBz1OO8iOax6FntWF9LKxoFX8PxNh9ZWaLjb3tpLJRitRczz6g5m2N6YTvLoYDiYa4p61YJk9M7wJr5HQoRs+3XMnYBvTo61g+56EcIuszH8SV9FGbaglemJJBZx3XTpms95S9SVMsDWROkpWHQT0LAtRi/PHDBm5vbJU60TCf4hKt6YjeLNYEWkpqX9OHmsF3eEM3HEPFhglX2/laGtalnFtQRM6AZmRa/nhjzR+3hQPDcIcIm5m9lbbkOvMuTL5LKkuNLJOde1lNRUkKsOCJeJUzau1rt2M+tzcdbI12aC6qbtGHIAKCE5oF4ypk6jZRg6YLKV1nVnmC0SexpcA62pcL9/NM8SDdZ8Q0CuNyMdpWjTeVCP4q6X371L8zXvFiYkUApLMhqjqE/zx8d1yGqvWwD+k/LTmTlTiae6uhWJ8oZhZZdNrBbc3exa3MSsUsfgU= 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: 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. > + * @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()`. > + 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. > + return (end - bit_off) * PCPU_MIN_ALLOC_SIZE; > +} > + > /** > * free_percpu - free percpu area > * @ptr: pointer to area to free > -- > 2.29.2 > Thanks, Dennis