kunit: string-stream: Simplify resource use
authorDavid Gow <davidgow@google.com>
Fri, 22 Jul 2022 17:15:30 +0000 (17:15 +0000)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 7 Oct 2022 16:15:22 +0000 (10:15 -0600)
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 <davidgow@google.com>
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
lib/kunit/string-stream.c
lib/kunit/string-stream.h
lib/kunit/test.c

index 141789ca8949b45d4481ecbaed86c1529f718bde..a2496abef152c05238c37bac3f5a7d78ef6406fb 100644 (file)
 
 #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);
-}
index 43f9508a55b40f883280ddf0bf63109a86ed236e..494dee0f24bdebd1712f003c8e6343d35113c19a 100644 (file)
@@ -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 */
index 1e54373309a411c90f70f0330fe18dc3d605d497..638df598bcf91a64c703dc5a69454eaefa0204db 100644 (file)
@@ -278,7 +278,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)