From patchwork Thu Jul 21 18:02:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 12925663 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4FABC433EF for ; Thu, 21 Jul 2022 18:02:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230006AbiGUSCY (ORCPT ); Thu, 21 Jul 2022 14:02:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232142AbiGUSCY (ORCPT ); Thu, 21 Jul 2022 14:02:24 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2A258B4B6 for ; Thu, 21 Jul 2022 11:02:22 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id k64-20020a253d43000000b0067080eb57fdso1792163yba.5 for ; Thu, 21 Jul 2022 11:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=bwyvk3adYUmO9nGXEI3CWcvpdi62CKirSTxuhR6/nsw=; b=bGIgau+M3Z6by2t8ZG1mDNv/rfxHg71VJ6r02h86mtLkjz3yz7kPA5bRFH/aHFWZKm hDQqHQvSdAhyNgesiCx+I1PS3tUFyNkk28CN3M+KnbO66yRNYW463e4QFLtj+fr97xPr FF6bIMcqjAnvG4x/RD9SJzN/zcisqr/4bMDx5zHsxDi95n0mVEQ190mZiONKTBjW3Uv0 QYZvQ6j+dKUlPqpp+XwHjou1cG9ZInz6JCwezLIaUR3306FDHrZvZoVC0EMhmbbeanJ7 bIEk7krx/STdj54NTcu+aFHQwzNZFISF/KD3it0WGedpyPnh4YUk6ew4ssLb1wpYDvWz v36w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=bwyvk3adYUmO9nGXEI3CWcvpdi62CKirSTxuhR6/nsw=; b=G3vlKw0bhMEBGfy+zgX4qsalVx0hnl/aVK3w7kFAk7Fnl6sXeWb/Bvpxr4b/m3bejm f7zXprM8DhQ3bVu2Ba/g/BHyXloqSZsu2hcVtG8u8pxoMxcB9zXrl3yUCatXSW0i2eDh jbirKLxp+3WI5xbzseyEdtT6g/sSA48JLQzCHuFne39GACH+X5SI2zynvXcPpcfSzr8y h6PGD4Ye5VwtvIVJFQRduGUDonsehsv9Rd5N826Lf3NquGHb+O9Di+LNoqdfYiXa9I4B XTFi0ChBX2wJYCZ3hceoB3zYbMNq1qY8kSwARKBbk05+YPvYzhJqi4SeJjUrs65b2YJx Qu0w== X-Gm-Message-State: AJIora9fWYRn6c0RJKuW/FdmFfUmRYF17hQUJ4zVt5YfHBEG+2qXx6PL qk9OMHw6tKYFx2vrGZTpXeJFgTERO4XtNQ== X-Google-Smtp-Source: AGRyM1uTOq1N3ksSuvvL7kgVik3r3sJYoVQHO/NLbxfRpEl0mgzzQXgZIDXp9dkO+rAvi+Z4KpB9DvPXFdnyxQ== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a81:9acd:0:b0:31d:2fd7:3331 with SMTP id r196-20020a819acd000000b0031d2fd73331mr47250554ywg.145.1658426542076; Thu, 21 Jul 2022 11:02:22 -0700 (PDT) Date: Thu, 21 Jul 2022 18:02:11 +0000 Message-Id: <20220721180214.3223778-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH 1/4] kunit: string-stream: Simplify resource use 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: David Gow Currently, KUnit's string streams are themselves "KUnit resources". This is redundant since the stream itself is already allocated with kunit_kzalloc() and will thus be freed automatically at the end of the test. string-stream is only used internally within KUnit, and isn't using the extra features that resources provide like reference counting, being able to locate them dynamically as "test-local variables", etc. Indeed, the resource's refcount is never incremented when the pointer is returned. The fact that it's always manually destroyed is more evidence that the reference counting is unused. Signed-off-by: David Gow Signed-off-by: Daniel Latypov --- lib/kunit/string-stream.c | 90 +++++++-------------------------------- lib/kunit/string-stream.h | 2 +- lib/kunit/test.c | 2 +- 3 files changed, 18 insertions(+), 76 deletions(-) base-commit: 94681e289bf5d10c9db9db143d1a22d8717205c5 diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index 141789ca8949..a2496abef152 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -12,64 +12,31 @@ #include "string-stream.h" -struct string_stream_fragment_alloc_context { - struct kunit *test; - int len; - gfp_t gfp; -}; -static int string_stream_fragment_init(struct kunit_resource *res, - void *context) +static struct string_stream_fragment *alloc_string_stream_fragment( + struct kunit *test, int len, gfp_t gfp) { - struct string_stream_fragment_alloc_context *ctx = context; struct string_stream_fragment *frag; - frag = kunit_kzalloc(ctx->test, sizeof(*frag), ctx->gfp); + frag = kunit_kzalloc(test, sizeof(*frag), gfp); if (!frag) - return -ENOMEM; + return ERR_PTR(-ENOMEM); - frag->test = ctx->test; - frag->fragment = kunit_kmalloc(ctx->test, ctx->len, ctx->gfp); + frag->test = test; + frag->fragment = kunit_kmalloc(test, len, gfp); if (!frag->fragment) - return -ENOMEM; + return ERR_PTR(-ENOMEM); - res->data = frag; - - return 0; + return frag; } -static void string_stream_fragment_free(struct kunit_resource *res) +static void string_stream_fragment_destroy(struct string_stream_fragment *frag) { - struct string_stream_fragment *frag = res->data; - list_del(&frag->node); kunit_kfree(frag->test, frag->fragment); kunit_kfree(frag->test, frag); } -static struct string_stream_fragment *alloc_string_stream_fragment( - struct kunit *test, int len, gfp_t gfp) -{ - struct string_stream_fragment_alloc_context context = { - .test = test, - .len = len, - .gfp = gfp - }; - - return kunit_alloc_resource(test, - string_stream_fragment_init, - string_stream_fragment_free, - gfp, - &context); -} - -static int string_stream_fragment_destroy(struct string_stream_fragment *frag) -{ - return kunit_destroy_resource(frag->test, - kunit_resource_instance_match, - frag); -} - int string_stream_vadd(struct string_stream *stream, const char *fmt, va_list args) @@ -169,48 +136,23 @@ struct string_stream_alloc_context { gfp_t gfp; }; -static int string_stream_init(struct kunit_resource *res, void *context) +struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) { struct string_stream *stream; - struct string_stream_alloc_context *ctx = context; - stream = kunit_kzalloc(ctx->test, sizeof(*stream), ctx->gfp); + stream = kunit_kzalloc(test, sizeof(*stream), gfp); if (!stream) - return -ENOMEM; + return ERR_PTR(-ENOMEM); - res->data = stream; - stream->gfp = ctx->gfp; - stream->test = ctx->test; + stream->gfp = gfp; + stream->test = test; INIT_LIST_HEAD(&stream->fragments); spin_lock_init(&stream->lock); - return 0; + return stream; } -static void string_stream_free(struct kunit_resource *res) +void string_stream_destroy(struct string_stream *stream) { - struct string_stream *stream = res->data; - string_stream_clear(stream); } - -struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) -{ - struct string_stream_alloc_context context = { - .test = test, - .gfp = gfp - }; - - return kunit_alloc_resource(test, - string_stream_init, - string_stream_free, - gfp, - &context); -} - -int string_stream_destroy(struct string_stream *stream) -{ - return kunit_destroy_resource(stream->test, - kunit_resource_instance_match, - stream); -} diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h index 43f9508a55b4..494dee0f24bd 100644 --- a/lib/kunit/string-stream.h +++ b/lib/kunit/string-stream.h @@ -46,6 +46,6 @@ int string_stream_append(struct string_stream *stream, bool string_stream_is_empty(struct string_stream *stream); -int string_stream_destroy(struct string_stream *stream); +void string_stream_destroy(struct string_stream *stream); #endif /* _KUNIT_STRING_STREAM_H */ diff --git a/lib/kunit/test.c b/lib/kunit/test.c index b73d5bb5c473..0fb2771ca03e 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -267,7 +267,7 @@ static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, kunit_print_string_stream(test, stream); - WARN_ON(string_stream_destroy(stream)); + string_stream_destroy(stream); } static void __noreturn kunit_abort(struct kunit *test) From patchwork Thu Jul 21 18:02:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 12925664 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C597C43334 for ; Thu, 21 Jul 2022 18:02:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230383AbiGUSC0 (ORCPT ); Thu, 21 Jul 2022 14:02:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232302AbiGUSCZ (ORCPT ); Thu, 21 Jul 2022 14:02:25 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C918C3F1 for ; Thu, 21 Jul 2022 11:02:24 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id u14-20020a056a00098e00b0052b433b04d6so1067137pfg.12 for ; Thu, 21 Jul 2022 11:02:24 -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=WUfBKha+wN2y470CZAy7DJLzLCBEl+eUrhpc5o/SEgw=; b=MwOgmxtfChwEa4koE9oW47ctSFqflkOEWHnedifyRZFqbTwmgXHflH/jrfFIi+r3o7 tE9VOf5nE33S+s7TF8YnvEQyUYp1yzEUYRU2RzMsjt+ZhFlYpTUw6L38z/a+0YNTvv0n 6koNSQE9neBWXGjcSVNfnYZzQjSlCSIDDQcPeaB5ekyJwldAk1mI0+rug82zEx1gl/Ju 4zvp8J/yakXkbFLG/sfDSg24BNuCwSocfE07wi4tjF8cDepS3gAR8MZrhEhXwuaaiYvi yCZ2KT9zMtnlvijuMlbRGrUzIxq8uXgAGLA0MkqKHoycF6PPyOx1rZaxfiKJvbSoJ0D/ Bd+w== 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=WUfBKha+wN2y470CZAy7DJLzLCBEl+eUrhpc5o/SEgw=; b=DDxWsZs8mDuicb9Im/aO83UiMzQ2B0Kzfmh6bQ4gyQvtTMFXcGJHW77C130R0qHrK2 TIaYIXTieCyZoi761CKnd0tNdOtFtOagnHOyTGniKQJ8kjoHqMOIWplWSHiejE74VTdI cSbUBKQD6ywOmqbcQ7MVk3YG4Nadya2pJwhTzTgoV3YF2Et9euTqgXCzI6XiORPteGdw 1bZcsu3AIZQ/7rEaoYEEXSnBQeptlNvlwSNqIcY5+YVqwUEdQ8SR1nVgWy92910HMZ04 c03OJN+voneqD0Q5GUgWOVcgoQucPCQIIRTJL1voXTRy/fLh7jK56SzowfSWDsr/YQOa FAcw== X-Gm-Message-State: AJIora+aIoQWEo3tEIcrOuvum+sfLIlkdcjOUuNEJZwEM9p5ou2r8LY+ In/dqrX1gcv+TMQPKGZJyr2gyJpwfRUp1A== X-Google-Smtp-Source: AGRyM1ufFgjrd6hgdZR2IbDFHPNCwVMnRqABFG+LJzOlUA9h/nW+2T8iEe0U/kzW28guEvRD7RXz0EwUTt0/+w== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:902:ecc2:b0:16c:5b89:fd0b with SMTP id a2-20020a170902ecc200b0016c5b89fd0bmr45437674plh.122.1658426543674; Thu, 21 Jul 2022 11:02:23 -0700 (PDT) Date: Thu, 21 Jul 2022 18:02:12 +0000 In-Reply-To: <20220721180214.3223778-1-dlatypov@google.com> Message-Id: <20220721180214.3223778-2-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 2/4] kunit: drop test pointer in string_stream_fragment 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org We already store the `struct kunit *test` in the string_stream object itself, so we need don't need to store a copy of this pointer in every fragment in the stream. Drop it, getting string_stream_fragment down the bare minimum: a list_head and the `char *` with the actual fragment. Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- lib/kunit/string-stream.c | 10 +++++----- lib/kunit/string-stream.h | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/kunit/string-stream.c b/lib/kunit/string-stream.c index a2496abef152..f5ae79c37400 100644 --- a/lib/kunit/string-stream.c +++ b/lib/kunit/string-stream.c @@ -22,7 +22,6 @@ static struct string_stream_fragment *alloc_string_stream_fragment( if (!frag) return ERR_PTR(-ENOMEM); - frag->test = test; frag->fragment = kunit_kmalloc(test, len, gfp); if (!frag->fragment) return ERR_PTR(-ENOMEM); @@ -30,11 +29,12 @@ static struct string_stream_fragment *alloc_string_stream_fragment( return frag; } -static void string_stream_fragment_destroy(struct string_stream_fragment *frag) +static void string_stream_fragment_destroy(struct kunit *test, + struct string_stream_fragment *frag) { list_del(&frag->node); - kunit_kfree(frag->test, frag->fragment); - kunit_kfree(frag->test, frag); + kunit_kfree(test, frag->fragment); + kunit_kfree(test, frag); } int string_stream_vadd(struct string_stream *stream, @@ -89,7 +89,7 @@ static void string_stream_clear(struct string_stream *stream) frag_container_safe, &stream->fragments, node) { - string_stream_fragment_destroy(frag_container); + string_stream_fragment_destroy(stream->test, frag_container); } stream->length = 0; spin_unlock(&stream->lock); diff --git a/lib/kunit/string-stream.h b/lib/kunit/string-stream.h index 494dee0f24bd..b669f9a75a94 100644 --- a/lib/kunit/string-stream.h +++ b/lib/kunit/string-stream.h @@ -14,7 +14,6 @@ #include struct string_stream_fragment { - struct kunit *test; struct list_head node; char *fragment; }; From patchwork Thu Jul 21 18:02:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 12925665 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4828CCA485 for ; Thu, 21 Jul 2022 18:02:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232149AbiGUSC1 (ORCPT ); Thu, 21 Jul 2022 14:02:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229511AbiGUSC1 (ORCPT ); Thu, 21 Jul 2022 14:02:27 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE1058B4B6 for ; Thu, 21 Jul 2022 11:02:25 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id r17-20020a056a00217100b0052ab8271e11so1071404pff.22 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=TtkTdPk5Y6exObqxt1ZeUNdL/ogECWfDqLCiQmqQDBK7sspyvgbqfv3h65bVWhL4yc TmUCMdy4MP0RgAJoXH4qfqp4b+uSQizXwVdCWfAarCHCUC1JBkculrYLFFMLkk6VSS86 2CGcna1O/J0IGPJgKxdIQSyYsgtKX7VuNTWFZgpDHXknyZ0CExUuVVcm19J6mKDp2Vt9 sl9G7aqbCOwmdgZjqNMZrl8JnT1W0E2EGqzqq9RVR8IxvlNP5uESeKGFMcNE74u+QP73 umcNJDDwnsDcK0Q5wfJb3tTkPLfC3hgqpV7aj05AJY8E1GEGya+Sfg7oFeoalC5j/CC0 j1jA== X-Gm-Message-State: AJIora+d0bYoItBUpA9BfbSYpUm2b3dGHrFQvgk/bnq9TFQIknmWNrbF bZcqHYZG6L7f74yulIG1WrSAapcZJTBH+g== X-Google-Smtp-Source: AGRyM1t2IpzFVpMpQ/fVMUMXE/kIZBLqojYsP6NJxMuU8Fw9yThIdW+j1+3O9PGlRaqXeTmu2lA5RKVGxp79KQ== 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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 Reviewed-by: David Gow --- 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 From patchwork Thu Jul 21 18:02:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 12925666 X-Patchwork-Delegate: brendanhiggins@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 73CEEC43334 for ; Thu, 21 Jul 2022 18:02:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232521AbiGUSCh (ORCPT ); Thu, 21 Jul 2022 14:02:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232496AbiGUSCa (ORCPT ); Thu, 21 Jul 2022 14:02:30 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 486F38C3FA for ; Thu, 21 Jul 2022 11:02:27 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id x34-20020a056a000be200b0052b7f102681so1051108pfu.5 for ; Thu, 21 Jul 2022 11:02:27 -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=0YIjNYIeyOZDc9A5uuO9JNEVyn7b9iKJjntQZNv0C9w=; b=m7P5LBC6qwAkLeVUmaZQT9OiQ2KDu1X8mjVgXdx2Rw0fFg2K3Z3YuPCbkKIHZTph48 /FR24RvEBnNmlwVIjFpoY5uPxK5w+teFs9H8Xnwv7HSzRTguJrw/fcD0w60PgPPChCzX GAFjLCQfqd1ZwxWyOpcd6BpWZ4BD7b4qF/mKqtFAZn7AO+gGo3VEXY0ZBf51MfnaCIuq BPnjFWSieuCCazgqznNpIFdpmnXNrDkRlUBOiLm3f5QWczrLiZQUMz3m+uAy5tvnbx7I 34WJ0kxPhEJdpO2DpsiAgmvxas99sKHw4QzR652r7uxuPq3j/luoKtU1YYdHWIpbs493 DCUg== 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=0YIjNYIeyOZDc9A5uuO9JNEVyn7b9iKJjntQZNv0C9w=; b=lLD/y/0O09ifIU6IfT36Vjf3QEF/OuPoGQED9JwHsf/Hx1n3WTVbTDKMdFNAOZvzIf DLTY4cTnBIgofmNByuIAOZopUj46vqTVGwEW4w78W5odpG3IpQz9JD4IA0QNyQmHKS/e qZtkNV2klQZGQVvIOdPDjyoWCFxvf0bLxzQ/EG83C7AVEsaEUgFsKdBt9odYUDtUVup7 UHymZiUr0fO402d7QaOhuFYCpec42MqaUZUk5ddpvGZ/NaI5JLb34kHa69nu/8G9vPqA 9if+UfO+ftz95QYmrtqWY0p6ezKMJLRMuYucVGJwcohGF1doOv0FQfpBeSsQX4Ti2JWW FDmQ== X-Gm-Message-State: AJIora9JYFtpaOqPZS/5elvxhNir4emTeI74hmcVTl7ie6IBYg+cmBAe NdVfqNqELn84e/h7H6361r93SJfrR0tL4g== X-Google-Smtp-Source: AGRyM1slgDzbjderufx0Gk0s4ikkdcXMK02fNU7yxvzQDFwCOq9ptovRR86MU1FUEsAKB/Q6nu0/HWy7W+UeQg== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:902:d48a:b0:16b:f101:b28b with SMTP id c10-20020a170902d48a00b0016bf101b28bmr45958440plg.148.1658426546834; Thu, 21 Jul 2022 11:02:26 -0700 (PDT) Date: Thu, 21 Jul 2022 18:02:14 +0000 In-Reply-To: <20220721180214.3223778-1-dlatypov@google.com> Message-Id: <20220721180214.3223778-4-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 4/4] kunit: make knuit_kfree() not segfault on invalid inputs 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 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org kunit_kfree() can only work on data ("resources") allocated by KUnit. Currently for code like this, > void *ptr = kmalloc(4, GFP_KERNEL); > kunit_kfree(test, ptr); kunit_kfree() will segfault. It'll try and look up the kunit_resource associated with `ptr` and get a NULL back, but it won't check for this. This means we also segfault if you double-free. Change kunit_kfree() so it'll notice these invalid pointers and respond by failing the test. Implementation: kunit_destroy_resource() does what kunit_kfree() does, but is more generic and returns -ENOENT when it can't find the resource. Sadly, unlike just letting it crash, this means we don't get a stack trace. But kunit_kfree() is so infrequently used it shouldn't be hard to track down the bad callsite anyways. After this change, the above code gives: > # example_simple_test: EXPECTATION FAILED at lib/kunit/test.c:702 > kunit_kfree: 00000000626ec200 already freed or not allocated by kunit Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- lib/kunit/test.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 82019a78462e..c7ca87484968 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -698,18 +698,8 @@ static inline bool kunit_kfree_match(struct kunit *test, void kunit_kfree(struct kunit *test, const void *ptr) { - struct kunit_resource *res; - - res = kunit_find_resource(test, kunit_kfree_match, (void *)ptr); - - /* - * Removing the resource from the list of resources drops the - * reference count to 1; the final put will trigger the free. - */ - kunit_remove_resource(test, res); - - kunit_put_resource(res); - + if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) + KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr); } EXPORT_SYMBOL_GPL(kunit_kfree);