From patchwork Fri Feb 12 09:45:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 12084953 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E762C433DB for ; Fri, 12 Feb 2021 09:46:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DA23B60241 for ; Fri, 12 Feb 2021 09:46:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230209AbhBLJqq (ORCPT ); Fri, 12 Feb 2021 04:46:46 -0500 Received: from mga03.intel.com ([134.134.136.65]:64846 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbhBLJqm (ORCPT ); Fri, 12 Feb 2021 04:46:42 -0500 IronPort-SDR: zKHwKHEfXD32G1M6ecg3K5gW1LYisMYlxYqxhEmB8MX/sk972sgRE6tP9NvjgNmkBXSFS1nPbf oKuDlG6lauJw== X-IronPort-AV: E=McAfee;i="6000,8403,9892"; a="182458953" X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="182458953" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:00 -0800 IronPort-SDR: VK1XWYuWz/RSvWJKt4rtTqGXJ82VKH5OhHmHuIwh9wqyEFC0/EE+QFflExeknadXoTPRz+sRGE ihZM/UF23UYg== X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="381387359" Received: from aaramire-mobl2.amr.corp.intel.com (HELO ldmartin-desk1.intel.com) ([10.212.210.126]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:45:59 -0800 From: Lucas De Marchi To: linux-modules@vger.kernel.org Cc: Jiri Slaby , Jessica Yu , Giovanni Gherdovich Subject: [PATCH 1/4] libkmod-config: revamp kcmdline parsing into a state machine Date: Fri, 12 Feb 2021 01:45:21 -0800 Message-Id: <20210212094524.170861-1-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 Precedence: bulk List-ID: The handling of spaces and quotes is becoming hard to maintain. Convert the parser into a state machine so we can check all the states. This should make it easier to fix a corner case we have right now: The kernel also accepts a quote before the module name instead of the value. But this additional is left for later. This is purely an algorithm change with no behavior change. --- libkmod/libkmod-config.c | 86 ++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index 971f20b..d3cd10d 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -499,7 +499,14 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) char buf[KCMD_LINE_SIZE]; int fd, err; char *p, *modname, *param = NULL, *value = NULL; - bool is_quoted = false, is_module = true; + bool is_quoted = false, iter = true; + enum state { + STATE_IGNORE, + STATE_MODNAME, + STATE_PARAM, + STATE_VALUE, + STATE_COMPLETE, + } state; fd = open("/proc/cmdline", O_RDONLY|O_CLOEXEC); if (fd < 0) { @@ -516,54 +523,65 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) return err; } - for (p = buf, modname = buf; *p != '\0' && *p != '\n'; p++) { - if (*p == '"') { + state = STATE_MODNAME; + for (p = buf, modname = buf; iter; p++) { + switch (*p) { + case '"': is_quoted = !is_quoted; - - if (is_quoted) { - /* don't consider a module until closing quotes */ - is_module = false; - } else if (param != NULL && value != NULL) { + break; + case '\0': + case '\n': + /* Stop iterating on new chars */ + iter = false; + /* fall-through */ + case ' ': + if (is_quoted && state == STATE_VALUE) { + /* no state change*/; + } else if (is_quoted) { + /* spaces are only allowed in the value part */ + state = STATE_IGNORE; + } else if (state == STATE_VALUE || state == STATE_PARAM) { + *p = '\0'; + state = STATE_COMPLETE; + } else { /* - * If we are indeed expecting a value and - * closing quotes, then this can be considered - * a valid option for a module + * go to next option, ignoring any possible + * partial match we have */ - is_module = true; + modname = p + 1; + state = STATE_MODNAME; } - - continue; - } - if (is_quoted) - continue; - - switch (*p) { - case ' ': - *p = '\0'; - if (is_module) - kcmdline_parse_result(config, modname, param, value); - param = value = NULL; - modname = p + 1; - is_module = true; break; case '.': - if (param == NULL) { + if (state == STATE_MODNAME) { *p = '\0'; param = p + 1; + state = STATE_PARAM; + } else if (state == STATE_PARAM) { + state = STATE_IGNORE; } break; case '=': - if (param != NULL) + if (state == STATE_PARAM) { + /* + * Don't set *p to '\0': the value var shadows + * param + */ value = p + 1; - else - is_module = false; + state = STATE_VALUE; + } else if (state == STATE_MODNAME) { + state = STATE_IGNORE; + } break; } - } - *p = '\0'; - if (is_module) - kcmdline_parse_result(config, modname, param, value); + if (state == STATE_COMPLETE) { + kcmdline_parse_result(config, modname, param, value); + /* start over on next iteration */ + modname = p + 1; + state = STATE_MODNAME; + } + } return 0; } From patchwork Fri Feb 12 09:45:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 12084951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A5EDC433E0 for ; Fri, 12 Feb 2021 09:46:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3A16960241 for ; Fri, 12 Feb 2021 09:46:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230235AbhBLJqp (ORCPT ); Fri, 12 Feb 2021 04:46:45 -0500 Received: from mga03.intel.com ([134.134.136.65]:64847 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230209AbhBLJqm (ORCPT ); Fri, 12 Feb 2021 04:46:42 -0500 IronPort-SDR: YRvln5xa1OdsoOn5EI+rAo9T7ejPszNsQO84NSUmucxhrVBWy//rPX1H17Z8S9Yy2FMOtdswlv 66fMt1sqibZw== X-IronPort-AV: E=McAfee;i="6000,8403,9892"; a="182458955" X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="182458955" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:01 -0800 IronPort-SDR: abaxuMQihDo3cfTj8tzKYXXiNIUKkdLO8b/RXsbR1Gj57HmsI+9Cuevh2ULbw0ZTDnT3Yy7vrY 9C1y0UUqyNzw== X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="381387367" Received: from aaramire-mobl2.amr.corp.intel.com (HELO ldmartin-desk1.intel.com) ([10.212.210.126]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:45:59 -0800 From: Lucas De Marchi To: linux-modules@vger.kernel.org Cc: Jiri Slaby , Jessica Yu , Giovanni Gherdovich Subject: [PATCH 2/4] libkmod-config: re-quote option from kernel cmdline Date: Fri, 12 Feb 2021 01:45:22 -0800 Message-Id: <20210212094524.170861-2-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210212094524.170861-1-lucas.demarchi@intel.com> References: <20210212094524.170861-1-lucas.demarchi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: It was reported that grub mangles the kernel cmdline. It turns acpi_cpufreq.dyndbg="file drivers/cpufreq/acpi-cpufreq.c +mpf" into "acpi_cpufreq.dyndbg=file drivers/cpufreq/acpi-cpufreq.c +mpf" However, even though we could blame grub for doing that, the kernel happily accepts and re-quotes it when the module is built-in. So, it's better if kmod also understands it this way and does the same. Here we basically add additional code to un-mangle it, moving the quote in way that is acceptable to pass through init_module(). Note that the interface [f]init_module() gives us mandates the quote to be part of the value: the module name is not passed and the options are separated by space. Reported-by: Jiri Slaby Link: https://bugzilla.suse.com/show_bug.cgi?id=1181111#c10 --- libkmod/libkmod-config.c | 36 ++++++++++++- .../module-param-kcmdline7/correct.txt | 5 ++ .../module-param-kcmdline7/correct.txt | 5 ++ .../module-param-kcmdline7/proc/cmdline | 1 + .../module-param-kcmdline7/proc/cmdline | 1 + .../module-param-kcmdline8/correct.txt | 5 ++ .../module-param-kcmdline7/correct.txt | 5 ++ .../module-param-kcmdline7/proc/cmdline | 1 + .../module-param-kcmdline8/proc/cmdline | 1 + testsuite/test-modprobe.c | 50 +++++++++++++++++++ 10 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/correct.txt create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/correct.txt create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/proc/cmdline create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/proc/cmdline create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/correct.txt create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/correct.txt create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/proc/cmdline create mode 100644 testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/proc/cmdline diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index d3cd10d..2873f06 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -498,7 +498,7 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) { char buf[KCMD_LINE_SIZE]; int fd, err; - char *p, *modname, *param = NULL, *value = NULL; + char *p, *p_quote_start, *modname, *param = NULL, *value = NULL; bool is_quoted = false, iter = true; enum state { STATE_IGNORE, @@ -524,10 +524,23 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) } state = STATE_MODNAME; + p_quote_start = NULL; for (p = buf, modname = buf; iter; p++) { switch (*p) { case '"': is_quoted = !is_quoted; + + /* + * only allow starting quote as first char when looking + * for a modname: anything else is considered ill-formed + */ + if (is_quoted && state == STATE_MODNAME && p == modname) { + p_quote_start = p; + modname = p + 1; + } else if (state != STATE_VALUE) { + state = STATE_IGNORE; + } + break; case '\0': case '\n': @@ -550,6 +563,7 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) */ modname = p + 1; state = STATE_MODNAME; + p_quote_start = NULL; } break; case '.': @@ -576,10 +590,30 @@ static int kmod_config_parse_kcmdline(struct kmod_config *config) } if (state == STATE_COMPLETE) { + /* + * We may need to re-quote to unmangle what the + * bootloader passed. Example: grub passes the option as + * "parport.dyndbg=file drivers/parport/ieee1284_ops.c +mpf" + * instead of + * parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf" + */ + if (p_quote_start && p_quote_start < modname) { + /* + * p_quote_start + * | + * |modname param value + * || | | + * vv v v + * "parport\0dyndbg=file drivers/parport/ieee1284_ops.c +mpf" */ + memmove(p_quote_start, modname, value - modname); + value--; modname--; param--; + *value = '"'; + } kcmdline_parse_result(config, modname, param, value); /* start over on next iteration */ modname = p + 1; state = STATE_MODNAME; + p_quote_start = NULL; } } diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/correct.txt b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/correct.txt new file mode 100644 index 0000000..d80da6d --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/correct.txt @@ -0,0 +1,5 @@ +options psmouse foo +options parport dyndbg="file drivers/parport/ieee1284_ops.c +mpf" + +# End of configuration files. Dumping indexes now: + diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/correct.txt b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/correct.txt new file mode 100644 index 0000000..d80da6d --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/correct.txt @@ -0,0 +1,5 @@ +options psmouse foo +options parport dyndbg="file drivers/parport/ieee1284_ops.c +mpf" + +# End of configuration files. Dumping indexes now: + diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/proc/cmdline b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/proc/cmdline new file mode 100644 index 0000000..86f9052 --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/module-param-kcmdline7/proc/cmdline @@ -0,0 +1 @@ +psmouse.foo parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf" quiet rw diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/proc/cmdline b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/proc/cmdline new file mode 100644 index 0000000..86f9052 --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline7/proc/cmdline @@ -0,0 +1 @@ +psmouse.foo parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf" quiet rw diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/correct.txt b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/correct.txt new file mode 100644 index 0000000..d80da6d --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/correct.txt @@ -0,0 +1,5 @@ +options psmouse foo +options parport dyndbg="file drivers/parport/ieee1284_ops.c +mpf" + +# End of configuration files. Dumping indexes now: + diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/correct.txt b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/correct.txt new file mode 100644 index 0000000..d80da6d --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/correct.txt @@ -0,0 +1,5 @@ +options psmouse foo +options parport dyndbg="file drivers/parport/ieee1284_ops.c +mpf" + +# End of configuration files. Dumping indexes now: + diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/proc/cmdline b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/proc/cmdline new file mode 100644 index 0000000..86f9052 --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/module-param-kcmdline7/proc/cmdline @@ -0,0 +1 @@ +psmouse.foo parport.dyndbg="file drivers/parport/ieee1284_ops.c +mpf" quiet rw diff --git a/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/proc/cmdline b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/proc/cmdline new file mode 100644 index 0000000..eab04ad --- /dev/null +++ b/testsuite/rootfs-pristine/test-modprobe/module-param-kcmdline8/proc/cmdline @@ -0,0 +1 @@ +psmouse.foo "parport.dyndbg=file drivers/parport/ieee1284_ops.c +mpf" quiet rw diff --git a/testsuite/test-modprobe.c b/testsuite/test-modprobe.c index f6bed8b..dbc54f3 100644 --- a/testsuite/test-modprobe.c +++ b/testsuite/test-modprobe.c @@ -359,6 +359,56 @@ DEFINE_TEST(modprobe_param_kcmdline6, ); +static noreturn int modprobe_param_kcmdline7(const struct test *t) +{ + const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; + const char *const args[] = { + progname, + "-c", + NULL, + }; + + test_spawn_prog(progname, args); + exit(EXIT_FAILURE); +} +DEFINE_TEST(modprobe_param_kcmdline7, + .description = "check if dots on other parts of kcmdline don't confuse our parser", + .config = { + [TC_UNAME_R] = "4.4.4", + [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline7", + }, + .output = { + .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline7/correct.txt", + }, + .modules_loaded = "", + ); + + +static noreturn int modprobe_param_kcmdline8(const struct test *t) +{ + const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; + const char *const args[] = { + progname, + "-c", + NULL, + }; + + test_spawn_prog(progname, args); + exit(EXIT_FAILURE); +} +DEFINE_TEST(modprobe_param_kcmdline8, + .description = "check if dots on other parts of kcmdline don't confuse our parser", + .config = { + [TC_UNAME_R] = "4.4.4", + [TC_ROOTFS] = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline8", + }, + .output = { + .out = TESTSUITE_ROOTFS "test-modprobe/module-param-kcmdline8/correct.txt", + }, + .modules_loaded = "", + ); + + static noreturn int modprobe_force(const struct test *t) { const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; From patchwork Fri Feb 12 09:45:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 12084955 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E3F9C433E6 for ; Fri, 12 Feb 2021 09:46:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EE58764E38 for ; Fri, 12 Feb 2021 09:46:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229497AbhBLJqq (ORCPT ); Fri, 12 Feb 2021 04:46:46 -0500 Received: from mga03.intel.com ([134.134.136.65]:64849 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230218AbhBLJqn (ORCPT ); Fri, 12 Feb 2021 04:46:43 -0500 IronPort-SDR: u6GIZSrqVdne6waJinyH098xjaPWLtY9N+Z+0q3xKyhBS45wyo5TktrImojySl3fy0Iq6BqZeW mwqCcPBFp4wA== X-IronPort-AV: E=McAfee;i="6000,8403,9892"; a="182458956" X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="182458956" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:01 -0800 IronPort-SDR: +nwaz3mrxqK+3PlGWyQVfDrb2FTEKRaU2wvbvEc2fHR93V/NaLL7XlsfrCrmTaxZQ8KG+3zClL 9BlBaIXqND4Q== X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="381387376" Received: from aaramire-mobl2.amr.corp.intel.com (HELO ldmartin-desk1.intel.com) ([10.212.210.126]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:00 -0800 From: Lucas De Marchi To: linux-modules@vger.kernel.org Cc: Jiri Slaby , Jessica Yu , Giovanni Gherdovich Subject: [PATCH 3/4] testsuite: allow to re-use single function for tests Date: Fri, 12 Feb 2021 01:45:23 -0800 Message-Id: <20210212094524.170861-3-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210212094524.170861-1-lucas.demarchi@intel.com> References: <20210212094524.170861-1-lucas.demarchi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: Add a new DEFINE_TEST_WITH_FUNC() that accepts the function alongside the test name. This will allow us to share a single function for different tests. --- testsuite/testsuite.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/testsuite/testsuite.h b/testsuite/testsuite.h index c74b648..44d1730 100644 --- a/testsuite/testsuite.h +++ b/testsuite/testsuite.h @@ -140,14 +140,16 @@ int test_run(const struct test *t); /* Test definitions */ -#define DEFINE_TEST(_name, ...) \ +#define DEFINE_TEST_WITH_FUNC(_name, _func, ...) \ static const struct test UNIQ(s##_name) \ __attribute__((used, section("kmod_tests"), aligned(8))) = { \ .name = #_name, \ - .func = _name, \ + .func = _func, \ ## __VA_ARGS__ \ }; +#define DEFINE_TEST(_name, ...) DEFINE_TEST_WITH_FUNC(_name, _name, __VA_ARGS__) + #define TESTSUITE_MAIN() \ extern struct test __start_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ extern struct test __stop_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ From patchwork Fri Feb 12 09:45:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lucas De Marchi X-Patchwork-Id: 12084957 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5264EC433E0 for ; Fri, 12 Feb 2021 09:47:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 211DA60241 for ; Fri, 12 Feb 2021 09:47:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230063AbhBLJrE (ORCPT ); Fri, 12 Feb 2021 04:47:04 -0500 Received: from mga03.intel.com ([134.134.136.65]:64847 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbhBLJrA (ORCPT ); Fri, 12 Feb 2021 04:47:00 -0500 IronPort-SDR: 5oRipH/8zSKQgqoqlB4odakxdCWOiGvT3u+ZaOojMHzK2II+JsWVQGL7ubcx+WQzZS56GyZp0n FfzT3ugoPavQ== X-IronPort-AV: E=McAfee;i="6000,8403,9892"; a="182458958" X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="182458958" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:02 -0800 IronPort-SDR: rM01DAaFmAG/tEvZYVEiZSdCD5CUTGyUJVydpLb7kEGZi819jwHhL+jMLAfvX7c85l9cVlDsaD jlKg7PgLRB1g== X-IronPort-AV: E=Sophos;i="5.81,173,1610438400"; d="scan'208";a="381387386" Received: from aaramire-mobl2.amr.corp.intel.com (HELO ldmartin-desk1.intel.com) ([10.212.210.126]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Feb 2021 01:46:01 -0800 From: Lucas De Marchi To: linux-modules@vger.kernel.org Cc: Jiri Slaby , Jessica Yu , Giovanni Gherdovich Subject: [PATCH 4/4] test-modprobe: share single function for kcmdline tests Date: Fri, 12 Feb 2021 01:45:24 -0800 Message-Id: <20210212094524.170861-4-lucas.demarchi@intel.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210212094524.170861-1-lucas.demarchi@intel.com> References: <20210212094524.170861-1-lucas.demarchi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: --- testsuite/test-modprobe.c | 95 +++++---------------------------------- 1 file changed, 10 insertions(+), 85 deletions(-) diff --git a/testsuite/test-modprobe.c b/testsuite/test-modprobe.c index dbc54f3..0255f1a 100644 --- a/testsuite/test-modprobe.c +++ b/testsuite/test-modprobe.c @@ -213,7 +213,7 @@ DEFINE_TEST(modprobe_install_cmd_loop, .modules_loaded = "mod-loop-b,mod-loop-a", ); -static noreturn int modprobe_param_kcmdline(const struct test *t) +static noreturn int modprobe_param_kcmdline_show_deps(const struct test *t) { const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; const char *const args[] = { @@ -225,7 +225,7 @@ static noreturn int modprobe_param_kcmdline(const struct test *t) test_spawn_prog(progname, args); exit(EXIT_FAILURE); } -DEFINE_TEST(modprobe_param_kcmdline, +DEFINE_TEST(modprobe_param_kcmdline_show_deps, .description = "check if params from kcmdline are passed to (f)init_module call", .config = { [TC_UNAME_R] = "4.4.4", @@ -237,7 +237,7 @@ DEFINE_TEST(modprobe_param_kcmdline, .modules_loaded = "", ); -static noreturn int modprobe_param_kcmdline2(const struct test *t) +static noreturn int modprobe_param_kcmdline(const struct test *t) { const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; const char *const args[] = { @@ -249,7 +249,7 @@ static noreturn int modprobe_param_kcmdline2(const struct test *t) test_spawn_prog(progname, args); exit(EXIT_FAILURE); } -DEFINE_TEST(modprobe_param_kcmdline2, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline2, modprobe_param_kcmdline, .description = "check if params with no value are parsed correctly from kcmdline", .config = { [TC_UNAME_R] = "4.4.4", @@ -261,19 +261,7 @@ DEFINE_TEST(modprobe_param_kcmdline2, .modules_loaded = "", ); -static noreturn int modprobe_param_kcmdline3(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline3, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline3, modprobe_param_kcmdline, .description = "check if unrelated strings in kcmdline are correctly ignored", .config = { [TC_UNAME_R] = "4.4.4", @@ -285,19 +273,7 @@ DEFINE_TEST(modprobe_param_kcmdline3, .modules_loaded = "", ); -static noreturn int modprobe_param_kcmdline4(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline4, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline4, modprobe_param_kcmdline, .description = "check if unrelated strings in kcmdline are correctly ignored", .config = { [TC_UNAME_R] = "4.4.4", @@ -309,19 +285,7 @@ DEFINE_TEST(modprobe_param_kcmdline4, .modules_loaded = "", ); -static noreturn int modprobe_param_kcmdline5(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline5, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline5, modprobe_param_kcmdline, .description = "check if params with spaces are parsed correctly from kcmdline", .config = { [TC_UNAME_R] = "4.4.4", @@ -333,20 +297,7 @@ DEFINE_TEST(modprobe_param_kcmdline5, .modules_loaded = "", ); - -static noreturn int modprobe_param_kcmdline6(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline6, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline6, modprobe_param_kcmdline, .description = "check if dots on other parts of kcmdline don't confuse our parser", .config = { [TC_UNAME_R] = "4.4.4", @@ -358,20 +309,7 @@ DEFINE_TEST(modprobe_param_kcmdline6, .modules_loaded = "", ); - -static noreturn int modprobe_param_kcmdline7(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline7, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline7, modprobe_param_kcmdline, .description = "check if dots on other parts of kcmdline don't confuse our parser", .config = { [TC_UNAME_R] = "4.4.4", @@ -383,20 +321,7 @@ DEFINE_TEST(modprobe_param_kcmdline7, .modules_loaded = "", ); - -static noreturn int modprobe_param_kcmdline8(const struct test *t) -{ - const char *progname = ABS_TOP_BUILDDIR "/tools/modprobe"; - const char *const args[] = { - progname, - "-c", - NULL, - }; - - test_spawn_prog(progname, args); - exit(EXIT_FAILURE); -} -DEFINE_TEST(modprobe_param_kcmdline8, +DEFINE_TEST_WITH_FUNC(modprobe_param_kcmdline8, modprobe_param_kcmdline, .description = "check if dots on other parts of kcmdline don't confuse our parser", .config = { [TC_UNAME_R] = "4.4.4",