Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1242645lfc; Wed, 1 Jun 2022 12:51:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwikdvI5b+MppfMQ5LJZtO1m8zt4f7Qez9GmRFHk6VeBKc1vXJh1EtmAFCpZMT+/xRbIRMY X-Received: by 2002:a65:428c:0:b0:3db:822e:2163 with SMTP id j12-20020a65428c000000b003db822e2163mr862694pgp.466.1654113098056; Wed, 01 Jun 2022 12:51:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654113098; cv=none; d=google.com; s=arc-20160816; b=CcQrb0uM7Gkmz9wW3mLkGjGctVBavLb4BbjKNpO0lLscX7gbeeNoIjSHleUSDMZ9eT C0u8Qxt9supFmP4qMFyJOyDil52F2CvaBhROF3VS+DgMvMNrMUFDIQRNu7yxV4lcx176 t6xx5VCV9pkuqSIRxw9imEeVFGpAQWsGqNVkm3tKtdmKb5jBoRdmcq5SGyQuFFBhKt1k qzGTjTZFWKjfl2oeAzgFxVQ1dWlSC+5HG6QYcfpbwlWAMr4fKcguJgkROXRpG5G9HPvr 3XBcy8S3upSFMyvVOgWOz2jx8Evwzc4LZbyzOaxS4jDF+JyPuFYeS0tXna3unzLunGHF SIEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Y3Y0Vm8AM3ctFMj4ucxyloDFr8HMFczx+ISP7NfoXWc=; b=WMPT6QJO+dyyN3jffbfkHLDWuSPWApNOA700zxiXWDjA4YwFLOAhHdWnLlnkhbNbsg wVmQ7uxxhGj2KTNZ8zGP9U3zlSC5X/p4Hm0HZFT6mis/TICNn/c/DKCra1b8c4V1iuv9 d9IuAM9u7GYeQmxFHGvMynnQec5cptu/wk3aIGt6MmMClLuvLb6WE0PHZF5vAhvzlWZD +nhA97nRQE+GuGeExa8dA8qvuAo4DNapFtNcS/gilckqY3J2r4IJW2cGdwMS86OGPlbu x9kEaPIiszMcBreEfoeh5F3OhaHEIYSPYdFk5hZIkgIhN5DI5YNGvykXU+RIm1/qg77n KTCQ== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id a10-20020a631a0a000000b003fbc2010a93si3399936pga.463.2022.06.01.12.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 12:51:38 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0DE8F1F89BE; Wed, 1 Jun 2022 12:13:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346161AbiEaQ3W (ORCPT + 99 others); Tue, 31 May 2022 12:29:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52718 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244326AbiEaQ3T (ORCPT ); Tue, 31 May 2022 12:29:19 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA33862CEE for ; Tue, 31 May 2022 09:29:18 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 85C5C60BA7 for ; Tue, 31 May 2022 16:29:18 +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-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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