Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp801613imi; Thu, 21 Jul 2022 11:12:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sctLjbINceKysvBxHXDoCbG//yeETDjQhtL5F3cxVoeDRW3zhKzODRSgBKr26dZvHk+yaV X-Received: by 2002:a63:314a:0:b0:412:b42c:693d with SMTP id x71-20020a63314a000000b00412b42c693dmr38288765pgx.20.1658427177015; Thu, 21 Jul 2022 11:12:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658427177; cv=none; d=google.com; s=arc-20160816; b=y8IZrds7pHSI/lh0hgPQH2WLHQpDZFwCOB/9kJqdRkv49a6opv2tFNqLPgV0d5NItr Hvm9yhh7e3K0kGdTCrXYDCVsTZGlI9TwS95IdbLMoau/XRHvakynKfhwh46IRNAcKVvb Yyvc6VyrgNIKz1RP+vrs6iDeLK4xKLrCLi7tNCKIMjHkfkDd35TVuV8nE9/+54ocdgce VNb6ZBvFF+QlPfL1O3ZRexnJ7HBax3jruC6BPrkEGAoi5W79Ru9r+VShRwqEQxs6BV3b ojKAtHzJ9xiFDpvB62/XCqp7A90RXveQYQ75bDJwSQlOn/aC8b8qy3cJHApp4f8kYQ+X 3O1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=RB2jOP3DJE7igCuPWPwpafzWrvgkOGEukNgncgY30Ss=; b=Qf1wVYfgF2OMGrhn3N8tKxi51MEDwA1wleKHOBF7ow5enVSvvCP5hWqwsVvjSQoPMS YLtl2qCkJtSgQlYTTbbL2Cgk3BK1IZ+QpKH80sQxpRQJAEjA3/p1jfvva0/b/h5gIcD4 uuvOLZcSH2bSeFCrcyFAKlnmHAnNTT9L7mwrqlSKapgHPw4ywqVYCToh/he9fctqm6G7 gzL/MMQUL0hyP64E/lS4WDSjVcoP55fYklzFSX4q6YHzs16tD3k4Pv5sVz5KffWhOD63 OSYzQ+E6IiPSpDpb7dLpbuQmI265v6FUR56Mbe2fU+zBDnAG356vY6LuBgzeGd2QdmuA 3zyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Nkqucz4X; 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 o26-20020aa7979a000000b0052ad60b0467si2721258pfp.380.2022.07.21.11.12.41; Thu, 21 Jul 2022 11:12:57 -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=Nkqucz4X; 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 S232490AbiGUSCa (ORCPT + 99 others); Thu, 21 Jul 2022 14:02:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232183AbiGUSC0 (ORCPT ); Thu, 21 Jul 2022 14:02:26 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB2B88AEFC for ; Thu, 21 Jul 2022 11:02:25 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 66-20020a630645000000b0041a63d6c638so1190090pgg.2 for ; Thu, 21 Jul 2022 11:02:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=RB2jOP3DJE7igCuPWPwpafzWrvgkOGEukNgncgY30Ss=; b=Nkqucz4Xgj5poC7ii3+Ggm7r66PaVVGf8ItlIahOIzMwAH+6RxP9SM8pzj6bE+N3B5 Zx2FbdftthkGT86bdPDwfW3nEAYceHXRmMez7wFj4eJuBV5voxQI9lTqjr2ENfTCMx04 fkUOVtKgwCwnX8iPdmVXQINFnBD4pSR4ZKO2O6Ft0E2CjiuHLLg6YpXwUxdVlEvFiqF3 /0Bpp+hz3u0wyMlu5e6Rs4rtBWHSSSWmmZSn2bQ4oB6OV1HMsqYTPGFIrxxduxVFOcJF GeBGcJn0yPkuZ9bRlXwVk+yRtQxmLme3MEUC+pRqshGVJUU+fgDyWtEYo3DOgQ0Oljdg kyLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RB2jOP3DJE7igCuPWPwpafzWrvgkOGEukNgncgY30Ss=; b=UKguPmagU6g5yGc9c2v46WxdYmhkI7EQJuoVeOipuoemHBAmCtCeS+oJpraxqdnKAL dn4YtBbW6UD2y51oHWIOGUqXv9w936vmQUZ2d/j3nNKLNssaVj7T5fwnMX4v0LlvpwL9 EGCu7bXVIeeYxuHJfzokrLLRJUHRzi0tnwHSxDIfBWMy6BUEG1oYH/5SuPjIbAOzYzhD l6Ehn0h+naPYWOBSlfT0TB0iDlrJIB8jMsI2zRf9mszMSJXt5/LLQ944dHuoYVjkDMxU n0GR14wzWvo2M6ss8OLkTfiv1Jlj3uEoyYDaBdqbVtD056uV0E0xE4k3RLv2bcyXpW2t zRPg== X-Gm-Message-State: AJIora9WprHp+KAKhqXAGKfpLtst/TOJMOtNycKHFXxm03qUQ7T5jhUR +LVx55TBsapIJA+VLdTcxBatLTXV8Bw1wg== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a62:be04:0:b0:52a:e089:99ee with SMTP id l4-20020a62be04000000b0052ae08999eemr44556758pff.26.1658426545233; Thu, 21 Jul 2022 11:02:25 -0700 (PDT) Date: Thu, 21 Jul 2022 18:02:13 +0000 In-Reply-To: <20220721180214.3223778-1-dlatypov@google.com> Message-Id: <20220721180214.3223778-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220721180214.3223778-1-dlatypov@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH 3/4] kunit: make kunit_kfree() only work on pointers from kunit_malloc() and friends From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, Daniel Latypov Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_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 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 --- 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