Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp1608886rwe; Sat, 15 Apr 2023 02:39:40 -0700 (PDT) X-Google-Smtp-Source: AKy350Y4CiCpMXJikPvibhA6jJ384PpVgBH4ZRaYJvmJzup8e9epA1HpdSs1f7mp+bKTm1hzfCZX X-Received: by 2002:a05:6a00:2392:b0:63b:7ae0:fdf0 with SMTP id f18-20020a056a00239200b0063b7ae0fdf0mr2496709pfc.2.1681551580307; Sat, 15 Apr 2023 02:39:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681551580; cv=none; d=google.com; s=arc-20160816; b=wCkmB58BZ4CUr3oiZE7FUJA7S6d98i9AmwnmKVY1rUMUxZZJSYfcS9hZ+vgQoamise HmmKtkcAJnceBoOFj6xycTEDh543qh614De3i1A1cJ2PHUaql6iJg3M58ffBw1jrlb3E GFGiMHmUZYpcwZUQoB4HvwUVHNBJ8Jxd0xUxYMgJyj7eOfH3D971JQjr971OmbYi9BE7 FGaMdng/Y/nqAu2KOhzv146XLTdLzlLfTvLELW1aD4NIHIMgRN+/cKoBawk4Irw/tpvw dztzCqP3EAyeY1L4Rw5+yeEM0LxvRBZxTl5tvb9XV3y3K+MKeAdFxq0C1nbPmXGhjWoD yX0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=YxqA3OdJk7nFUYIy/f4+LqvfjVovuJayx7DKH4ia7AY=; b=XVZ5CBuCAKlsorD4riuVrAquauCEDMtw6vRoNMWiEUaLS2WSZIZwmPp3tz7rzPLWEe 94tc8rHV5mkAHTu87e7GSy6KA6ovXcAg7rB2nV+HFatcyuCip2Y3xhplE6AwHkmr9JgT 73GBayDySj5sOoBdClNDneh27i3LscaNPXUxvSDXcIk0eBZNJA5EmBjyxcPFh74ZX9NJ e70v9yQi4qmbNmCpk3NRbILLcfGQaMyeI2ERnAsLwmrSr6/mtws2TGZGfPB69r2+vyr1 HBJa+MyPACAWpMCGzYLjOLpwIlJMImliTgzQ3HsrpGttMqPOsl7jRMbzIXWZV0ZGiXx+ jKdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=34ulQaUo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 201-20020a6307d2000000b0051b29733bc3si6879621pgh.422.2023.04.15.02.39.19; Sat, 15 Apr 2023 02:39:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=34ulQaUo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229678AbjDOJQY (ORCPT + 99 others); Sat, 15 Apr 2023 05:16:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229717AbjDOJQW (ORCPT ); Sat, 15 Apr 2023 05:16:22 -0400 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 C5D8F4689 for ; Sat, 15 Apr 2023 02:16:18 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54f8b46f399so114238227b3.10 for ; Sat, 15 Apr 2023 02:16:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681550178; x=1684142178; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=YxqA3OdJk7nFUYIy/f4+LqvfjVovuJayx7DKH4ia7AY=; b=34ulQaUoyZ40OPv+19h/cSS4j0YeL57TMODtsNO17IFyN9zvWnIz671Iep7fssfxo3 6Wilo1WSpjiUysFb+dKCtMn7NdPa1iGLE3U8UWvFEjV42ZwOXxsNgWlC/hi4F1+UHv4Y U9oXNGqKKtohYRva3bnREjRk4Mg/y9W/pqsXxf89YOaNoNWK4LldIU5IjsAFTWC6y6js 2KclKgn9XziMAv7UuA9yj//3gq+1AdhP0K9syfKbcfeE0Mvi9AIn6IH04IYlit7Em/nN GwqD20EMi6G4izG7HUIhvS+uvO1oi+JsZVXWa6IWCkcviDxUtOlnIZ7gnbgoNE4TfoRC nncQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681550178; x=1684142178; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=YxqA3OdJk7nFUYIy/f4+LqvfjVovuJayx7DKH4ia7AY=; b=d9dyGVfEqo6lIj8Hh7fZ9jGK2OCH7RUebNNjfYAMPnj5+UWsv2pqQJ+hU7pfx5l79G g4CpZQzVulY+KiFG7zsz0KArq41J93gBHKXZ+qrlsPZv+84cF0hmClQWIH+SAoQY8YDq wlcgKsc++YqvA7nkeChfTjyjFY8k4sbebBlz0Rupe2YNwAH6Y7/nPNMRfXIeZbTJ6+4r kO1ZPbpPU4FJSu6xGUW7ExsuOvcsHMjVZEAjXLKL6pgmebME+4qlVUCu5DmFVyfpYU4x mNYC5prI6F44jWW0AL9LVJxtHT1UVNLZCLN0i0BsqSBZteinD9nRgIPsBFdzq5wMO3aL PhGQ== X-Gm-Message-State: AAQBX9c9Rr1J4EuE+N2abVe+iROeiAezs7kpLD+pbM7vrC9f6lJUNs1k jiITGcY47mTYFK+Og761QD4kr929/X/knA== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a81:4428:0:b0:54f:a542:4b4b with SMTP id r40-20020a814428000000b0054fa5424b4bmr5399588ywa.6.1681550177959; Sat, 15 Apr 2023 02:16:17 -0700 (PDT) Date: Sat, 15 Apr 2023 17:14:01 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <20230415091401.681395-1-davidgow@google.com> Subject: [PATCH] kunit: Set the current KUnit context when cleaning up From: David Gow To: Brendan Higgins , Rae Moar , Daniel Latypov , maxime@cerno.tech, Benjamin Berg Cc: Stephen Boyd , Shuah Khan , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, David Gow Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KUnit tests run in a kthread, with the current->kunit_test pointer set to the test's context. This allows the kunit_get_current_test() and kunit_fail_current_test() macros to work. Normally, this pointer is still valid during test shutdown (i.e., the suite->exit function, and any resource cleanup). However, if the test has exited early (e.g., due to a failed assertion), the cleanup is done in the parent KUnit thread, which does not have an active context. Fix this by setting the active KUnit context for the duration of the test shutdown procedure. When the test exits normally, this does nothing. When run from the KUits previous value (probably NULL) afterwards. This should make it easier to get access to the KUnit context, particularly from within resource cleanup functions, which may, for example, need access to data in test->priv. Signed-off-by: David Gow --- This becomes useful with the current kunit_add_action() implementation, as actions do not get the KUnit context passed in by default: https://lore.kernel.org/linux-kselftest/CABVgOSmjs0wLUa4=ErkB9tH8p6A1P6N33befv63whF+hECRExQ@mail.gmail.com/ I think it's probably correct anyway, though, so we should either do this, or totally rule out using kunit_get_current_test() here at all, by resetting current->kunit_test to NULL before running cleanup even in the normal case. I've only given this the most cursory testing so far (I'm not sure how much of the executor innards I want to expose to be able to actually write a proper test for it), so more eyes and/or suggestions are welcome. Cheers, -- David --- lib/kunit/test.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index e2910b261112..2d7cad249863 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -392,10 +392,21 @@ static void kunit_case_internal_cleanup(struct kunit *test) static void kunit_run_case_cleanup(struct kunit *test, struct kunit_suite *suite) { + /* + * If we're no-longer running from within the test kthread() because it failed + * or timed out, we still need the context to be okay when running exit and + * cleanup functions. + */ + struct kunit *old_current = current->kunit_test; + + current->kunit_test = test; if (suite->exit) suite->exit(test); kunit_case_internal_cleanup(test); + + /* Restore the thread's previous test context (probably NULL or test). */ + current->kunit_test = old_current; } struct kunit_try_catch_context { -- 2.40.0.634.g4ca3ef3211-goog