From patchwork Fri Jul 22 17:15:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 592677 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 34160C433EF for ; Fri, 22 Jul 2022 17:15:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235657AbiGVRPt (ORCPT ); Fri, 22 Jul 2022 13:15:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235585AbiGVRPs (ORCPT ); Fri, 22 Jul 2022 13:15:48 -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 3ECAB1C93C for ; Fri, 22 Jul 2022 10:15:47 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id r7-20020aa79627000000b00528beaf82c3so2115221pfg.8 for ; Fri, 22 Jul 2022 10:15:47 -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=n2afkeTfe3v6NX+a4LX8KQS9apVldnTaH4FqlKFd5cE=; b=rYRZsPNDk7iY+cJAzYWzPI5DfOv5pG5H98CZzRr1mPd7pSkcUtCj00U/X98BfQ+2Xp zOnLs7E+fOJYxAoZLCluhYc5MSYj/x3VDWAbr86oikWBjC8ObSRrT+AF+6PkK1NWr/qb FSIRUk0agk93yTeUG58h++czK8zn8t8Tx7w5o4AeyNxxnkHSIM3GsZ6hWPorJAOGc0cD F0tsLaadlquno13jRHEdWzKchIL+wzwE1bx/59k+r1yxhycHGMnziRqeaJ86SC0l0IfR fpAaiDHgpxHmZj20YeveZWlcSIqHJGgj785TVarw+zOpM3v7Gipy+EUjVEDayoop8zqS k/PA== 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=n2afkeTfe3v6NX+a4LX8KQS9apVldnTaH4FqlKFd5cE=; b=2UPurlxTigX1bHQw4a9Va34CVEJLeMBUK7JXu4KjoTRKGHQzDABYVsYUOGv06AF7pM IydSlDykd6BHRrMmCPcaECGffF79E6BsO7G8Helm+STktX/kGn3AHPi51r7pmkIkSGJK qMdMxuSEcn1xfZ6o4tZlflmUgfJvFljKihZ2ZrNbs1AOr9aI3V7lAzNlsrZ7lhGSPx3B 5SsgNFj8elrk6l/5LMNmn2FRSJubtIL+odLOBlEYZWHbh/NfybMvbJdZtnyyJkcrG5GQ NpVGzj6VKkTxBE/qbBUrVMIxBQ2U0boQDAT0tIXfyYWd8PTmOp48HnD03UREOeN9hWj1 kXqA== X-Gm-Message-State: AJIora9rbi+xfc9wwU4106ZCZv2qPm/YWjJdgBHmtqHWam3hhgFDtNjQ XGRnv3jqYaHj+Y7xO2Vj2ZNa/enhnRXLPg== X-Google-Smtp-Source: AGRyM1v3Qw9ExJryeZJQZ2/oV4569CQWIrgy2Z3zVJxHKCP57A23OZhXtVsNf4/TFJWfxNSa5jpZDXcN7NdTpQ== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:90a:9a83:b0:1f2:23fa:ea2d with SMTP id e3-20020a17090a9a8300b001f223faea2dmr14264586pjp.25.1658510146718; Fri, 22 Jul 2022 10:15:46 -0700 (PDT) Date: Fri, 22 Jul 2022 17:15:30 +0000 Message-Id: <20220722171534.3576419-1-dlatypov@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 1/5] 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 Reviewed-by: Brendan Higgins --- v1 -> v2: no changes --- 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 Fri Jul 22 17:15:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 593376 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 5A9E6C43334 for ; Fri, 22 Jul 2022 17:15:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235699AbiGVRPu (ORCPT ); Fri, 22 Jul 2022 13:15:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235669AbiGVRPt (ORCPT ); Fri, 22 Jul 2022 13:15:49 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F04391C93C for ; Fri, 22 Jul 2022 10:15:48 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id z9-20020a170903018900b0016c39b2c1ffso2992914plg.12 for ; Fri, 22 Jul 2022 10:15:48 -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=gq9vbxPAOwHYvlzwiSQtId4LrMlH0FUkyQCy4RHppRE=; b=iXVdLKcEomziIHggkUjy86YhATorBopk3UBt6C1zRmJk/B/4U8Ej81rCJWm8c1Y6Rd U1BarKkvjldx+jMx1H0vPiW/ppLkS7ufn2Kz1NmBStOqPhNKQUjqYDnANB4vyFSuT3tP YnUyp2txdO9gBkSh2FEQIm4jwFBO4giIpf3MAwBDL33PVgNPBg44T+m/kL1UkeIDsxeg 2epZGY8sb0VeIKcoc9TuNdiGqHfp4jNw/M/TR3leqoqii6tgOsCKXZ089AQOfJFbzI7H qO+VABGC2e385h5KjwlhSWE2eeJcYqVm9jJx0gEfvW2zKn+ntkzAfsfyG8IeDJr0V+ml 78RQ== 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=gq9vbxPAOwHYvlzwiSQtId4LrMlH0FUkyQCy4RHppRE=; b=Xy3MinTc/65KIo/9o4QBMc9snnRfcF0pMuFe1/sPdetAQTZVN5+4X02SfHQGJpP/l3 MiaXapOMk6OSP5x9dx6y2ehDIVT99+ASgYPkPJnP+ueFQMyWk5DCCKyZkK3sOzZe4bQm Jaf1chRH6yX+ja2TJ4lrnuU25EgazxmpvcS8k4rGKvSKi6Eeug9JWIdaUQXlcv4WTit8 7hOlajtSpwP+/eHCgkHM8qWB7ORS1XFzCSrl5owU6NX2X+Vk0JW4n+Hj0pVj4x/htoBj rFBruc8eq85x7HNDFNXVnfvFG2TralBVQtMx2K5pkoKqGVs4dpj87RPKD8X6mxV5hZtd oMTw== X-Gm-Message-State: AJIora/JywJ0X/T3N62b5uQgE5Zehu9iVMpFQUSLtq2MzItnDieza7ti 0blz0+Jz44t3jlEPNg9vBqrnlhAz/4vMZQ== X-Google-Smtp-Source: AGRyM1uPxVd4kD3hkJLQPplHzxKJYD7ADNoMqovcPfELWI8lAoPtaW2gU3Zb+xUboC74fKpOTy4/k0WlYi+lTw== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:90a:249:b0:1e0:a8a3:3c6c with SMTP id t9-20020a17090a024900b001e0a8a33c6cmr1064402pje.0.1658510148119; Fri, 22 Jul 2022 10:15:48 -0700 (PDT) Date: Fri, 22 Jul 2022 17:15:31 +0000 In-Reply-To: <20220722171534.3576419-1-dlatypov@google.com> Message-Id: <20220722171534.3576419-2-dlatypov@google.com> Mime-Version: 1.0 References: <20220722171534.3576419-1-dlatypov@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 2/5] 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 Reviewed-by: Brendan Higgins --- v1 -> v2: no changes --- 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 Fri Jul 22 17:15:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 592676 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 B4D1BC433EF for ; Fri, 22 Jul 2022 17:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235882AbiGVRPx (ORCPT ); Fri, 22 Jul 2022 13:15:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235785AbiGVRPw (ORCPT ); Fri, 22 Jul 2022 13:15:52 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAC3E1CFDD for ; Fri, 22 Jul 2022 10:15:50 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id u6-20020a25b7c6000000b00670862c5b16so4084795ybj.12 for ; Fri, 22 Jul 2022 10:15:50 -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=fHf/2y2ImS8+g0A88yDniXGhaU3PcBI0GaxEtqPM90E=; b=KPDS4nOtrJFuhxfRuEPmu6keOaCejjdI4L8zfS7T8WvzBIjnBggIF903aLC/t2LP2I qs0GJzxxwcWZTCyznbQcKz/ax8qo9gJ56zr4JFcCfNbnKmYs80X0u5aOWaETsBaOvvT8 3/IzwJbrOZvgun8XL9IDlJuQMbyEFAmvkftxX5byvo1sc/ybR4Pq3c2mLdb6pc0vTLdz kZOOiE3o/ncmoMtM+hNK40IgNqdjmE6masrljJDeXwk4iJ90u+IX8BmLxcHY2G21QN70 Rlj4aRmoUo4EeaZq5FBE/DI6V4hlXdPpv6JFEhiOB4VNkJG8Hq8iqZ5BvtKxFULf+UD3 eJ0Q== 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=fHf/2y2ImS8+g0A88yDniXGhaU3PcBI0GaxEtqPM90E=; b=THgxoh7UZGNLip/AqlQnqjLvl3LTWUe3XIleC275PmBG4f161UhTckbGkcshGg9IrK CotdOMiK3WNlJ92ayTT++Du6sjpV0FdCLkkbBT5UkyKr1LVvbiKu49GxtmDNxumVK50G yiYflulkZzsP90G25kzpXL5AB9cgmpgqNit6anOhlW3TcfeHtuG80ikPB6iNjzdGLn7i 9gAdi2CC6eIgrszRKVknr0xGaL5qvJpQLoxAD8a/1QK5XasA/rauf9bGnb2UD8y+HJiY tEvAhzRxj4e4lSkVVAo3afts5XVXfJoGRGu97bCN8C5XwHLN40SlFVnscOt2T3jaD2Zm J6EQ== X-Gm-Message-State: AJIora819y9xd0KIoNLGl6661X0sh/k6xuFL4fgseXUR0Xb0JpjCZo5V QHDQgqFshgenHFvCCfecdcUNdK+vb+RJTw== X-Google-Smtp-Source: AGRyM1s1MTEqIuf9GSvKTbAzIhLe0gqPi896Xp1NxiIVUp1bME6wgEMhY5v/yecLl67eFteJez0fVHI6FiNpdw== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a25:9f87:0:b0:669:4345:a8c0 with SMTP id u7-20020a259f87000000b006694345a8c0mr871017ybq.472.1658510149999; Fri, 22 Jul 2022 10:15:49 -0700 (PDT) Date: Fri, 22 Jul 2022 17:15:32 +0000 In-Reply-To: <20220722171534.3576419-1-dlatypov@google.com> Message-Id: <20220722171534.3576419-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220722171534.3576419-1-dlatypov@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 3/5] 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 Reviewed-by: Brendan Higgins --- v1 -> v2: no changes --- 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 Fri Jul 22 17:15:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 593375 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 7F3A7CCA489 for ; Fri, 22 Jul 2022 17:15:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235856AbiGVRP4 (ORCPT ); Fri, 22 Jul 2022 13:15:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235866AbiGVRPx (ORCPT ); Fri, 22 Jul 2022 13:15:53 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19C0E1D32C for ; Fri, 22 Jul 2022 10:15:52 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id o6-20020a17090aac0600b001f23d8bfe2bso1890811pjq.7 for ; Fri, 22 Jul 2022 10:15:52 -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=RUauCLQfxebD9yFL6jKMCH6mXVYBNZNS09oviaAOZNY=; b=lyRj2rD2miVN/fQNO6oQK3dum8RZnMnojLOuGspbXBmNmkjX045GZuNv3i1Ni3uaN4 MhJd8/XCX5JR0DAOaLFIE3hXI3qfqylhRUTteCMEKqewpBDHYI2b3xP+oCuZUWZIJC8s VunIE0zfDrEDMjIKUqxpxX5m35EBMNfYYa0lStcVKeeqlsVWcTB3OniwLX7Rb1j2KycT uTP6zwK38WEpr1ceTWcIXpAiQjn3LxJxA7D3QjTkjSRO9IgE6gVbuofc8wxABJ50KyDM Nc40qeWxTp0el63R/HFId2ONad3dUem4ufZZp+mux9H1+vxR1o6CSLOrH0dTeSPSLaXb 4NZg== 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=RUauCLQfxebD9yFL6jKMCH6mXVYBNZNS09oviaAOZNY=; b=76vya4BnCpqyfixdK+jDjex1jD0d/fcrbJAtku2miJHXiJR3CkRiU8r0OEZauCV8tQ wgxDp/YWdlcpmiPE3DGpT6K136XJjjOmGN+6xw/nOhGxxZoXhYQEq2cheW6zE9iFVbON xzSBlTnfTNkJkgWbTqE5AX59yhIzG2Oq6+tG7Bt1pshmXLXEqppl3GsBrE6qD/4fQsH4 wcSnVRSyj2y7hRXmctroMlD3pRauLYMmiB0yp2EmhJgBxQQwrVp+lv1vlyleOKHeMxao dNZl04GZyAu2FbzW+dLuA2yJnuKCsgaibqq2QG1Q8jtX5XEXpn4nQOjV/oGq/sbw+C2K ERMA== X-Gm-Message-State: AJIora+NlH8WbFvlCFZNNGQZ5+d+0HSmivkMifGcTlrDV/4WZpIBeTz4 NhgAYJl7ZJVEYx6m8kiXJlX0rA7F6U4VTg== X-Google-Smtp-Source: AGRyM1unJ/KGpdJLj10Pr2atWngJAHG5XIMztglIQ6IIW45XdLr1SMv/95HqFIYe4ZUgLsSDxzifVYnKKd0z/g== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a05:6a00:2181:b0:51b:560b:dd30 with SMTP id h1-20020a056a00218100b0051b560bdd30mr788343pfi.44.1658510151585; Fri, 22 Jul 2022 10:15:51 -0700 (PDT) Date: Fri, 22 Jul 2022 17:15:33 +0000 In-Reply-To: <20220722171534.3576419-1-dlatypov@google.com> Message-Id: <20220722171534.3576419-4-dlatypov@google.com> Mime-Version: 1.0 References: <20220722171534.3576419-1-dlatypov@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 4/5] kunit: make kunit_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 Reviewed-by: Brendan Higgins --- v1 -> v2: fix typo in commit desc. --- 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); From patchwork Fri Jul 22 17:15:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Latypov X-Patchwork-Id: 592675 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 3C036C433EF for ; Fri, 22 Jul 2022 17:15:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235870AbiGVRP5 (ORCPT ); Fri, 22 Jul 2022 13:15:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235894AbiGVRPy (ORCPT ); Fri, 22 Jul 2022 13:15:54 -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 C90E31E3E6 for ; Fri, 22 Jul 2022 10:15:53 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id bd7-20020a656e07000000b00412a946da8eso2625455pgb.20 for ; Fri, 22 Jul 2022 10:15:53 -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=S5cNbvav8USkmIEO4JyFG7st/81vwwIaAFb/mwn7PBw=; b=ZijylNG/dIr7hWeGOThw0F1FX6gZqPIINac1RikPNL2Vu4wePiDr7jZuU6qIC5cuaG XNRKI0kBU7C3JPm7J/65+xrByvLzo8y3HdvpHHbqFBzAEfRJE7ZnXXTyRgtjt6cfLkY9 2MsO7lMPJE3fjnRDwsl6JN0jwfY/JMVyASX8WBCPMS/OJlPJFckOgUmN6cJlGGjYrkei 6laVWLPSFcnMjJHQR0Z8ciO2egpgl/YamGSq5ON1PWDG2UVKiYa+aRUx4KAEUdTeFMt8 cGxNS2blvqFhWmOAwIYvNE1soe3QsVF6CH2lupiR2xU1Ap49O7N/Al4TmVzxVXOPpCVr mwMg== 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=S5cNbvav8USkmIEO4JyFG7st/81vwwIaAFb/mwn7PBw=; b=oPcgjGGOpeGKonYAWRlIbeq2pxA0ufJpgrL88STa3t+d6WioN4JyDkOE+dpUMbKb6e A+B7h/pKxeoegLViQ9X2QpsLT6NOnNAg5Fcl7B/uLeDTnR7xGslBw+rbdD7zeUkGa9lk jh+e6c+9dke5QKRIm9DP7/gVJ/NvCeudiQ7iU8baumwc27YInySr6j99OjJL6JWqvli4 soU8Ce7EgKq2c8aazsFB/44cV9sDpeNUPH0bdQTPFJnzW9ePOsm8TfDiQSIGxveXxIcT RIvJ2SBMykmllt7UEX/E/qzev/4DUzAh4eeFKm/kK9090ZLdwpr1SAcGCCdEO18clCQF 7hFg== X-Gm-Message-State: AJIora/FgldeLx+hi6zWtRomgdByPX8/yG1a3mYcW62fe0hx8Aa4rbDi wAJCc5IftzxF2TmeHu0FdEw3wlxXJKcuTg== X-Google-Smtp-Source: AGRyM1t7wmAT+3kke9a3GR632MG8LKFNIvuSwnvOkrBbzMVDoTtceecqxaXTlOz1WmmOvfcM1J4KJniDJP7MEA== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:aa7:888a:0:b0:52b:17e8:fd1 with SMTP id z10-20020aa7888a000000b0052b17e80fd1mr785288pfe.46.1658510153314; Fri, 22 Jul 2022 10:15:53 -0700 (PDT) Date: Fri, 22 Jul 2022 17:15:34 +0000 In-Reply-To: <20220722171534.3576419-1-dlatypov@google.com> Message-Id: <20220722171534.3576419-5-dlatypov@google.com> Mime-Version: 1.0 References: <20220722171534.3576419-1-dlatypov@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [PATCH v2 5/5] kunit: make kunit_kfree(NULL) a no-op to match kfree() 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 The real kfree() function will silently return when given a NULL. So a user might reasonably think they can write the following code: char *buffer = NULL; if (param->use_buffer) buffer = kunit_kzalloc(test, 10, GFP_KERNEL); ... kunit_kfree(test, buffer); As-is, kunit_kfree() will mark the test as FAILED when buffer is NULL. (And in earlier times, it would segfault). Let's match the semantics of kfree(). Suggested-by: David Gow Signed-off-by: Daniel Latypov Reviewed-by: David Gow Reviewed-by: Brendan Higgins --- v1 -> v2: add this patch to the series. --- lib/kunit/test.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index c7ca87484968..879c8db36cb5 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -698,6 +698,9 @@ static inline bool kunit_kfree_match(struct kunit *test, void kunit_kfree(struct kunit *test, const void *ptr) { + if (!ptr) + return; + if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr); }