Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp365749imi; Fri, 22 Jul 2022 00:18:54 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t1o9sfM+AVDlalgek26V752SfVlj4Kog7o+l7CvRDvy6Caym4MGl2R5C27BXqFWAwOGR4L X-Received: by 2002:a05:6a00:298d:b0:52b:4f4c:295a with SMTP id cj13-20020a056a00298d00b0052b4f4c295amr2101708pfb.57.1658474334308; Fri, 22 Jul 2022 00:18:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658474334; cv=none; d=google.com; s=arc-20160816; b=UA4es8S+74Ytsg5Tp8JfGIQVelKj7fTzQb6NCxcOKTKuqMcoL6AcTvqJ52pUoWrCJz pyNkkIustw9WGl0eCuKjFt4yCoofp6WF1U00tjsoyTeJbgHSeEWG1yIO9Mp77WB1junL KwyTJhnUHrdMkqHBleJj41qwqar/LPf5T+x/ciugxnZah9L8FR9i/aXwrW5hnbyqNTb6 l5jUwWVxQX/fY/4W9z5Fqs4SKqZR0Rg5Cy1GLlYM7jkFDIkuqHh8GfbBgkz2RQ3cDTHB 96B0Y7koQOLE3X6oJaX2BVxYZqSi0Q3qb3Uw68OlaEli9vjoVmmaHNfVvHpkQQRJ2JOT aN8g== 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=oSWCzyyi5A473F09U+VaQnX1hvRxVtUcH+BPYGqOwMw=; b=nzRpX0WNE70VFbPqZgEK7M3FEl1jiAwujlKtlgC6J9/NCDFw6saUS4DYp8CGBJj8YL IQc3bb12EkNFHUWzqf5+KUoJ44/dQ5Bta9/5DubmNSx3gUqTvyDCqlQMqSpxfiCDgbBu mhCIYy/0EYE0I1ONOHcaVWp1WxQKvFyGgmpjihRHstkPAvIZQMim7eeCjUO67DkqOyk/ i3nj4mWQlrDXhsGhabRUqaeZCxQ9WqrBrb5YDPV1Re47YkixWhTpCw6CouqmFBgUKR6G 9AHchO49h6hPnZ5b/8xKnFBfp22iDmT0OL7SvLW4LqFOkL0Kq9K2wR54AYTWR8WKBtBs 1NyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=phMS3u4d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j8-20020a17090276c800b0016c51203bdcsi4330894plt.184.2022.07.22.00.18.38; Fri, 22 Jul 2022 00:18:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=phMS3u4d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234590AbiGVHRx (ORCPT + 99 others); Fri, 22 Jul 2022 03:17:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234536AbiGVHRh (ORCPT ); Fri, 22 Jul 2022 03:17:37 -0400 Received: from mail-vs1-xe2e.google.com (mail-vs1-xe2e.google.com [IPv6:2607:f8b0:4864:20::e2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 181B8B33 for ; Fri, 22 Jul 2022 00:17:01 -0700 (PDT) Received: by mail-vs1-xe2e.google.com with SMTP id t127so3588197vsb.8 for ; Fri, 22 Jul 2022 00:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=oSWCzyyi5A473F09U+VaQnX1hvRxVtUcH+BPYGqOwMw=; b=phMS3u4dG0RDRkFoL5//kQJVpEoveww37/02BA+AafOIl9LwjUYqwXjZhH5uCVgn1U IvTdGfm22QxNwkYjqw0apNFP0fcLbozonxbdYjvF3Q9slfEcN1apNAbwGEiNgm1s45Og tTE5RGrW5Rip8lZDf68qakiZ0P3xqDNAtKm7kBbjm+Phavfg/pbYn9o90CfZxuws+MmT 66PwaQa505wrJ1DS+lwFuEUdra/BBMiIX7jRUzrbUKSC9Zw1Mpr3VGbw+7+JBihod/Pg GwyaMDjM3AULZpVZdYWXQ+MuBfIdOL2zXwp8d+PyvmTLIFfGExkBh00gVmYNw2o7dLVg M+wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=oSWCzyyi5A473F09U+VaQnX1hvRxVtUcH+BPYGqOwMw=; b=2DS20gN9uRUmWv8oZhJkCX2xkXUWlNleksol+sbNsHTBwz6ohmkvmJm3k0Agx+3Fmh jeRmFVQnXYFHjSunVhWPhObqxyowhsypAf25chZSPWlFFs+Wrwbxrt7/H+e9GJBisypM px+HkO4m2VTiqXX8Eu0+yW46lUzHVbz4JYplFlPZHPG1FiwY6Qt6oMsHvNOSOfPAJ+Cv LLQEREZVgYLJVC7iEbhEcdEKnPipZv1TwUtOnm/FHulnSHz/4ir9qpTqXeioWlaMHmg4 AyQHQjQ7R736lQHQLIDQW2KeYAPhPMg/qIOay0k8PT/h0vokYVDRVIEWUfScFUIH6dUE kE/A== X-Gm-Message-State: AJIora8eFHbHZaoW06CkLKPGtySvkcWpDmizbAYSIi6uG/qo9yOprVTM bnHEYuGH/Wj29H/dTfFGs1i7LHWAc2WiEWm1ZlkyUQ== X-Received: by 2002:a05:6102:381:b0:357:a112:adc7 with SMTP id m1-20020a056102038100b00357a112adc7mr610910vsq.38.1658474220111; Fri, 22 Jul 2022 00:17:00 -0700 (PDT) MIME-Version: 1.0 References: <20220721180214.3223778-1-dlatypov@google.com> <20220721180214.3223778-3-dlatypov@google.com> In-Reply-To: <20220721180214.3223778-3-dlatypov@google.com> From: David Gow Date: Fri, 22 Jul 2022 15:16:49 +0800 Message-ID: Subject: Re: [PATCH 3/4] kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends To: Daniel Latypov Cc: Brendan Higgins , Linux Kernel Mailing List , KUnit Development , "open list:KERNEL SELFTEST FRAMEWORK" , Shuah Khan Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 On Fri, Jul 22, 2022 at 2:02 AM 'Daniel Latypov' via KUnit Development wrote: > > kunit_kfree() exists to clean up allocations from kunit_kmalloc() and > friends early instead of waiting for this to happen automatically at the > end of the test. > > But it can be used on *anything* registered with the kunit resource API. > > E.g. the last 2 statements are equivalent: > struct kunit_resource *res = something(); > kfree(res->data); > kunit_put_resource(res); > > The problem is that there could be multiple resources that point to the > same `data`. > > E.g. you can have a named resource acting as a pseudo-global variable in > a test. If you point it to data allocated with kunit_kmalloc(), then > calling `kunit_kfree(ptr)` has the chance to delete either the named > resource or to kfree `ptr`. > Which one it does depends on the order the resources are registered as > kunit_kfree() will delete resources in LIFO order. > > So this patch restricts kunit_kfree() to only working on resources > created by kunit_kmalloc(). Calling it is therefore guaranteed to free > the memory, not do anything else. > > Note: kunit_resource_instance_match() wasn't used outside of KUnit, so > it should be safe to remove from the public interface. It's also > generally dangerous, as shown above, and shouldn't be used. > > Signed-off-by: Daniel Latypov > --- This is basically part of a sneaky, but sensible trend to make resources more obviously "typed". Given how many issues that can cause, this is definitely a worthwhile change. I have some plans to further refactor some of the resources stuff down the line (and to improve the documentation somewhat), so something not dissimilar to this was going to happen eventually. In any case, Reviewed-by: David Gow Cheers, -- David > include/kunit/resource.h | 16 ---------------- > lib/kunit/kunit-test.c | 7 +++++++ > lib/kunit/test.c | 10 ++++++++-- > 3 files changed, 15 insertions(+), 18 deletions(-) > > diff --git a/include/kunit/resource.h b/include/kunit/resource.h > index 09c2b34d1c61..cf6fb8f2ac1b 100644 > --- a/include/kunit/resource.h > +++ b/include/kunit/resource.h > @@ -300,22 +300,6 @@ typedef bool (*kunit_resource_match_t)(struct kunit *test, > struct kunit_resource *res, > void *match_data); > > -/** > - * kunit_resource_instance_match() - Match a resource with the same instance. > - * @test: Test case to which the resource belongs. > - * @res: The resource. > - * @match_data: The resource pointer to match against. > - * > - * An instance of kunit_resource_match_t that matches a resource whose > - * allocation matches @match_data. > - */ > -static inline bool kunit_resource_instance_match(struct kunit *test, > - struct kunit_resource *res, > - void *match_data) > -{ > - return res->data == match_data; > -} > - > /** > * kunit_resource_name_match() - Match a resource with the same name. > * @test: Test case to which the resource belongs. > diff --git a/lib/kunit/kunit-test.c b/lib/kunit/kunit-test.c > index 13d0bd8b07a9..4df0335d0d06 100644 > --- a/lib/kunit/kunit-test.c > +++ b/lib/kunit/kunit-test.c > @@ -161,6 +161,13 @@ static void kunit_resource_test_alloc_resource(struct kunit *test) > kunit_put_resource(res); > } > > +static inline bool kunit_resource_instance_match(struct kunit *test, > + struct kunit_resource *res, > + void *match_data) > +{ > + return res->data == match_data; > +} > + > /* > * Note: tests below use kunit_alloc_and_get_resource(), so as a consequence > * they have a reference to the associated resource that they must release > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index 0fb2771ca03e..82019a78462e 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -689,12 +689,18 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) > } > EXPORT_SYMBOL_GPL(kunit_kmalloc_array); > > +static inline bool kunit_kfree_match(struct kunit *test, > + struct kunit_resource *res, void *match_data) > +{ > + /* Only match resources allocated with kunit_kmalloc() and friends. */ > + return res->free == kunit_kmalloc_array_free && res->data == match_data; > +} > + > void kunit_kfree(struct kunit *test, const void *ptr) > { > struct kunit_resource *res; > > - res = kunit_find_resource(test, kunit_resource_instance_match, > - (void *)ptr); > + res = kunit_find_resource(test, kunit_kfree_match, (void *)ptr); > > /* > * Removing the resource from the list of resources drops the > -- > 2.37.1.359.gd136c6c3e2-goog > > -- > You received this message because you are subscribed to the Google Groups "KUnit Development" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20220721180214.3223778-3-dlatypov%40google.com.