Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1948938rdb; Mon, 9 Oct 2023 07:58:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEZUnS41fBs4Y2tzfCN/9Jq0urrtG2fjlSZVvZEY7mzgZXd3gM9ps2cojNQeyUXVyuzKez7 X-Received: by 2002:a05:6e02:156b:b0:352:6fff:ccb2 with SMTP id k11-20020a056e02156b00b003526fffccb2mr19144109ilu.16.1696863508037; Mon, 09 Oct 2023 07:58:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696863508; cv=none; d=google.com; s=arc-20160816; b=plTgBrnGILRJTldTzlIuypdM68xCzCZgT3O7DpjozWVacTt/JEVDKhNucVGouHomml 6rPSIpup8geU6BOhsY8YlQjg/jrLR3lzEhwsOG3eOAPkAW69yQ8FWVrXDFYbxpjRAjq2 bPRaiiiaS8W7DJ2aBTQTwGDSkPN24qytsIhJ89cL+hi2NAxjyaRCcfeTxa4eZxPlFXlz wWVynmn6s9phiP6bm3qqN0dlEGFcjiN9jYplh0bvA4hCifGE7exvQSzXCT8MhlpSd+tW 3rs31MzwRN+eXdGrZQIS+PCI9DDywD+NKS+SR13sFW8l1XF+/z/Gd4lGTm1TkLCT+3Id eAvg== 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=Et8Cavte9xeVyUOG1EtKKdbAQ0L2gs+H8z8zyirYU8o=; fh=wwGr4anhM3kpcO85TN/G/Arg4GwT9oL7sUCyXzkyrT8=; b=OcQSRT0uCYKYwx/DysPl2rvTxR9v384a7hw6BP+vX4muA8rpy5wdDRHBElXRhGyokF BFf3M9QVHBh+NhN023FGo7vWWCxbaeGyTXtXLsoKJs0dqNVqF+zTSIeJQP2xshSwu5Bz B759fLvnvIsXY4FNn8umSUmjY1MPr6xnPGvgXP9ZVMi2BPNIQtK9q/ASMGzdjlOdugE/ Z6OqPeZp7kZfdyHbUkadSP3cA56PSZcFLU0PoyTOoOKOAAbYJbehZBkVH88IXQXo2yPp DhVdeEhvyOirw7V18/c4q+4HAXPXi26qHzjJgd9aMEMDLTMqrcF0ktWoGzyorsDrAh5r pQYA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id bv124-20020a632e82000000b00564efb3bae6si9661372pgb.564.2023.10.09.07.58.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 07:58:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 91C478046BEA; Mon, 9 Oct 2023 07:58:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344625AbjJIO6Q (ORCPT + 99 others); Mon, 9 Oct 2023 10:58:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234568AbjJIO6P (ORCPT ); Mon, 9 Oct 2023 10:58:15 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9DD5A3 for ; Mon, 9 Oct 2023 07:58:14 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B250C433C7; Mon, 9 Oct 2023 14:58:13 +0000 (UTC) Date: Mon, 9 Oct 2023 15:58:10 +0100 From: Catalin Marinas To: Liu Shixin Cc: Patrick Wang , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 3/4] mm/kmemleak: fix partially freeing unknown object warning Message-ID: References: <20231008023317.3015699-1-liushixin2@huawei.com> <20231008023317.3015699-4-liushixin2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231008023317.3015699-4-liushixin2@huawei.com> X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 09 Oct 2023 07:58:25 -0700 (PDT) X-Spam-Level: ** On Sun, Oct 08, 2023 at 10:33:16AM +0800, Liu Shixin wrote: > delete_object_part() can be called by multiple callers in the same time. > If an object is found and removed by a caller, and then another caller > try to find it too, it failed and return directly. The secound part still > be recorded by kmemleak even if it has alreadly been freed to buddy. > With DEBUG on, kmemleak will report the following warning: > > kmemleak: Partially freeing unknown object at 0xa1af86000 (size 4096) > CPU: 0 PID: 742 Comm: test_huge Not tainted 6.6.0-rc3kmemleak+ #54 > Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 > Call Trace: > > dump_stack_lvl+0x37/0x50 > kmemleak_free_part_phys+0x50/0x60 > hugetlb_vmemmap_optimize+0x172/0x290 > ? __pfx_vmemmap_remap_pte+0x10/0x10 > __prep_new_hugetlb_folio+0xe/0x30 > prep_new_hugetlb_folio.isra.0+0xe/0x40 > alloc_fresh_hugetlb_folio+0xc3/0xd0 > alloc_surplus_hugetlb_folio.constprop.0+0x6e/0xd0 > hugetlb_acct_memory.part.0+0xe6/0x2a0 > hugetlb_reserve_pages+0x110/0x2c0 > hugetlbfs_file_mmap+0x11d/0x1b0 > mmap_region+0x248/0x9a0 > ? hugetlb_get_unmapped_area+0x15c/0x2d0 > do_mmap+0x38b/0x580 > vm_mmap_pgoff+0xe6/0x190 > ksys_mmap_pgoff+0x18a/0x1f0 > do_syscall_64+0x3f/0x90 > entry_SYSCALL_64_after_hwframe+0x6e/0xd8 > > Fix the problem by adding a new mutex lock to make sure all objects are > deleted sequentially in delete_object_part(). The kmemleak_lock is not > suitable here because there is a memory allocation with flag GFP_KERNEL. > > Fixes: 53238a60dd4a ("kmemleak: Allow partial freeing of memory blocks") > Signed-off-by: Liu Shixin > --- > mm/kmemleak.c | 9 ++++++++- > 1 file changed, 8 insertions(+), 1 deletion(-) > > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 54c2c90d3abc..ed497866361a 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -208,6 +208,8 @@ static struct rb_root object_tree_root = RB_ROOT; > 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); > +/* Serial delete_object_part() to ensure all objects are deleted correctly */ > +static DEFINE_MUTEX(delete_object_part_mutex); I mentioned in my reply on v1, do we actually need a lock at all? https://lore.kernel.org/r/ZRRhDgR/SIxbOCDk@arm.com I think we can rework the core a bit to only use kmemleak_lock. We could use GFP_ATOMIC when invoking __create_object(), it won't matter much as partial freeing is used rarely and only during boot. Alternatively, we could allocate two objects outside the lock and just pass them to a new, modified __create_object() function which would avoid re-allocation. The mutex looks simpler but then we need to be careful we only calls these functions in a sleepable context. -- Catalin