From patchwork Tue Nov 28 10:16:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 170678 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp3809568vqx; Tue, 28 Nov 2023 02:17:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IFID+3vKCpwl718k7PW/w1eicsKhFrDapzMlysSwv01lp92RfQ+a2m8DNDohiijSbx6onhq X-Received: by 2002:a05:6a00:2909:b0:68c:69ca:2786 with SMTP id cg9-20020a056a00290900b0068c69ca2786mr14785121pfb.34.1701166625868; Tue, 28 Nov 2023 02:17:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701166625; cv=none; d=google.com; s=arc-20160816; b=UgTZ1Mu0XMCNkfG2gBzx/SbC54blQpEjA+B1YA3Pss5YdthQQKtNdAPAenTOv3U/fm RYw+W9ynEuTu5kPcj/6JxASn0A+jCGC+dES1saySaX5bnu295CjHgsn5+X6jWsDen3Y/ PRjW0E5ELASf+oFMP9Lw/Z4Vn6XwVirjDJr23FIbvMNaQsZM796eWMAP/IfsBN12icUq iszarfNR4o+h30Z5Fs9WYdMPeLKunQbdXXg8NJsaj+zn+UzvUCOi8hQ31pZN6oTKIm1M YeAt0XcYLWhxViz0UzgZNVbmOeASMpnfaj6RcIIhlMNJvuqjPvP+KIqYMXbvtz6MFn81 aIcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=2mlv9LGhIVFVkJH7cobh6u5ej8q8EOjabrk7WGvxaI4=; fh=Lf2i+0iuQIN/eIZuCIEjnUTIxaGJyoKaYs5xowNrTKE=; b=R5DSRV+Ut6uBe8cTLrfJT3oJThSPQQt79JoC4wl06mnFF63sU5cvu7+bBJ7R79UD1G nXyxOetZDxahRnZi921CY8YSuY20BUuA1IggOuOt5m/bcsoA7fbwuNnd7tkDwk5hw/FZ /7veqPPmORi3aqovu87RJupsY9bRGxM4aAiYdOySKiXkSaazwNMMenAEIo4wN0Y1tOpr a6hQoEX48bnTYOsSyTzW8x+jIHE+gBTM+1S8/RIvWuY57CS0UxxjnmhX1jCVn9hvdxbg trPj8L60HqrVrN9Mzv2KUIOSEJCIIrG8y7Fulkczm7VwxB0K7vsUmQvwjWTewkLKiZ0i VDcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CGx8oBC6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id t32-20020a634620000000b005a9fde46fa0si11766112pga.130.2023.11.28.02.17.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:17:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CGx8oBC6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 0C724809B6F6; Tue, 28 Nov 2023 02:17:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234795AbjK1KQh (ORCPT + 99 others); Tue, 28 Nov 2023 05:16:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234651AbjK1KQf (ORCPT ); Tue, 28 Nov 2023 05:16:35 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE5C110E for ; Tue, 28 Nov 2023 02:16:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701166600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=2mlv9LGhIVFVkJH7cobh6u5ej8q8EOjabrk7WGvxaI4=; b=CGx8oBC6JJpEXvrVDoGi/Es5T4FJNSe9NkmFtj9B9vYtUGvfNFHn6IpRrQtQTceD+DflUl LzZ1d74IY0en4F/qtMwYzk3Jbmu4Rsyfbebs0yErmgHWYfnBAN3pqMQ9d2W8cIYLxWcrD2 AuBAbgoxDRaSQL0g/IYYjvWyvNe+61M= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-424-QpEHxa1CP8WMFtg5Y5LTVg-1; Tue, 28 Nov 2023 05:16:38 -0500 X-MC-Unique: QpEHxa1CP8WMFtg5Y5LTVg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40b4e24adc7so2247885e9.0 for ; Tue, 28 Nov 2023 02:16:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701166597; x=1701771397; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2mlv9LGhIVFVkJH7cobh6u5ej8q8EOjabrk7WGvxaI4=; b=QExp5jM6B8lhnEZ+GyXRe4+0E7rmTZLPar6/POqCAlroELZe9kUSNfSyVvI0NsQ4t7 oMj5FIq6wf0W5xvUZJ1bwv/MVp5gUeLa39ZfJELhKq1hPBm2pvGuZU3ACipoeM1L4AIx hiMxGZYrnBPYD9mA/ccv07t4AD6xapBXaYaGi7aL4kRq7hmM7ioNogDJAvL+djmieE3L m0bVeDY8i14qyhb1+G7rzBisftE/ATK+PURbScHVca5dsdfN5wVctGeyOkmvWSFXWqdS dOKvH3lHgWlCopn2mGEayMtokI6KtSFlFVpA7oAB/tOzF96yOwCcdaPo+ZTNnI5qgtwv hsaA== X-Gm-Message-State: AOJu0YwuPQB9KyWMjHd4a8KXSW8WG1w33fHdpGrdGTb7EZLlzbTe6nVX 6dZhLe7IDFArjaS9yIiblNrLTGW8yhpYS2LTTMiU41Gz9DhKYHQxA0avZOuKZIww2wBSyprCzfQ w9/gq+tkIE0eC58TvLQ7fPzw= X-Received: by 2002:a05:600c:3550:b0:40b:415e:c044 with SMTP id i16-20020a05600c355000b0040b415ec044mr7207124wmq.37.1701166597207; Tue, 28 Nov 2023 02:16:37 -0800 (PST) X-Received: by 2002:a05:600c:3550:b0:40b:415e:c044 with SMTP id i16-20020a05600c355000b0040b415ec044mr7207105wmq.37.1701166596883; Tue, 28 Nov 2023 02:16:36 -0800 (PST) Received: from klayman.redhat.com (net-2-34-24-178.cust.vodafonedsl.it. [2.34.24.178]) by smtp.gmail.com with ESMTPSA id z15-20020a05600c220f00b003fe1fe56202sm16584252wml.33.2023.11.28.02.16.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 02:16:36 -0800 (PST) From: Marco Pagani To: Brendan Higgins , David Gow , Rae Moar , Jinjie Ruan , Shuah Khan Cc: Marco Pagani , Javier Martinez Canillas , linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: [PATCH v2] kunit: run test suites only after module initialization completes Date: Tue, 28 Nov 2023 11:16:27 +0100 Message-ID: <20231128101627.65399-1-marpagan@redhat.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 28 Nov 2023 02:17:03 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783802496186344931 X-GMAIL-MSGID: 1783802496186344931 Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") fixed a wild-memory-access bug that could have happened during the loading phase of test suites built and executed as loadable modules. However, it also introduced a problematic side effect that causes test suites modules to crash when they attempt to register fake devices. When a module is loaded, it traverses the MODULE_STATE_UNFORMED and MODULE_STATE_COMING states before reaching the normal operating state MODULE_STATE_LIVE. Finally, when the module is removed, it moves to MODULE_STATE_GOING before being released. However, if the loading function load_module() fails between complete_formation() and do_init_module(), the module goes directly from MODULE_STATE_COMING to MODULE_STATE_GOING without passing through MODULE_STATE_LIVE. This behavior was causing kunit_module_exit() to be called without having first executed kunit_module_init(). Since kunit_module_exit() is responsible for freeing the memory allocated by kunit_module_init() through kunit_filter_suites(), this behavior was resulting in a wild-memory-access bug. Commit 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") fixed this issue by running the tests when the module is still in MODULE_STATE_COMING. However, modules in that state are not fully initialized, lacking sysfs kobjects. Therefore, if a test module attempts to register a fake device, it will inevitably crash. This patch proposes a different approach to fix the original wild-memory-access bug while restoring the normal module execution flow by making kunit_module_exit() able to detect if kunit_module_init() has previously initialized the tests suite set. In this way, test modules can once again register fake devices without crashing. This behavior is achieved by checking whether mod->kunit_suites is a virtual or direct mapping address. If it is a virtual address, then kunit_module_init() has allocated the suite_set in kunit_filter_suites() using kmalloc_array(). On the contrary, if mod->kunit_suites is still pointing to the original address that was set when looking up the .kunit_test_suites section of the module, then the loading phase has failed and there's no memory to be freed. v2: - add include Fixes: 2810c1e99867 ("kunit: Fix wild-memory-access bug in kunit_free_suite_set()") Signed-off-by: Marco Pagani Reviewed-by: Javier Martinez Canillas Tested-by: Richard Fitzgerald --- lib/kunit/test.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) base-commit: 2cc14f52aeb78ce3f29677c2de1f06c0e91471ab diff --git a/lib/kunit/test.c b/lib/kunit/test.c index f2eb71f1a66c..0e829b9f8ce5 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "debugfs.h" #include "hooks-impl.h" @@ -737,12 +738,14 @@ static void kunit_module_exit(struct module *mod) }; const char *action = kunit_action(); + if (!suite_set.start || !virt_addr_valid(suite_set.start)) + return; + if (!action) __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites); - if (suite_set.start) - kunit_free_suite_set(suite_set); + kunit_free_suite_set(suite_set); } static int kunit_module_notify(struct notifier_block *nb, unsigned long val, @@ -752,12 +755,12 @@ static int kunit_module_notify(struct notifier_block *nb, unsigned long val, switch (val) { case MODULE_STATE_LIVE: + kunit_module_init(mod); break; case MODULE_STATE_GOING: kunit_module_exit(mod); break; case MODULE_STATE_COMING: - kunit_module_init(mod); break; case MODULE_STATE_UNFORMED: break;