Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp1342328ybd; Wed, 26 Jun 2019 16:01:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqx4jfIGlUeUr84LYeVwBXd1uPXFGMYZZOtiFQh5ttdmgJOeVOfzU4cemH3vKJrmDd+g03Fd X-Received: by 2002:a17:90a:32ed:: with SMTP id l100mr1857928pjb.11.1561590077152; Wed, 26 Jun 2019 16:01:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561590077; cv=none; d=google.com; s=arc-20160816; b=q3nKSvtwGFWap4LCUbZ2OxFiH6dJ0/1JgOM5S68Dm8O1qC5y/C/l8POpXB+v63dog2 f+xF3R97gqPgDUB7NeKgSoK0ZWr/emUp89eHnAhX05hPxzCRwiQtwIKwcmtTPxlD+a/k Q95BriTdFTiXELdD1fv+GVGZTdMeQy0CXh7IM1bPy4MzpHppczKuLmiYPeqv17bOkmh1 /J84swUQwjvyZvnyUkfut/f2Ng3qmP6YmBGCCquhe+4vs/HvLdglVHtFTULRH2+C+Q7r fAJEOKu2v8FjhmJVfjN0lDXzzL0+07/7kbTyHbITzCAWN+8DZxb+yHw90MngEO14XnYa OjHA== 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=turlNrZcH8tuujf37Q7HEe8hyhwhiMkMAGSNncSmJd0=; b=wStxaJG8e9LaQyYe8n/y2qVG/v0bNPvy22B0CW52KDP0wf2uREdxQYWqks+/MCTfJd 7j/0o7eBI+4AJ76QCC34tvV6ow9W1NdcfzYGso4OwoZ7FfoN8Al3c3p/9rCknazHkt8f u8HubGItn9axPOk6mFw6IlcEqHWSXZK9AbD0RdFLxHATagKvFXRDSqWSuRX7U27i9BuF 9VL5DZwbmRgEtxyv59nVQ14z2b7zfMJFF+2qsd0F0di1ywFf173Q9i9VBKP9xoQ7l21u riFbmbFOvfd4dsnqGiFo00Uc7FpsJ+XViPRXoRbn7SrP30EhgOkc0N90lAGJWFl884oO lEXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tPbLhI6p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id u62si320235pgu.334.2019.06.26.16.00.59; Wed, 26 Jun 2019 16:01:17 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tPbLhI6p; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726587AbfFZXAy (ORCPT + 99 others); Wed, 26 Jun 2019 19:00:54 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:43642 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726503AbfFZXAx (ORCPT ); Wed, 26 Jun 2019 19:00:53 -0400 Received: by mail-pl1-f195.google.com with SMTP id cl9so138518plb.10 for ; Wed, 26 Jun 2019 16:00:53 -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=turlNrZcH8tuujf37Q7HEe8hyhwhiMkMAGSNncSmJd0=; b=tPbLhI6p7V77PIMP2Ixa9TxEo7roY6MVHPMR6jgk7ZuALrFWvqeemx+Wplsc+xCraa pdqaYu92o/6BUX2InP+i1EHq6YxT7QAYYWFgD1MBvytI5KSVDogEPNSSYbdTqT2lCSQ0 Fn3GAaSQ9CIrxWUpqqCpg+X3DttzdAEUoMp1XrupALh+W8vxcCBtUuzpQXYzG6c3iMf7 UhPmfILyu8sJnTsr66nAy/yDGC9ePdyx7iIS/nUONAHd9nJ8GTtoBAj4b7HYxRUEhl1e BHevIfb/hk6Fobzekl76hHs0k5V4Gu3QLYcO3BnbTSkfvMzcisvJPBxENYWRxYw+rWCH v0ew== 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=turlNrZcH8tuujf37Q7HEe8hyhwhiMkMAGSNncSmJd0=; b=UtDlQdFhCKYAc+/4GVnPEG3IQ6a7dEjP0VIn1Zd2BESWikc7anJH7skt+3X0g7JfnS 1pTXZVb47xigHg12go6szIA6izfnPxptPonMhYvrOjgF5OJuhe1R9Osc12dhe5A0Mmph za5R7Eoiy+aW4gUcrU4gtDU4aYGkQeovJYsMyXanfmoBd/o9OZD140TXecF3lzXqoU5I yw5SnGNrrI2ewPwBmVnEe+y3hg6FvGoG0cJy2CxBXlq+8AbeN/fpIJxYaHFJv57ZaFSB xSJpT8VFayaDLZ+6l7V1IJH+K48Hbpv9FraHwwzhcQV2443uBDg4JpOj+6Dji6Q8dEUm Wubg== X-Gm-Message-State: APjAAAVpL9ABt0mjqMeT2sqFEX9w2EBdw3X/UC125j7YnuKOcUaqdN6h CASXq4oHP421d5RhFTux0Btx/qjHCh0j+5aEwkFRXw== X-Received: by 2002:a17:902:2006:: with SMTP id n6mr669016pla.232.1561590052378; Wed, 26 Jun 2019 16:00:52 -0700 (PDT) MIME-Version: 1.0 References: <20190617082613.109131-1-brendanhiggins@google.com> <20190617082613.109131-2-brendanhiggins@google.com> <20190620001526.93426218BE@mail.kernel.org> <20190626034100.B238520883@mail.kernel.org> In-Reply-To: <20190626034100.B238520883@mail.kernel.org> From: Brendan Higgins Date: Wed, 26 Jun 2019 16:00:40 -0700 Message-ID: Subject: Re: [PATCH v5 01/18] kunit: test: add KUnit test runner core To: Stephen Boyd Cc: Frank Rowand , Greg KH , Josh Poimboeuf , Kees Cook , Kieran Bingham , Luis Chamberlain , Peter Zijlstra , Rob Herring , shuah , "Theodore Ts'o" , Masahiro Yamada , devicetree , dri-devel , kunit-dev@googlegroups.com, "open list:DOCUMENTATION" , linux-fsdevel@vger.kernel.org, linux-kbuild , Linux Kernel Mailing List , "open list:KERNEL SELFTEST FRAMEWORK" , linux-nvdimm , linux-um@lists.infradead.org, Sasha Levin , "Bird, Timothy" , Amir Goldstein , Dan Carpenter , Daniel Vetter , Jeff Dike , Joel Stanley , Julia Lawall , Kevin Hilman , Knut Omang , Logan Gunthorpe , Michael Ellerman , Petr Mladek , Randy Dunlap , Richard Weinberger , David Rientjes , Steven Rostedt , wfg@linux.intel.com 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 Tue, Jun 25, 2019 at 8:41 PM Stephen Boyd wrote: > > Quoting Brendan Higgins (2019-06-25 13:28:25) > > On Wed, Jun 19, 2019 at 5:15 PM Stephen Boyd wrote: > > > > > > Quoting Brendan Higgins (2019-06-17 01:25:56) > > > > diff --git a/kunit/test.c b/kunit/test.c > > > > new file mode 100644 > > > > index 0000000000000..d05d254f1521f > > > > --- /dev/null > > > > +++ b/kunit/test.c > > > > @@ -0,0 +1,210 @@ > > > > +// SPDX-License-Identifier: GPL-2.0 > > > > +/* > > > > + * Base unit test (KUnit) API. > > > > + * > > > > + * Copyright (C) 2019, Google LLC. > > > > + * Author: Brendan Higgins > > > > + */ > > > > + > > > > +#include > > > > +#include > > > > + > > > > +static bool kunit_get_success(struct kunit *test) > > > > +{ > > > > + unsigned long flags; > > > > + bool success; > > > > + > > > > + spin_lock_irqsave(&test->lock, flags); > > > > + success = test->success; > > > > + spin_unlock_irqrestore(&test->lock, flags); > > > > > > I still don't understand the locking scheme in this code. Is the > > > intention to make getter and setter APIs that are "safe" by adding in a > > > spinlock that is held around getting and setting various members in the > > > kunit structure? > > > > Yes, your understanding is correct. It is possible for a user to write > > a test such that certain elements may be updated in different threads; > > this would most likely happen in the case where someone wants to make > > an assertion or an expectation in a thread created by a piece of code > > under test. Although this should generally be avoided, it is possible, > > and there are occasionally good reasons to do so, so it is > > functionality that we should support. > > > > Do you think I should add a comment to this effect? > > No, I think the locking should be removed. > > > > > > In what situation is there more than one thread reading or writing the > > > kunit struct? Isn't it only a single process that is going to be > > > > As I said above, it is possible that the code under test may spawn a > > new thread that may make an expectation or an assertion. It is not a > > super common use case, but it is possible. > > Sure, sounds super possible and OK. > > > > > > operating on this structure? And why do we need to disable irqs? Are we > > > expecting to be modifying the unit tests from irq contexts? > > > > There are instances where someone may want to test a driver which has > > an interrupt handler in it. I actually have (not the greatest) example > > here. Now in these cases, I expect someone to use a mock irqchip or > > some other fake mechanism to trigger the interrupt handler and not > > actual hardware; technically speaking in this case, it is not going to > > be accessed from a "real" irq context; however, the code under test > > should think that it is in an irq context; given that, I figured it is > > best to just treat it as a real irq context. Does that make sense? > > Can you please describe the scenario in which grabbing the lock here, > updating a single variable, and then releasing the lock right after > does anything useful vs. not having the lock? I'm looking for a two CPU Sure. > scenario like below, but where it is a problem. There could be three > CPUs, or even one CPU and three threads if you want to describe the > extra thread scenario. > > Here's my scenario where it isn't needed: > > CPU0 CPU1 > ---- ---- > kunit_run_test(&test) > test_case_func() > .... > [mock hardirq] > kunit_set_success(&test) > [hardirq ends] > ... > complete(&test_done) > wait_for_completion(&test_done) > kunit_get_success(&test) > > We don't need to care about having locking here because success or > failure only happens in one place and it's synchronized with the > completion. Here is the scenario I am concerned about: CPU0 CPU1 CPU2 ---- ---- ---- kunit_run_test(&test) test_case_func() .... schedule_work(foo_func) [mock hardirq] foo_func() ... ... kunit_set_success(false) kunit_set_success(false) [hardirq ends] ... ... complete(&test_done) wait_for_completion(...) kunit_get_success(&test) In my scenario, since both CPU1 and CPU2 update the success status of the test simultaneously, even though they are setting it to the same value. If my understanding is correct, this could result in a write-tear on some architectures in some circumstances. I suppose we could just make it an atomic boolean, but I figured locking is also fine, and generally preferred. Also, to be clear, I am onboard with dropping then IRQ stuff for now. I am fine moving to a mutex for the time being. > > > > > > > + > > > > + return success; > > > > +} > > > > + > > > > +static void kunit_set_success(struct kunit *test, bool success) > > > > +{ > > > > + unsigned long flags; > > > > + > > > > + spin_lock_irqsave(&test->lock, flags); > > > > + test->success = success; > > > > + spin_unlock_irqrestore(&test->lock, flags); > > > > +}