Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp263pxb; Wed, 14 Apr 2021 08:05:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKf9+Q7hkofkIBoOWSzGsJFvPm4WkCsjSRh7G8jKuSlfVzxd5bVlOiiRyz8nj/FrIztfNL X-Received: by 2002:a05:6402:3587:: with SMTP id y7mr43374510edc.54.1618412746758; Wed, 14 Apr 2021 08:05:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618412746; cv=none; d=google.com; s=arc-20160816; b=YWuNATSpE7NEzeaOTQgsvoE9CcILm6zS/cnKdPXGgmccjU975wW3YjhqgnSNa3E+28 K8RHFRMuVNpi2+WdK+nMjwxMpv1xxtr3rHgOwaZakmQiCcJ7LoIvDz0Ge1KGkYpzYlmR ycSPp4iW3seZyCHofLFfjf1E37Z6AP9vhRESrt9ZMXFw3zL1wOHm0EqKsSwl/qqkScTp UWwb3qe1M5AyMDQThu1tY0W6eFXpWKJinSmwb/eujuQrflNIGzFZyQjme91D3CmnGzkk y3c18ln+AJWNUCV0fiAAxkMovXauDfeLbWNDfAlsgWkKpvb3rVQm5YbBWJR7OwKcCdc5 7Q7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=h6merFAI8nWuQUtaPMgNZzm0Zs+GfD/helzkO3aNm6M=; b=fZ7wQWeuSTByypMupUO459BVk4a3QeOsiElkJZaf3S3pra+UjdcyM/vuvIu01qj66f T0d7O/yj9cFi+lDlljGEGD1XhQWyVY8ikSiy6U7w8/K2KobrdfJK1vZEaMz4xNtVYjt9 RhJ/GPZyAyb5WWht7uIJc9Fas/sEeHTGYXedOZwsyBuX4rRaEooEi67TybBjzPnCthGL Ecn9pvfYiWjjohVsC/kwvJ5PW1Kv7xIpgelzZvUiAm3c/sKWgVhSTZM2Qz6gSzTZQ5tA VEmbNotUpMtM8yKwujGLvMTnOBMO/HmvJbCZpvmeRu+UuAz8LUF42o3tOyPfceG8Y2hI HxJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Gpi0MuCR; 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 bi22si13541819edb.191.2021.04.14.08.05.00; Wed, 14 Apr 2021 08:05:46 -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=Gpi0MuCR; 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 S232470AbhDNJkQ (ORCPT + 99 others); Wed, 14 Apr 2021 05:40:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231782AbhDNJkO (ORCPT ); Wed, 14 Apr 2021 05:40:14 -0400 Received: from mail-oo1-xc2c.google.com (mail-oo1-xc2c.google.com [IPv6:2607:f8b0:4864:20::c2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B669C061756 for ; Wed, 14 Apr 2021 02:39:53 -0700 (PDT) Received: by mail-oo1-xc2c.google.com with SMTP id v21-20020a4ae0550000b02901e6970ea355so376837oos.7 for ; Wed, 14 Apr 2021 02:39: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:content-transfer-encoding; bh=h6merFAI8nWuQUtaPMgNZzm0Zs+GfD/helzkO3aNm6M=; b=Gpi0MuCR1W1QhfNJMyeuLEMzdGv2u9cUgcQKDLr2A2Z7SlRIk59djZP/Zf2H2SyFPx 3VEVmAZd++akAu43BavcVWFhv9+DDOwmy/w75/C8gJu84rvaeDKX3u8PRUDObn13vjsQ EH0FugmPO04mpdbLrUyBhZezJXw8AmazdE4oNGrziKL2NBpWqW6axbhhveuM8DG10Z5w HvxLjD53Swn9y2BGDbZ20Ln0jlLgm+eV5Jyyiq6JMc7yimjZlApDjq2kQY+nRW4ZrP9J WYCO3nUgu1Fa/EVFWIVYKC3JXOSWIFw6Sbd5W3br/yYvm/xOc2xatsMsVkwwKp2sZ2yR z5XA== 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:content-transfer-encoding; bh=h6merFAI8nWuQUtaPMgNZzm0Zs+GfD/helzkO3aNm6M=; b=JPiIC7BQPaQetknSXyaZgHAAIw4Pk3Bavcq1hfZjjXC+8j7aqur9tJ7GMsbtokWPrK urtNit4P5dCHz5nPzPlOHL+UzElzmiaIBJDF0Q2FrOfQxLQTQ+oYHnM26pE+rMU0rlqn 5XxcBaNiQlghCG3D4fflVNDOFPFNegmidrq1O3cUKYmnu5gM1Ouxo++ZmTy12tu5ChhZ 3HRNU9x7h0Blct8l+OauUc6JxSUz/H08DOwtgdqcmU3sRIxXZ97RVd9xTTDhl+A73PQx foYzCT3tt9K2A15Po7nXZKkEZ5+FlWJID0fHW4KGI1HJHKaUdXXkZDm+OnYUUGymSHZT 8qxg== X-Gm-Message-State: AOAM530ZRT524Rn2zaX+9o5ymTic5TQFIDb7quRTXB+Y4aumaXy4l77Y gb9fpw9o6LsClyzULoqH/2faora0jZK/iABFePECnA== X-Received: by 2002:a4a:d0ce:: with SMTP id u14mr30250246oor.36.1618393192568; Wed, 14 Apr 2021 02:39:52 -0700 (PDT) MIME-Version: 1.0 References: <20210414081428.337494-1-davidgow@google.com> In-Reply-To: <20210414081428.337494-1-davidgow@google.com> From: Marco Elver Date: Wed, 14 Apr 2021 11:39:39 +0200 Message-ID: Subject: Re: [PATCH v2] Documentation: dev-tools: Add Testing Overview To: David Gow Cc: Jonathan Corbet , Shuah Khan , Andrew Morton , Dmitry Vyukov , Brendan Higgins , Daniel Latypov , "open list:DOCUMENTATION" , KUnit Development , "open list:KERNEL SELFTEST FRAMEWORK" , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 14 Apr 2021 at 10:15, David Gow wrote: > > The kernel now has a number of testing and debugging tools, and we've > seen a bit of confusion about what the differences between them are. > > Add a basic documentation outlining the testing tools, when to use each, > and how they interact. > > This is a pretty quick overview rather than the idealised "kernel > testing guide" that'd probably be optimal, but given the number of times > questions like "When do you use KUnit and when do you use Kselftest?" > are being asked, it seemed worth at least having something. Hopefully > this can form the basis for more detailed documentation later. > > Signed-off-by: David Gow Reviewed-by: Marco Elver Looks good to me, thanks. I think one can write whole articles (or even books) about this topic, so it's easy to forget this is a quick overview, and keep on adding. > --- > Thanks, everyone, for the comments on the doc. I've made a few of the > suggested changes. Please let me know what you think! > > -- David > > Changes since v1: > https://lore.kernel.org/linux-kselftest/20210410070529.4113432-1-davidgow= @google.com/ > - Note KUnit's speed and that one should provide selftests for syscalls > - Mention lockdep as a Dynamic Analysis Tool > - Refer to "Dynamic Analysis Tools" instead of "Sanitizers" > - A number of minor formatting tweaks and rewordings for clarity. > > Not changed: > - I haven't included an exhaustive list of differences, advantages, etc, > between KUnit and kselftest: for now, the doc continues to focus on > the difference between 'in-kernel' and 'userspace' testing here. > - Similarly, I'm not linking out to docs defining and describing "Unit" > tests versus "End-to-end" tests. None of the existing documentation > elsewhere quite matches what we do in the kernel perfectly, so it > seems less confusing to focus on the 'in-kernel'/'userspace' > distinction, and leave other definitions as a passing mention for > those who are already familiar with the concepts. > - I haven't linked to any talk videos here: a few of them are linked on > (e.g.) the KUnit webpage, but I wanted to keep the Kernel documentation > more self-contained for now. No objection to adding them in a follow-up > patch if people feel strongly about it, though. > - The link from index.rst to this doc is unchanged. I personally think > that the link is prominent enough there: it's the first link, and > shows up a few times. One possibility if people disagreed would be to > merge this page with the index, but given not all dev-tools are going > to be testing-related, it seemed a bit arrogant. :-) > > Documentation/dev-tools/index.rst | 3 + > Documentation/dev-tools/testing-overview.rst | 117 +++++++++++++++++++ > 2 files changed, 120 insertions(+) > create mode 100644 Documentation/dev-tools/testing-overview.rst > > diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/= index.rst > index 1b1cf4f5c9d9..f590e5860794 100644 > --- a/Documentation/dev-tools/index.rst > +++ b/Documentation/dev-tools/index.rst > @@ -7,6 +7,8 @@ be used to work on the kernel. For now, the documents hav= e been pulled > together without any significant effort to integrate them into a coheren= t > whole; patches welcome! > > +A brief overview of testing-specific tools can be found in :doc:`testing= -overview`. > + > .. class:: toc-title > > Table of contents > @@ -14,6 +16,7 @@ whole; patches welcome! > .. toctree:: > :maxdepth: 2 > > + testing-overview > coccinelle > sparse > kcov > diff --git a/Documentation/dev-tools/testing-overview.rst b/Documentation= /dev-tools/testing-overview.rst > new file mode 100644 > index 000000000000..ce36a8cdf6b5 > --- /dev/null > +++ b/Documentation/dev-tools/testing-overview.rst > @@ -0,0 +1,117 @@ > +.. SPDX-License-Identifier: GPL-2.0 > + > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > +Kernel Testing Guide > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > + > + > +There are a number of different tools for testing the Linux kernel, so k= nowing > +when to use each of them can be a challenge. This document provides a ro= ugh > +overview of their differences, and how they fit together. > + > + > +Writing and Running Tests > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D > + > +The bulk of kernel tests are written using either the kselftest or KUnit > +frameworks. These both provide infrastructure to help make running tests= and > +groups of tests easier, as well as providing helpers to aid in writing n= ew > +tests. > + > +If you're looking to verify the behaviour of the Kernel =E2=80=94 partic= ularly specific > +parts of the kernel =E2=80=94 then you'll want to use KUnit or kselftest= . > + > + > +The Difference Between KUnit and kselftest > +------------------------------------------ > + > +KUnit (Documentation/dev-tools/kunit/index.rst) is an entirely in-kernel= system > +for "white box" testing: because test code is part of the kernel, it can= access > +internal structures and functions which aren't exposed to userspace. > + > +KUnit tests therefore are best written against small, self-contained par= ts > +of the kernel, which can be tested in isolation. This aligns well with t= he > +concept of 'unit' testing. > + > +For example, a KUnit test might test an individual kernel function (or e= ven a > +single codepath through a function, such as an error handling case), rat= her > +than a feature as a whole. > + > +This also makes KUnit tests very fast to build and run, allowing them to= be > +run frequently as part of the development process. > + > +There is a KUnit test style guide which may give further pointers in > +Documentation/dev-tools/kunit/style.rst > + > + > +kselftest (Documentation/dev-tools/kselftest.rst), on the other hand, is > +largely implemented in userspace, and tests are normal userspace scripts= or > +programs. > + > +This makes it easier to write more complicated tests, or tests which nee= d to > +manipulate the overall system state more (e.g., spawning processes, etc.= ). > +However, it's not possible to call kernel functions directly from kselft= est. > +This means that only kernel functionality which is exposed to userspace = somhow > +(e.g. by a syscall, device, filesystem, etc.) can be tested with kselfte= st. To > +work around this, some tests include a companion kernel module which exp= oses > +more information or functionality. If a test runs mostly or entirely wit= hin the > +kernel, however, KUnit may be the more appropriate tool. > + > +kselftest is therefore suited well to tests of whole features, as these = will > +expose an interface to userspace, which can be tested, but not implement= ation > +details. This aligns well with 'system' or 'end-to-end' testing. > + > +For example, all new system calls should be accompanied by kselftest tes= ts. > + > +Code Coverage Tools > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > + > +The Linux Kernel supports two different code coverage measurement tools.= These > +can be used to verify that a test is executing particular functions or l= ines > +of code. This is useful for determining how much of the kernel is being = tested, > +and for finding corner-cases which are not covered by the appropriate te= st. > + > +:doc:`gcov` is GCC's coverage testing tool, which can be used with the k= ernel > +to get global or per-module coverage. Unlike KCOV, it does not record pe= r-task > +coverage. Coverage data can be read from debugfs, and interpreted using = the > +usual gcov tooling. > + > +:doc:`kcov` is a feature which can be built in to the kernel to allow > +capturing coverage on a per-task level. It's therefore useful for fuzzin= g and > +other situations where information about code executed during, for examp= le, a > +single syscall is useful. > + > + > +Dynamic Analysis Tools > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > + > +The kernel also supports a number of dynamic analysis tools, which attem= pt to > +detect classes of issues when the occur in a running kernel. These typic= ally > +look for undefined behaviour of some kind, such as invalid memory access= es, > +concurrency issues such as data races, or other undefined behaviour like > +integer overflows. > + > +Some of these tools are listed below: > + > +* kmemleak detects possible memory leaks. See > + Documentation/dev-tools/kmemleak.rst > +* KASAN detects invalid memory accesses such as out-of-bounds and > + use-after-free errors. See Documentation/dev-tools/kasan.rst > +* UBSAN detects behaviour that is undefined by the C standard, like inte= ger > + overflows. See Documentation/dev-tools/ubsan.rst > +* KCSAN detects data races. See Documentation/dev-tools/kcsan.rst > +* KFENCE is a low-overhead detector of memory issues, which is much fast= er than > + KASAN and can be used in production. See Documentation/dev-tools/kfenc= e.rst > +* lockdep is a locking correctness validator. See > + Documentation/locking/lockdep-design.rst > +* There are several other pieces of debug instrumentation in the kernel,= many > + of which can be found in lib/Kconfig.debug > + > +These tools tend to test the kernel as a whole, and do not "pass" like > +kselftest or KUnit tests. They can be combined with KUnit or kselftest b= y > +running tests on a kernel with a sanitizer enabled: you can then be sure > +that none of these errors are occurring during the test. > + > +Some of these tools integrate with KUnit or kselftest and will > +automatically fail tests if an issue is detected. > + > -- > 2.31.1.295.g9ea45b61b8-goog >