Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3924568rdg; Wed, 18 Oct 2023 09:39:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtJtrt9ANDfBQjC4OK/GHydJZibVJEj4ih838dbpuNcKeT/2yK9+1VGyXcEsTylePG31Gx X-Received: by 2002:a17:902:e801:b0:1ca:8169:e850 with SMTP id u1-20020a170902e80100b001ca8169e850mr6214278plg.3.1697647158194; Wed, 18 Oct 2023 09:39:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697647158; cv=none; d=google.com; s=arc-20160816; b=TRR3hORmmZQiK5T/Eg6QvVgvKKNpEoAAHrWFhdnUkXo1gDi45d6BXlaNhZQ7G5Cq10 d7aEy/wC/81Ar+IxwS40NUgkFzI71mXLj6IXEuUCgsNixB8wYpOUb1c6I3eJTGBNJsNw sqNCQEw5efZo9uDl0TCBvnx2Zyx4hMtG88A/vXgjemoVZZmVnFUMMmDieXZcao7nXoXH TxUW7YaZ3V51ujeO8aiF17ISeZJQ9MoATtd8AbR5mBkfEgOVJWmRSHLcmgl2DDyeEmXe XR+TtTzo1jDDNcWasjTK2HAoNwED05SbdyUFWYkPiMXj9qnAjgBJYrQDOUmbA+2dn/sV 77cA== 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=uyRIJePuKgYlcDApi43VH3EHFq5uTyC1JCkve6blZkQ=; fh=vJuql4DTq6KnxQZ/B563xy3AlrOr/OkDGOAqBtLkTrE=; b=U2zQcxpJvFdfhPoYb3dE40EN0OIV412lOiN5PysJt255fr9lpYz1vFf8A7G3BRtNAd cFb3XBjsYGnXekIUMFcEhoCB0A1JAVwhj+8fqzFSV/Bm2xHga4IL0tYYei4QnxBF6Yw8 kgqfLUeVeHzrLBNbhdEYVysT2lvYTxgECR815ZTf3XgYrlGUuFtrytnsReeBCG9l1UWe 7G8RmGtCpWlmk9mANO8dCBItwzovnCBhebNU8Z4Y7zY1wH0gOGq79Ll/i3yxLCzysDGZ isK/4SIJNOkt0H0l0yK0izsUrZeWabhOKqnXQOhDKdNm9QUJCqcQNjxa+bvOItt3JWfw jiVw== 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:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id v4-20020a170902b7c400b001b69ede5b79si211324plz.470.2023.10.18.09.39.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 09:39:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id F1895812AD1D; Wed, 18 Oct 2023 09:39:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230500AbjJRQjK (ORCPT + 99 others); Wed, 18 Oct 2023 12:39:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231254AbjJRQjJ (ORCPT ); Wed, 18 Oct 2023 12:39:09 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9813118 for ; Wed, 18 Oct 2023 09:39:06 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2389FC433AD; Wed, 18 Oct 2023 16:39:04 +0000 (UTC) Date: Wed, 18 Oct 2023 17:39:02 +0100 From: Catalin Marinas To: Liu Shixin Cc: Patrick Wang , Andrew Morton , Kefeng Wang , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 7/7] mm/kmemleak: fix partially freeing unknown object warning Message-ID: References: <20231018102952.3339837-1-liushixin2@huawei.com> <20231018102952.3339837-8-liushixin2@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231018102952.3339837-8-liushixin2@huawei.com> X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 09:39:15 -0700 (PDT) On Wed, Oct 18, 2023 at 06:29:52PM +0800, Liu Shixin wrote: > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 7c9125c18956..a956b2734324 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -816,16 +816,25 @@ static void delete_object_full(unsigned long ptr) > */ > static void delete_object_part(unsigned long ptr, size_t size, bool is_phys) > { > - struct kmemleak_object *object; > - unsigned long start, end; > + struct kmemleak_object *object, *object_l, *object_r; > + unsigned long start, end, flags; > + > + object_l = __alloc_object(GFP_KERNEL); > + if (!object_l) > + return; > > - object = find_and_remove_object(ptr, 1, is_phys); > + object_r = __alloc_object(GFP_KERNEL); > + if (!object_r) > + goto out; > + > + raw_spin_lock_irqsave(&kmemleak_lock, flags); > + object = __find_and_remove_object(ptr, 1, is_phys); > if (!object) { > #ifdef DEBUG > kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n", > ptr, size); > #endif > - return; > + goto unlock; > } > > /* > @@ -835,14 +844,25 @@ static void delete_object_part(unsigned long ptr, size_t size, bool is_phys) > */ > start = object->pointer; > end = object->pointer + object->size; > - if (ptr > start) > - __create_object(start, ptr - start, object->min_count, > - GFP_KERNEL, is_phys); > - if (ptr + size < end) > - __create_object(ptr + size, end - ptr - size, object->min_count, > - GFP_KERNEL, is_phys); > + if ((ptr > start) && > + !__link_object(object_l, start, ptr - start, > + object->min_count, is_phys)) > + object_l = NULL; > + if ((ptr + size < end) && > + !__link_object(object_r, ptr + size, end - ptr - size, > + object->min_count, is_phys)) > + object_r = NULL; > + > +unlock: > + raw_spin_unlock_irqrestore(&kmemleak_lock, flags); > + if (object) > + __delete_object(object); > > - __delete_object(object); > +out: > + if (object_l) > + mem_pool_free(object_l); > + if (object_r) > + mem_pool_free(object_r); > } Ah, I see now why __link_object() needs to do further object initialisation under the lock as prior to find_and_remove_object() we don't have all the information needed for object_l and object_r. Maybe in the __create_object splitting patch you can leave __alloc_object() just do the actual allocation and __link_object() do the full initialisation. Up to you, you can send a separate patch on top as I can see Andrew picked them up already. Otherwise the series looks fine. Reviewed-by: Catalin Marinas