Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1250046pxv; Fri, 25 Jun 2021 08:37:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx0C/hz1a+xtEnDuVc331JNua2Vi0iXHUeiTlkw5eP+p9lrQ9d4CYwK76AGcD+t39o4s8qq X-Received: by 2002:a5d:8453:: with SMTP id w19mr6776888ior.105.1624635466418; Fri, 25 Jun 2021 08:37:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624635466; cv=none; d=google.com; s=arc-20160816; b=C3IfiiSBRHPz9MN80bY0P97HTVcCUR4OseotLz5QDR5iiAnlYJSsIlGzB/NIX1uXtU OH7Al7f3v8FSFqLoPrYz4HfmYmhEDAUnv+ah1gzrTzQzKZPslnSxepWfmADcYX4RbLbo rfRh7LRzsPxcUY/Ps9Okb8+f0OrzfMz1daBkFVvyC5bWlF1WQoK/F2XfZRz6B7Of/HQD pQOGFYnfuLDD8VRai+OGEM9qfiyTmPVUmDSQ7T+rFDCMo1keIblhrBmUJEOuCsto2iMM 46EfJO+j81ak3VR3/cbA96GkQP3HRVV5o+gpAgk+4GRwS09WHL51BJ5MKlOOwZmBG4wT cEYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=y6YthVsh8dQjzhz84cmChJODP2HFEjc+K0QQHEGVnrE=; b=dXLrw1zNaKpjfBNFEZPcSy+Gyzf+11E6jcDz8hzoGMtGeqxQi70IVMGzHt1lo+aQMQ Q/BAMsUXQREHO4hNIl5tXTzMv3JN292l2YtMrjw5D4UubqlazPNCpIosg8dBYBMTuMSe 2Mj7lqF/CW5eFm6nmImwgM+7VOfXOVIZfkpzHFdXGZ8HLKKTTmjKEFj1TNRwtbuKkHja eyJZjD2Evzbfn63RafIGcc2B5/T1rhE5rtJcHNXELc8+jBoBdcOiMvetehGg06CotlJB TNrMa+kNzTtBbolV1WS211hUuDng8VZINJcPpMSgXFenjQvJFQwbN/SB4twe0lSe2/wV msDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=T2BwYjUY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l17si1356709jas.38.2021.06.25.08.37.34; Fri, 25 Jun 2021 08:37:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=T2BwYjUY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbhFYPjU (ORCPT + 99 others); Fri, 25 Jun 2021 11:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229630AbhFYPjT (ORCPT ); Fri, 25 Jun 2021 11:39:19 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 291A5C061574 for ; Fri, 25 Jun 2021 08:36:58 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id t9so7755099qtw.7 for ; Fri, 25 Jun 2021 08:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=y6YthVsh8dQjzhz84cmChJODP2HFEjc+K0QQHEGVnrE=; b=T2BwYjUYm8dCG6g1wWe1slEjX7RbA8+gxd4uTXeIpz6MAqZBFjXxO0iI79vx0SE1SM XPYTNq+OMcqMEJMfp75JkHVzfwTSRKyvafXN3ce6757htf4jUVoMPll5xM7QLCuqwfq4 MgN8O/SiibJV+/qfM7Q+odwqYOr9ZkXGs9CG4YpJ6YWIv6yqcNpUSyM2hQIGN9G/l3Sg TS4ePF+Sqj02U5zfYCZ+irvBvHX5G1HOwLm1AIQzBOpgLj1uCrez3v+3OgSEEU8SdV27 RrUXMrvKGbMRnPKH7bjkGtQtvgIelcb7/U7g9ICmJl3B9i91kJD6oT3oc8NapNCVnEnd jSQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=y6YthVsh8dQjzhz84cmChJODP2HFEjc+K0QQHEGVnrE=; b=gaNcJSRmW4g+YwmVARXA7KB/OfaTu6p8OIzECqRgReV12sZsTN8glEGIbnq0P/nx6b 65XMDq5dkQ8nTM1mcvfi+2lloqGk0r8feo7GJmjW2y9J9u58mTusk8+DZ/6vpzcPEYvl bSaKnH3t1lGut+uvIROWebOXUV2CDa572wK22kesaVpkepE/bS19jjpust8yBr18i4x4 BlKQVJrZMImEHo3M95wuzyS+Wp6ljzR1rtTPNubnyyA09civFMd0krVP6all+YEHEpXV IDNiBILHTSE7aD2VYgiA5nbM2DS+dtOadte9Mcmn/aK4+vW9DIsAqz0ckVE8/+CBLoxr wnPQ== X-Gm-Message-State: AOAM533Q3XTzG18C1aNdE7yJ3fz18MYDvECNkDNhXJaluKR65cDUqbQk bylNi9HPVCCGm5lgMM/lbElLvl7sY/tP9V8Cyo6GlQ== X-Received: by 2002:ac8:7616:: with SMTP id t22mr9735295qtq.43.1624635417070; Fri, 25 Jun 2021 08:36:57 -0700 (PDT) MIME-Version: 1.0 References: <20210625150132.GF20835@arm.com> In-Reply-To: From: Dmitry Vyukov Date: Fri, 25 Jun 2021 17:36:44 +0200 Message-ID: Subject: Re: kmemleak memory scanning To: Rustam Kovhaev Cc: Catalin Marinas , Andrew Morton , Linux-MM , LKML , Greg Kroah-Hartman Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 25, 2021 at 5:28 PM Rustam Kovhaev wrote: > > Hi Catalin, > > On Fri, Jun 25, 2021 at 04:01:33PM +0100, Catalin Marinas wrote: > > On Thu, Jun 24, 2021 at 10:36:50AM -0700, Rustam Kovhaev wrote: > > > On Wed, Jun 16, 2021 at 11:25:22AM -0700, Rustam Kovhaev wrote: > > > > On Tue, Jun 15, 2021 at 07:15:24AM +0200, Dmitry Vyukov wrote: > > > > > On Mon, Jun 14, 2021 at 10:31 PM Rustam Kovhaev wrote: > > > > > > > > > > > > hello Catalin, Andrew! > > > > > > > > > > > > while troubleshooting a false positive syzbot kmemleak report i have > > > > > > noticed an interesting behavior in kmemleak and i wonder whether it is > > > > > > behavior by design and should be documented, or maybe something to > > > > > > improve. > > > > > > apologies if some of the questions do not make sense, i am still going > > > > > > through kmemleak code.. > > > > > > > > > > > > a) kmemleak scans struct page (kmemleak.c:1462), but it does not scan > > > > > > the actual contents (page_address(page)) of the page. > > > > > > if we allocate an object with kmalloc(), then allocate page with > > > > > > alloc_page(), and if we put kmalloc pointer somewhere inside that page, > > > > > > kmemleak will report kmalloc pointer as a false positive. > > > > > > should we improve kmemleak and make it scan page contents? > > > > > > or will this bring too many false negatives? > > > > > > > > > > Hi Rustam, > > > > > > > > > > Nice debugging! > > > > > I assume lots of pages are allocated for slab and we don't want to > > > > > scan the whole page if only a few slab objects are alive on the page. > > > > > However alloc_pages() can be called by end kernel code as well. > > > > > I grepped for any kmemleak annotations around existing calls to > > > > > alloc_pages, but did not find any... > > > > > Does it require an explicit kmemleak_alloc() after allocating the page > > > > > and kmemleak_free () before freeing the page? > > > > > > > > hi Dmitry, thank you! > > > > yes, as Catalin has pointed out, there are a few places where we call > > > > kmemleak_alloc()/kmemleak_free() explicitly in order for the pages to be > > > > scanned, like in blk_mq_alloc_rqs() > > > > > > > > > If there are more than one use case for this, I guess we could add > > > > > some GFP flag for this maybe. > > > > > > > > and this way kernel users won't have to use kmemleak fuctions mentioned > > > > above including some or most kmemleak_not_leak() calls and basically > > > > kmemleak will be kind of "transparent" to them? and they will only need > > > > to use the GFP flag to instruct kmemleak to scan the page contents? > > > > it sounds like a good idea to me.. > > > > > > > > > > i've been thinking about this and it seems like in the scenario where we > > > want kmemleak to scan only some part of the page, we will have to either > > > do separate alloc_page() calls with different flags or use > > > kmemleak_scan_area() to limit the memory scan area. maybe this approach > > > won't simplify things and will produce more code instead of reducing it > > > > Since page allocation is not tracked by kmemleak, you can always do an > > explicit kmemleak_alloc() call with a smaller size than a full page. > > > right, but if i understood Dmitry's idea correctly, he was thinking > about using a new GFP flag, like GFP_KMEMLEAK, and burying > kmemleak_alloc() in page allocator I don't have a strong opinion either way. Especially since we have only 1 use case so far.