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 3CFE0C71155 for ; Fri, 20 Jun 2025 04:35:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0DC06B007B; Fri, 20 Jun 2025 00:35:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CE5A96B0089; Fri, 20 Jun 2025 00:35:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C22B16B008A; Fri, 20 Jun 2025 00:35:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B362E6B007B for ; Fri, 20 Jun 2025 00:35:20 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 5935C1D8BB9 for ; Fri, 20 Jun 2025 04:35:20 +0000 (UTC) X-FDA: 83574514800.07.169079F Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) by imf28.hostedemail.com (Postfix) with ESMTP id 14A44C0008 for ; Fri, 20 Jun 2025 04:35:16 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=htCmZpxy; spf=pass (imf28.hostedemail.com: domain of hao.ge@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=hao.ge@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750394118; a=rsa-sha256; cv=none; b=6L2UYggTUR68UZJmx9TvKsoxVgp+LP1/O9ukqQ4QpvmlgLCR+aDHDd4ju6x6LFee+7PFAw H57O/mXXk4x6hYX1LMQ2AyLZb3LetTTvmznsr8MVezLkbeYf6h1FgcvDFuEO8ApZ/Zy4KL Lx18ItqClkGhnljLhwm5FkovgN6c9oM= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=htCmZpxy; spf=pass (imf28.hostedemail.com: domain of hao.ge@linux.dev designates 95.215.58.181 as permitted sender) smtp.mailfrom=hao.ge@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750394118; 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:references:dkim-signature; bh=dCfbh59L3+a418zRxE1MPwsIl8LJAqGJTQFz5jc97nQ=; b=YTSc+eWhNKOD06R+XBxkaTV8DoKw24PSgBTMQjti0YfRifjLtZ752mFZWNHH1c5xQu9gns +dmlQaIbZ/YQ1CRTzwDzaKUuXgD1rk4Ax92OXfyCD/waY6mL9FsSOFsFKoGMYTHktIujyn WvRwvstx/5VCv7RQxR/hgecAl6DwHkY= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1750394114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=dCfbh59L3+a418zRxE1MPwsIl8LJAqGJTQFz5jc97nQ=; b=htCmZpxyuQhreLswoptrNPUNZm1Rih+g7uUvx74tWdnGvDM44zBHd2BNP6rQK5oze8Fe7w H1JuIO3OXUzW9SaP3DpHobS5ghvLu951EJ1AyzJicKFQnlbu/Qa3jJOGITGqYN4pL1yrho d5HO1chiEYqYicsAzk5FAOAjo4gma4o= From: Hao Ge To: Catalin Marinas , Suren Baghdasaryan , Kent Overstreet , Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Hao Ge , Hao Ge Subject: [PATCH v2] mm/alloc_tag: Fix the kmemleak false positive issue in the allocation of the percpu variable tag->counters Date: Fri, 20 Jun 2025 12:34:55 +0800 Message-Id: <20250620043455.2320765-1-hao.ge@linux.dev> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 14A44C0008 X-Stat-Signature: 69cexahphytkpo89p69bejjy759tujws X-Rspam-User: X-HE-Tag: 1750394116-455239 X-HE-Meta: U2FsdGVkX18zpYg7WnKRvbOlYccVVn23eafqB7VnoGuvP6sy0sBhsels0nHQY44flzCf9IaKibMaibDLurgDA/4Ib97ux66If47GRIdVVt1VMZMIS1G99d7W6xkMZIG2V4a6J5PzgU9tUcmf3Re/Gs0q/Kmzdj5EppyC+gO4vPZhwEbUQoqF33E2qxNWUWtR7/CsDj4FVJY3keWQh7sotBb7GkDP4TjvH1+EpjQoeyJGJsEdGFlT20Jnp4EVWBK5PfKr5jqJkSw20/abU4FMb89h75UrWLXs0riXCQrzzxGX0vDPI7LhvMami4Q6VGd8Os+NhfEKwv+c47KHsIrUUEmkY4qujQiLyyyY4KtrshHEVwVU/XnX649lEVlO9NooCzOYrJy/0N91bZE6QQ9MYDNemwQvtgciVEFSwr3m+w6bjV8ahfhpmpVRt8TB3uNs/eI6EsR2tM+ij7+xCWT1fmSyKSR+I2GLQMoDUk0sWoSUxW/PCzHgHgniDJM9ie0iE8sBoREt4UAy2DyeT2l4p/FcgQjDV3XlYqax+3Ej5zrnYMKd+i6NyeDotkdXsNshzy2AKKMWBis3WcFkopyWVL3MaVcFHyA4cAyXbK9ygctls72t95GVAMc0s+pyVLe9DrD1jc6YmZo4qFgvX9XZRLV0UydOauADZohLP9g0dylODdwyJ9LhyVUg35GkYQ4rMLnIoo1KaVhlcGi2rE9UnOoEGPQIEOkIA2aY57m+LMISy6RRAxc0UBV5NeDokGw1X7m82Z8Q5gT1AYFvBVCeaASyzvKowdqYEj5lpGW7YvL9UFap/6AQTStuWn9v2V3dum04QGysJK8TwXypkoXr9DQ2ZBUhsaiitL+KVMP2XvdysHP6bLu9XWyw++Rse82/uftES1ykcZLoC0ZTIdv06KMdALCrPb8ipqBtJbQWNc6CXNsPSTnDqPABImo8SbBmKDRaR3eJKZl2uwqImuj zSgfI+Sx hGLccwv8kU/Tz+wzUOhZvfBVEj1lrXlzRQNm1l+YccOwPnDt9mz5c6vJjfLj5UQk/Cnsfxuux9RsvP90D6KLDEiPZtxlxQmCIOjLk9xqc7RYvFo37cWJEt0S524rxttMHMdSIUoLlHOkjOX+w6EB83VI6WXavwKaC46rSe3D4f8TlfUOFy9u0EOWWpH3togdl9hPRHTcpo5cMNKM5AGchWNDtH1rC79hEqOAhi1sfCrAefHnHgjPC+fEBiTkWOGYHRDYpqG9COxUAHzYjA7KKlvUq6fArR9dIm/Qz 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: List-Subscribe: List-Unsubscribe: From: Hao Ge When loading a module, as long as the module has memory allocation operations, kmemleak produces a false positive report that resembles the following: unreferenced object (percpu) 0x7dfd232a1650 (size 16): comm "modprobe", pid 1301, jiffies 4294940249 hex dump (first 16 bytes on cpu 2): 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace (crc 0): kmemleak_alloc_percpu+0xb4/0xd0 pcpu_alloc_noprof+0x700/0x1098 load_module+0xd4/0x348 codetag_module_init+0x20c/0x450 codetag_load_module+0x70/0xb8 load_module+0xef8/0x1608 init_module_from_file+0xec/0x158 idempotent_init_module+0x354/0x608 __arm64_sys_finit_module+0xbc/0x150 invoke_syscall+0xd4/0x258 el0_svc_common.constprop.0+0xb4/0x240 do_el0_svc+0x48/0x68 el0_svc+0x40/0xf8 el0t_64_sync_handler+0x10c/0x138 el0t_64_sync+0x1ac/0x1b0 This is because the module can only indirectly reference alloc_tag_counters through the alloc_tag section, which misleads kmemleak. However, we don't have a kmemleak ignore interface for percpu allocations yet. So let's create one and invoke it for tag->counters. Fixes: 12ca42c23775 ("alloc_tag: allocate percpu counters for module tags dynamically") Signed-off-by: Hao Ge --- v2: In version 1, I mistakenly wrote "ignore" as "igonore", and I'm sorry for causing trouble to Andrew. --- include/linux/kmemleak.h | 1 + lib/alloc_tag.c | 8 +++++++- mm/kmemleak.c | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h index 93a73c076d16..c6898a874b8d 100644 --- a/include/linux/kmemleak.h +++ b/include/linux/kmemleak.h @@ -28,6 +28,7 @@ extern void kmemleak_update_trace(const void *ptr) __ref; extern void kmemleak_not_leak(const void *ptr) __ref; extern void kmemleak_transient_leak(const void *ptr) __ref; extern void kmemleak_ignore(const void *ptr) __ref; +extern void kmemleak_ignore_percpu(const void __percpu *ptr) __ref; extern void kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp) __ref; extern void kmemleak_no_scan(const void *ptr) __ref; extern void kmemleak_alloc_phys(phys_addr_t phys, size_t size, diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index d48b80f3f007..3a74d63a959e 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -10,6 +10,7 @@ #include #include #include +#include #define ALLOCINFO_FILE_NAME "allocinfo" #define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag)) @@ -632,8 +633,13 @@ static int load_module(struct module *mod, struct codetag *start, struct codetag mod->name); return -ENOMEM; } - } + /* + * Avoid a kmemleak false positive. The pointer to the counters is stored + * in the alloc_tag section of the module and cannot be directly accessed. + */ + kmemleak_ignore_percpu(tag->counters); + } return 0; } diff --git a/mm/kmemleak.c b/mm/kmemleak.c index da9cee34ee1b..8d588e685311 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1246,6 +1246,20 @@ void __ref kmemleak_transient_leak(const void *ptr) } EXPORT_SYMBOL(kmemleak_transient_leak); +/** + * kmemleak_ignore_percpu - similar to kmemleak_ignore but taking a percpu + * address argument + * @ptr: percpu address of the object + */ +void __ref kmemleak_ignore_percpu(const void __percpu *ptr) +{ + pr_debug("%s(0x%px)\n", __func__, ptr); + + if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr)) + make_black_object((unsigned long)ptr, OBJECT_PERCPU); +} +EXPORT_SYMBOL_GPL(kmemleak_ignore_percpu); + /** * kmemleak_ignore - ignore an allocated object * @ptr: pointer to beginning of the object -- 2.25.1