From patchwork Mon Jan 6 02:11:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weikang Guo X-Patchwork-Id: 13926853 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 6B348E77197 for ; Mon, 6 Jan 2025 02:12:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F3A386B0089; Sun, 5 Jan 2025 21:12:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EEA266B008A; Sun, 5 Jan 2025 21:12:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB1986B008C; Sun, 5 Jan 2025 21:12:03 -0500 (EST) 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 C00CB6B0089 for ; Sun, 5 Jan 2025 21:12:03 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 47AD3C12E0 for ; Mon, 6 Jan 2025 02:12:03 +0000 (UTC) X-FDA: 82975401726.09.21DD419 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf13.hostedemail.com (Postfix) with ESMTP id 77C8E20007 for ; Mon, 6 Jan 2025 02:12:01 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WEvxBqtI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=guoweikang.kernel@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736129521; a=rsa-sha256; cv=none; b=Kf0krTtWi7abu5Bi+Nb+iokM0OcKXn3juG30hiL+AjTx7xi7eEHPBND7igjvlKrqIl9dRz /SLDklqxj+8WHe9ppROdZ2VxolPamKoXrgrcldE71wHHWjMOJD8O7btXHlBJAtmUCbC5F1 UQU7MjvkXEaSxW4l5xoC22N8k6insHM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=WEvxBqtI; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=guoweikang.kernel@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736129521; 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=wNQ3+OhGA/okuRaWCqvdwEXXhxzwpukELt61tmiGCsE=; b=ks0nuH7GvjYfe/TJlYZf39Jn8lf/PvpbgvCtnOpcQFVmGX0VkbpTNxhSf5vn/gVl9XjYwC wobb6bRB3fZCVyigt3VIK3dhXhYmM3pn/Bm+zkhD3Yk7Wua9D925CASvcq2zCWgSxPu6vl AWAlEni+pjLmwXLDp7icRO/p+6ezRwg= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2163b0c09afso194412165ad.0 for ; Sun, 05 Jan 2025 18:12:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736129520; x=1736734320; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=wNQ3+OhGA/okuRaWCqvdwEXXhxzwpukELt61tmiGCsE=; b=WEvxBqtIu+iRMz+PST4RfaRCevYwl84XflS0O8s3hOb3mRLUbCmDAJ7P7gdNLq+Qsv ESoy8CMpjOwgNssH7Q4W8GTaueDBkxahRzT1vCHSiAGItoELlyh4V9B/AtJg7diCuNUt zZwo0+gSPwIrQDHtMptTRdn8spZArDmR0XC4qZLEHCc1wgxFEg1p47nqeZtkSd4xo0yt HVSiUvieDJJmd4TCofo6w1vW4fTzAVNuJaJEukps0sTJ5PZI942fTaxRAtAtqffmWnh5 ZZZx0jUxbsLOQb9SdY413mUFpxR2LCj+PFvCzTKMyCABp7xla5n2HDOGlol3dWd1E7bC shlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736129520; x=1736734320; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wNQ3+OhGA/okuRaWCqvdwEXXhxzwpukELt61tmiGCsE=; b=UBwieohQ7PXPVHk/Ton9gh7iivwXUI6wzLQw2Qb2GhjqHo2kqEC3vFM8Ro7f+Ez240 G7kvnUwot7f+HwBg0Y6+gI2C/DpROle8EtG6l4pr1uq/2sxxtkdvAaUNtdegSKybLd80 b81WSJKkQqj4k52E5SqnxheL7sCOFriGKjo9YVhClMvRFmI5mX5RtN4u/aNYCgrmS2cg Dr9aVQxdPsKHj7BksBn5q/sYAO+T3y05WZj+LMnAZ4kEP4u6HJkLwFldCv1MtMVu5RXX StT1MY+jQNllNmif/rIyH1Ibtp5UPYsUN6KTvPaoQdGSKNHNLwpbzeFmJ5t8YGGzvp2Q 42Sg== X-Forwarded-Encrypted: i=1; AJvYcCXYK3Q1tVrHgZH/4TrHZsOw7bxysAsie6rV519MoRhX/TUaK+1L6epzTMeb6OUwV5GAvOxxawGMOA==@kvack.org X-Gm-Message-State: AOJu0YwnO6NUA+2Guk/0wlnvibbcylCwS6HAT6ec3+v26wuusO/wsBK7 nc3vQc9qEh/hfGstJWBeaa1W0iWFkBKYNT6MOu9M5d3EpplTKMmd X-Gm-Gg: ASbGnct/5WPlLQIHDYS6h2iRQZr7+sv/ddvgrLz1GJ9OzJkfXOVj9hF5aD6lEhf2JGj INH43LOB1F3VCnB3vBwyhcc6LHlRnfqx1jfP/XDwStkxqqLAvCleWy8FZkePE3J9KYLKY8+FCN0 N8HmnyxPBrS4/B7eeKnO3SmNwGXFJmUokWibztJ1OeIgRZV8Yf3wkIGH54GcNPLxE1E8PBwQ9R0 o7dg0kjzMmsRsTSS6vXE5nitMvcIr+MENrv3A2+fu7GwKJfMFbl6sFeqK2hUR1WSfn8fFcxa1TY bj9c X-Google-Smtp-Source: AGHT+IEum2NYKYJvlaXzzJeKplrYOuspfxlytqzExwVSEwDvU8NodICTE3AW+84TVD7JBcwX2dbHeQ== X-Received: by 2002:a05:6a21:8cc2:b0:1e1:adfe:c43 with SMTP id adf61e73a8af0-1e5e045874amr89051901637.9.1736129519936; Sun, 05 Jan 2025 18:11:59 -0800 (PST) Received: from localhost.localdomain ([36.110.106.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad8dbbebsm30215336b3a.114.2025.01.05.18.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jan 2025 18:11:59 -0800 (PST) From: Guo Weikang To: Catalin Marinas , Mike Rapoport , Andrew Morton Cc: Guo Weikang , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] mm/memmap: Prevent double scanning of memmap by kmemleak Date: Mon, 6 Jan 2025 10:11:25 +0800 Message-Id: <20250106021126.1678334-1-guoweikang.kernel@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 77C8E20007 X-Stat-Signature: cga5u4tm633jot5x6ni1jfr1atae7bmn X-HE-Tag: 1736129521-102645 X-HE-Meta: U2FsdGVkX19kEzmfFyKrZqTyn5luReeY70zhvYGZX/j0KtSBCb8mX7P3Nv8jsewwRHtYnJs7DZkXRdPpbWB5u5V8coF3wFS82v96CeOHt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FWeN6Ykh/IFyuttFd7HiPcu5893AV5QdoCKJoCaCVBJv05QcGxsvgZ7Y3gglYNSX0/9fiSpszXJ45jjMykm63HiDAvfTntfLP/QuOz/0w48Vn39+Qn7z1QTkdYjJ58GeSASkvO//ijHug9FE0IQKlrfExl/JfIcH1BKXKE2TLbwwdVfr5UHL+2uhWnsYs38v+e+c7nFSZCgEP3q7Yu4YJ/LJZcKJ2kh49uRmWlVBxdEmZU4wp37uLq92I00MGdNu965Xq603KFoKexUxombAmT/WMDLD8iKQ1rz3ePDquQArfKhmAYTdtWI9TJYI7UjXMSeWIBLWC+xVx+/KB4wZterkiY 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: kmemleak explicitly scans the mem_map through the valid struct page objects. However, memmap_alloc() was also adding this memory to the gray object list, causing it to be scanned twice. Removes memmap_alloc() from the scan list and adds a comment to clarify the behavior. Link: https://lore.kernel.org/lkml/CAOm6qn=FVeTpH54wGDFMHuCOeYtvoTx30ktnv9-w3Nh8RMofEA@mail.gmail.com/ Signed-off-by: Guo Weikang Reviewed-by: Catalin Marinas --- v1 -> v2: Fix CI: Recover __earlyonly_bootmem_alloc that is marked with __ref to prevent init section mismatch WARNING. Links: https://lore.kernel.org/oe-kbuild-all/202501021601.jub4p3EM-lkp@intel.com/ v2 -> v3: Rebase on latest mm-everything and modified some minor comments. Reviewed-by: Catalin Marinas Links: https://lore.kernel.org/lkml/20250103110150.1352226-1-guoweikang.kernel@gmail.com/ --- include/linux/memblock.h | 4 ++++ mm/mm_init.c | 8 ++++++-- mm/sparse-vmemmap.c | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index dee628350cd1..e79eb6ac516f 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -378,6 +378,10 @@ static inline int memblock_get_region_node(const struct memblock_region *r) /* Flags for memblock allocation APIs */ #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0) #define MEMBLOCK_ALLOC_ACCESSIBLE 0 +/* + * MEMBLOCK_ALLOC_NOLEAKTRACE avoids kmemleak tracing. It implies + * MEMBLOCK_ALLOC_ACCESSIBLE + */ #define MEMBLOCK_ALLOC_NOLEAKTRACE 1 /* We are using top down, so it is safe to use 0 here */ diff --git a/mm/mm_init.c b/mm/mm_init.c index 24b68b425afb..2630cc30147e 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1585,13 +1585,17 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align, { void *ptr; + /* + * Kmemleak will explicitly scan mem_map by traversing all valid + * `struct *page`,so memblock does not need to be added to the scan list. + */ if (exact_nid) ptr = memblock_alloc_exact_nid_raw(size, align, min_addr, - MEMBLOCK_ALLOC_ACCESSIBLE, + MEMBLOCK_ALLOC_NOLEAKTRACE, nid); else ptr = memblock_alloc_try_nid_raw(size, align, min_addr, - MEMBLOCK_ALLOC_ACCESSIBLE, + MEMBLOCK_ALLOC_NOLEAKTRACE, nid); if (ptr && size > 0) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index cec67c5f37d8..3287ebadd167 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -31,6 +31,8 @@ #include #include +#include "internal.h" + /* * Allocate a block of memory to be used to back the virtual memory map * or to back the page tables that are used to create the mapping. @@ -42,8 +44,7 @@ static void * __ref __earlyonly_bootmem_alloc(int node, unsigned long align, unsigned long goal) { - return memblock_alloc_try_nid_raw(size, align, goal, - MEMBLOCK_ALLOC_ACCESSIBLE, node); + return memmap_alloc(size, align, goal, node, false); } void * __meminit vmemmap_alloc_block(unsigned long size, int node)