From patchwork Thu Jan 12 15:53:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13098266 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 0FD44C63797 for ; Thu, 12 Jan 2023 15:53:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A7E108E0005; Thu, 12 Jan 2023 10:53:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A07158E0001; Thu, 12 Jan 2023 10:53:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 837038E0005; Thu, 12 Jan 2023 10:53:38 -0500 (EST) 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 67FF28E0001 for ; Thu, 12 Jan 2023 10:53:38 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4D1E11C5D21 for ; Thu, 12 Jan 2023 15:53:38 +0000 (UTC) X-FDA: 80346592116.21.77BDB84 Received: from mail-qt1-f169.google.com (mail-qt1-f169.google.com [209.85.160.169]) by imf22.hostedemail.com (Postfix) with ESMTP id A3DF7C0004 for ; Thu, 12 Jan 2023 15:53:36 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Cs86hcYw; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673538816; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0GttG9cGo2fGw7Ie6+QYs6E6KpCITpSOtaS4vLsszc8=; b=5oqFgQ50uDbcEUBYdSVaxszQ18dWtrqtTK7abYeNb14xGhI1c3RqbSY7n9vFyoeNWmPkdh 57z1heMhR4WVug7ZMKL4f2XhSSnkEp6oU94UxfBGNOa+8MQFkQyJ+7XtD5F91UhWGeB7Ph cX5WMDhq2q43iIDXRJLcnQrKqjn0/ME= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Cs86hcYw; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.160.169 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673538816; a=rsa-sha256; cv=none; b=f4DIxO+vI0/HEIK3AA1dJH6WlQ3ocMTObKnWmpAby+k3IAcQ8TqVYPBXqNR5o8EEX99D8b 1tsZTdQ++HrHkNZvjOjc/BkQt8LXsAdaFu0/yOfOvkAONzZDdqWDGSf3lrESQIT15F+V2G rfiw98OluyW6fAqSoKbXfs4yOJEiGQ8= Received: by mail-qt1-f169.google.com with SMTP id jr10so9500000qtb.7 for ; Thu, 12 Jan 2023 07:53:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0GttG9cGo2fGw7Ie6+QYs6E6KpCITpSOtaS4vLsszc8=; b=Cs86hcYwXW1kyT1Ar3qnL76ZCbnEz0PuLFPljmDEP02SjXoY1Z3RjcF2o1cZcrfit5 QsRJZD+pHkF5Lp65YJU8iZVYq4d7OoijI3kadHA/0fI4edzur6B4RGyBR3E4vF662I/j wNEecnuesci1bDHq9WwQtoP5yUf7oWgsfjkZY6J0nUR1rgflYRRAS4sWd2NT04xkGNd1 biHCt1RQu1AIGF++m/rRURQXtxdvBzCNjbZDBLpYJd7CRbWoeaX/X1UlYq/ECAWaQ3II //PX23Rf28b9O8k3iB6GI8CVbc4v8x/BmlLTWUreFOpaZbasteZ4LBpLbnySid+akRmc d17g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0GttG9cGo2fGw7Ie6+QYs6E6KpCITpSOtaS4vLsszc8=; b=ikRncnEveqR0GCEhnGiTJzX8+oniQnBSm58PGSwLVrIWQ+npPA/sqXYuNAkl61y1DB vZGUIDPfi9WR4ZKalADQZdK83KnAKd9iPpMrfaK75EkDSpPk5Ir5xjsF4iH6qTmINgaN w5EM0KBd7sGXGF944nig6mwEDiPzjDsyoTlKNkQnz+94qkMrjafgltUvADS3VH1zoqjQ Orru1SbNC/8wFmQPee5sXZAjMEJODNy9ng1Z3TNB/Qa22l+30GhSXfiMWRW9i4Fqj/5f 78Twv0HiIj8ES6K0cUv518rGEDFFztNU0kzytgbaiAUcowkcvFGTBmgOQRE8cvCRW3CA cqrA== X-Gm-Message-State: AFqh2kr3yeR7h3cqC5ewa8ce00kA8XmfWjQw9BergYU6EeH8PxytROUp nB6olnoRnS+OOlnEafO4jKU= X-Google-Smtp-Source: AMrXdXujjvIkIMxOnezoY+bqzXiyRl7Otg+3lH6xNrFMGBGSGIg4NYiYb1pdv7pwlHe5tHHIls2pwQ== X-Received: by 2002:ac8:7409:0:b0:3b1:691f:3d7a with SMTP id p9-20020ac87409000000b003b1691f3d7amr5867423qtq.68.1673538815891; Thu, 12 Jan 2023 07:53:35 -0800 (PST) Received: from vultr.guest ([173.199.122.241]) by smtp.gmail.com with ESMTPSA id l17-20020ac848d1000000b003ab43dabfb1sm9280836qtr.55.2023.01.12.07.53.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 07:53:35 -0800 (PST) From: Yafang Shao To: 42.hyeyoo@gmail.com, vbabka@suse.cz, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, dennis@kernel.org, cl@linux.com, akpm@linux-foundation.org, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, roman.gushchin@linux.dev Cc: linux-mm@kvack.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 02/11] mm: percpu: introduce percpu_size() Date: Thu, 12 Jan 2023 15:53:17 +0000 Message-Id: <20230112155326.26902-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230112155326.26902-1-laoar.shao@gmail.com> References: <20230112155326.26902-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: apoc37gxz8773m5traswnhxtr55zcrkf X-Rspam-User: X-Rspamd-Queue-Id: A3DF7C0004 X-Rspamd-Server: rspam06 X-HE-Tag: 1673538816-802064 X-HE-Meta: U2FsdGVkX19uVNIYFm1RXmyDRz9elphZGKNd8XjvlNjKaNnS4LM/SEBg6EUUEcCpf7E6CIYVUhtQ8FSQyjRsIT6l6gzkUSLebTgpcaYOKAPMWYR4swqOSbZCx2XE56bX5Oopf6cNAgeSxH50ioPp4QQ8PJy13zPG66i8chXHSz3SBaMDxIguy8fi1JIn3rGiP4CdH9jMAg4B16ub04E+7jWEK76EV2Fy9XwknYVI5zFf8+oTKTpF+rL0rAvuxX+9Pn/dE2E4hOJ+xSaOTupZFeYJm75hOwdn1rACfLa2rwHF6YVjhMn7fmct7PFejINXurpTW0QXRTOyxbkP9jfwqSR5KEa9yJb0BsTS2tpH+UnOl7aLP9nHqXv6KQkKdXhS8Hs0DkicsZgQSWBtoUQg9MusOSpUedv+ZgAPAI2LFtCQrMnXSpgl3xUQ2Tjk15XOfkdoAk3XjK1JEbGQQ/WNOyH6vzDB94b8z9p5V6h+b9K61Y/BsUxyVrzY7VwAMQrgTfSSWTrlOwn6UmNmoSRg5h7o5xZ/3bNyXAuw19lSEu1wHffkxMQ2L2e9Y7peCI/Q3p12cdGuTsuSKricwtod+GVlOiShUkZAJeu/VMeoMak768PXHiYzrjgX9beTEbEpJu9bzQMHsxIAaleE6Tm9R4ubQ6aqVvJYNbGuAumY7Nrro+LzMuzl59eGArFSt99A5LBSp2lKku+tSaJWg9U3m1POdUfXBAN1fd3aNCM/AqUyhxX6obmfJEALF5cTGTiIyXwrwIk+eC+IctY88Zw0ulFlfEs/em+VG3iG2HsPsrVtUF2i4k+A9eDpIlE6tSFR0rf3uzvCVzB+VGRtPj3EsLT4QncITXj8+XswGixuD6KV+wKa2rxGYXFjlOlTebAJBYGsCUkaaYlG6Eh0PGtTOXx/cmCUfvvU5lBxuwYw/3Kl9eVnaZs1nZQ1ixO6speIlOOnCHsJGiN/lQ7WbZF Xvj5SLl0 4l4+I5/bc98Xysr0IkTHEufRUiS896mXB10EPZ/dSBZjFL6voE/fnCxP0GnOeNQkuwcm6m//0mIHaxG/pgsQFS42z8U5vXIf8+yGf+wHMdSKR4uHlckixWSd++L4lnOt8N8BbaeKUzglEYUC4+euJGNNR/rq/rV8SfFhOjp/sOp+LTRW6Aka8ktCEr5w7SEg1S4D0PTnpQ82ALk7bEcDP4pBbDAp608BvHnq8a7WeNdKNYHKroOlDXZtbNkR4v4fKqDPhX9adpFfSmuJySG9bx5JcNZ2HwA62wHPW 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: Introduce a new helper percpu_size() to report full size of underlying allocation of a percpu address. Signed-off-by: Yafang Shao --- include/linux/percpu.h | 1 + mm/percpu.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/percpu.h b/include/linux/percpu.h index 1338ea2..7be4234 100644 --- a/include/linux/percpu.h +++ b/include/linux/percpu.h @@ -137,5 +137,6 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size, __alignof__(type)) extern unsigned long pcpu_nr_pages(void); +extern size_t percpu_size(void __percpu *ptr); #endif /* __LINUX_PERCPU_H */ diff --git a/mm/percpu.c b/mm/percpu.c index acd78da..5580688 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -2302,6 +2302,41 @@ void free_percpu(void __percpu *ptr) } EXPORT_SYMBOL_GPL(free_percpu); +/** + * percpu_size - report full size of underlying allocation of percpu addr + * @ptr: pointer to percpu area + * + * CONTEXT: + * Can be called from atomic context. + */ +size_t percpu_size(void __percpu *ptr) +{ + int bit_off, bits, end, off, size; + struct pcpu_chunk *chunk; + unsigned long flags; + void *addr; + + if (!ptr) + return 0; + + addr = __pcpu_ptr_to_addr(ptr); + + spin_lock_irqsave(&pcpu_lock, flags); + chunk = pcpu_chunk_addr_search(addr); + off = addr - chunk->base_addr; + bit_off = off / PCPU_MIN_ALLOC_SIZE; + + /* find end index */ + end = find_next_bit(chunk->bound_map, pcpu_chunk_map_bits(chunk), + bit_off + 1); + spin_unlock_irqrestore(&pcpu_lock, flags); + + bits = end - bit_off; + size = bits * PCPU_MIN_ALLOC_SIZE; + + return pcpu_obj_full_size(size); +} + bool __is_kernel_percpu_address(unsigned long addr, unsigned long *can_addr) { #ifdef CONFIG_SMP