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 40DA8C433EF for ; Wed, 1 Jun 2022 10:24:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A58546B00B2; Wed, 1 Jun 2022 06:24:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A0B846B00B3; Wed, 1 Jun 2022 06:24:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D2436B00B4; Wed, 1 Jun 2022 06:24:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7C9C56B00B2 for ; Wed, 1 Jun 2022 06:24:41 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 38345338ED for ; Wed, 1 Jun 2022 10:24:41 +0000 (UTC) X-FDA: 79529283162.25.51D9280 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf16.hostedemail.com (Postfix) with ESMTP id DF551180079 for ; Wed, 1 Jun 2022 10:24:23 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id e66so1489824pgc.8 for ; Wed, 01 Jun 2022 03:24:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=/xae2cHy0Ag/cM7+bO/e+/rnzKdVsm0R+aXgJWwYsv8=; b=bqXZVclN2iOI5+a9KOMvt8fJ8VNdk0HLE3YEJAm6RA2yU2NJauOPO64SSR76neoxy/ b4X2JMtiqz8CzjV5BrPMdSxs97i7ssNqGZHqnRsrR7VGCSSXgDnk6DkN6PZqs48LP1ew zqEJ8+yiTMCvxi/tLS95nfIG/tbCepUIsyiowj0J4kypllRaey79NO6neOtZpbQUK2Sf JXEwM1yP4FiTxmY+l7Iqukx6iY4isM1co17dRaMR6dc3r2yH+SNMLAp6oNmdqfGv5KUo 7T5IL62CcIiObi84bnlZHxKBpoDtkNO/3dWZT7q3EWRFOcTThSOmeFqIRUnkrFtpgHAn mzuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=/xae2cHy0Ag/cM7+bO/e+/rnzKdVsm0R+aXgJWwYsv8=; b=cV08EYMb3laHXGCPXHHzeI2wmvFyv7mjHqjYhebsDjLWDKBOmg2ZXErlLfmkBvujXM 8dQN4lxeNk5QyNOiJwyfVbsHmHgnSkO58cC2WO9JNgqGF/kLeVHw+PrT/JcdutiLFr1V JeMETB42iyvyTaf/zdCoH05bFeghE+MCEHlOVUOw9L2SGImODHwByN0QZTytX2NoZbMp tfyqRAlGlWNSAz3vsmWZF/o4Tj6Ersyw+JZDpmpioTSkjYoLIs1DljYRnbwg7lpKCOHi /RCtpXXOpAmgOEXXor5ag9zza314XfBudVygKJWLYDnUUIr3zY267tJslutTYRxJynPA URIg== X-Gm-Message-State: AOAM530Y3bFmVNbFOAgKnnnCU7D6gMfydH9l4zmGULggBhOQdPP4yDc4 77NR+sUCw0pKghvB/FC6xh0= X-Google-Smtp-Source: ABdhPJxyOeb4YSMhduYjFv3Qq/yxj1KqB6M7rN2kLa3xLTtflpwL8GL1JYtrWwQniot6S1t+VBNQxw== X-Received: by 2002:a63:2c49:0:b0:3fb:1b5e:45c with SMTP id s70-20020a632c49000000b003fb1b5e045cmr26142346pgs.474.1654079079553; Wed, 01 Jun 2022 03:24:39 -0700 (PDT) Received: from [192.168.1.104] ([101.86.206.159]) by smtp.gmail.com with ESMTPSA id b11-20020a170902d50b00b0015e8d4eb276sm1170387plg.192.2022.06.01.03.24.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Jun 2022 03:24:39 -0700 (PDT) Message-ID: <99faf6b0-30bf-f87c-2620-1eafb4eac1ac@gmail.com> Date: Wed, 1 Jun 2022 18:24:34 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH] mm: kmemleak: check boundary of objects allocated with physical address when scan Content-Language: en-US To: Catalin Marinas Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, yee.lee@mediatek.com References: <20220531150823.1004101-1-patrick.wang.shcn@gmail.com> From: Patrick Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Stat-Signature: j35tgzr9g3fspdy7n3zm5jxdijqneinx X-Rspam-User: Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=bqXZVclN; spf=pass (imf16.hostedemail.com: domain of patrick.wang.shcn@gmail.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=patrick.wang.shcn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DF551180079 X-HE-Tag: 1654079063-893027 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 2022/6/1 00:29, Catalin Marinas wrote: > 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. Will do. > >> + 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. In this commit: 972fa3a7c17c(mm: kmemleak: alloc gray object for reserved region with direct map) The kmemleak_alloc_phys() function is called directly by passing physical address from devicetree. So I'm concerned that could __va() => __pa() convert always get the phys back? I thought check for __va(phys) might help, but it probably dosen't work and using IS_ERR is indeed inappropriate. We might have to store phys in object and convert it via __va() for normal use like: #define object_pointer(obj) \ (obj->flags & OBJECT_PHYS ? (unsigned long)__va((void *)obj->pointer) \ : obj->pointer) > >> @@ -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) */ Will do. > >> + 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); > } Right, much more thorough. Will do. Thanks, Patrick