Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp756157pxa; Wed, 19 Aug 2020 14:07:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQ751lQ4g4jsloTkbuHHAz+V1WxRBJULFR4pxIEeM82Rmix1qGZ99EMYh7zGNj2qqSymum X-Received: by 2002:a17:907:447d:: with SMTP id oo21mr205026ejb.462.1597871220479; Wed, 19 Aug 2020 14:07:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597871220; cv=none; d=google.com; s=arc-20160816; b=szDG1hY5gKt068OoPPa5io4cmnnmh3Og/wlsuqboAzURUj+x8S6YuMzpeC7ETOnRKi hGdn0bKhd+xkH9IpWuaAlDQvauQ9uXn3RS4P8XozCbjcumHGM4evMSaH08H0dHPTCM4h qqBtcgzAkEXqvAZgZhRA2SMG01gF2jA0zJ93Oyw7KsFfRJrFE4WfJXXujlN+y9rZDEp2 uj1DqG9e+43+/MKf1GuTiOXRbGVkSlJUjgLiHB7cyDP3AUi0MAF7F/rfVyPj+3l8iJF3 Oh9muRGHLCpKRXEBy/CF9ull7DjY+/VvEMhSEeHUpUbLa+ZyUeIdf6LF8OH7vpGFaScL q6TQ== 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=E9eRVOLaul6cq01mtbbr/ED3T1zhaooeYNDSyj6PbBQ=; b=sGdf0sJDl+JsGeZSGGsllRORt+Phq+RAi2Wgob/PXwpHrmDCMr7UGS6+2kQXIYkK/J mVscJ0RYVi763SKTrFdsiEK8+9numTvz/X6JfMOXN0csFMQEBuLnDyV0Ip5hjsGCZ72o ihbHIgvFMDvr1Z1k5Id9jey45fZtCgO0zTKOCTiD4QoFyamNG33KTUk4Oo/8IHhI7mcr +Uuzns0s4CKfsnmyDLrk41k287QT9JIOVkd3aUbNeJyF89qPUx5xGwH6LKRy44Qt+/Av oa1qcEk5Ci+foEGKVfB1cunZl3+EvPC1qU7lGk0xfM/B66cG8mZwMQvGI+oZcOh/Y5pD 6WbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=bWhJfXIH; 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 cf25si18209982ejb.690.2020.08.19.14.06.35; Wed, 19 Aug 2020 14:07:00 -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=bWhJfXIH; 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 S1727006AbgHSVFx (ORCPT + 99 others); Wed, 19 Aug 2020 17:05:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726841AbgHSVFu (ORCPT ); Wed, 19 Aug 2020 17:05:50 -0400 Received: from mail-pj1-x1044.google.com (mail-pj1-x1044.google.com [IPv6:2607:f8b0:4864:20::1044]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1B70C061383 for ; Wed, 19 Aug 2020 14:05:50 -0700 (PDT) Received: by mail-pj1-x1044.google.com with SMTP id d4so1681585pjx.5 for ; Wed, 19 Aug 2020 14:05:50 -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=E9eRVOLaul6cq01mtbbr/ED3T1zhaooeYNDSyj6PbBQ=; b=bWhJfXIHoYrd3Cbwkg7cRhBSQajpxTQ6y9B4X7AwebO+WOLWJmZB5uFsklsfyrcPbk E38o8S/CJmckAgblLWsrO/ZpA4M5275XCCP4rgvZje1Ap+6V3YRqg0XT/r+XaZaptPEi oQq9Erv0sQWQAsjNxM++KsGSm7VtsLsFs+OeghXEhlLjiQL0VHhJYmKgZw1DfobEJaPz fczscWNyP7MB7AJ5hFVAN3UgiDRMagfqEcNw/RGUSE3IS4p9DRv9SdmlLG+WbkxgkXNO pdL3TBJyFeS+BF2vH8azBWUlj7TxadcYNtc+j+bb1Gqzxwpq1uOZ3O3hmQDDhBS2+nTi P8jA== 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=E9eRVOLaul6cq01mtbbr/ED3T1zhaooeYNDSyj6PbBQ=; b=thNhX/hxsYkpZsGyYeNKOJnhj0WClxpy19abg2V1QKdm6zvtvDgOiMZ/4dBkL2diFV NWtun0lJLiI73x2G7c40+D6y9IF0UTsfIr/cGkGqsuCndmekrT/x5vZWtUoUs+1HXBUO JHExu2i/m8dJ2kZXQMR1ZAB8Xdlx3oVUi4k7BeD/83hLM9P60ZfYhAJJMWVCrdQomhRH EvGCPWG+e5qLytftaHFdhSn3I1rwMe/Y1Wa+r1FeUt/xwb4nUR4jgA1RV2zqWPyTnkFf WyxiB2Prz0WJpImzVBD9wbFdkj1mtd8C29DZ3nWwe/VMxEiQGHigbr9Cpmb47Db0klA5 z8eA== X-Gm-Message-State: AOAM532xyqMEPQZYh1Ix/4eaEtgnp4qC+u/hCuG0XwmWLeyaUVZTyGnv qhW9KTwqOZK3awVfHas3WOz/26uWCgjFGjhuLaNiZw== X-Received: by 2002:a17:902:c382:: with SMTP id g2mr1602728plg.297.1597871149723; Wed, 19 Aug 2020 14:05:49 -0700 (PDT) MIME-Version: 1.0 References: <20200718005025.440320-1-vitor@massaru.org> In-Reply-To: <20200718005025.440320-1-vitor@massaru.org> From: Brendan Higgins Date: Wed, 19 Aug 2020 14:05:38 -0700 Message-ID: Subject: Re: [PATCH v2] lib: kunit: Provides a userspace memory context when tests are compiled as module To: Vitor Massaru Iha Cc: KUnit Development , "open list:KERNEL SELFTEST FRAMEWORK" , Linux Kernel Mailing List , Kees Cook , David Gow , Shuah Khan , linux-kernel-mentees@lists.linuxfoundation.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 Fri, Jul 17, 2020 at 5:50 PM Vitor Massaru Iha wrote: > > KUnit test cases run on kthreads, and kthreads don't have an > adddress space (current->mm is NULL), but processes have mm. > > The purpose of this patch is to allow to borrow mm to KUnit kthread > after userspace is brought up, because we know that there are processes > running, at least the process that loaded the module to borrow mm. > > This allows, for example, tests such as user_copy_kunit, which uses > vm_mmap, which needs current->mm. > > Signed-off-by: Vitor Massaru Iha Can you put these together in the same patch series as you had before? When I asked you to split the patch up, I was just asking about that specific patch within the series. I still think all the patches go together. As for this specific patch, I see one minor issue below; other than that, this looks good to me, so: Reviewed-by: Brendan Higgins > --- > v2: > * splitted patch in 3: > - Allows to install and load modules in root filesystem; > - Provides an userspace memory context when tests are compiled > as module; > - Convert test_user_copy to KUnit test; > * added documentation; > * added more explanation; > * tested a pointer; > * released mput(); > --- > Documentation/dev-tools/kunit/usage.rst | 14 ++++++++++++++ > include/kunit/test.h | 12 ++++++++++++ > lib/kunit/try-catch.c | 15 ++++++++++++++- > 3 files changed, 40 insertions(+), 1 deletion(-) > > diff --git a/Documentation/dev-tools/kunit/usage.rst b/Documentation/dev-tools/kunit/usage.rst > index 3c3fe8b5fecc..9f909157be34 100644 > --- a/Documentation/dev-tools/kunit/usage.rst > +++ b/Documentation/dev-tools/kunit/usage.rst > @@ -448,6 +448,20 @@ We can now use it to test ``struct eeprom_buffer``: > > .. _kunit-on-non-uml: > > +User-space context > +------------------ > + > +I case you need a user-space context, for now this is only possible through > +tests compiled as a module. And it will be necessary to use a root filesystem > +and uml_utilities. > + > +Example: > + > +.. code-block:: bash > + > + ./tools/testing/kunit/kunit.py run --timeout=60 --uml_rootfs_dir=.uml_rootfs > + > + > KUnit on non-UML architectures > ============================== I think the above documentation change belongs in the other related patch where you introduce the --uml_rootfs_dir flag. > diff --git a/include/kunit/test.h b/include/kunit/test.h > index 59f3144f009a..ae3337139c65 100644 > --- a/include/kunit/test.h > +++ b/include/kunit/test.h > @@ -222,6 +222,18 @@ struct kunit { > * protect it with some type of lock. > */ > struct list_head resources; /* Protected by lock. */ > + /* > + * KUnit test cases run on kthreads, and kthreads don't have an > + * adddress space (current->mm is NULL), but processes have mm. > + * > + * The purpose of this mm_struct is to allow to borrow mm to KUnit kthread > + * after userspace is brought up, because we know that there are processes > + * running, at least the process that loaded the module to borrow mm. > + * > + * This allows, for example, tests such as user_copy_kunit, which uses > + * vm_mmap, which needs current->mm. > + */ > + struct mm_struct *mm; > }; > > void kunit_init_test(struct kunit *test, const char *name, char *log); > diff --git a/lib/kunit/try-catch.c b/lib/kunit/try-catch.c > index 0dd434e40487..d03e2093985b 100644 > --- a/lib/kunit/try-catch.c > +++ b/lib/kunit/try-catch.c > @@ -11,7 +11,8 @@ > #include > #include > #include > - > +#include > +#include > #include "try-catch-impl.h" > > void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) > @@ -24,8 +25,17 @@ EXPORT_SYMBOL_GPL(kunit_try_catch_throw); > static int kunit_generic_run_threadfn_adapter(void *data) > { > struct kunit_try_catch *try_catch = data; > + struct kunit *test = try_catch->test; > + > + if (test != NULL && test->mm != NULL) > + kthread_use_mm(test->mm); > > try_catch->try(try_catch->context); > + if (test != NULL && test->mm != NULL) { > + kthread_unuse_mm(test->mm); > + mmput(test->mm); > + test->mm = NULL; > + } > > complete_and_exit(try_catch->try_completion, 0); > } > @@ -65,6 +75,9 @@ void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) > try_catch->context = context; > try_catch->try_completion = &try_completion; > try_catch->try_result = 0; > + > + test->mm = get_task_mm(current); > + > task_struct = kthread_run(kunit_generic_run_threadfn_adapter, > try_catch, > "kunit_try_catch_thread"); > > base-commit: 725aca9585956676687c4cb803e88f770b0df2b2 > prerequisite-patch-id: 5e5f9a8a05c5680fda1b04c9ab1b95ce91dc88b2 > prerequisite-patch-id: 4d997940f4a9f303424af9bac412de1af861f9d9 > prerequisite-patch-id: 582b6d9d28ce4b71628890ec832df6522ca68de0 > -- > 2.26.2 >