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 1B4A0C433EF for ; Tue, 31 May 2022 16:29:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 817396B0072; Tue, 31 May 2022 12:29:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C2B86B0073; Tue, 31 May 2022 12:29:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B2F76B0074; Tue, 31 May 2022 12:29:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5C0876B0072 for ; Tue, 31 May 2022 12:29:21 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 396F0350A7 for ; Tue, 31 May 2022 16:29:21 +0000 (UTC) X-FDA: 79526573322.01.6D693AF Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf18.hostedemail.com (Postfix) with ESMTP id 144781C004F for ; Tue, 31 May 2022 16:29:00 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 30298B810B9; Tue, 31 May 2022 16:29:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AEC2AC385A9; Tue, 31 May 2022 16:29:16 +0000 (UTC) Date: Tue, 31 May 2022 17:29:13 +0100 From: Catalin Marinas To: Patrick Wang Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com Subject: Re: [PATCH] mm: kmemleak: check boundary of objects allocated with physical address when scan Message-ID: References: <20220531150823.1004101-1-patrick.wang.shcn@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220531150823.1004101-1-patrick.wang.shcn@gmail.com> X-Stat-Signature: 9cqme8tysbia7zd6xnq98kjcc9x4gjhh X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 144781C004F X-HE-Tag: 1654014540-681218 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: On Tue, May 31, 2022 at 11:08:23PM +0800, Patrick Wang wrote: > @@ -1132,8 +1135,13 @@ EXPORT_SYMBOL(kmemleak_no_scan); > void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, int min_count, > gfp_t gfp) > { > - if (PHYS_PFN(phys) >= min_low_pfn && PHYS_PFN(phys) < max_low_pfn) > - kmemleak_alloc(__va(phys), size, min_count, gfp); > + pr_debug("%s(0x%p, %zu, %d)\n", __func__, __va(phys), size, min_count); I'd print just phys here since that's the function argument. > + if (kmemleak_enabled && (unsigned long)__va(phys) >= PAGE_OFFSET && > + !IS_ERR(__va(phys))) > + /* create object with OBJECT_PHYS flag */ > + create_object((unsigned long)__va(phys), size, min_count, > + gfp, true); Do we still need to check for __va(phys) >= PAGE_OFFSET? Also I don't think IS_ERR(__va(phys)) makes sense, we can't store an error in a physical address. The kmemleak_alloc_phys() function is only called on successful allocation, so shouldn't bother with error codes. > @@ -1436,6 +1441,13 @@ static void kmemleak_scan(void) > dump_object_info(object); > } > #endif > + > + /* outside lowmem, make it black */ Maybe a bit more verbose: /* ignore objects outside lowmem (paint them black) */ > + if (object->flags & OBJECT_PHYS) > + if (PHYS_PFN(__pa((void *)object->pointer)) < min_low_pfn || > + PHYS_PFN(__pa((void *)object->pointer)) >= max_low_pfn) > + __paint_it(object, KMEMLEAK_BLACK); I'd skip the checks if the object is OBJECT_NO_SCAN (side-effect of __paint_it()) so that the next scan won't have to go through the __pa() checks again. It's also probably more correct to check the upper object boundary). Something like: if ((object->flags & OBJECT_PHYS) && !(object->flags & OBJECT_NO_SCAN)) { unsigned long phys = __pa((void *)object->pointer); if (PHYS_PFN(phys) < min_low_pfn || PHYS_PFN(phys + object->size) >= max_low_pfn) __paint_it(object, KMEMLEAK_BLACK); } Thanks. -- Catalin