Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp39627ybh; Fri, 17 Jul 2020 17:54:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXA7nLl0+q6w7y1Oq0exOMWcLbih32Nz6SwSMUUP5FWzd/j1wdFZ9SSNeG3Jb9/bTvTB/r X-Received: by 2002:a17:906:80d3:: with SMTP id a19mr11564873ejx.217.1595033641303; Fri, 17 Jul 2020 17:54:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595033641; cv=none; d=google.com; s=arc-20160816; b=ozBE333plrHxMYugptxfj33tS6075Fb2muBxUX9fq4qRRY9Gu1TLIxC7i5ZMIGizD+ Qvz8QRcTHHJGD6H/yC2avWoZQ2PmOZoew6/gP1QmmqUDT53hSSa4a2vAsRQ87Y23KxcE ESCUbzK0pgvk/rT9DGyqjDdfwdxAkeW7eHzvhorN8Y98nqQJinlftA7J21X89QcDnLnZ QrC7D5bV0D0Oyp/U56gQiShLsfoZaDviWrZNBF8XqL9WR/RX1KnsOPsTFcCTPGeMGMWG XpptMBeFAxV0maAzrkmGtRT/7LJpmDMd8KambsSWbvdEsbp4FlrMPAM2zIxyviElZ47H CcBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=ropALfHo2IvR5T23vKKGhsuUheVbO2NjXVQ5Zr9SRlU=; b=y6goGr0Pcx5HvVVo4RuyhRMsxKNk4lrschUq33kZLZJcDLnpx+K8fxfdQp6ceJrg6x eP2QZ3plMYRtEFekhb/c6KM4LDt5WxI7m0ipCLd5Z6PcfE9pT3cwrhgGZ/CUa2G11oA0 0c/O6R4FSvYS/w92bte/AVJd3RazePIhp46xk1HmQ9CROEbOf+DlJCzS20KIsaAgfMbF +C9GxiJTeVri4QjXGDN7YwSF9D8nXIYBb7t4lIpV46PjyDnbTu5HuIp6ae/NHcjIdDxu WYFTAb3X0aYiFcjP7zCByGqls+OhrL43JvoG/XrDnFfIN20fLsMFkL9MIGeJq1oPtKWa 2ZOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@massaru-org.20150623.gappssmtp.com header.s=20150623 header.b=DNopG01W; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cm10si6230025edb.208.2020.07.17.17.53.37; Fri, 17 Jul 2020 17:54:01 -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=@massaru-org.20150623.gappssmtp.com header.s=20150623 header.b=DNopG01W; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728054AbgGRAub (ORCPT + 99 others); Fri, 17 Jul 2020 20:50:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727858AbgGRAua (ORCPT ); Fri, 17 Jul 2020 20:50:30 -0400 Received: from mail-qv1-xf42.google.com (mail-qv1-xf42.google.com [IPv6:2607:f8b0:4864:20::f42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA0F5C0619D2 for ; Fri, 17 Jul 2020 17:50:30 -0700 (PDT) Received: by mail-qv1-xf42.google.com with SMTP id h18so5045043qvl.3 for ; Fri, 17 Jul 2020 17:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=massaru-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ropALfHo2IvR5T23vKKGhsuUheVbO2NjXVQ5Zr9SRlU=; b=DNopG01WNl07mIZ+CvyOYvXSwX6B5gomk7j5uvYELm20sKi8Svy3hdKCSl+Vpm0Uom QYpu9kdBfFL9JpMddqyjBWeRjqm1GBAExwWrBQJnjwnov+oML1QwUfLx28P7OqmnbpF0 vSZJ4QHTDqFHCJZV31fhHPcwzNJLc9UTlyWFZ2orEpVgp+h/6oPDeIUjrbg9ldeucZwb EDY/fr7UXbukdksP59SF4Phzqc0lIRjfw9RQ2NpIJYLuH3XAxXbnkRk1BYqF09HiM2oW A9hn3NeLOQN0o8Crwb1ehHgQzdwcGQxhE+/uuwVUWzG2rbRwZIptEbCQv0nPZKTy6RO2 i8og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ropALfHo2IvR5T23vKKGhsuUheVbO2NjXVQ5Zr9SRlU=; b=CZCdV7Tg6ETbAyeMfm1l/2+sCsHnLTgw88+JXB7QVQpLQdXR7o8BxxZK4WGGHJClg9 I9uO88ziZKoMXMTLo2Ow34rVHsVMbdig9eM4ulMpGm3rx9QXUZZNcG8C2P28DNdw6GLn st0Th5gjuGkGn0oIs09B7YpxAa2Vf3gBuBIlJTgIpm05pXAycyiAcUTaeuPNcmrRLXYE QAwfrNCjQLlhs8XWq5EwYlQxx0HWCGaTh5vbOyosIdu9OeNuvx7+2yowaOQvGNVE4/gC acbxFjZddhDpQBSOMe3vPp/Zxl6Xb6+3OM6mDB2boSpAWUa84nn9lbADAI3CEhb0vdIm ag4w== X-Gm-Message-State: AOAM5329hZfz874vXUo1KCGUkcQVNpEhub10ys8hJuYKuICrdUfPEnso MUiwzS7+YTcHV9fdpIsJ/3NJCw== X-Received: by 2002:a05:6214:289:: with SMTP id l9mr11698857qvv.238.1595033429865; Fri, 17 Jul 2020 17:50:29 -0700 (PDT) Received: from bbking.lan ([2804:14c:4a5:36c::cd2]) by smtp.gmail.com with ESMTPSA id q65sm4003557qkf.50.2020.07.17.17.50.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 17:50:29 -0700 (PDT) From: Vitor Massaru Iha To: kunit-dev@googlegroups.com Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, brendanhiggins@google.com, keescook@chromium.org, davidgow@google.com, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org Subject: [PATCH v2] lib: kunit: Provides a userspace memory context when tests are compiled as module Date: Fri, 17 Jul 2020 21:50:25 -0300 Message-Id: <20200718005025.440320-1-vitor@massaru.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 ============================== 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