From patchwork Fri Apr 22 21:29:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824076 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A2BCC433F5 for ; Fri, 22 Apr 2022 21:29:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 94C336B0074; Fri, 22 Apr 2022 17:29:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FB1A6B0075; Fri, 22 Apr 2022 17:29:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 774156B0078; Fri, 22 Apr 2022 17:29:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 6AC3D6B0074 for ; Fri, 22 Apr 2022 17:29:54 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4A966612D1 for ; Fri, 22 Apr 2022 21:29:54 +0000 (UTC) X-FDA: 79385807508.08.E7312A8 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf05.hostedemail.com (Postfix) with ESMTP id 5AFAC10003A for ; Fri, 22 Apr 2022 21:29:50 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id d129-20020a254f87000000b006411bf3f331so8223507ybb.4 for ; Fri, 22 Apr 2022 14:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=/z1dLT2VgPLiDImKUFQxfJeCLXLbaB4rbFSQTc8Z7xg=; b=T7Y5iN0BPf96fnzNK7ZHU6vX0fBe/A3dst2c12cELyfKQ79U/HOQor03zypmVLp1If uI8UFZO5MKdK3bq3HmTy6OycGmGbCr8KvVuboRoot8Dmc47nfO5+vN9qfcW9MqGlSZ7l PRJ2hmyct17qJuveqLhAfzJw5ps3ymAMGZ1k31aYcUwdb+GtUMEGkKPlOcqofEoBIlHp pwIx3/Q96ETwqhck8u3dbsamL8jS0omSGAoJtcg/cJqrzRfcOKdB4BklXofPQ4mG7YPR o6lZ1l3IUTdb5dxqkIIoZHbZ7xu4gYhZCpWvRRUJynxmAwAapUiyc6b3GBWS6hzsOm6Y 30ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/z1dLT2VgPLiDImKUFQxfJeCLXLbaB4rbFSQTc8Z7xg=; b=JKAnrWFb60oeK7f62zOe5vEM5sfPg/GdWxu23zPkGe/Cr6s25BqANddUzrJ72M7YJa z3sxDbyPdA/Mug8vBm0ingCUFQhKVCqBCwvc408AzRg28ScWiSnJENKAChAwE6HyMFgs JMt7x/MMi6fK1XtmM4jlHeyuSXJhUATzV3fdmBvpjOW0uyGe1SeBZ1JB1ZBZ97qoTqyg 1T7EBLQEF3iNzPDN0rkYCL0NJXM1PB08268866Vvsou6b0eJOWpiJw84uGEQp5sq8b4M Wt74zI4fiLrnE9UTrPEvdik5TzsjdjGHD2GJcwAlna+F8088V7RWUI6Jot4XbOvgEHoa a6MA== X-Gm-Message-State: AOAM533fYhW0vyj4jGhV1HZzCAAxR1URhd7HX3+GVh1yCp2ys0bnwX/Z b8srdV6IpMkjUhlmQMa7SLWKT6XnYHwzOqhyxxQC X-Google-Smtp-Source: ABdhPJxdF3c0ToaXlnNGfx2U55uFir0mnBR1xoNncmXKoo64JVGSzaYeOVGtOAX0slR2R1/1i4hxxc2yi3eMNWQBn9Hr X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a25:cad8:0:b0:645:7633:d179 with SMTP id a207-20020a25cad8000000b006457633d179mr6471821ybg.304.1650662993181; Fri, 22 Apr 2022 14:29:53 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:40 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-2-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 1/6] selftests: vm: add hugetlb_shared userfaultfd test to run_vmtests.sh From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org X-Stat-Signature: wg511hyemxbg311chms8z6nrx3xmxody X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5AFAC10003A Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=T7Y5iN0B; spf=pass (imf05.hostedemail.com: domain of 3UR5jYg0KCDQQnUbhQickiiUdWeeWbU.SecbYdkn-ccalQSa.ehW@flex--axelrasmussen.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3UR5jYg0KCDQQnUbhQickiiUdWeeWbU.SecbYdkn-ccalQSa.ehW@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-HE-Tag: 1650662990-723882 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This not being included was just a simple oversight. There are certain features (like minor fault support) which are only enabled on shared mappings, so without including hugetlb_shared we actually lose a significant amount of test coverage. Signed-off-by: Axel Rasmussen Reviewed-by: Shuah Khan Reviewed-by: Peter Xu --- tools/testing/selftests/vm/run_vmtests.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh index a2302b5faaf2..5065dbd89bdb 100755 --- a/tools/testing/selftests/vm/run_vmtests.sh +++ b/tools/testing/selftests/vm/run_vmtests.sh @@ -121,9 +121,11 @@ run_test ./gup_test -a run_test ./gup_test -ct -F 0x1 0 19 0x1000 run_test ./userfaultfd anon 20 16 -# Test requires source and destination huge pages. Size of source -# (half_ufd_size_MB) is passed as argument to test. +# Hugetlb tests require source and destination huge pages. Pass in half the +# size ($half_ufd_size_MB), which is used for *each*. run_test ./userfaultfd hugetlb "$half_ufd_size_MB" 32 +run_test ./userfaultfd hugetlb_shared "$half_ufd_size_MB" 32 "$mnt"/uffd-test +rm -f "$mnt"/uffd-test run_test ./userfaultfd shmem 20 16 #cleanup From patchwork Fri Apr 22 21:29:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824077 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8014BC433EF for ; Fri, 22 Apr 2022 21:29:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10C876B0075; Fri, 22 Apr 2022 17:29:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E3736B0078; Fri, 22 Apr 2022 17:29:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC6F26B007D; Fri, 22 Apr 2022 17:29:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id DEA786B0075 for ; Fri, 22 Apr 2022 17:29:56 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BD191252ED for ; Fri, 22 Apr 2022 21:29:56 +0000 (UTC) X-FDA: 79385807592.24.D645234 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 6271018003B for ; Fri, 22 Apr 2022 21:29:54 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-2eb7d137101so81744567b3.12 for ; Fri, 22 Apr 2022 14:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ZKwYZFMlPmL6KMhQQz8m//PR2YxeSx2kvEK7QsVigJI=; b=csJYqjI7+4H3NJVJax9NGS3pNlt2RUYB3+3McYgDnf45op0d8iUq0yCVP4z+6JvUFf 2Cpb+TtSM43LXHqZExuKefroEz7gh03gyTWpMXuqRZSxtuP2YUeMnuRu69CM4h9NHqu1 Rt1m64uZho/RVKCFiWhSXEHProJQP+tXVbfJDgG/1mE9kiZDWXOLeAbrnJgOQ5uYKaBq Baf2Z8XNif2KvlAHnovD2kwKU8KrPFdCWw6E/ubPmigUIOAAwqklhC5yORD60kwFj8M7 dV8w1yeJbu4JN5R08puEouNqPqaZwWoAvF3Gdbt5JP6S2N5c4GIRczR2DvpbQeHh3uEK 7FCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ZKwYZFMlPmL6KMhQQz8m//PR2YxeSx2kvEK7QsVigJI=; b=djuNn2Q3vMPe/2U3/guAzgMsqyY2QAI1X+KOj20mE7JpnpBSZ98POqVy47BTYhFLEQ FIIBxpoXJAF0WycHxIovAV9ENj8BVPYAgAqUO67cG3pxmePD6iQe9J757RxLuDN+faqQ Rnzl3rWujrr3OLPTp7HamLWLJO7W35Cg5abvKTCrhPySuKOqFO3n81o38mIkOAG6PAaV MO2HsDbuv4npSdzCpNmbB59osCs2HGvkuwQYqCmPXKDFndBVVlY1Tg7RKPkwRC3HldJl dQjuIBswTPWpY7VewrBVO8dZZqo4A9Utszqq+yeXCC9fio+mIxeHC7WrQNpLyV03Lkyk LbmA== X-Gm-Message-State: AOAM5329DOurc4fehYtWxI/WuK1XkFY+qLlO8o+2CQZssx+vNMOnfunU PwAPTnO1mZzStAU1iBhR+RhkRqfh6zEG3LtdHHhd X-Google-Smtp-Source: ABdhPJwxCsTz9OxLMDOgPGwJ76n2p6bPU/yWKc6iqu2BypRG7Slj3og+uNJnovkB/PwFjYqyrErsxCwHfIK4qfTFl31r X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a25:8382:0:b0:63d:6201:fa73 with SMTP id t2-20020a258382000000b0063d6201fa73mr6272316ybk.55.1650662995479; Fri, 22 Apr 2022 14:29:55 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:41 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 2/6] userfaultfd: add /dev/userfaultfd for fine grained access control From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=csJYqjI7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3Ux5jYg0KCDYSpWdjSkemkkWfYggYdW.Ugedafmp-eecnSUc.gjY@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3Ux5jYg0KCDYSpWdjSkemkkWfYggYdW.Ugedafmp-eecnSUc.gjY@flex--axelrasmussen.bounces.google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 6271018003B X-Rspam-User: X-Stat-Signature: enegndtgjfznwjhhjuentf148nu5ofws X-HE-Tag: 1650662994-546910 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Historically, it has been shown that intercepting kernel faults with userfaultfd (thereby forcing the kernel to wait for an arbitrary amount of time) can be exploited, or at least can make some kinds of exploits easier. So, in 37cd0575b8 "userfaultfd: add UFFD_USER_MODE_ONLY" we changed things so, in order for kernel faults to be handled by userfaultfd, either the process needs CAP_SYS_PTRACE, or this sysctl must be configured so that any unprivileged user can do it. In a typical implementation of a hypervisor with live migration (take QEMU/KVM as one such example), we do indeed need to be able to handle kernel faults. But, both options above are less than ideal: - Toggling the sysctl increases attack surface by allowing any unprivileged user to do it. - Granting the live migration process CAP_SYS_PTRACE gives it this ability, but *also* the ability to "observe and control the execution of another process [...], and examine and change [its] memory and registers" (from ptrace(2)). This isn't something we need or want to be able to do, so granting this permission violates the "principle of least privilege". This is all a long winded way to say: we want a more fine-grained way to grant access to userfaultfd, without granting other additional permissions at the same time. To achieve this, add a /dev/userfaultfd misc device. This device provides an alternative to the userfaultfd(2) syscall for the creation of new userfaultfds. The idea is, any userfaultfds created this way will be able to handle kernel faults, without the caller having any special capabilities. Access to this mechanism is instead restricted using e.g. standard filesystem permissions. Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 79 ++++++++++++++++++++++++++------ include/uapi/linux/userfaultfd.h | 4 ++ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index aa0c47cb0d16..16d7573ab41a 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -29,6 +29,7 @@ #include #include #include +#include int sysctl_unprivileged_userfaultfd __read_mostly; @@ -65,6 +66,8 @@ struct userfaultfd_ctx { unsigned int flags; /* features requested from the userspace */ unsigned int features; + /* whether or not to handle kernel faults */ + bool handle_kernel_faults; /* released */ bool released; /* memory mappings are changing because of non-cooperative event */ @@ -410,13 +413,8 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) if (ctx->features & UFFD_FEATURE_SIGBUS) goto out; - if ((vmf->flags & FAULT_FLAG_USER) == 0 && - ctx->flags & UFFD_USER_MODE_ONLY) { - printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd " - "sysctl knob to 1 if kernel faults must be handled " - "without obtaining CAP_SYS_PTRACE capability\n"); + if (!(vmf->flags & FAULT_FLAG_USER) && !ctx->handle_kernel_faults) goto out; - } /* * If it's already released don't get it. This avoids to loop @@ -2064,19 +2062,33 @@ static void init_once_userfaultfd_ctx(void *mem) seqcount_spinlock_init(&ctx->refile_seq, &ctx->fault_pending_wqh.lock); } -SYSCALL_DEFINE1(userfaultfd, int, flags) +static inline bool userfaultfd_allowed(bool is_syscall, int flags) +{ + bool kernel_faults = !(flags & UFFD_USER_MODE_ONLY); + bool allow_unprivileged = sysctl_unprivileged_userfaultfd; + + /* userfaultfd(2) access is controlled by sysctl + capability. */ + if (is_syscall && kernel_faults) { + if (!allow_unprivileged && !capable(CAP_SYS_PTRACE)) + return false; + } + + /* + * For /dev/userfaultfd, access is to be controlled using e.g. + * permissions on the device node. We assume this is correctly + * configured by userspace, so we simply allow access here. + */ + + return true; +} + +static int new_userfaultfd(bool is_syscall, int flags) { struct userfaultfd_ctx *ctx; int fd; - if (!sysctl_unprivileged_userfaultfd && - (flags & UFFD_USER_MODE_ONLY) == 0 && - !capable(CAP_SYS_PTRACE)) { - printk_once(KERN_WARNING "uffd: Set unprivileged_userfaultfd " - "sysctl knob to 1 if kernel faults must be handled " - "without obtaining CAP_SYS_PTRACE capability\n"); + if (!userfaultfd_allowed(is_syscall, flags)) return -EPERM; - } BUG_ON(!current->mm); @@ -2095,6 +2107,11 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) refcount_set(&ctx->refcount, 1); ctx->flags = flags; ctx->features = 0; + /* + * If UFFD_USER_MODE_ONLY is not set, then userfaultfd_allowed() above + * decided that kernel faults were allowed and should be handled. + */ + ctx->handle_kernel_faults = !(flags & UFFD_USER_MODE_ONLY); ctx->released = false; atomic_set(&ctx->mmap_changing, 0); ctx->mm = current->mm; @@ -2110,8 +2127,42 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) return fd; } +SYSCALL_DEFINE1(userfaultfd, int, flags) +{ + return new_userfaultfd(true, flags); +} + +static int userfaultfd_dev_open(struct inode *inode, struct file *file) +{ + return 0; +} + +static long userfaultfd_dev_ioctl(struct file *file, unsigned int cmd, unsigned long flags) +{ + if (cmd != USERFAULTFD_IOC_NEW) + return -EINVAL; + + return new_userfaultfd(false, flags); +} + +static const struct file_operations userfaultfd_dev_fops = { + .open = userfaultfd_dev_open, + .unlocked_ioctl = userfaultfd_dev_ioctl, + .compat_ioctl = compat_ptr_ioctl, + .owner = THIS_MODULE, + .llseek = noop_llseek, +}; + +static struct miscdevice userfaultfd_misc = { + .minor = MISC_DYNAMIC_MINOR, + .name = "userfaultfd", + .fops = &userfaultfd_dev_fops +}; + static int __init userfaultfd_init(void) { + WARN_ON(misc_register(&userfaultfd_misc)); + userfaultfd_ctx_cachep = kmem_cache_create("userfaultfd_ctx_cache", sizeof(struct userfaultfd_ctx), 0, diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index ef739054cb1c..032a35b3bbd2 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -12,6 +12,10 @@ #include +/* ioctls for /dev/userfaultfd */ +#define USERFAULTFD_IOC 0xAA +#define USERFAULTFD_IOC_NEW _IOWR(USERFAULTFD_IOC, 0x00, int) + /* * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR. In From patchwork Fri Apr 22 21:29:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824078 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E31E0C433EF for ; Fri, 22 Apr 2022 21:29:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74EBB6B0078; Fri, 22 Apr 2022 17:29:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FF486B007D; Fri, 22 Apr 2022 17:29:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C9016B007E; Fri, 22 Apr 2022 17:29:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 4E8186B0078 for ; Fri, 22 Apr 2022 17:29:59 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 26F6480D78 for ; Fri, 22 Apr 2022 21:29:59 +0000 (UTC) X-FDA: 79385807718.27.2D0DCE7 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf18.hostedemail.com (Postfix) with ESMTP id 3D5521C0028 for ; Fri, 22 Apr 2022 21:29:56 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-2ec0490dc1bso81613167b3.5 for ; Fri, 22 Apr 2022 14:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3u4/NkkFqdFi5mqy1tDjNb7vVlfXM8Eh6uSpLtQT30E=; b=ZGYpRbKPRC+XBA+J9Pgv5tGqSeG77393GIEZtav9y5jdJsYVHZu6DE9FQuFF3DOF0J FuNiC+t/6dWddinYIxjJazPTVsQGm4aYgH1xVag1JT6yX/rutxG2D6vqOIO+3jvrxoR8 LlhdASQ+SYkAZPZBLnxf4QMwcqJESUmBcKLtRd4PizkS49zRHMGslMRd8FZVM8HCDQAt pWB3BEBVFbk8ZupnaeI9yKetCZBkqXpEgICQya4F2EDK8WndIiiM1gyXfMdKP/S13U/+ dxciIAdWsYURvLxXEf7S3BZ8X/iFqLMyaOoYK1FYEq3MotK1Ig03rgQM9CL5HwBdNlNN PzAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3u4/NkkFqdFi5mqy1tDjNb7vVlfXM8Eh6uSpLtQT30E=; b=Tgg0gKqWeQdwIH7wmooUsmpHUicuvl73YdZDAku+rFt+TYW3VjFdXuMIFA5iU3AgLo QVj74XuGHErOiZXUs28i7gY3rkozymU/9MEBxt3zWkevbcnkfiZ1pfwgruAYkWjmaPpN CzVotaOFHcv7iAGXaSKjCLyPJOiXh5wiGFL3Vjiqh+r4jsSNCVASmpS/r+88aGSSSBgO /eqGx6YPFvt/yYmbOHG1TM36HgTK4TlR5irmBxWba/hDwFwUUYP2+nqBryind+wRVtp/ 6zh8jYm2X9McGkSjlszz2nnXFhMOwk73ukvKT0oo2ltPKJ/Ur2HFKzOaG1Xuv3KlR7ff rmlw== X-Gm-Message-State: AOAM53177A860t8oRoMPyscot7QIXs07JMzC6WCEs0o0xSskctmAue2+ 2s+w1prCSNT4nc7D5+ONQYJyGAsmU0V9E7ijiGpu X-Google-Smtp-Source: ABdhPJwut5kVL9//x/VuhvU7tnMAPFLYjWnVV3LoUrrKiz5XT6olvUf5+q/YJhl4rhX6qANLKIq5vC52x37lwR9oMUtA X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a25:40c4:0:b0:645:88a3:eb8b with SMTP id n187-20020a2540c4000000b0064588a3eb8bmr5324286yba.193.1650662997965; Fri, 22 Apr 2022 14:29:57 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:42 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-4-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 3/6] userfaultfd: selftests: modify selftest to use /dev/userfaultfd From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 3D5521C0028 X-Stat-Signature: rdpres4k6ixkcwt1ic4fs33y9m9h13wq Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ZGYpRbKP; spf=pass (imf18.hostedemail.com: domain of 3VR5jYg0KCDgUrYflUmgommYhaiiafY.Wigfchor-ggepUWe.ila@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3VR5jYg0KCDgUrYflUmgommYhaiiafY.Wigfchor-ggepUWe.ila@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1650662996-574410 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: We clearly want to ensure both userfaultfd(2) and /dev/userfaultfd keep working into the future, so just run the test twice, using each interface. Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 31 ++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 92a4516f8f0d..12ae742a9981 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -77,6 +77,9 @@ static int bounces; #define TEST_SHMEM 3 static int test_type; +/* test using /dev/userfaultfd, instead of userfaultfd(2) */ +static bool test_dev_userfaultfd; + /* exercise the test_uffdio_*_eexist every ALARM_INTERVAL_SECS */ #define ALARM_INTERVAL_SECS 10 static volatile bool test_uffdio_copy_eexist = true; @@ -383,13 +386,31 @@ static void assert_expected_ioctls_present(uint64_t mode, uint64_t ioctls) } } +static void __userfaultfd_open_dev(void) +{ + int fd; + + uffd = -1; + fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); + if (fd < 0) + return; + + uffd = ioctl(fd, USERFAULTFD_IOC_NEW, + O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY); + close(fd); +} + static void userfaultfd_open(uint64_t *features) { struct uffdio_api uffdio_api; - uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY); + if (test_dev_userfaultfd) + __userfaultfd_open_dev(); + else + uffd = syscall(__NR_userfaultfd, + O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY); if (uffd < 0) - err("userfaultfd syscall not available in this kernel"); + err("creating userfaultfd failed"); uffd_flags = fcntl(uffd, F_GETFD, NULL); uffdio_api.api = UFFD_API; @@ -1698,6 +1719,12 @@ int main(int argc, char **argv) } printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n", nr_pages, nr_pages_per_cpu); + + test_dev_userfaultfd = false; + if (userfaultfd_stress()) + return 1; + + test_dev_userfaultfd = true; return userfaultfd_stress(); } From patchwork Fri Apr 22 21:29:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824079 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3004CC433EF for ; Fri, 22 Apr 2022 21:30:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A35896B007D; Fri, 22 Apr 2022 17:30:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BD8E6B007E; Fri, 22 Apr 2022 17:30:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 885686B0080; Fri, 22 Apr 2022 17:30:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 7B37E6B007D for ; Fri, 22 Apr 2022 17:30:01 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 5B14160F54 for ; Fri, 22 Apr 2022 21:30:01 +0000 (UTC) X-FDA: 79385807802.13.BD741DE Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 27C1C1C0025 for ; Fri, 22 Apr 2022 21:29:59 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-2f4dee8688cso43169867b3.16 for ; Fri, 22 Apr 2022 14:30:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=BoyidSMyUmqDlYzeEP7zIQ3rGwHdvB7avXDVy08761s=; b=i6OlZZVuB93oXNW0rTUa3oTx5Wl6MFZGMztF7zW6l8uv5ZXP4VZ9/BlQ/fIJ2bNj5S czzlvWKkurK9uKgLUtRxQx52c1k65fe/FZAngeSjsnDddI45UExXmV5LwP9IrqFxD2IL rtOStuM0sMGjHQrlSNKyNGZRZVNEsl2JeX44VJMGESx5Y7WYCCaPLOPxkCwlFNBhKVDk Qw8fn7GcIem22OY4dCLEKW72OsbVeJfMJDIVUycXbzEA3IIOpcnx7NgdQXCA8ZQAouml KftTE01eZE8W63qMuu8ZiPsmT/z34kLTk6gjP+7TIvUM+Wt/kSuOwHlhRL1JNn39s4sE 62UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=BoyidSMyUmqDlYzeEP7zIQ3rGwHdvB7avXDVy08761s=; b=MocexiFBbFKVoGjTk+w7rDaZ1nmzb6DpSSmxbpz4YDaT7tG6keY/6nQ1KzzoeriXgB +GXBVNxybcMZwgfdd1fO2gLCbRydD4BPxg7fkeMkIPyFQFGMCKTU2fxKzIxSZv8Ccmn4 pGapWoJ9QxlCg09QBli2E3MfB1Zn0Ec6EBXeXsTsYlE0QPYRlBFZ4xo0TFRDpQiHWPwx aGnDc9U0+qpV+rTDPPA8JYpA7hwIJUpzW/ZHsrSAE3+61FSSPhvjlz80xsqa6vjNhZUC p7jDdFRlEW7n+eJIFfjyPbb4PJDzCXb5t94YX5Gc2anxYrQecT8kXTLOEqCwbzAcaQN3 OU/w== X-Gm-Message-State: AOAM530ZAjf55U+YlRt3/PXakxcfy8/CaBLlhpeRE9r7OGNUAIpDmEb0 T/a7e55o2S58whjg1IQCtFIDt0AuEN6syPdfnZ1G X-Google-Smtp-Source: ABdhPJyyc9freLfS6Q7maf39aEdLRh07bXNJn2H9FTDAgzp1lfS3Y4cI9xMNuZVlhb0cZNxgd4sxJOv/WouvvrTj1H7F X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a81:56d5:0:b0:2f4:c9b2:575f with SMTP id k204-20020a8156d5000000b002f4c9b2575fmr6944685ywb.111.1650663000214; Fri, 22 Apr 2022 14:30:00 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:43 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-5-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 4/6] userfaultfd: update documentation to describe /dev/userfaultfd From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=i6OlZZVu; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3WB5jYg0KCDsXubioXpjrppbkdlldib.Zljifkru-jjhsXZh.lod@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3WB5jYg0KCDsXubioXpjrppbkdlldib.Zljifkru-jjhsXZh.lod@flex--axelrasmussen.bounces.google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 27C1C1C0025 X-Rspam-User: X-Stat-Signature: gwwrstz1i96gxt8wefkx343jtc3macus X-HE-Tag: 1650662999-16366 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Explain the different ways to create a new userfaultfd, and how access control works for each way. Signed-off-by: Axel Rasmussen --- Documentation/admin-guide/mm/userfaultfd.rst | 38 ++++++++++++++++++-- Documentation/admin-guide/sysctl/vm.rst | 3 ++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 6528036093e1..4c079b5377d4 100644 --- a/Documentation/admin-guide/mm/userfaultfd.rst +++ b/Documentation/admin-guide/mm/userfaultfd.rst @@ -17,7 +17,10 @@ of the ``PROT_NONE+SIGSEGV`` trick. Design ====== -Userfaults are delivered and resolved through the ``userfaultfd`` syscall. +Userspace creates a new userfaultfd, initializes it, and registers one or more +regions of virtual memory with it. Then, any page faults which occur within the +region(s) result in a message being delivered to the userfaultfd, notifying +userspace of the fault. The ``userfaultfd`` (aside from registering and unregistering virtual memory ranges) provides two primary functionalities: @@ -39,7 +42,7 @@ Vmas are not suitable for page- (or hugepage) granular fault tracking when dealing with virtual address spaces that could span Terabytes. Too many vmas would be needed for that. -The ``userfaultfd`` once opened by invoking the syscall, can also be +The ``userfaultfd``, once created, can also be passed using unix domain sockets to a manager process, so the same manager process could handle the userfaults of a multitude of different processes without them being aware about what is going on @@ -50,6 +53,37 @@ is a corner case that would currently return ``-EBUSY``). API === +Creating a userfaultfd +---------------------- + +There are two mechanisms to create a userfaultfd. There are various ways to +restrict this too, since userfaultfds which handle kernel page faults have +historically been a useful tool for exploiting the kernel. + +The first is the userfaultfd(2) syscall. Access to this is controlled in several +ways: + +- By default, the userfaultfd will be able to handle kernel page faults. This + can be disabled by passing in UFFD_USER_MODE_ONLY. + +- If vm.unprivileged_userfaultfd is 0, then the caller must *either* have + CAP_SYS_PTRACE, or pass in UFFD_USER_MODE_ONLY. + +- If vm.unprivileged_userfaultfd is 1, then no particular privilege is needed to + use this syscall, even if UFFD_USER_MODE_ONLY is *not* set. + +Alternatively, userfaultfds can be created by opening /dev/userfaultfd, and +issuing a USERFAULTFD_IOC_NEW ioctl to this device. Access to this device is +controlled via normal filesystem permissions (user/group/mode for example) - no +additional permission (capability/sysctl) is needed to be able to handle kernel +faults this way. This is useful because it allows e.g. a specific user or group +to be able to create kernel-fault-handling userfaultfds, without allowing it +more broadly, or granting more privileges in addition to that particular ability +(CAP_SYS_PTRACE). In other words, it allows permissions to be minimized. + +Initializing up a userfaultfd +------------------------ + When first opened the ``userfaultfd`` must be enabled invoking the ``UFFDIO_API`` ioctl specifying a ``uffdio_api.api`` value set to ``UFFD_API`` (or a later API version) which will specify the ``read/POLLIN`` protocol diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index f4804ce37c58..8682d5fbc8ea 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -880,6 +880,9 @@ calls without any restrictions. The default value is 0. +An alternative to this sysctl / the userfaultfd(2) syscall is to create +userfaultfds via /dev/userfaultfd. See +Documentation/admin-guide/mm/userfaultfd.rst. user_reserve_kbytes =================== From patchwork Fri Apr 22 21:29:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824080 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4F37C433EF for ; Fri, 22 Apr 2022 21:30:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37CB86B007E; Fri, 22 Apr 2022 17:30:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32CF96B0080; Fri, 22 Apr 2022 17:30:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CE346B0081; Fri, 22 Apr 2022 17:30:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 0A3976B007E for ; Fri, 22 Apr 2022 17:30:04 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D482120DA2 for ; Fri, 22 Apr 2022 21:30:03 +0000 (UTC) X-FDA: 79385807886.09.7266064 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf08.hostedemail.com (Postfix) with ESMTP id 1742C160025 for ; Fri, 22 Apr 2022 21:29:59 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-2eb2bc9018aso81477147b3.18 for ; Fri, 22 Apr 2022 14:30:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=1YOjrnQqyN8cWPWkvfNUjTufFbltn2OwC2/XNWaN1e0=; b=VXaVbbtBV03bulzAic2gmN8NCLz/V3ogNL6IMpUPoIEsATtTIJStusANwVvrAoLxjv fkvqlb6oQ96giIPTD0RY1s7jdxptuPWu8kfoVzvuOg8C0C32WXl8qZzv11n9fppg3P7z MCzA2nE/4x8vHUZC/oFKsQJxeBxpCSO9DZyv/RHB6igxtBeXTcNLvhOBUrMONmTXITmC Q1HuwamYVWWP6rtLGSDmD58hOW5WRi7ZxZVg45RtfHx4jYax2LP8OdDXzya11iu+o6tk 88F+2mzpijpTSk69INGoR0ljgkz4YKldnOs3vhQc2Ls/zSToXGQz4AiQRTmuyuOTSk79 WQdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=1YOjrnQqyN8cWPWkvfNUjTufFbltn2OwC2/XNWaN1e0=; b=nN6QL70HTc5hNUzFaP4p6c1VNE/UIcFTgopAmmiLV9l7p8cQ0l6n9c4jZ83AfF57+Z 1SlUYVHREA/b2gQoINIgSlmz7Skd3YxSA/NLnAGRVXR77il1FDWRz5kKAfevDq2r0JbQ 5SsW041YuUfq6H2otoa0H7/66Opmd55kGf3i5KT+EeGoZR9g4u6C7b5KAl3K/SVrsoIx XJ6f0/udNKgQWk0YQ7E+Yr7tHn/Xjek3o2behYDIWqZihtXH6pikcxyUxcoFnpwb/IMB J0rB2Z4AKixl43SzJPcCUdPMfsEHo7Trtb4M8xho0RIyw6v+yneyCF6C/9iz4dJhzXMZ Eyzg== X-Gm-Message-State: AOAM531qJzIR5h+R9oUcHr/wStsUC9mP+erwVH6lUQgT/95ckCz5D2Ru 5IL869wgyXLEojk/CSh/rXA8ZxFBHe4uAR70S53I X-Google-Smtp-Source: ABdhPJyvEbcz/g37JS1iHe7tFbj9IzynivIbmgA81koqkS0c5Hqgo9XHv7TCJvVa1vy3xfQL4hFPgo3isBOBJ39pAMse X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a25:df8a:0:b0:647:4954:83e8 with SMTP id w132-20020a25df8a000000b00647495483e8mr645787ybg.526.1650663002551; Fri, 22 Apr 2022 14:30:02 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:44 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-6-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 5/6] userfaultfd: selftests: make /dev/userfaultfd testing configurable From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=VXaVbbtB; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf08.hostedemail.com: domain of 3Wh5jYg0KCD0ZwdkqZrltrrdmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Wh5jYg0KCD0ZwdkqZrltrrdmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--axelrasmussen.bounces.google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1742C160025 X-Rspam-User: X-Stat-Signature: x9hi66jt577y6a9ud139tgpeoyxj3kf8 X-HE-Tag: 1650662999-408256 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Instead of always testing both userfaultfd(2) and /dev/userfaultfd, let the user choose which to test. As with other test features, change the behavior based on a new command line flag. Introduce the idea of "test mods", which are generic (not specific to a test type) modifications to the behavior of the test. This is sort of borrowed from this RFC patch series [1], but simplified a bit. The benefit is, in "typical" configurations this test is somewhat slow (say, 30sec or something). Testing both clearly doubles it, so it may not always be desirable, as users are likely to use one or the other, but never both, in the "real world". [1]: https://patchwork.kernel.org/project/linux-mm/patch/20201129004548.1619714-14-namit@vmware.com/ Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 41 +++++++++++++++++------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 12ae742a9981..274522704e40 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -142,8 +142,17 @@ static void usage(void) { fprintf(stderr, "\nUsage: ./userfaultfd " "[hugetlbfs_file]\n\n"); + fprintf(stderr, "Supported : anon, hugetlb, " "hugetlb_shared, shmem\n\n"); + + fprintf(stderr, "'Test mods' can be joined to the test type string with a ':'. " + "Supported mods:\n"); + fprintf(stderr, "\tdev - Use /dev/userfaultfd instead of userfaultfd(2)\n"); + fprintf(stderr, "\nExample test mod usage:\n"); + fprintf(stderr, "# Run anonymous memory test with /dev/userfaultfd:\n"); + fprintf(stderr, "./userfaultfd anon:dev 100 99999\n\n"); + fprintf(stderr, "Examples:\n\n"); fprintf(stderr, "%s", examples); exit(1); @@ -1610,8 +1619,6 @@ unsigned long default_huge_page_size(void) static void set_test_type(const char *type) { - uint64_t features = UFFD_API_FEATURES; - if (!strcmp(type, "anon")) { test_type = TEST_ANON; uffd_test_ops = &anon_uffd_test_ops; @@ -1631,10 +1638,28 @@ static void set_test_type(const char *type) test_type = TEST_SHMEM; uffd_test_ops = &shmem_uffd_test_ops; test_uffdio_minor = true; - } else { - err("Unknown test type: %s", type); + } +} + +static void parse_test_type_arg(const char *raw_type) +{ + char *buf = strdup(raw_type); + uint64_t features = UFFD_API_FEATURES; + + while (buf) { + const char *token = strsep(&buf, ":"); + + if (!test_type) + set_test_type(token); + else if (!strcmp(token, "dev")) + test_dev_userfaultfd = true; + else + err("unrecognized test mod '%s'", token); } + if (!test_type) + err("failed to parse test type argument: '%s'", raw_type); + if (test_type == TEST_HUGETLB) page_size = default_huge_page_size(); else @@ -1681,7 +1706,7 @@ int main(int argc, char **argv) err("failed to arm SIGALRM"); alarm(ALARM_INTERVAL_SECS); - set_test_type(argv[1]); + parse_test_type_arg(argv[1]); nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); nr_pages_per_cpu = atol(argv[2]) * 1024*1024 / page_size / @@ -1719,12 +1744,6 @@ int main(int argc, char **argv) } printf("nr_pages: %lu, nr_pages_per_cpu: %lu\n", nr_pages, nr_pages_per_cpu); - - test_dev_userfaultfd = false; - if (userfaultfd_stress()) - return 1; - - test_dev_userfaultfd = true; return userfaultfd_stress(); } From patchwork Fri Apr 22 21:29:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12824081 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2A3CC433F5 for ; Fri, 22 Apr 2022 21:30:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 664E16B0080; Fri, 22 Apr 2022 17:30:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EEFF6B0081; Fri, 22 Apr 2022 17:30:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 467CC6B0082; Fri, 22 Apr 2022 17:30:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 35DED6B0080 for ; Fri, 22 Apr 2022 17:30:06 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 028071040 for ; Fri, 22 Apr 2022 21:30:05 +0000 (UTC) X-FDA: 79385808012.12.AB4A9C4 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf12.hostedemail.com (Postfix) with ESMTP id DC9DC40020 for ; Fri, 22 Apr 2022 21:30:00 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-2dc7bdd666fso81166247b3.7 for ; Fri, 22 Apr 2022 14:30:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=SZmluZmQNNsXve64J622YTKh8VersM5cFsD6n/swy/o=; b=CwnEIUaF3mjN6791/zZx82W3m8Exz8sNaQFrvncFQdFNqY6vXWH9txSyPZcUa6s7zU FquAT5n+wNVDvxWTlzo/RUKpWYw1yMbTjAi8NIpj3NafWQdtSPuuty+uwNLUlt3AAtCw jtKplPIzTy3XtR5yJLijXD/YaGEGMs/5YGMxvcQ693F7IcnH/6dvsTUROAOwAkt1zWPU F9o0CFhL2LLdc0Ux7YGm7rSheRQi84Z9fILU+CiARmFTzIMZxSnIL/U4i78zi2Y05eOt a8PrneNMF+e6r6JMUvT7IhY1+7LEt4a/J2MV0mLR6CN+mSjFjG2HxzZKsOT0hNGpM/Iq M/8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=SZmluZmQNNsXve64J622YTKh8VersM5cFsD6n/swy/o=; b=S90/GEUV4rjav+XQzqHEqel/NxWvhytLcAIQ5DeeDJ0I11nDIAw1hM4g6sJDnDKnVv SfGDzMQvMYodVNWSKQ9QxFxofsWIjOTwLfecyArPGY/BaMwGWt9aMHaHfrAg31TRP8Ho eUOArr1AYXtRnUJTOAxmYU3Xk8pTHKfYYyHSqnWa8eI/oB8rNK04MhlHcNKvF+lEwB+Q VbseuNr5g6B34bZ3zIZYd8eWJ7JKY/Zj3CWeqPIIdGTUy0T30kCN0M0sGfWodAZJy+2F kWIgAGI8KISMC3XUYMNZ1fJm6+6uSVPfGvKpQgbm7FGI+EuUNl8MmD6UrgoPij54L8Yz dhgw== X-Gm-Message-State: AOAM533wAJAoEJcDzmZVn3b8cQXUc6C5wUMqeiRKv8aM8qar6PYViZoL CnQGkZYkZP4b7CGrqt2+6ZwmD04EQSh+4ybSwdLn X-Google-Smtp-Source: ABdhPJyQekwJggjgopvUAQj4NzrkTcMUe5kxTBnnnRYDiIgpf9GP2ohyRZR4/MzEbrDU4XLJ7n4OkBFO7wDmylaZWt9p X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7ba6:20ac:a8f7:1dbd]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:84:b0:63d:4a3d:eb5 with SMTP id h4-20020a056902008400b0063d4a3d0eb5mr6809723ybs.145.1650663004873; Fri, 22 Apr 2022 14:30:04 -0700 (PDT) Date: Fri, 22 Apr 2022 14:29:45 -0700 In-Reply-To: <20220422212945.2227722-1-axelrasmussen@google.com> Message-Id: <20220422212945.2227722-7-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220422212945.2227722-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.36.0.rc2.479.g8af0fa9b8e-goog Subject: [PATCH v2 6/6] selftests: vm: add /dev/userfaultfd test cases to run_vmtests.sh From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Charan Teja Reddy , Dave Hansen , "Dmitry V . Levin" , Gleb Fotengauer-Malinovskiy , Hugh Dickins , Jan Kara , Jonathan Corbet , Mel Gorman , Mike Kravetz , Mike Rapoport , Nadav Amit , Peter Xu , Shuah Khan , Suren Baghdasaryan , Vlastimil Babka , zhangyi Cc: Axel Rasmussen , linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org X-Rspamd-Queue-Id: DC9DC40020 X-Stat-Signature: ryurm3qg4kza148skbd1a61dtworcgsy X-Rspam-User: Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=CwnEIUaF; spf=pass (imf12.hostedemail.com: domain of 3XB5jYg0KCD8byfmsbtnvttfohpphmf.dpnmjovy-nnlwbdl.psh@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3XB5jYg0KCD8byfmsbtnvttfohpphmf.dpnmjovy-nnlwbdl.psh@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1650663000-325674 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This new mode was recently added to the userfaultfd selftest. We want to exercise both userfaultfd(2) as well as /dev/userfaultfd, so add both test cases to the script. Signed-off-by: Axel Rasmussen Reviewed-by: Shuah Khan --- tools/testing/selftests/vm/run_vmtests.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh index 5065dbd89bdb..57f01505c719 100755 --- a/tools/testing/selftests/vm/run_vmtests.sh +++ b/tools/testing/selftests/vm/run_vmtests.sh @@ -121,12 +121,17 @@ run_test ./gup_test -a run_test ./gup_test -ct -F 0x1 0 19 0x1000 run_test ./userfaultfd anon 20 16 +run_test ./userfaultfd anon:dev 20 16 # Hugetlb tests require source and destination huge pages. Pass in half the # size ($half_ufd_size_MB), which is used for *each*. run_test ./userfaultfd hugetlb "$half_ufd_size_MB" 32 +run_test ./userfaultfd hugetlb:dev "$half_ufd_size_MB" 32 run_test ./userfaultfd hugetlb_shared "$half_ufd_size_MB" 32 "$mnt"/uffd-test rm -f "$mnt"/uffd-test +run_test ./userfaultfd hugetlb_shared:dev "$half_ufd_size_MB" 32 "$mnt"/uffd-test +rm -f "$mnt"/uffd-test run_test ./userfaultfd shmem 20 16 +run_test ./userfaultfd shmem:dev 20 16 #cleanup umount "$mnt"