From patchwork Thu Jan 2 06:57:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Weikang Guo X-Patchwork-Id: 13924304 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 150C4E77188 for ; Thu, 2 Jan 2025 06:57:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 257986B007B; Thu, 2 Jan 2025 01:57:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 207D56B0083; Thu, 2 Jan 2025 01:57:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0CFB56B0085; Thu, 2 Jan 2025 01:57:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id DC1D46B007B for ; Thu, 2 Jan 2025 01:57:41 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 58AD11A1509 for ; Thu, 2 Jan 2025 06:57:41 +0000 (UTC) X-FDA: 82961604306.27.21E680B Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by imf06.hostedemail.com (Postfix) with ESMTP id 55BBD180004 for ; Thu, 2 Jan 2025 06:57:04 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mMMIBYuA; spf=pass (imf06.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=guoweikang.kernel@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=1735801026; 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=65RIXZaQDNZZs9h8XjHyHNBnS0NP8JFMbESmaGLNetQ=; b=NyYQ/9aWUUBJ3S4GWh3+lqXVy+TwqpegbadFx7wXmjuVXmcsMA7x8SW0BSizukPQDlVTio CcRfA2vMq2h5G9M3v0uGS0Vt4HJWQyvLDwQHDavHRPceJrUafxg5TLDoORcIQA41mwvnBA uFAOHQ+6LCY4bVBudLrO7lWfGtwBRjY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735801026; a=rsa-sha256; cv=none; b=6YrBgurKaYEYh61x/adreDwYOIbK4F5BP+OFifvvB50XGNl+bxRCwsuW+iTxFF8ODLmlF/ U81m7PkLCnDuT2ze5F7II7zBc69RhejnUNfjQEQSglkwa2+9Gx9w20oMAPR6bIvAKkfLxW gva2FOLxUwkQWbXqv4opd3c6PpJBrH4= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=mMMIBYuA; spf=pass (imf06.hostedemail.com: domain of guoweikang.kernel@gmail.com designates 209.85.214.175 as permitted sender) smtp.mailfrom=guoweikang.kernel@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-21644aca3a0so65716595ad.3 for ; Wed, 01 Jan 2025 22:57:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735801058; x=1736405858; 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=65RIXZaQDNZZs9h8XjHyHNBnS0NP8JFMbESmaGLNetQ=; b=mMMIBYuAJxG6tnKxcXZFWBDkBQvicH/IHVcBJ4vbKpHC1fP7XvackEWDyH2aWTWWP2 NRLd3Kt/O4DCY3x+8ySgWUxWixcqU5tfyvV0JjP9+Dr4y7ZT3Xdw3WX/Amrmq6/YE8y0 RSK+RLMZmyWrRD81ZDYvZblZBA5L0PLOu/YGAR8TAlp76qO4EvY/5zqM8wM3KYTbG+Wh 9DDOXaDVAGbftgrtGRYbhUqjeQ5ulX1rJyIDP/n/e1YEOAosla8ooh1WbSaA6CorRENN ngxZ31i3dOgjLFWZ96vSxMirjiInJpAk/oorOhUu1EoFQWX2y8Sv1G8BArXj8nyu+h46 AkdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735801058; x=1736405858; 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=65RIXZaQDNZZs9h8XjHyHNBnS0NP8JFMbESmaGLNetQ=; b=uGJBK6TN20BMRJKYDjIt/ZD+B81BMe4AFKAzC/9q+vS4LJU9PMT+iahx32Yu+Q2Qcj 6anvC+rS9oAyA78E+rwUbT0ckcupYUf4DXN4PPWi34y9IAaPau999gIf4GjE5bdwi9Al Of6hiJZ1d7tqNgIRzD1sHSx4mpdDyO3CIZ+maLDelIqRqzwC+I32hYdqTNTNbPwP1ZuY 75YhzYpawOk4uZ44g1kY99thb+VR/X17d5e6ECD3l5fzKsX1jmsgu2vV46NVohy8PE7a kdoMoVvI5a7MN4qh+WSHzeFt0cUrm+Sf5HyRrtBoBSFcGNqSfFi6viSBOOZf8L7BqcOh TvCw== X-Forwarded-Encrypted: i=1; AJvYcCVD6k1KYJpeqE22AprsjrGpJ6k2iQYQD0MzXDJalW+WUfE+PXXNP/krT74icE9LTsOW7j6AaPrUYQ==@kvack.org X-Gm-Message-State: AOJu0YzQOqespZKoED07AnqJ9LhPGjtueKjXJtydYL71fyUMsvk3oOsM ze4U0PafUBhvaC9MULqKZSstGmeMZzf6K9SU5tKsH6YI2IXjojA9 X-Gm-Gg: ASbGncs+6fa/47MM1aXhXN+Or4LIVShwJVoDuuyP3AOrUi+//u0wJLUjHGZL2m0Bik3 0Rndc6nkz0KVnrA75oDmlxb8i+FUXCEys8K1ZL4Zj6Z0j9WdvmVQ17/1KcFcp+I0zMiY6sa9ACD LrSk42A4FsHNx6AZ5X65YaYeUHSxpfZVdGn852X9i5fgM2tuw+P6wDyqDW9QhQrzZSKzD3DB97A ELL2rmOR41nZlCenzkri0rqiPb8CnROHWgV0zW0M3g+wCvM8QX66qxysNh+BFLh7tNqIlKzZbqn DRiu X-Google-Smtp-Source: AGHT+IH4RvnnZ4DnvjtG3s0DHdfZzhIzz3QnrHMNv2553ao1Wb0/9glnxupSYooV32hv2MHRVX8bBg== X-Received: by 2002:a17:902:c943:b0:215:72aa:693f with SMTP id d9443c01a7336-219e6e8bb89mr686026635ad.9.1735801058053; Wed, 01 Jan 2025 22:57:38 -0800 (PST) Received: from localhost.localdomain ([36.110.106.149]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9f6156sm213883775ad.209.2025.01.01.22.57.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jan 2025 22:57:37 -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] mm/memmap: Prevent double scanning of memmap by kmemleak Date: Thu, 2 Jan 2025 14:57:03 +0800 Message-Id: <20250102065704.647693-1-guoweikang.kernel@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 55BBD180004 X-Stat-Signature: gid1zudue3g5pnkesfptohhjkjnhb95w X-Rspam-User: X-HE-Tag: 1735801024-58492 X-HE-Meta: U2FsdGVkX18oplqEk1jYw8rZ2FmzYGhEhiYPDezHtyoVKS0zt1ANONiTLiT/yPvv7rAvn61Wi4jKQXtlHF4cuhkUJ/lEzeAnSUdIFAURo9V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QZN1ZSJg+bylsecXgklKsTdg7HRYO6bITzB5uTNV8CIY6/9XsbUuhz/MadX4TmrVsLXmEMlF4ik5x5K9o6yKMR95kguzytWsZ2l0qpNSvxx0aTv/KReSNB7yCx/FTJ61ymw6vFP5GxK7Ilbnh1prjDNAf9i8na6iQfIqgRYqvdgNmX/IymnttbXJn6o/cWwd+gyEnWWIQVucluMm/u7Pwy+0wlEJrr+jrdJDvHIP1ox+3alfE+jf9t1H3Ea9MBPfOY6Fp7ZpU35yZIh4K33MUjr5pW8CM0912/gS/cJ5Q== 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 --- include/linux/memblock.h | 8 +++++++- mm/mm_init.c | 8 ++++++-- mm/sparse-vmemmap.c | 20 ++------------------ 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 673d5cae7c81..b0483c534ef7 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -375,7 +375,13 @@ static inline int memblock_get_region_node(const struct memblock_region *r) } #endif /* CONFIG_NUMA */ -/* Flags for memblock allocation APIs */ +/* + * Flags for memblock allocation APIs + * MEMBLOCK_ALLOC_ANYWHERE and MEMBLOCK_ALLOC_ACCESSIBLE + * indicates wheather the allocation is limited by memblock.current_limit. + * MEMBLOCK_ALLOC_NOLEAKTRACE not only indicates that it does not need to + * be scanned by kmemleak, but also implies MEMBLOCK_ALLOC_ACCESSIBLE + */ #define MEMBLOCK_ALLOC_ANYWHERE (~(phys_addr_t)0) #define MEMBLOCK_ALLOC_ACCESSIBLE 0 #define MEMBLOCK_ALLOC_NOLEAKTRACE 1 diff --git a/mm/mm_init.c b/mm/mm_init.c index 24b68b425afb..71b58f5f2492 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -1580,6 +1580,10 @@ static void __init free_area_init_core(struct pglist_data *pgdat) } } +/* + * Kmemleak will explicitly scan mem_map by traversing all valid `struct *page`, + * so memblock does not need to be added to the scan list. + */ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align, phys_addr_t min_addr, int nid, bool exact_nid) { @@ -1587,11 +1591,11 @@ void __init *memmap_alloc(phys_addr_t size, phys_addr_t align, 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..b6ac9b1d4ff7 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -27,25 +27,10 @@ #include #include #include - +#include "internal.h" #include #include -/* - * 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. - * Uses the main allocators if they are available, else bootmem. - */ - -static void * __ref __earlyonly_bootmem_alloc(int node, - unsigned long size, - unsigned long align, - unsigned long goal) -{ - return memblock_alloc_try_nid_raw(size, align, goal, - MEMBLOCK_ALLOC_ACCESSIBLE, node); -} - void * __meminit vmemmap_alloc_block(unsigned long size, int node) { /* If the main allocator is up use that, fallback to bootmem. */ @@ -66,8 +51,7 @@ void * __meminit vmemmap_alloc_block(unsigned long size, int node) } return NULL; } else - return __earlyonly_bootmem_alloc(node, size, size, - __pa(MAX_DMA_ADDRESS)); + return memmap_alloc(size, size, __pa(MAX_DMA_ADDRESS), node, false); } static void * __meminit altmap_alloc_block_buf(unsigned long size,