From patchwork Thu Jun 9 12:49:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: patrick wang X-Patchwork-Id: 12875547 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 DE1CDC433EF for ; Thu, 9 Jun 2022 12:51:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 783B28D0017; Thu, 9 Jun 2022 08:51:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 732758D0015; Thu, 9 Jun 2022 08:51:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5ABE88D0017; Thu, 9 Jun 2022 08:51:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4B7988D0015 for ; Thu, 9 Jun 2022 08:51:00 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 293183584A for ; Thu, 9 Jun 2022 12:51:00 +0000 (UTC) X-FDA: 79558682280.08.A86D898 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf25.hostedemail.com (Postfix) with ESMTP id A4BCCA0074 for ; Thu, 9 Jun 2022 12:50:59 +0000 (UTC) Received: by mail-pj1-f49.google.com with SMTP id v11-20020a17090a4ecb00b001e2c5b837ccso26558764pjl.3 for ; Thu, 09 Jun 2022 05:50:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EATzTqJA26CcqIUbCI5V7nNL9XJq45wIulrxBrVLYks=; b=Nr4lPTaBeFU/+ThnnKI5ZNeBs4isqeW/eCkDJqq9HffaN3pRiipHzVVf6GA3vU2F1N biOi5JsicylGuvpsd0eDUQY224O0fKgUe5y1xiZlEt1W7saYBn3N+Lz/dg2IFGwg6oua L6a3UjjXwzwzTZhPk8qquS2/O0EztFAkXVCyFcYHmhujMF1bbOooo0+eU4foE0FCSoyI ZF5YjADR6p2sBkhSF5AKia1IzcomLMypmaKPVeJfu9ihTyAliSHRZVmv5rqgVPSP6RXf 6Q1STj3KtMI+Yrko16o5QcVpRVoUsQwd8PHdukPC7V13qVYxg4JqgqIK7pmEqX1EuCvI qzRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EATzTqJA26CcqIUbCI5V7nNL9XJq45wIulrxBrVLYks=; b=PWbwLXhFjr4VRY9USczW2bcT/C9OrjYY4Yucsj5ckpvbh3baRDAKthMwOomriud06N oRat3Sx3Wxn4SkmIjD3nS1k8zzYgSHMTmdn2PKs/Z3YjvaNrgqSw7+saviObI0BomDLf Mhc5DJu9CpQOdjch8CeoTL7MM73svMM+6onzwhED1X+dEq27mdrUeHJ946anpslAYqFy 2OV/vUe1RAGLqwA5FkMtXcGVoeG+1Hruv4Datvsenz89rru9I/XnYp9lX40MGm9HLYkW u+hQrwyvc5y8+zAp2X4/l3tyQVj+sAKvNwaRR2H3FREgcnPRSfSj29Dfl/qDoPuUYSyC oMNQ== X-Gm-Message-State: AOAM533HRsLizKFC5gsqa0ya5AobabSh5ZTrg7X7DyjmRCcbhWYlGH46 6fixgTsSSTKNkquJPmk00k4= X-Google-Smtp-Source: ABdhPJyqLFXK6TLZ5EjskzrqgxTJtqZHVVP6dGqh5/Fk1wWI6DsuvX5n6d53xH7ufOvPa1ojiTtwdw== X-Received: by 2002:a17:90a:3182:b0:1e3:530d:6994 with SMTP id j2-20020a17090a318200b001e3530d6994mr3341173pjb.69.1654779058703; Thu, 09 Jun 2022 05:50:58 -0700 (PDT) Received: from localhost ([101.86.206.203]) by smtp.gmail.com with ESMTPSA id b20-20020aa79514000000b0051b97828505sm17262562pfp.166.2022.06.09.05.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 05:50:58 -0700 (PDT) From: Patrick Wang To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com, patrick.wang.shcn@gmail.com Subject: [PATCH v3 1/3] mm: kmemleak: add OBJECT_PHYS flag for objects allocated with physical address Date: Thu, 9 Jun 2022 20:49:48 +0800 Message-Id: <20220609124950.1694394-2-patrick.wang.shcn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> References: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654779059; 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=EATzTqJA26CcqIUbCI5V7nNL9XJq45wIulrxBrVLYks=; b=j1HyMEDeGSaz0ui2roc0wx+Fp0A191FnekeKuCt+GlS8OEfZCnEiVZsvHcx98nlS/8BUsd MVDsbbKvLjLfqKzJem+QcB4uzi2/uuf06MmXmQ4YnPZvSzjfVOoB9k7Dc4T0ErdMu1Iviz wg4YbSHUsxHBkttrobpGxTHDGkJ+RZo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654779059; a=rsa-sha256; cv=none; b=zLRXhTIitJq74THgLUcbUn3KR3kzFi6JruA5QTXEfY6BH2rz3/+gWtcR+JislyIdigtSXk ztnHSjvEmrtjNOC0bNZBYo9qEIv6DJxV5ZVhPBWSiqLpoihHBbn33ZpZvZU1uk5QDh9faY CWmmhTYQUF4WUexSM3gC0Nsk/n9ylUw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Nr4lPTaB; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com X-Rspamd-Server: rspam11 X-Rspam-User: Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Nr4lPTaB; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf25.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com X-Stat-Signature: 5yfi9p5bnd3miaf3contbhtohrbm57g1 X-Rspamd-Queue-Id: A4BCCA0074 X-HE-Tag: 1654779059-260391 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: Add OBJECT_PHYS flag for object. This flag is used to identify the objects allocated with physical address.The create_object_phys() function is added as well to set that flag. And remove the min_count argument to kmemleak_alloc_phys() function, assume it's 0. Suggested-by: Catalin Marinas Signed-off-by: Patrick Wang Reviewed-by: Catalin Marinas --- drivers/of/fdt.c | 2 +- include/linux/kmemleak.h | 4 +-- mm/kmemleak.c | 44 ++++++++++++++++++------- mm/memblock.c | 14 ++++---- tools/testing/memblock/linux/kmemleak.h | 2 +- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index a8f5b6532165..2c677e84c3f5 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -529,7 +529,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node, pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n", uname, &base, (unsigned long)(size / SZ_1M)); if (!nomap) - kmemleak_alloc_phys(base, size, 0, 0); + kmemleak_alloc_phys(base, size, 0); } else pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n", diff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h index 34684b2026ab..df245d875b1e 100644 --- a/include/linux/kmemleak.h +++ b/include/linux/kmemleak.h @@ -29,7 +29,7 @@ extern void kmemleak_not_leak(const void *ptr) __ref; extern void kmemleak_ignore(const void *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, int min_count, +extern void kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) __ref; extern void kmemleak_free_part_phys(phys_addr_t phys, size_t size) __ref; extern void kmemleak_not_leak_phys(phys_addr_t phys) __ref; @@ -107,7 +107,7 @@ static inline void kmemleak_no_scan(const void *ptr) { } static inline void kmemleak_alloc_phys(phys_addr_t phys, size_t size, - int min_count, gfp_t gfp) + gfp_t gfp) { } static inline void kmemleak_free_part_phys(phys_addr_t phys, size_t size) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index a182f5ddaf68..85ed719e750d 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -172,6 +172,8 @@ struct kmemleak_object { #define OBJECT_NO_SCAN (1 << 2) /* flag set to fully scan the object when scan_area allocation failed */ #define OBJECT_FULL_SCAN (1 << 3) +/* flag set for object allocated with physical address */ +#define OBJECT_PHYS (1 << 4) #define HEX_PREFIX " " /* number of bytes to print per line; must be 16 or 32 */ @@ -574,8 +576,9 @@ static int __save_stack_trace(unsigned long *trace) * Create the metadata (struct kmemleak_object) corresponding to an allocated * memory block and add it to the object_list and object_tree_root. */ -static struct kmemleak_object *create_object(unsigned long ptr, size_t size, - int min_count, gfp_t gfp) +static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, + int min_count, gfp_t gfp, + bool is_phys) { unsigned long flags; struct kmemleak_object *object, *parent; @@ -595,7 +598,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, INIT_HLIST_HEAD(&object->area_list); raw_spin_lock_init(&object->lock); atomic_set(&object->use_count, 1); - object->flags = OBJECT_ALLOCATED; + object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0); object->pointer = ptr; object->size = kfence_ksize((void *)ptr) ?: size; object->excess_ref = 0; @@ -662,6 +665,20 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size, return object; } +/* Create kmemleak object which allocated with virtual address. */ +static struct kmemleak_object *create_object(unsigned long ptr, size_t size, + int min_count, gfp_t gfp) +{ + return __create_object(ptr, size, min_count, gfp, false); +} + +/* Create kmemleak object which allocated with physical address. */ +static struct kmemleak_object *create_object_phys(unsigned long ptr, size_t size, + int min_count, gfp_t gfp) +{ + return __create_object(ptr, size, min_count, gfp, true); +} + /* * Mark the object as not allocated and schedule RCU freeing via put_object(). */ @@ -728,11 +745,11 @@ static void delete_object_part(unsigned long ptr, size_t size) start = object->pointer; end = object->pointer + object->size; if (ptr > start) - create_object(start, ptr - start, object->min_count, - GFP_KERNEL); + __create_object(start, ptr - start, object->min_count, + GFP_KERNEL, object->flags & OBJECT_PHYS); if (ptr + size < end) - create_object(ptr + size, end - ptr - size, object->min_count, - GFP_KERNEL); + __create_object(ptr + size, end - ptr - size, object->min_count, + GFP_KERNEL, object->flags & OBJECT_PHYS); __delete_object(object); } @@ -1125,15 +1142,18 @@ EXPORT_SYMBOL(kmemleak_no_scan); * address argument * @phys: physical address of the object * @size: size of the object - * @min_count: minimum number of references to this object. - * See kmemleak_alloc() * @gfp: kmalloc() flags used for kmemleak internal memory allocations */ -void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, int min_count, - gfp_t gfp) +void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) { + pr_debug("%s(0x%pa, %zu)\n", __func__, &phys, size); + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) - kmemleak_alloc(__va(phys), size, min_count, gfp); + /* + * Create object with OBJECT_PHYS flag and + * assume min_count 0. + */ + create_object_phys((unsigned long)__va(phys), size, 0, gfp); } EXPORT_SYMBOL(kmemleak_alloc_phys); diff --git a/mm/memblock.c b/mm/memblock.c index e4f03a6e8e56..749abd2685c4 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -1345,8 +1345,8 @@ __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, * from the regions with mirroring enabled and then retried from any * memory region. * - * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for - * allocated boot memory block, so that it is never reported as leaks. + * In addition, function using kmemleak_alloc_phys for allocated boot + * memory block, it is never reported as leaks. * * Return: * Physical address of allocated memory block on success, %0 on failure. @@ -1398,12 +1398,12 @@ phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, */ if (end != MEMBLOCK_ALLOC_NOLEAKTRACE) /* - * The min_count is set to 0 so that memblock allocated - * blocks are never reported as leaks. This is because many - * of these blocks are only referred via the physical - * address which is not looked up by kmemleak. + * Memblock allocated blocks are never reported as + * leaks. This is because many of these blocks are + * only referred via the physical address which is + * not looked up by kmemleak. */ - kmemleak_alloc_phys(found, size, 0, 0); + kmemleak_alloc_phys(found, size, 0); return found; } diff --git a/tools/testing/memblock/linux/kmemleak.h b/tools/testing/memblock/linux/kmemleak.h index 462f8c5e8aa0..5fed13bb9ec4 100644 --- a/tools/testing/memblock/linux/kmemleak.h +++ b/tools/testing/memblock/linux/kmemleak.h @@ -7,7 +7,7 @@ static inline void kmemleak_free_part_phys(phys_addr_t phys, size_t size) } static inline void kmemleak_alloc_phys(phys_addr_t phys, size_t size, - int min_count, gfp_t gfp) + gfp_t gfp) { } From patchwork Thu Jun 9 12:49:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: patrick wang X-Patchwork-Id: 12875548 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 C94CBCCA473 for ; Thu, 9 Jun 2022 12:51:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FC0E8D0018; Thu, 9 Jun 2022 08:51:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5AD2E8D0015; Thu, 9 Jun 2022 08:51:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3FED78D0018; Thu, 9 Jun 2022 08:51:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2CFDF8D0015 for ; Thu, 9 Jun 2022 08:51:03 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id F129120F84 for ; Thu, 9 Jun 2022 12:51:02 +0000 (UTC) X-FDA: 79558682364.13.DB29E17 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by imf03.hostedemail.com (Postfix) with ESMTP id 9781220071 for ; Thu, 9 Jun 2022 12:51:02 +0000 (UTC) Received: by mail-pl1-f176.google.com with SMTP id s14so20164253plk.8 for ; Thu, 09 Jun 2022 05:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qYSkHm4LJuMMNn1EvNnF9NrFFi9PNazYuwVt4YiSVRY=; b=FfnTpZfhy6hP9hsslZaJR5322RLF2L1jQH2YQrGTnWPgzeHqPJLvBoOPzWAZU6oLaL RwFsLDcF1y0/H7TSQaVNeH2Z95xMMpKQBMM0zPoiBCJBizLwi/NxxssxXxkYgF20H+GG 6XGHlzWi3DCApLVxuKFCC1Rcd6wGkz3/FddDSkenGOD0hdCPKlMcrfBCHCM6RHNRX+VB A2sTitWcFr6d0IVuTDXAUiMQGF1dux8apzJyv0kow09ON/+TlI9fyF+HG1L37WidDxLI O4H0JptisrIZTaH47hpQ+xaQEZPrChUfC24O+NbGacmw113yNpUBYLkiZywU5VEWbMhV NGpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qYSkHm4LJuMMNn1EvNnF9NrFFi9PNazYuwVt4YiSVRY=; b=h3PGJ3A1CUwKQApa0qfN1XigPNqbGquzkcVqT+H8Xawit/ksSZAsm5OzQwXKsx9O34 lh3t+OTumYu7nx0HzxrLDypULXdXNYoVw4ZbR/Nulu04YqfjzGlEfvekPTk+jqCUie5B 4hSM6GPTH+lmZbEoobT47zYY+vFzMxaG7GjEkTX90vaSecvZxvh5QdTmvboaKwT3obUf Uc3orql+zSNBrUt+HJvbbwhsjsSwHGJiajn8SzILo4Ourz+Fqu9KvcqN6p2dQgV+RYIc PjhT+6JFduYBiWdiAA31DX/QpkTptmVkk40foZ1HV2Sn5KGQ9nXebjWFpd7r69QERgYu eKGg== X-Gm-Message-State: AOAM530RuVqA6uyxlBASYgcUwTgnWn12+YaL55Ff+Iiym2+w2kIz/Exj ByIPpE+4FQI/kRuavwr0um4= X-Google-Smtp-Source: ABdhPJxv0E/kRgVzmLCsZx/Iw45K0Bxs1cNpNhwiFMYHCEQ4WejaqNie1hDZH9MByvTpUzdlAakubA== X-Received: by 2002:a17:90b:4f88:b0:1e8:4013:e5f5 with SMTP id qe8-20020a17090b4f8800b001e84013e5f5mr3366307pjb.220.1654779061385; Thu, 09 Jun 2022 05:51:01 -0700 (PDT) Received: from localhost ([101.86.206.203]) by smtp.gmail.com with ESMTPSA id f6-20020a170902ab8600b0015e8d4eb25fsm16702195plr.169.2022.06.09.05.51.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 05:51:01 -0700 (PDT) From: Patrick Wang To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com, patrick.wang.shcn@gmail.com Subject: [PATCH v3 2/3] mm: kmemleak: add rbtree and store physical address for objects allocated with PA Date: Thu, 9 Jun 2022 20:49:49 +0800 Message-Id: <20220609124950.1694394-3-patrick.wang.shcn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> References: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654779062; a=rsa-sha256; cv=none; b=xV0I3M1h/ctj+cvr9FDZV4crjUQONykYwknLdULKsXw3o3PnaWAN/6g8edo5ExKMK4uSK/ L0xZRCcUzHPUUMHsMdU0atblIQlng2GQY4xCK9ODrBLchi+ilsHer5xuDNNOlGiBnEr4Jl CrNcWba8A8VrGvbe7mzynYMyHUeVTAM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FfnTpZfh; spf=pass (imf03.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=patrick.wang.shcn@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=1654779062; 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=qYSkHm4LJuMMNn1EvNnF9NrFFi9PNazYuwVt4YiSVRY=; b=lslJfrlKbCI6GaeexWlV+ZOSfnSD8+5GIbasPXDAB+N8Mn+weaz67zO+kem8pHsfmSWlUz a1NhEs+l+jWL0gnTkHzan/+ae+TVX8/h18oj6JDYeWeqZ4YxojMx8HXgsIj3neDC+FveEc fVq65EJZKpzMY9Xi72vWQQgmhOJ6H2c= X-Rspamd-Queue-Id: 9781220071 Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=FfnTpZfh; spf=pass (imf03.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.214.176 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: dqbnteooeufzbkzfgjhexesd44n76xn6 X-HE-Tag: 1654779062-268007 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: Add object_phys_tree_root to store the objects allocated with physical address. Distinguish it from object_tree_root by OBJECT_PHYS flag or function argument. The physical address is stored directly in those objects. And remove the unused kmemleak_not_leak_phys() function. Suggested-by: Catalin Marinas Signed-off-by: Patrick Wang Reviewed-by: Catalin Marinas --- Documentation/dev-tools/kmemleak.rst | 1 - include/linux/kmemleak.h | 4 - mm/kmemleak.c | 145 +++++++++++++++++---------- 3 files changed, 91 insertions(+), 59 deletions(-) diff --git a/Documentation/dev-tools/kmemleak.rst b/Documentation/dev-tools/kmemleak.rst index 1c935f41cd3a..5483fd39ef29 100644 --- a/Documentation/dev-tools/kmemleak.rst +++ b/Documentation/dev-tools/kmemleak.rst @@ -174,7 +174,6 @@ mapping: - ``kmemleak_alloc_phys`` - ``kmemleak_free_part_phys`` -- ``kmemleak_not_leak_phys`` - ``kmemleak_ignore_phys`` Dealing with false positives/negatives diff --git a/include/linux/kmemleak.h b/include/linux/kmemleak.h index df245d875b1e..6a3cd1bf4680 100644 --- a/include/linux/kmemleak.h +++ b/include/linux/kmemleak.h @@ -32,7 +32,6 @@ extern void kmemleak_no_scan(const void *ptr) __ref; extern void kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) __ref; extern void kmemleak_free_part_phys(phys_addr_t phys, size_t size) __ref; -extern void kmemleak_not_leak_phys(phys_addr_t phys) __ref; extern void kmemleak_ignore_phys(phys_addr_t phys) __ref; static inline void kmemleak_alloc_recursive(const void *ptr, size_t size, @@ -113,9 +112,6 @@ static inline void kmemleak_alloc_phys(phys_addr_t phys, size_t size, static inline void kmemleak_free_part_phys(phys_addr_t phys, size_t size) { } -static inline void kmemleak_not_leak_phys(phys_addr_t phys) -{ -} static inline void kmemleak_ignore_phys(phys_addr_t phys) { } diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 85ed719e750d..155f50e1a604 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -14,14 +14,16 @@ * The following locks and mutexes are used by kmemleak: * * - kmemleak_lock (raw_spinlock_t): protects the object_list modifications and - * accesses to the object_tree_root. The object_list is the main list - * holding the metadata (struct kmemleak_object) for the allocated memory - * blocks. The object_tree_root is a red black tree used to look-up - * metadata based on a pointer to the corresponding memory block. The - * kmemleak_object structures are added to the object_list and - * object_tree_root in the create_object() function called from the - * kmemleak_alloc() callback and removed in delete_object() called from the - * kmemleak_free() callback + * accesses to the object_tree_root (or object_phys_tree_root). The + * object_list is the main list holding the metadata (struct kmemleak_object) + * for the allocated memory blocks. The object_tree_root and object_phys_tree_root + * are red black trees used to look-up metadata based on a pointer to the + * corresponding memory block. The object_phys_tree_root is for objects + * allocated with physical address. The kmemleak_object structures are + * added to the object_list and object_tree_root (or object_phys_tree_root) + * in the create_object() function called from the kmemleak_alloc() (or + * kmemleak_alloc_phys()) callback and removed in delete_object() called from + * the kmemleak_free() callback * - kmemleak_object.lock (raw_spinlock_t): protects a kmemleak_object. * Accesses to the metadata (e.g. count) are protected by this lock. Note * that some members of this structure may be protected by other means @@ -195,7 +197,9 @@ static int mem_pool_free_count = ARRAY_SIZE(mem_pool); static LIST_HEAD(mem_pool_free_list); /* search tree for object boundaries */ static struct rb_root object_tree_root = RB_ROOT; -/* protecting the access to object_list and object_tree_root */ +/* search tree for object (with OBJECT_PHYS flag) boundaries */ +static struct rb_root object_phys_tree_root = RB_ROOT; +/* protecting the access to object_list, object_tree_root (or object_phys_tree_root) */ static DEFINE_RAW_SPINLOCK(kmemleak_lock); /* allocation caches for kmemleak internal data */ @@ -287,6 +291,9 @@ static void hex_dump_object(struct seq_file *seq, const u8 *ptr = (const u8 *)object->pointer; size_t len; + if (WARN_ON_ONCE(object->flags & OBJECT_PHYS)) + return; + /* limit the number of lines to HEX_MAX_LINES */ len = min_t(size_t, object->size, HEX_MAX_LINES * HEX_ROW_SIZE); @@ -380,9 +387,11 @@ static void dump_object_info(struct kmemleak_object *object) * beginning of the memory block are allowed. The kmemleak_lock must be held * when calling this function. */ -static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) +static struct kmemleak_object *__lookup_object(unsigned long ptr, int alias, + bool is_phys) { - struct rb_node *rb = object_tree_root.rb_node; + struct rb_node *rb = is_phys ? object_phys_tree_root.rb_node : + object_tree_root.rb_node; unsigned long untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); while (rb) { @@ -408,6 +417,12 @@ static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) return NULL; } +/* Look-up a kmemleak object which allocated with virtual address. */ +static struct kmemleak_object *lookup_object(unsigned long ptr, int alias) +{ + return __lookup_object(ptr, alias, false); +} + /* * Increment the object use_count. Return 1 if successful or 0 otherwise. Note * that once an object's use_count reached 0, the RCU freeing was already @@ -517,14 +532,15 @@ static void put_object(struct kmemleak_object *object) /* * Look up an object in the object search tree and increase its use_count. */ -static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) +static struct kmemleak_object *__find_and_get_object(unsigned long ptr, int alias, + bool is_phys) { unsigned long flags; struct kmemleak_object *object; rcu_read_lock(); raw_spin_lock_irqsave(&kmemleak_lock, flags); - object = lookup_object(ptr, alias); + object = __lookup_object(ptr, alias, is_phys); raw_spin_unlock_irqrestore(&kmemleak_lock, flags); /* check whether the object is still available */ @@ -535,28 +551,39 @@ static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) return object; } +/* Look up and get an object which allocated with virtual address. */ +static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias) +{ + return __find_and_get_object(ptr, alias, false); +} + /* - * Remove an object from the object_tree_root and object_list. Must be called - * with the kmemleak_lock held _if_ kmemleak is still enabled. + * Remove an object from the object_tree_root (or object_phys_tree_root) + * and object_list. Must be called with the kmemleak_lock held _if_ kmemleak + * is still enabled. */ static void __remove_object(struct kmemleak_object *object) { - rb_erase(&object->rb_node, &object_tree_root); + rb_erase(&object->rb_node, object->flags & OBJECT_PHYS ? + &object_phys_tree_root : + &object_tree_root); list_del_rcu(&object->object_list); } /* * Look up an object in the object search tree and remove it from both - * object_tree_root and object_list. The returned object's use_count should be - * at least 1, as initially set by create_object(). + * object_tree_root (or object_phys_tree_root) and object_list. The + * returned object's use_count should be at least 1, as initially set + * by create_object(). */ -static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias) +static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int alias, + bool is_phys) { unsigned long flags; struct kmemleak_object *object; raw_spin_lock_irqsave(&kmemleak_lock, flags); - object = lookup_object(ptr, alias); + object = __lookup_object(ptr, alias, is_phys); if (object) __remove_object(object); raw_spin_unlock_irqrestore(&kmemleak_lock, flags); @@ -574,7 +601,8 @@ static int __save_stack_trace(unsigned long *trace) /* * Create the metadata (struct kmemleak_object) corresponding to an allocated - * memory block and add it to the object_list and object_tree_root. + * memory block and add it to the object_list and object_tree_root (or + * object_phys_tree_root). */ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, int min_count, gfp_t gfp, @@ -631,9 +659,16 @@ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, raw_spin_lock_irqsave(&kmemleak_lock, flags); untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr); - min_addr = min(min_addr, untagged_ptr); - max_addr = max(max_addr, untagged_ptr + size); - link = &object_tree_root.rb_node; + /* + * Only update min_addr and max_addr with object + * storing virtual address. + */ + if (!is_phys) { + min_addr = min(min_addr, untagged_ptr); + max_addr = max(max_addr, untagged_ptr + size); + } + link = is_phys ? &object_phys_tree_root.rb_node : + &object_tree_root.rb_node; rb_parent = NULL; while (*link) { rb_parent = *link; @@ -657,7 +692,8 @@ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, } } rb_link_node(&object->rb_node, rb_parent, link); - rb_insert_color(&object->rb_node, &object_tree_root); + rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root : + &object_tree_root); list_add_tail_rcu(&object->object_list, &object_list); out: @@ -707,7 +743,7 @@ static void delete_object_full(unsigned long ptr) { struct kmemleak_object *object; - object = find_and_remove_object(ptr, 0); + object = find_and_remove_object(ptr, 0, false); if (!object) { #ifdef DEBUG kmemleak_warn("Freeing unknown object at 0x%08lx\n", @@ -723,12 +759,12 @@ static void delete_object_full(unsigned long ptr) * delete it. If the memory block is partially freed, the function may create * additional metadata for the remaining parts of the block. */ -static void delete_object_part(unsigned long ptr, size_t size) +static void delete_object_part(unsigned long ptr, size_t size, bool is_phys) { struct kmemleak_object *object; unsigned long start, end; - object = find_and_remove_object(ptr, 1); + object = find_and_remove_object(ptr, 1, is_phys); if (!object) { #ifdef DEBUG kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n", @@ -746,10 +782,10 @@ static void delete_object_part(unsigned long ptr, size_t size) end = object->pointer + object->size; if (ptr > start) __create_object(start, ptr - start, object->min_count, - GFP_KERNEL, object->flags & OBJECT_PHYS); + GFP_KERNEL, is_phys); if (ptr + size < end) __create_object(ptr + size, end - ptr - size, object->min_count, - GFP_KERNEL, object->flags & OBJECT_PHYS); + GFP_KERNEL, is_phys); __delete_object(object); } @@ -770,11 +806,11 @@ static void paint_it(struct kmemleak_object *object, int color) raw_spin_unlock_irqrestore(&object->lock, flags); } -static void paint_ptr(unsigned long ptr, int color) +static void paint_ptr(unsigned long ptr, int color, bool is_phys) { struct kmemleak_object *object; - object = find_and_get_object(ptr, 0); + object = __find_and_get_object(ptr, 0, is_phys); if (!object) { kmemleak_warn("Trying to color unknown object at 0x%08lx as %s\n", ptr, @@ -792,16 +828,16 @@ static void paint_ptr(unsigned long ptr, int color) */ static void make_gray_object(unsigned long ptr) { - paint_ptr(ptr, KMEMLEAK_GREY); + paint_ptr(ptr, KMEMLEAK_GREY, false); } /* * Mark the object as black-colored so that it is ignored from scans and * reporting. */ -static void make_black_object(unsigned long ptr) +static void make_black_object(unsigned long ptr, bool is_phys) { - paint_ptr(ptr, KMEMLEAK_BLACK); + paint_ptr(ptr, KMEMLEAK_BLACK, is_phys); } /* @@ -1007,7 +1043,7 @@ void __ref kmemleak_free_part(const void *ptr, size_t size) pr_debug("%s(0x%p)\n", __func__, ptr); if (kmemleak_enabled && ptr && !IS_ERR(ptr)) - delete_object_part((unsigned long)ptr, size); + delete_object_part((unsigned long)ptr, size, false); } EXPORT_SYMBOL_GPL(kmemleak_free_part); @@ -1095,7 +1131,7 @@ void __ref kmemleak_ignore(const void *ptr) pr_debug("%s(0x%p)\n", __func__, ptr); if (kmemleak_enabled && ptr && !IS_ERR(ptr)) - make_black_object((unsigned long)ptr); + make_black_object((unsigned long)ptr, false); } EXPORT_SYMBOL(kmemleak_ignore); @@ -1153,7 +1189,7 @@ void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) * Create object with OBJECT_PHYS flag and * assume min_count 0. */ - create_object_phys((unsigned long)__va(phys), size, 0, gfp); + create_object_phys((unsigned long)phys, size, 0, gfp); } EXPORT_SYMBOL(kmemleak_alloc_phys); @@ -1166,22 +1202,12 @@ EXPORT_SYMBOL(kmemleak_alloc_phys); */ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size) { - if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) - kmemleak_free_part(__va(phys), size); -} -EXPORT_SYMBOL(kmemleak_free_part_phys); + pr_debug("%s(0x%pa)\n", __func__, &phys); -/** - * kmemleak_not_leak_phys - similar to kmemleak_not_leak but taking a physical - * address argument - * @phys: physical address of the object - */ -void __ref kmemleak_not_leak_phys(phys_addr_t phys) -{ if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) - kmemleak_not_leak(__va(phys)); + delete_object_part((unsigned long)phys, size, true); } -EXPORT_SYMBOL(kmemleak_not_leak_phys); +EXPORT_SYMBOL(kmemleak_free_part_phys); /** * kmemleak_ignore_phys - similar to kmemleak_ignore but taking a physical @@ -1190,8 +1216,10 @@ EXPORT_SYMBOL(kmemleak_not_leak_phys); */ void __ref kmemleak_ignore_phys(phys_addr_t phys) { + pr_debug("%s(0x%pa)\n", __func__, &phys); + if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) - kmemleak_ignore(__va(phys)); + make_black_object((unsigned long)phys, true); } EXPORT_SYMBOL(kmemleak_ignore_phys); @@ -1202,6 +1230,9 @@ static bool update_checksum(struct kmemleak_object *object) { u32 old_csum = object->checksum; + if (WARN_ON_ONCE(object->flags & OBJECT_PHYS)) + return false; + kasan_disable_current(); kcsan_disable_current(); object->checksum = crc32(0, kasan_reset_tag((void *)object->pointer), object->size); @@ -1355,6 +1386,7 @@ static void scan_object(struct kmemleak_object *object) { struct kmemleak_scan_area *area; unsigned long flags; + void *obj_ptr; /* * Once the object->lock is acquired, the corresponding memory block @@ -1366,10 +1398,15 @@ static void scan_object(struct kmemleak_object *object) if (!(object->flags & OBJECT_ALLOCATED)) /* already freed object */ goto out; + + obj_ptr = object->flags & OBJECT_PHYS ? + __va((void *)object->pointer) : + (void *)object->pointer; + if (hlist_empty(&object->area_list) || object->flags & OBJECT_FULL_SCAN) { - void *start = (void *)object->pointer; - void *end = (void *)(object->pointer + object->size); + void *start = obj_ptr; + void *end = obj_ptr + object->size; void *next; do { From patchwork Thu Jun 9 12:49:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: patrick wang X-Patchwork-Id: 12875549 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 779CBCCA473 for ; Thu, 9 Jun 2022 12:51:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEB0A8D0019; Thu, 9 Jun 2022 08:51:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9B5E8D0015; Thu, 9 Jun 2022 08:51:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3BE28D0019; Thu, 9 Jun 2022 08:51:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B42EB8D0015 for ; Thu, 9 Jun 2022 08:51:05 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9500135843 for ; Thu, 9 Jun 2022 12:51:05 +0000 (UTC) X-FDA: 79558682490.06.0965A96 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf24.hostedemail.com (Postfix) with ESMTP id EF1EF180070 for ; Thu, 9 Jun 2022 12:51:04 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id 184so5793315pga.12 for ; Thu, 09 Jun 2022 05:51:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KwSRcViehwfAYeRTO3icyIMXkIxuHW5eq5bOlxud59g=; b=NOqJF7S/WByS4u665bimD3BKbv7ahHS1l6RPUqD0TU5ms64qlM4kF5yV7KJC89nNHv a17tntzu44cTUIoP8sSPb6j87kVUhI/unLOiNJNhaNEwSlOy/1wkKEwuGoFp20WZ6Eeh rRbbhfDIC5738q4r1eRNRkWTD1pmIwbvuv7zZGApID/OoUv7fr+jIwIoVwIFa19T6U2U Fxfpm7umKjzBN7pyUYtp2+7NdwJ/axPsWaCm6tA//usReZMhlnpEtOeCE1hiolMUKL64 D7uSlc3ofD5f6FnlZZTkZH8+mxYZiXrURz6eEM3Y9p7ATd+yBSQ57nLxaMv2skT8IT6o Ftxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KwSRcViehwfAYeRTO3icyIMXkIxuHW5eq5bOlxud59g=; b=KUK9smDoZO069/ewVZlkrRjSO3mui2wvRszq0mRV1/j/mI8XaHiL/26EkYVX5Jenvb uWAHLMUgsJj9+oQ3dJ1dg2mnzazu49JubDqlgWlzCn20Z3ArdVDtZRTzaJ9BZk2m1w5G q0abVaFCuyug/BLaSUl1sKHujgamsdi5o2BZzl9bvIXoOQEDLcP+PYX98HbxIyBiK0R+ itdNQV7gUS76hSA6ggNpp4N6xzcwOlBPRflcFhWkq/G/gIW8FTx1by7uscvzZaNjdLR8 I3x4jcjpWmLsIsAuMx0KcRyPbfe5O2v/ROiXIB1BXDn0dAXG/nm7l2dk0uVvl3JOJKdQ wVTg== X-Gm-Message-State: AOAM530GEbbugxd2i+W3W0mROYXbj8m/lOdIvuvSAXKkajk9sy2jzTbb 7879nuUX/yyWl40NwjMOPoo= X-Google-Smtp-Source: ABdhPJwKD7D9ZF1N62MeDGI5N2JXZ4WpXk3reHXe99z40A6BXMvU8ygtsV1HC2LEIcBh9MHFwuZQTw== X-Received: by 2002:a63:82c6:0:b0:3fe:3600:f816 with SMTP id w189-20020a6382c6000000b003fe3600f816mr7343707pgd.155.1654779064103; Thu, 09 Jun 2022 05:51:04 -0700 (PDT) Received: from localhost ([101.86.206.203]) by smtp.gmail.com with ESMTPSA id a190-20020a6390c7000000b003c62fa02f08sm17294112pge.43.2022.06.09.05.51.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jun 2022 05:51:03 -0700 (PDT) From: Patrick Wang To: catalin.marinas@arm.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com, patrick.wang.shcn@gmail.com Subject: [PATCH v3 3/3] mm: kmemleak: check physical address when scan Date: Thu, 9 Jun 2022 20:49:50 +0800 Message-Id: <20220609124950.1694394-4-patrick.wang.shcn@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> References: <20220609124950.1694394-1-patrick.wang.shcn@gmail.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654779065; a=rsa-sha256; cv=none; b=gjDRBi5IkQEC4XOx8clHgUeea2a6e69TFtipNa5mkuY8gF9FyoHZq5P2IWGvkb5SozqbEm vAVfiMN97NPAC4zK+qrf9/pHHUNTXFxdVEA38kNNx7t/EMT4MQmx2DCWU3L7yadu/jhCcG ryBMzDl0VDNZhhOoyqKvLdQYcABAH5Q= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="NOqJF7S/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654779065; 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=KwSRcViehwfAYeRTO3icyIMXkIxuHW5eq5bOlxud59g=; b=k6yWPhcRTYpkF6kTm4URz73RSanufRqNfC3flrXHENvjhVLW+wLFfCtyMn6f7+dwPZf5K3 335MI2wY/jO5FBn7Ub5V/Gvwt7bHMdoMU/Y2tzauQzM0YoWoZB7HMDXLo39p04Mm5BLbfV TzfoGl3tSNu4T750csrZqMZAlgfHoTM= X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EF1EF180070 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="NOqJF7S/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com X-Stat-Signature: b4qqz5jyd3huesnwhe45qfq6he68fct7 X-HE-Tag: 1654779064-203032 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: Check the physical address of objects for its boundary when scan instead of in kmemleak_*_phys(). Fixes: 23c2d497de21 ("mm: kmemleak: take a full lowmem check in kmemleak_*_phys()") Suggested-by: Catalin Marinas Signed-off-by: Patrick Wang Reviewed-by: Catalin Marinas --- mm/kmemleak.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mm/kmemleak.c b/mm/kmemleak.c index 155f50e1a604..387d6fa402c6 100644 --- a/mm/kmemleak.c +++ b/mm/kmemleak.c @@ -1184,7 +1184,7 @@ void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp) { pr_debug("%s(0x%pa, %zu)\n", __func__, &phys, size); - if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) + if (kmemleak_enabled) /* * Create object with OBJECT_PHYS flag and * assume min_count 0. @@ -1204,7 +1204,7 @@ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size) { pr_debug("%s(0x%pa)\n", __func__, &phys); - if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) + if (kmemleak_enabled) delete_object_part((unsigned long)phys, size, true); } EXPORT_SYMBOL(kmemleak_free_part_phys); @@ -1218,7 +1218,7 @@ void __ref kmemleak_ignore_phys(phys_addr_t phys) { pr_debug("%s(0x%pa)\n", __func__, &phys); - if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) + if (kmemleak_enabled) make_black_object((unsigned long)phys, true); } EXPORT_SYMBOL(kmemleak_ignore_phys); @@ -1493,6 +1493,17 @@ static void kmemleak_scan(void) dump_object_info(object); } #endif + + /* ignore objects outside lowmem (paint them black) */ + if ((object->flags & OBJECT_PHYS) && + !(object->flags & OBJECT_NO_SCAN)) { + unsigned long phys = object->pointer; + + if (PHYS_PFN(phys) < min_low_pfn || + PHYS_PFN(phys + object->size) >= max_low_pfn) + __paint_it(object, KMEMLEAK_BLACK); + } + /* reset the reference count (whiten the object) */ object->count = 0; if (color_gray(object) && get_object(object))