Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kunit: string-stream: Simplify resource use

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>

authored by

David Gow and committed by
Shuah Khan
78b1c658 4e370573

+24 -82
+22 -80
lib/kunit/string-stream.c
··· 12 12 13 13 #include "string-stream.h" 14 14 15 - struct string_stream_fragment_alloc_context { 16 - struct kunit *test; 17 - int len; 18 - gfp_t gfp; 19 - }; 20 - 21 - static int string_stream_fragment_init(struct kunit_resource *res, 22 - void *context) 23 - { 24 - struct string_stream_fragment_alloc_context *ctx = context; 25 - struct string_stream_fragment *frag; 26 - 27 - frag = kunit_kzalloc(ctx->test, sizeof(*frag), ctx->gfp); 28 - if (!frag) 29 - return -ENOMEM; 30 - 31 - frag->test = ctx->test; 32 - frag->fragment = kunit_kmalloc(ctx->test, ctx->len, ctx->gfp); 33 - if (!frag->fragment) 34 - return -ENOMEM; 35 - 36 - res->data = frag; 37 - 38 - return 0; 39 - } 40 - 41 - static void string_stream_fragment_free(struct kunit_resource *res) 42 - { 43 - struct string_stream_fragment *frag = res->data; 44 - 45 - list_del(&frag->node); 46 - kunit_kfree(frag->test, frag->fragment); 47 - kunit_kfree(frag->test, frag); 48 - } 49 15 50 16 static struct string_stream_fragment *alloc_string_stream_fragment( 51 17 struct kunit *test, int len, gfp_t gfp) 52 18 { 53 - struct string_stream_fragment_alloc_context context = { 54 - .test = test, 55 - .len = len, 56 - .gfp = gfp 57 - }; 19 + struct string_stream_fragment *frag; 58 20 59 - return kunit_alloc_resource(test, 60 - string_stream_fragment_init, 61 - string_stream_fragment_free, 62 - gfp, 63 - &context); 21 + frag = kunit_kzalloc(test, sizeof(*frag), gfp); 22 + if (!frag) 23 + return ERR_PTR(-ENOMEM); 24 + 25 + frag->test = test; 26 + frag->fragment = kunit_kmalloc(test, len, gfp); 27 + if (!frag->fragment) 28 + return ERR_PTR(-ENOMEM); 29 + 30 + return frag; 64 31 } 65 32 66 - static int string_stream_fragment_destroy(struct string_stream_fragment *frag) 33 + static void string_stream_fragment_destroy(struct string_stream_fragment *frag) 67 34 { 68 - return kunit_destroy_resource(frag->test, 69 - kunit_resource_instance_match, 70 - frag); 35 + list_del(&frag->node); 36 + kunit_kfree(frag->test, frag->fragment); 37 + kunit_kfree(frag->test, frag); 71 38 } 72 39 73 40 int string_stream_vadd(struct string_stream *stream, ··· 136 169 gfp_t gfp; 137 170 }; 138 171 139 - static int string_stream_init(struct kunit_resource *res, void *context) 172 + struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) 140 173 { 141 174 struct string_stream *stream; 142 - struct string_stream_alloc_context *ctx = context; 143 175 144 - stream = kunit_kzalloc(ctx->test, sizeof(*stream), ctx->gfp); 176 + stream = kunit_kzalloc(test, sizeof(*stream), gfp); 145 177 if (!stream) 146 - return -ENOMEM; 178 + return ERR_PTR(-ENOMEM); 147 179 148 - res->data = stream; 149 - stream->gfp = ctx->gfp; 150 - stream->test = ctx->test; 180 + stream->gfp = gfp; 181 + stream->test = test; 151 182 INIT_LIST_HEAD(&stream->fragments); 152 183 spin_lock_init(&stream->lock); 153 184 154 - return 0; 185 + return stream; 155 186 } 156 187 157 - static void string_stream_free(struct kunit_resource *res) 188 + void string_stream_destroy(struct string_stream *stream) 158 189 { 159 - struct string_stream *stream = res->data; 160 - 161 190 string_stream_clear(stream); 162 - } 163 - 164 - struct string_stream *alloc_string_stream(struct kunit *test, gfp_t gfp) 165 - { 166 - struct string_stream_alloc_context context = { 167 - .test = test, 168 - .gfp = gfp 169 - }; 170 - 171 - return kunit_alloc_resource(test, 172 - string_stream_init, 173 - string_stream_free, 174 - gfp, 175 - &context); 176 - } 177 - 178 - int string_stream_destroy(struct string_stream *stream) 179 - { 180 - return kunit_destroy_resource(stream->test, 181 - kunit_resource_instance_match, 182 - stream); 183 191 }
+1 -1
lib/kunit/string-stream.h
··· 46 46 47 47 bool string_stream_is_empty(struct string_stream *stream); 48 48 49 - int string_stream_destroy(struct string_stream *stream); 49 + void string_stream_destroy(struct string_stream *stream); 50 50 51 51 #endif /* _KUNIT_STRING_STREAM_H */
+1 -1
lib/kunit/test.c
··· 278 278 279 279 kunit_print_string_stream(test, stream); 280 280 281 - WARN_ON(string_stream_destroy(stream)); 281 + string_stream_destroy(stream); 282 282 } 283 283 284 284 static void __noreturn kunit_abort(struct kunit *test)