Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1697371pxa; Thu, 6 Aug 2020 13:44:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyRIH37lW9LN0FVQOu0BUyz8nX0FTzs1KEiFnslS0q3uRRU9hQT+xiaxiVj9AllYV03l6OY X-Received: by 2002:a05:6402:1643:: with SMTP id s3mr6034648edx.185.1596746693380; Thu, 06 Aug 2020 13:44:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596746693; cv=none; d=google.com; s=arc-20160816; b=u3dTkj5xF30BBRgkLyKAzJXurhGTP0E1Uj8LSAuJ5+wTJkp1Edlay7KtgCHSc7P6IH ptVDkohJPdy65es+DvrExYf4mYWqlEAbz/6jmRFhggoiPRG7QF2jNr7dfVWKgsz8Z/7r R2cYs7q3EAuCMy3wmHzrd2AizWFvXzFMuwOL5bR+7XqmYMwdL1tqozqgj0asU13/oBfV nbH/7ZZ+bq/dfpZjPI6RteFedeBtiiiuLzN4ioizFXP18DhXgabK7AXVI+DlMhelCFwN 6xiXY43cf/tvco3z9c+G0NJ7IofkSnGXxapDu2Bpb/+fdDSBbPCk5hP90sMXkSQKdlGM n12A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=T6Ysoq0U1zVmxOP6risM52XyEbhlxIpm08Hb7kCGz70=; b=akqT0L+K/N7PI2oQSSHo6eIBHPjyV0uxKbA11/Fm3evzu+vj0C2X1PxRv7TN6fFfel 5BGJmnmC5K8U1s4ww62ISHQGF9v0dbVdJE81ItA9IVDd8OErIoaVyD21F0SDh23r1olc IEoMVPSj6xM0gKffXuS0EVlbs++0dfcUX+BgtmO9ZYw3dW5Xx7i8oJ9t4x7jl1iBLkaU +2Ud3TJlfRpltnbr0jmp+mJrvHoPZ0JL8/FAGLN6GHuTDV+BavCTMNH50XpLZfLs3di2 2cpewErFyhQlY6Eg+Zgwhue269NeKnvUJkBQgA2hMqPTZPwowf4rZIhz8W3MwpVvAPIG DEPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Auy2akYz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a22si4047798ejj.163.2020.08.06.13.44.29; Thu, 06 Aug 2020 13:44:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Auy2akYz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1726126AbgHFUnx (ORCPT + 99 others); Thu, 6 Aug 2020 16:43:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725272AbgHFUnw (ORCPT ); Thu, 6 Aug 2020 16:43:52 -0400 Received: from mail-oo1-xc41.google.com (mail-oo1-xc41.google.com [IPv6:2607:f8b0:4864:20::c41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC4DBC061574 for ; Thu, 6 Aug 2020 13:43:52 -0700 (PDT) Received: by mail-oo1-xc41.google.com with SMTP id k63so6999388oob.1 for ; Thu, 06 Aug 2020 13:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=T6Ysoq0U1zVmxOP6risM52XyEbhlxIpm08Hb7kCGz70=; b=Auy2akYzBH3AZup7SZnPiLijTH+mPW/zHHW0rr1XxDtwoGUS4L4D4xMhT+VV/NP2kK okYdK6X5WRe3jPL2HdYTKlMkVWwgbI7njMZTuAs/tkzEByjP0yHdXW2kNAutO4PY0cIK qN+u8mRlPwOl/KwI7CSnjSkXHrWGDPGjNXe003V2G7Vgf+js9Lz5iqT70sFcbREyAOg9 MpiGZUwBzC9bXMbfgGpJ7giwkDrI2fpqzx6RMxVRTTE9DZ6aRQMBiAWjijqqaCOQLYwV SNeKbHjpQjgF6v21u4yvAVsiIElYCgaV25xyuUjLK01QTy8aIaZTIFyMVy7YfpQhj9pO L5Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=T6Ysoq0U1zVmxOP6risM52XyEbhlxIpm08Hb7kCGz70=; b=eTqMwwALnUCk1JIVjewkf8qka8fLuQoVVb+EM9PUKB8Hz/5GOlyvwpVJAkG8gEpfzH nai5SuKlpvV4o5BBFQIw31vmYCAMyhJ4r4is4tAp5MT4EmZCiuQPbRRwYiAERnrH6mOB mH+lEOzMZzwNjOtPFg8E7bKzb4hpCAkfSl2FpGceOxeYENRpnb8ihuyxI362kFbxYgp8 RsICMb04V2lbw3xZ7fIjaK+9/eOWpJQMomaaW+Fvh1GT1yjKhBcq01MYreAQ/DIlQz0O 0SdeqxDkNiKPjqY6G+vCz5INXg1zKSg0H2LxaTDX2snQjfGNAacnB2j+2rdluOODaD8c C5ng== X-Gm-Message-State: AOAM53358PC1mPW/KXyyJK0Q6U7O0Zl3HO/tJK1svHL3psYBjH8HRqgJ +8apg8WlUt6K8xdhD161IMaMHCnjYjhtrLyhT3DIcA== X-Received: by 2002:a4a:d04f:: with SMTP id x15mr9627153oor.87.1596746631868; Thu, 06 Aug 2020 13:43:51 -0700 (PDT) MIME-Version: 1.0 References: <20200806203737.3636830-1-urielguajardojr@gmail.com> In-Reply-To: <20200806203737.3636830-1-urielguajardojr@gmail.com> From: Uriel Guajardo Date: Thu, 6 Aug 2020 15:43:39 -0500 Message-ID: Subject: Re: [PATCH] kunit: added lockdep support To: Uriel Guajardo Cc: Brendan Higgins , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 6, 2020 at 3:37 PM Uriel Guajardo wrote: > > From: Uriel Guajardo > > KUnit tests will now fail if lockdep detects an error during a test > case. > > The idea comes from how lib/locking-selftest [1] checks for lock errors: we > first if lock debugging is turned on. If not, an error must have > occurred, so we fail the test and restart lockdep for the next test case. > > Like the locking selftests, we also fix possible preemption count > corruption from lock bugs. > > Depends on kunit: support failure from dynamic analysis tools [2] > > [1] https://elixir.bootlin.com/linux/v5.7.12/source/lib/locking-selftest.c#L1137 > > [2] https://lore.kernel.org/linux-kselftest/20200806174326.3577537-1-urielguajardojr@gmail.com/ > > Signed-off-by: Uriel Guajardo > --- > lib/kunit/test.c | 26 +++++++++++++++++++++++++- > 1 file changed, 25 insertions(+), 1 deletion(-) > > diff --git a/lib/kunit/test.c b/lib/kunit/test.c > index d8189d827368..0838ececa005 100644 > --- a/lib/kunit/test.c > +++ b/lib/kunit/test.c > @@ -11,6 +11,8 @@ > #include > #include > #include > +#include > +#include > > #include "debugfs.h" > #include "string-stream.h" > @@ -22,6 +24,26 @@ void kunit_fail_current_test(void) > kunit_set_failure(current->kunit_test); > } > > +static inline void kunit_check_locking_bugs(struct kunit *test, > + unsigned long saved_preempt_count) > +{ > + preempt_count_set(saved_preempt_count); > +#ifdef CONFIG_TRACE_IRQFLAGS > + if (softirq_count()) > + current->softirqs_enabled = 0; > + else > + current->softirqs_enabled = 1; > +#endif I am not entirely sure why lib/locking-selftests enables/disables softirqs, but I suspect it has to do with the fact that preempt_count became corrupted, and somehow softirqs became incorrectly enabled/disabled as a result. The resetting of the preemption count will undo the enabling/disabling accordingly. Any insight on this would be appreciated! > +#if IS_ENABLED(CONFIG_LOCKDEP) > + local_irq_disable(); > + if (!debug_locks) { > + kunit_set_failure(test); > + lockdep_reset(); > + } > + local_irq_enable(); > +#endif > +} > + > static void kunit_print_tap_version(void) > { > static bool kunit_has_printed_tap_version; > @@ -289,6 +311,7 @@ static void kunit_try_run_case(void *data) > struct kunit *test = ctx->test; > struct kunit_suite *suite = ctx->suite; > struct kunit_case *test_case = ctx->test_case; > + unsigned long saved_preempt_count = preempt_count(); > > current->kunit_test = test; > > @@ -298,7 +321,8 @@ static void kunit_try_run_case(void *data) > * thread will resume control and handle any necessary clean up. > */ > kunit_run_case_internal(test, suite, test_case); > - /* This line may never be reached. */ > + /* These lines may never be reached. */ > + kunit_check_locking_bugs(test, saved_preempt_count); > kunit_run_case_cleanup(test, suite); > } > > -- > 2.28.0.236.gb10cc79966-goog >