From patchwork Mon Dec 4 22:19:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479156 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2P6JA5qQ" Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AE5ECB for ; Mon, 4 Dec 2023 14:19:38 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5caf86963ecso69992157b3.3 for ; Mon, 04 Dec 2023 14:19:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728377; x=1702333177; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=YjixCCMvWXGgvjOEZP9yI8xNcwVr5lemGtrMuIweNfo=; b=2P6JA5qQlREMT52OEUc2cIyZGj9qBE7ezwroygk+1oxeTFUzVQhIEUIrcUilmHVm08 FlZ8x1P4Iz3Hr6YSvtudwmWGgH4gJRvM5752aROIvTXMxXUKCn3sZ4frU6ukk9cq1j2J SuqP+Wztq5G8SPHsi5kiMW0HEZKwuxKuhiQrEKcPysxMg4Qp069GErIuTbdsRc0I1+WN ehcJuqCgCju9Q8zPXNmZYuN3ETh0QsaJCDtXzdtykp2h7S8r0ESx1HVMHWR1ZouO+UJZ CvrtpOasehfnFyRb67BN4Nwort78gkdF/ZQhlS+7LKshuLKEN+hZEnY2btlK4iXIh33G OeRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728377; x=1702333177; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=YjixCCMvWXGgvjOEZP9yI8xNcwVr5lemGtrMuIweNfo=; b=Qr3pxtFKnVI370ElnahIvcLN6inzvzLoWDorNRgRewCLXTnN2sX84RNZLGo/r1GMdD 70UZ3uUTWyivQs/YmNTl+WUGS2CJnjGwwQrVFIem8WaIJKoSjtCJH9oFpySxz5sTGKu8 0YzTEVb35qJyx6DUzIVPvwAoEB0F2yPV1FudIjMS/SGkjaQDooB95emuFKofa0nVTqJE nEJZHnKPX7WBalvcpxsyH/mmc4pD3cb11t1LnnMBfDywOQf7aV/dngK3gLs2vjUTV97v GWEwZfGGsRQYHZ06wCFNChsX3MA5yYhXWCw35xhXxvPbfISWU/LXd6uE4yvjRGf+AoSI Y7SQ== X-Gm-Message-State: AOJu0YwMTNEdB7NCQsIydrqfr02iiblYrb690qYj9K0hNN/xcyWeUte1 PMnUqpyc65Wv5LYmJUK1tw6TC+tmlQ== X-Google-Smtp-Source: AGHT+IG3UzSly7olN8rLOVzLWmjvlzlBKa8sk/0NbpKjrd0oLTQugt5BmAC6ZKFZMeIS6NMXOFkU5H26Nw== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a05:690c:2509:b0:5d8:5d2c:121d with SMTP id dt9-20020a05690c250900b005d85d2c121dmr91236ywb.7.1701728377512; Mon, 04 Dec 2023 14:19:37 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:26 +0000 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-1-rmoar@google.com> Subject: [PATCH v3 1/6] kunit: move KUNIT_TABLE out of INIT_DATA From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Alter the linker section of KUNIT_TABLE to move it out of INIT_DATA and into DATA_DATA. Data for KUnit tests does not need to be in the init section. In order to run tests again after boot the KUnit data cannot be labeled as init data as the kernel could write over it. Add a KUNIT_INIT_TABLE in the next patch for KUnit tests that test init data/functions. Signed-off-by: Rae Moar Reviewed-by: David Gow --- include/asm-generic/vmlinux.lds.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) base-commit: b85ea95d086471afb4ad062012a4d73cd328fa86 diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index bae0fe4d499b..1107905d37fc 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -370,7 +370,8 @@ BRANCH_PROFILE() \ TRACE_PRINTKS() \ BPF_RAW_TP() \ - TRACEPOINT_STR() + TRACEPOINT_STR() \ + KUNIT_TABLE() /* * Data section helpers @@ -699,8 +700,7 @@ THERMAL_TABLE(governor) \ EARLYCON_TABLE() \ LSM_TABLE() \ - EARLY_LSM_TABLE() \ - KUNIT_TABLE() + EARLY_LSM_TABLE() #define INIT_TEXT \ *(.init.text .init.text.*) \ From patchwork Mon Dec 4 22:19:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479157 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ac7mQyoL" Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0F99129 for ; Mon, 4 Dec 2023 14:19:39 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ca61d84dc3so66847237b3.0 for ; Mon, 04 Dec 2023 14:19:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728379; x=1702333179; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G6/xsp/08ebsCbpaOu2zw7fiAx95DZqURF0y3jqrrm0=; b=Ac7mQyoLult1CTmfc0baS2746lyGRg/DkSTfGYUeC62Jt4JCQA7E3I1tjst1bjkUSv eGhlKS88a8knizqfV9rLpLGjRi9+FH7Y8QCFIW86CvFP4UUzBu8N1oa3MlGYS9xIhLK1 mQmIhntgC/G3wA/eO61QE8jUQ2SQcC3egvGISSyBi8g3yKAYcSIqPvGXA4CIdBJdEODa GC6grKGBT7Ny74m2GeU+AhTXOrfkwToWv46JoANHhJlm+Fq75zqgC4rVXsUCAAOL7G1t kBuVmJ74aQMJtcdLXN3OeciaErAhYkNhOx6Zvkh0B54KxikOJ3GaQRv84y0DHUn8TFhi v+6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728379; x=1702333179; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G6/xsp/08ebsCbpaOu2zw7fiAx95DZqURF0y3jqrrm0=; b=Rhu/KyFhg+vPTSQF0Z/dKo+GvD7Xpz3WM67ii1LDngr6Ywgk6qfGFbNh/uOGYhb6ev Dxu/2UZ4wlZg8UowfyryKKfvCcsohKnkuef4AjXhgkkxvUy89G/wYSjAbnxOgmxUl/x4 JWRTlTcnz+7tH4uj1HOMynvY4jwRoIUV3VvfZE8GYp7A3nDAntNFY8CVUr+q92vkJpu/ /tq1QQhktr/pAKLDTI/E6IOuirNY0oHSZGD7JM939RLMIYHWccS0un0drmCb+/R9lFbE T8b+yIFvR1rLTN2IfNNQu5yQN2EZ3dwPM0kMzM2c2ABEkNHbQLBrlOpJx/46oJ0So5zc 0Ezw== X-Gm-Message-State: AOJu0Yypwx1Q8btJRJK3V9dvWyJlJwg/U9OMFuixdgjXRNAcHkHYDMwQ HPZxfyxaMmEpWiAUl0qGA0vO94mjig== X-Google-Smtp-Source: AGHT+IF/hi7xWVRvUmfAYK2GkkcH7P0JS14ArSV7g3dal1RCvBTJEI/qyrvRO8yLzDuFDE19rnbb03SlvA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a81:b60d:0:b0:5d7:545e:3bd0 with SMTP id u13-20020a81b60d000000b005d7545e3bd0mr160730ywh.4.1701728379215; Mon, 04 Dec 2023 14:19:39 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:27 +0000 In-Reply-To: <20231204221932.1465004-1-rmoar@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231204221932.1465004-1-rmoar@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-2-rmoar@google.com> Subject: [PATCH v3 2/6] kunit: add KUNIT_INIT_TABLE to init linker section From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Add KUNIT_INIT_TABLE to the INIT_DATA linker section. Alter the KUnit macros to create init tests: kunit_test_init_section_suites Update lib/kunit/executor.c to run both the suites in KUNIT_TABLE and KUNIT_INIT_TABLE. Signed-off-by: Rae Moar Reviewed-by: David Gow --- include/asm-generic/vmlinux.lds.h | 9 ++++- include/kunit/test.h | 10 ++++- include/linux/module.h | 2 + kernel/module/main.c | 3 ++ lib/kunit/executor.c | 64 ++++++++++++++++++++++++++++--- lib/kunit/test.c | 26 +++++++++---- 6 files changed, 99 insertions(+), 15 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 1107905d37fc..5dd3a61d673d 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -700,7 +700,8 @@ THERMAL_TABLE(governor) \ EARLYCON_TABLE() \ LSM_TABLE() \ - EARLY_LSM_TABLE() + EARLY_LSM_TABLE() \ + KUNIT_INIT_TABLE() #define INIT_TEXT \ *(.init.text .init.text.*) \ @@ -926,6 +927,12 @@ . = ALIGN(8); \ BOUNDED_SECTION_POST_LABEL(.kunit_test_suites, __kunit_suites, _start, _end) +/* Alignment must be consistent with (kunit_suite *) in include/kunit/test.h */ +#define KUNIT_INIT_TABLE() \ + . = ALIGN(8); \ + BOUNDED_SECTION_POST_LABEL(.kunit_init_test_suites, \ + __kunit_init_suites, _start, _end) + #ifdef CONFIG_BLK_DEV_INITRD #define INIT_RAM_FS \ . = ALIGN(4); \ diff --git a/include/kunit/test.h b/include/kunit/test.h index 20ed9f9275c9..06e826a0b894 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -337,6 +337,9 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites); void kunit_exec_run_tests(struct kunit_suite_set *suite_set, bool builtin); void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr); +struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, + struct kunit_suite_set suite_set); + #if IS_BUILTIN(CONFIG_KUNIT) int kunit_run_all_tests(void); #else @@ -371,6 +374,11 @@ static inline int kunit_run_all_tests(void) #define kunit_test_suite(suite) kunit_test_suites(&suite) +#define __kunit_init_test_suites(unique_array, ...) \ + static struct kunit_suite *unique_array[] \ + __aligned(sizeof(struct kunit_suite *)) \ + __used __section(".kunit_init_test_suites") = { __VA_ARGS__ } + /** * kunit_test_init_section_suites() - used to register one or more &struct * kunit_suite containing init functions or @@ -392,7 +400,7 @@ static inline int kunit_run_all_tests(void) * this manner. */ #define kunit_test_init_section_suites(__suites...) \ - __kunit_test_suites(CONCATENATE(__UNIQUE_ID(array), _probe), \ + __kunit_init_test_suites(__UNIQUE_ID(array), \ ##__suites) #define kunit_test_init_section_suite(suite) \ diff --git a/include/linux/module.h b/include/linux/module.h index a98e188cf37b..9cd0009bd050 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -540,6 +540,8 @@ struct module { struct static_call_site *static_call_sites; #endif #if IS_ENABLED(CONFIG_KUNIT) + int num_kunit_init_suites; + struct kunit_suite **kunit_init_suites; int num_kunit_suites; struct kunit_suite **kunit_suites; #endif diff --git a/kernel/module/main.c b/kernel/module/main.c index 98fedfdb8db5..36681911c05a 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2199,6 +2199,9 @@ static int find_module_sections(struct module *mod, struct load_info *info) mod->kunit_suites = section_objs(info, ".kunit_test_suites", sizeof(*mod->kunit_suites), &mod->num_kunit_suites); + mod->kunit_init_suites = section_objs(info, ".kunit_init_test_suites", + sizeof(*mod->kunit_init_suites), + &mod->num_kunit_init_suites); #endif mod->extable = section_objs(info, "__ex_table", diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c index 1236b3cd2fbb..847329c51e91 100644 --- a/lib/kunit/executor.c +++ b/lib/kunit/executor.c @@ -12,6 +12,8 @@ */ extern struct kunit_suite * const __kunit_suites_start[]; extern struct kunit_suite * const __kunit_suites_end[]; +extern struct kunit_suite * const __kunit_init_suites_start[]; +extern struct kunit_suite * const __kunit_init_suites_end[]; static char *action_param; @@ -292,6 +294,33 @@ void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr) } } +struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, + struct kunit_suite_set suite_set) +{ + struct kunit_suite_set total_suite_set = {NULL, NULL}; + struct kunit_suite **total_suite_start = NULL; + size_t init_num_suites, num_suites, suite_size; + + init_num_suites = init_suite_set.end - init_suite_set.start; + num_suites = suite_set.end - suite_set.start; + suite_size = sizeof(suite_set.start); + + /* Allocate memory for array of all kunit suites */ + total_suite_start = kmalloc_array(init_num_suites + num_suites, suite_size, GFP_KERNEL); + if (!total_suite_start) + return total_suite_set; + + /* Append init suites and then all other kunit suites */ + memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size); + memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size); + + /* Set kunit suite set start and end */ + total_suite_set.start = total_suite_start; + total_suite_set.end = total_suite_start + (init_num_suites + num_suites); + + return total_suite_set; +} + #if IS_BUILTIN(CONFIG_KUNIT) static char *kunit_shutdown; @@ -313,21 +342,41 @@ static void kunit_handle_shutdown(void) int kunit_run_all_tests(void) { - struct kunit_suite_set suite_set = { + struct kunit_suite_set suite_set = {NULL, NULL}; + struct kunit_suite_set filtered_suite_set = {NULL, NULL}; + struct kunit_suite_set init_suite_set = { + __kunit_init_suites_start, __kunit_init_suites_end, + }; + struct kunit_suite_set normal_suite_set = { __kunit_suites_start, __kunit_suites_end, }; + size_t init_num_suites = init_suite_set.end - init_suite_set.start; int err = 0; + + if (init_num_suites > 0) { + suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set); + if (!suite_set.start) + goto out; + } else + suite_set = normal_suite_set; + if (!kunit_enabled()) { pr_info("kunit: disabled\n"); - goto out; + goto free_out; } if (filter_glob_param || filter_param) { - suite_set = kunit_filter_suites(&suite_set, filter_glob_param, + filtered_suite_set = kunit_filter_suites(&suite_set, filter_glob_param, filter_param, filter_action_param, &err); + + /* Free original suite set before using filtered suite set */ + if (init_num_suites > 0) + kfree(suite_set.start); + suite_set = filtered_suite_set; + if (err) { pr_err("kunit executor: error filtering suites: %d\n", err); - goto out; + goto free_out; } } @@ -340,9 +389,12 @@ int kunit_run_all_tests(void) else pr_err("kunit executor: unknown action '%s'\n", action_param); - if (filter_glob_param || filter_param) { /* a copy was made of each suite */ +free_out: + if (filter_glob_param || filter_param) kunit_free_suite_set(suite_set); - } + else if (init_num_suites > 0) + /* Don't use kunit_free_suite_set because suites aren't individually allocated */ + kfree(suite_set.start); out: kunit_handle_shutdown(); diff --git a/lib/kunit/test.c b/lib/kunit/test.c index f2eb71f1a66c..8bae6e2bc6a0 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -704,28 +704,40 @@ EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); #ifdef CONFIG_MODULES static void kunit_module_init(struct module *mod) { - struct kunit_suite_set suite_set = { + struct kunit_suite_set suite_set, filtered_set; + struct kunit_suite_set normal_suite_set = { mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites, }; + struct kunit_suite_set init_suite_set = { + mod->kunit_init_suites, mod->kunit_init_suites + mod->num_kunit_init_suites, + }; const char *action = kunit_action(); int err = 0; - suite_set = kunit_filter_suites(&suite_set, + if (mod->num_kunit_init_suites > 0) + suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set); + else + suite_set = normal_suite_set; + + filtered_set = kunit_filter_suites(&suite_set, kunit_filter_glob() ?: "*.*", kunit_filter(), kunit_filter_action(), &err); if (err) pr_err("kunit module: error filtering suites: %d\n", err); - mod->kunit_suites = (struct kunit_suite **)suite_set.start; - mod->num_kunit_suites = suite_set.end - suite_set.start; + mod->kunit_suites = (struct kunit_suite **)filtered_set.start; + mod->num_kunit_suites = filtered_set.end - filtered_set.start; + + if (mod->num_kunit_init_suites > 0) + kfree(suite_set.start); if (!action) - kunit_exec_run_tests(&suite_set, false); + kunit_exec_run_tests(&filtered_set, false); else if (!strcmp(action, "list")) - kunit_exec_list_tests(&suite_set, false); + kunit_exec_list_tests(&filtered_set, false); else if (!strcmp(action, "list_attr")) - kunit_exec_list_tests(&suite_set, true); + kunit_exec_list_tests(&filtered_set, true); else pr_err("kunit: unknown action '%s'\n", action); } From patchwork Mon Dec 4 22:19:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479158 X-Patchwork-Delegate: brendanhiggins@google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZKGiKz/m" Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8F60113 for ; Mon, 4 Dec 2023 14:19:41 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5caf61210e3so70273947b3.0 for ; Mon, 04 Dec 2023 14:19:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728381; x=1702333181; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FcCQtNmXpvtLNOrtNSc8AW+fCSTy7TRIqx1fWXO6kAs=; b=ZKGiKz/m9hGw7LczcQpuxT3EhjjjO87Nzy4G/3By6FqipWn8u12Tz3dkSc1CykkjZL BD86sdoNv+tqPoKuvlGV3YAPB0vumPi6mhXTcXzIocbIMt49cXIAGnnv0KF5wS/NeQYT IJxkUvLFPPj2Xu25KNXnxasSi67aTnhlMyI7K48B+8zleccPh+GdSY4KwUGEwKfKdNXR OsRgY5db7IMr3i1EIKLBkWdE0akV03KeZcVy0VCRSlWu2xK+3kIMgQeFpvHjWdPLbUFg nEB7FxCTvYGGU3VuzQ/LGz7mb7tJYPS1KVIiWv1J3iD8P1PsUGV1HYJxUmhtW7SnpMAe 2FFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728381; x=1702333181; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FcCQtNmXpvtLNOrtNSc8AW+fCSTy7TRIqx1fWXO6kAs=; b=DcWTE4YI2nucNtbujh4Qcopr1YKB3peCpfl2B3Y9Kw4Tu0bx7Yb6C+zmhKhu2H+xac m/ajWnlaNOP6A25QEInRqvoKSGX1LFeE8cCeXcH9bgwT4hpMboB1b36jFwvO2EDGAr6c 96fAXKSiGZ0vlIT99k5QyroebkGsMlZL2QZNoS5CzZmAwO3xzH5fm0xLIc7AxwqkcweO wxqFw7waiF5rcyTnZIK0jUa2SKLAiTbm3Zmzveo9M0vhO7qRdQHyZtv4xc7I8MJvurU+ xRbifJkNkm9IQrue1hh009zOXkhSAZ+0/q0fHOyN8uh54LeTHx4oe22PTXac8ocNNBZ1 3d4w== X-Gm-Message-State: AOJu0YxCzTjoDjzLdw9CR+aXzISqqt1YXlgft4FpdbVlxVacZUWJS2Br O45Pc1kNBMrQ0R4HIs0M25roJZQBOw== X-Google-Smtp-Source: AGHT+IFEt3AEceWIxqns4ZzJfYejgTDzoUUTYAgBnKLjQfaNWDnwUkVOcY3Scc+JgzZPWiTKdUHhbNE0uA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a25:687:0:b0:db4:5e66:4a05 with SMTP id 129-20020a250687000000b00db45e664a05mr949795ybg.2.1701728381191; Mon, 04 Dec 2023 14:19:41 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:28 +0000 In-Reply-To: <20231204221932.1465004-1-rmoar@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231204221932.1465004-1-rmoar@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-3-rmoar@google.com> Subject: [PATCH v3 3/6] kunit: add example suite to test init suites From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Add example_init_test_suite to allow for testing the feature of running test suites marked as init to indicate they use init data and/or functions. This suite should always pass and uses a simple init function. This suite can also be used to test the is_init attribute introduced in the next patch. Signed-off-by: Rae Moar Reviewed-by: David Gow --- lib/kunit/kunit-example-test.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index 6bb5c2ef6696..262a68a59800 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -287,4 +287,33 @@ static struct kunit_suite example_test_suite = { */ kunit_test_suites(&example_test_suite); +static int __init init_add(int x, int y) +{ + return (x + y); +} + +/* + * This test should always pass. Can be used to test init suites. + */ +static void example_init_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, init_add(1, 1), 2); +} + +static struct kunit_case example_init_test_cases[] = { + KUNIT_CASE(example_init_test), + {} +}; + +static struct kunit_suite example_init_test_suite = { + .name = "example_init", + .test_cases = example_init_test_cases, +}; + +/* + * This registers the test suite and marks the suite as using init data + * and/or functions. + */ +kunit_test_init_section_suites(&example_init_test_suite); + MODULE_LICENSE("GPL v2"); From patchwork Mon Dec 4 22:19:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479159 X-Patchwork-Delegate: brendanhiggins@google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zd1C37Cj" Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 808F5136 for ; Mon, 4 Dec 2023 14:19:43 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d8d3271ff5so18996287b3.2 for ; Mon, 04 Dec 2023 14:19:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728382; x=1702333182; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FbKw92LwzxqFMUL9Ga0oOICdSX1CkrZsFK7gwu7z4qs=; b=zd1C37Cj2xtdk9jCiPLarmSLsnGa+vzL7ytwVlrPmbY/bsUz0Rl3gU32DI8qcl1alG /xR0SjCH9iNuJn+kBEwyGbYkK+INcks3VG3CnExAkPwZpGSDl1FaUdPLKf+OlDs3/l8k sgBtqSBI7sI2XQXcvHodguUvNc9smn+rNNEfjxQvf3EYPjpkKG0b3GuWXViyVoQkB8/S /GXZwnUXm6nwzjHyCvdY+QL5C2Ubq7xpocrmBInBAbCRLLj5cSAak54stZnKI3lIzOj0 4StzBMrqiUBEqKQJz6YM4EI+O60yFWA1GdFiMmdmae3dDqULnbqpXuRiqh6vadyAaHr0 Ubgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728382; x=1702333182; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FbKw92LwzxqFMUL9Ga0oOICdSX1CkrZsFK7gwu7z4qs=; b=TChaziqxuORnMWsea7kLtJ6iVY7msr6YohG8LQ/Cov8Z1XTiSp3SjC2RBaERzBeQQd VNifLYgsPMQ+FpWDTcX97LTU6mBlH6ClAMMRAng2mdyRtU9XXkrcVu8sSCbi2F6mIGNP 8fS+6XOj3a2gOIMciwD5NuJYYn2mEib0UgmqfjVLterLxcu0izBKqbheH/4YCHb3BOTf p3lFNygIBl/zmYPoWTEa2OPWItmYK2y+OUITl2Y02vrW2kdDdIE8Hw1mTW7lw0oqQpuC q2ZM89C573ciQGxhjmx/ZXQeodUGI8C3N+XUzd5hjbwKehHssdp7+4oTtnnPMhm2o2xZ xkkA== X-Gm-Message-State: AOJu0YzWiMZ2sUEH9QZOEITC4mz7Sx1ZUw6BfUh8egHdH52m/U/kwTHl AmF7g0JzFf7lIieaubBYZDBfHzLfHw== X-Google-Smtp-Source: AGHT+IE/Juv/ilq0n8BRZJDwim3hTfDwC3y2oS6G+zSu0NFvfggKc7aq+kR+KKtxvzBC3vexvVf7MpQqWQ== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a05:690c:98a:b0:5d4:2ff3:d280 with SMTP id ce10-20020a05690c098a00b005d42ff3d280mr320818ywb.7.1701728382783; Mon, 04 Dec 2023 14:19:42 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:29 +0000 In-Reply-To: <20231204221932.1465004-1-rmoar@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231204221932.1465004-1-rmoar@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-4-rmoar@google.com> Subject: [PATCH v3 4/6] kunit: add is_init test attribute From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Add is_init test attribute of type bool. Add to_string, get, and filter methods to lib/kunit/attributes.c. Mark each of the tests in the init section with the is_init=true attribute. Add is_init to the attributes documentation. Signed-off-by: Rae Moar --- .../dev-tools/kunit/running_tips.rst | 7 +++ include/kunit/test.h | 3 + lib/kunit/attributes.c | 60 +++++++++++++++++++ lib/kunit/executor.c | 6 +- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Documentation/dev-tools/kunit/running_tips.rst b/Documentation/dev-tools/kunit/running_tips.rst index 766f9cdea0fa..024e9ad1d1e9 100644 --- a/Documentation/dev-tools/kunit/running_tips.rst +++ b/Documentation/dev-tools/kunit/running_tips.rst @@ -428,3 +428,10 @@ This attribute indicates the name of the module associated with the test. This attribute is automatically saved as a string and is printed for each suite. Tests can also be filtered using this attribute. + +``is_init`` + +This attribute indicates whether the test uses init data or functions. + +This attribute is automatically saved as a boolean and tests can also be +filtered using this attribute. diff --git a/include/kunit/test.h b/include/kunit/test.h index 06e826a0b894..65583782903d 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -82,6 +82,9 @@ enum kunit_speed { /* Holds attributes for each test case and suite */ struct kunit_attributes { enum kunit_speed speed; + + /* private: internal use only */ + bool is_init; }; /** diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c index 1b512f7e1838..ddacec6a3337 100644 --- a/lib/kunit/attributes.c +++ b/lib/kunit/attributes.c @@ -58,6 +58,16 @@ static const char *attr_enum_to_string(void *attr, const char * const str_list[] return str_list[val]; } +static const char *attr_bool_to_string(void *attr, bool *to_free) +{ + bool val = (bool)attr; + + *to_free = false; + if (val) + return "true"; + return "false"; +} + static const char *attr_speed_to_string(void *attr, bool *to_free) { return attr_enum_to_string(attr, speed_str_list, to_free); @@ -166,6 +176,37 @@ static int attr_string_filter(void *attr, const char *input, int *err) return false; } +static int attr_bool_filter(void *attr, const char *input, int *err) +{ + int i, input_int = -1; + long val = (long)attr; + const char *input_str = NULL; + + for (i = 0; input[i]; i++) { + if (!strchr(op_list, input[i])) { + input_str = input + i; + break; + } + } + + if (!input_str) { + *err = -EINVAL; + pr_err("kunit executor: filter value not found: %s\n", input); + return false; + } + + if (!strcmp(input_str, "true")) + input_int = (int)true; + else if (!strcmp(input_str, "false")) + input_int = (int)false; + else { + *err = -EINVAL; + pr_err("kunit executor: invalid filter input: %s\n", input); + return false; + } + + return int_filter(val, input, input_int, err); +} /* Get Attribute Methods */ @@ -194,6 +235,17 @@ static void *attr_module_get(void *test_or_suite, bool is_test) return (void *) ""; } +static void *attr_is_init_get(void *test_or_suite, bool is_test) +{ + struct kunit_suite *suite = is_test ? NULL : test_or_suite; + struct kunit_case *test = is_test ? test_or_suite : NULL; + + if (test) + return ((void *) test->attr.is_init); + else + return ((void *) suite->attr.is_init); +} + /* List of all Test Attributes */ static struct kunit_attr kunit_attr_list[] = { @@ -212,6 +264,14 @@ static struct kunit_attr kunit_attr_list[] = { .filter = attr_string_filter, .attr_default = (void *)"", .print = PRINT_SUITE, + }, + { + .name = "is_init", + .get_attr = attr_is_init_get, + .to_string = attr_bool_to_string, + .filter = attr_bool_filter, + .attr_default = (void *)false, + .print = PRINT_SUITE, } }; diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c index 847329c51e91..594de994161a 100644 --- a/lib/kunit/executor.c +++ b/lib/kunit/executor.c @@ -300,6 +300,7 @@ struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_ struct kunit_suite_set total_suite_set = {NULL, NULL}; struct kunit_suite **total_suite_start = NULL; size_t init_num_suites, num_suites, suite_size; + int i = 0; init_num_suites = init_suite_set.end - init_suite_set.start; num_suites = suite_set.end - suite_set.start; @@ -310,8 +311,11 @@ struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_ if (!total_suite_start) return total_suite_set; - /* Append init suites and then all other kunit suites */ + /* Append and mark init suites and then append all other kunit suites */ memcpy(total_suite_start, init_suite_set.start, init_num_suites * suite_size); + for (i = 0; i < init_num_suites; i++) + total_suite_start[i]->attr.is_init = true; + memcpy(total_suite_start + init_num_suites, suite_set.start, num_suites * suite_size); /* Set kunit suite set start and end */ From patchwork Mon Dec 4 22:19:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479160 X-Patchwork-Delegate: brendanhiggins@google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BaRuDXqD" 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 67B01188 for ; Mon, 4 Dec 2023 14:19:45 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db5416d0fccso2700739276.1 for ; Mon, 04 Dec 2023 14:19:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728384; x=1702333184; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R10zjW0JVsp8M4qo1xhNAkM6zfuGofXdUJMDPp7V66o=; b=BaRuDXqDWbREMkH5y2FpCN+H6VV89rt+pQUcWxSLAIJzD6Qu0ygl3Ht931FdVuUOUp GNp2byCZ94V6JaUZg42oMXxwrMiLXw+FdhCzfV2KZfZtq6l2LzF6uWpUpg/pIxnA/wFu rh8TzbvJuaYhvwMcKRgAfdXAt11d6M+k38eL08cL2BOALyOFDUcqEMpMhFvbLYQbRLJh ujBi8sHUosLL8zKVzBByyF1JGkIz0s61iH8m67gx0P0jcKHHnHHzAzj93GhjVnabXzvV kaxbkF4o//s8FhUi8zTn3YLqOAd+hPG/QWjCS6AB+Fh0iXjQBOJN+X+RErrHYr41UoZD WHrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728384; x=1702333184; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R10zjW0JVsp8M4qo1xhNAkM6zfuGofXdUJMDPp7V66o=; b=W4rc1ic6QPjmFI2vV1U3MPuMTFIPZmXpBRi6XeObLbmy4Hg+gs+nqmx9BPzhn76Zvc i+bhIx+9O6yLbsnmvMfxHQZK5l+97sDWw5WuWUXm2bg5T6ERjo4JQJieZK9Apo+1diT0 6kxPyLCZthZCJcoT88zA+Uc8G+KMfcMn/vl8lksLoEnRYvcFoR9lW7w0n6vBGNyYin6f fBsxtV2Ap4mJZHTEYzgnNVSf3b44yy3yw939xlDSYztvw2/7bz6DM6pfpwygfPIDwd7Y gVoUDALpH7Bboy3YJvGBHD7x7gQmTm33smS9XI9gn8t4i2LWJCzjtHGDd1n2oAMd9rEz TcFQ== X-Gm-Message-State: AOJu0YxuDwjMM8R4LSTB/kEol6U4+iWlGexP7QD9UdZXBRUphPESl/o9 Er4VrM+SptClbHQ4IOuZRKzzLFlA9Q== X-Google-Smtp-Source: AGHT+IHNfy9Vk4gwqHkiSrh5NojiSYAW0yNLm2Rr7Kv9KdzVBIWaEBgpLQlFQQgTvxlSw5wFF19+m0P/LA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a25:3106:0:b0:da3:723b:b2a4 with SMTP id x6-20020a253106000000b00da3723bb2a4mr867924ybx.7.1701728384660; Mon, 04 Dec 2023 14:19:44 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:30 +0000 In-Reply-To: <20231204221932.1465004-1-rmoar@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231204221932.1465004-1-rmoar@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-5-rmoar@google.com> Subject: [PATCH v3 5/6] kunit: add ability to run tests after boot using debugfs From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Add functionality to run built-in tests after boot by writing to a debugfs file. Add a new debugfs file labeled "run" for each test suite to use for this purpose. As an example, write to the file using the following: echo "any string" > /sys/kernel/debugfs/kunit//run This will trigger the test suite to run and will print results to the kernel log. To guard against running tests concurrently with this feature, add a mutex lock around running kunit. This supports the current practice of not allowing tests to be run concurrently on the same kernel. This new functionality could be used to design a parameter injection feature in the future. Signed-off-by: Rae Moar Reviewed-by: David Gow --- Changes since v2: - Move resetting the log to test.c - Add is_init attribute and patches to change linker sections to avoid re-running tests that use init data and functions lib/kunit/debugfs.c | 68 +++++++++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 10 +++++++ 2 files changed, 78 insertions(+) diff --git a/lib/kunit/debugfs.c b/lib/kunit/debugfs.c index 270d185737e6..2e0a92a0c461 100644 --- a/lib/kunit/debugfs.c +++ b/lib/kunit/debugfs.c @@ -8,12 +8,14 @@ #include #include +#include #include "string-stream.h" #include "debugfs.h" #define KUNIT_DEBUGFS_ROOT "kunit" #define KUNIT_DEBUGFS_RESULTS "results" +#define KUNIT_DEBUGFS_RUN "run" /* * Create a debugfs representation of test suites: @@ -21,6 +23,8 @@ * Path Semantics * /sys/kernel/debug/kunit//results Show results of last run for * testsuite + * /sys/kernel/debug/kunit//run Write to this file to trigger + * testsuite to run * */ @@ -99,6 +103,51 @@ static int debugfs_results_open(struct inode *inode, struct file *file) return single_open(file, debugfs_print_results, suite); } +/* + * Print a usage message to the debugfs "run" file + * (/sys/kernel/debug/kunit//run) if opened. + */ +static int debugfs_print_run(struct seq_file *seq, void *v) +{ + struct kunit_suite *suite = (struct kunit_suite *)seq->private; + + seq_puts(seq, "Write to this file to trigger the test suite to run.\n"); + seq_printf(seq, "usage: echo \"any string\" > /sys/kernel/debugfs/kunit/%s/run\n", + suite->name); + return 0; +} + +/* + * The debugfs "run" file (/sys/kernel/debug/kunit//run) + * contains no information. Write to the file to trigger the test suite + * to run. + */ +static int debugfs_run_open(struct inode *inode, struct file *file) +{ + struct kunit_suite *suite; + + suite = (struct kunit_suite *)inode->i_private; + + return single_open(file, debugfs_print_run, suite); +} + +/* + * Trigger a test suite to run by writing to the suite's "run" debugfs + * file found at: /sys/kernel/debug/kunit//run + * + * Note: what is written to this file will not be saved. + */ +static ssize_t debugfs_run(struct file *file, + const char __user *buf, size_t count, loff_t *ppos) +{ + struct inode *f_inode = file->f_inode; + struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private; + + __kunit_test_suites_init(&suite, 1); + + return count; +} + static const struct file_operations debugfs_results_fops = { .open = debugfs_results_open, .read = seq_read, @@ -106,10 +155,22 @@ static const struct file_operations debugfs_results_fops = { .release = debugfs_release, }; +static const struct file_operations debugfs_run_fops = { + .open = debugfs_run_open, + .read = seq_read, + .write = debugfs_run, + .llseek = seq_lseek, + .release = debugfs_release, +}; + void kunit_debugfs_create_suite(struct kunit_suite *suite) { struct kunit_case *test_case; + /* If suite log already allocated, do not create new debugfs files. */ + if (suite->log) + return; + /* Allocate logs before creating debugfs representation. */ suite->log = alloc_string_stream(GFP_KERNEL); string_stream_set_append_newlines(suite->log, true); @@ -124,6 +185,13 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite) debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444, suite->debugfs, suite, &debugfs_results_fops); + + /* Do not create file to re-run test if test runs on init */ + if (!suite->attr.is_init) { + debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644, + suite->debugfs, + suite, &debugfs_run_fops); + } } void kunit_debugfs_destroy_suite(struct kunit_suite *suite) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 8bae6e2bc6a0..58e46bb3b4c4 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,8 @@ #include "string-stream.h" #include "try-catch-impl.h" +static DEFINE_MUTEX(kunit_run_lock); + /* * Hook to fail the current test and print an error message to the log. */ @@ -654,6 +657,7 @@ static void kunit_init_suite(struct kunit_suite *suite) kunit_debugfs_create_suite(suite); suite->status_comment[0] = '\0'; suite->suite_init_err = 0; + string_stream_clear(suite->log); } bool kunit_enabled(void) @@ -670,6 +674,11 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ return 0; } + /* Use mutex lock to guard against running tests concurrently. */ + if (mutex_lock_interruptible(&kunit_run_lock)) { + pr_err("kunit: test interrupted\n"); + return -EINTR; + } static_branch_inc(&kunit_running); for (i = 0; i < num_suites; i++) { @@ -678,6 +687,7 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ } static_branch_dec(&kunit_running); + mutex_unlock(&kunit_run_lock); return 0; } EXPORT_SYMBOL_GPL(__kunit_test_suites_init); From patchwork Mon Dec 4 22:19:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13479161 X-Patchwork-Delegate: brendanhiggins@google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qU2haV9N" Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E3A7197 for ; Mon, 4 Dec 2023 14:19:47 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d351694be7so75767107b3.3 for ; Mon, 04 Dec 2023 14:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701728386; x=1702333186; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lXInaHt13KB+V9pIyuwZVrz/xxGvGn/o8w1U8yLGi6Y=; b=qU2haV9NZiWWVLABx/42Qw6bHDcwmIVttFsTWErKV2+dEnYdLgZ+2TRmJMtjHg6CRV 84BVrL1H/dOc/zNGxYiwjEfLx4Z/vapizGqdFFSo57xaD8g+utjtDBh7KpAXOrbiPeoe tGs9orevXgwD0chywh9iKzsXOHZAiukgw/iwoaFMrBiIN2cpvqS0h+k/zpLbGv7ElqwO juqHcQiw29xGXOB56g0wCQZInqJr3nB9DSsilB4u5+CboW5Uku3WIC/zkd3v9v1s/gWj 8WMqDEo75rwkVI93/cIBmIERegDqg0YR87MVtJuXi62ufduUfqauTgB2jeWCy5qGzDcw KCTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701728386; x=1702333186; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lXInaHt13KB+V9pIyuwZVrz/xxGvGn/o8w1U8yLGi6Y=; b=aiC5yOboCwr5lr6gV1CLHCSoAe1QElvaAqgWE310LgCDldU9uIV8GjHq0Y7iDAhYNK ja84XXrLD/0fz5L20rcMXBskN9y01hrUsh3v6BwE/hCzIu2P2Rjap8csJCOPE88djiBF L/+AYLjYMWVylxcI+p+6iglRWrJA1VI3cdfPc2IWHwg31DAQXjQBdHz015xSGYUMC8z1 re57aNs/lBZwh3u/qtXYYzz9uKNNW29TLyEWha2rDAGE/FoFaGxTUyvWzA9uPRrj8A+d +HeFRWaHYK//dZ5ifVoOr97ZfEXZKmI1ZnoDhgqz0M9GiZ2RAGWFQk/PkMx+H6xWDwsd PcmA== X-Gm-Message-State: AOJu0YwiYCpnxkxH4FdcvitFN7w3+kiclkQx1ME9BLWdKFPPRYfhSO8l 2UHg0t4SHz9rYgR8gG0teM3WUKIu7g== X-Google-Smtp-Source: AGHT+IHynJIC5QGUDpxsV5RmDAee4mt7x7k1mIDUotJWtWP+9oiPNk2RReFWkIP59RbKajgfBsafs1HFcA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a5b:c50:0:b0:db7:e75c:24c1 with SMTP id d16-20020a5b0c50000000b00db7e75c24c1mr172123ybr.9.1701728386307; Mon, 04 Dec 2023 14:19:46 -0800 (PST) Date: Mon, 4 Dec 2023 22:19:31 +0000 In-Reply-To: <20231204221932.1465004-1-rmoar@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231204221932.1465004-1-rmoar@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231204221932.1465004-6-rmoar@google.com> Subject: [PATCH v3 6/6] Documentation: Add debugfs docs with run after boot From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: keescook@chromium.org, arnd@arndb.de, linux-kselftest@vger.kernel.org, linux-arch@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Expand the documentation on the KUnit debugfs filesystem on the run_manual.rst page. Add section describing how to access results using debugfs. Add section describing how to run tests after boot using debugfs. Signed-off-by: Rae Moar Reviewed-by: David Gow --- Changes since v2: - Add info to documentation about cleaning up data, init tests, and running tests concurrently Documentation/dev-tools/kunit/run_manual.rst | 49 ++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/Documentation/dev-tools/kunit/run_manual.rst b/Documentation/dev-tools/kunit/run_manual.rst index e7b46421f247..aebb52ba9605 100644 --- a/Documentation/dev-tools/kunit/run_manual.rst +++ b/Documentation/dev-tools/kunit/run_manual.rst @@ -49,9 +49,50 @@ loaded. The results will appear in TAP format in ``dmesg``. +debugfs +======= + +``debugfs`` is a file system that enables user interaction with the files to +make kernel information available to user space (See more information at +Documentation/filesystems/debugfs.html) + +By default, only the root user has access to the debugfs directory. + +If ``CONFIG_KUNIT_DEBUGFS`` is enabled, you can use KUnit debugfs +filesystem to perform the following actions. + +Retrieve Test Results +===================== + +You can use debugfs to retrieve KUnit test results. The test results are +accessible from the debugfs filesystem in the following read-only file: + +.. code-block :: bash + + /sys/kernel/debug/kunit//results + +The test results are available in KTAP format. + +Run Tests After Kernel Has Booted +================================= + +You can use the debugfs filesystem to trigger built-in tests to run after +boot. To run the test suite, you can use the following command to write to +the ``/sys/kernel/debug/kunit//run`` file: + +.. code-block :: bash + + echo "any string" > /sys/kernel/debugfs/kunit//run + +As a result, the test suite runs and the results are printed to the kernel +log. + +However, this feature is not available with KUnit tests that use init data. + +Also, you cannot use this feature to run tests concurrently as there is a +mutex lock around running KUnit tests at the same time. + .. note :: - If ``CONFIG_KUNIT_DEBUGFS`` is enabled, KUnit test results will - be accessible from the ``debugfs`` filesystem (if mounted). - They will be in ``/sys/kernel/debug/kunit//results``, in - TAP format. + For test authors, to use this feature, tests will need to correctly initialise + and/or clean up any data, so the test runs correctly a second time.