From patchwork Fri Aug 19 20:51:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12949220 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 A101EC32772 for ; Fri, 19 Aug 2022 20:52:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16E756B0073; Fri, 19 Aug 2022 16:52:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D0738D0003; Fri, 19 Aug 2022 16:52:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8BFA6B0075; Fri, 19 Aug 2022 16:52:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D28FA6B0073 for ; Fri, 19 Aug 2022 16:52:13 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id AF56840425 for ; Fri, 19 Aug 2022 20:52:13 +0000 (UTC) X-FDA: 79817539746.20.3EFAC27 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf02.hostedemail.com (Postfix) with ESMTP id 65D0B80006 for ; Fri, 19 Aug 2022 20:52:13 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-31f5960500bso94208127b3.14 for ; Fri, 19 Aug 2022 13:52:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=cU7/6qINBgFAfTXfLa178LOY1yjmrSCVESmZNe7FU4M=; b=bC4lMI8huw1uRRzwGbHtFYE2tB88na003CekKY0ACebDw98OX4FLPryvf3oIjLvbpT 2Z94mnqJPrWYcjP3lmF0GohedIfZnStwZI38zjxUUxQx48j9wG65sUDiEbDITVLYRKcL mVtlRswKM70g0APD+T61jU6cKtU63dY0BC21ebFhGj0hneMmEysLC4lYHb5oDAVNIzpp mb1WTwMI3YxYE/fxX+RjWDwy8EJG82QrF3wHtWjI0Yh4kNcN3PEFdFExtn3hpGXpQYOj rmZcPvO+scdHRyaPD9DDquqWTzT/hefOPdLuGFDA0SzUJxmBxqB86WHfSQvwRiAOGOM6 arxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=cU7/6qINBgFAfTXfLa178LOY1yjmrSCVESmZNe7FU4M=; b=6fyYIzgW6IYsgh/lagqGDQ20F6m6HIV5q9s3+UUa3sxxmmWH62behp3r0QlJgjNc0y RXVUU/ysNeRGaEwegxRl33iC9ve44pCJmhTcUwfQ9GJxx0ZfcCwWtyryR9qaOE+mviQu /YG2evwWFe1jACGwh+P6Nq7fHD0RNroFOntTUF9OMk+quVbZQhJmosOaUrdzoxxr2DLn hvuXeTGzoPhqGyV54kZV5iAL374N/iuqrdGpohwzmM2JX9LZokJb9LJa888pJw8dIHFn jqOeuAAC+TE0eSMrUjxrXbbxbWqIW3BBNWH+afZaB1aB/LrVBcQFZKaERe0W0XElXgk5 zo2Q== X-Gm-Message-State: ACgBeo1KzV/cIepKWTO5ZSOX57mmn/tNKAYM/IGg/l3dLwziAOJhgOS6 5qcxHAHomU7+c749msKBMt046GOBsxnm3GuDXaoc X-Google-Smtp-Source: AA6agR597bmial/03bil7OFmDdc43FwP1zL4eJRRyQeIFMDrkuV6ElwIhtWPrgaR/KqOaLYWWlpmihWtAUKx254JQc// X-Received: from ajr0.svl.corp.google.com ([2620:15c:2d4:203:baf:4c5:18b:2c4b]) (user=axelrasmussen job=sendgmr) by 2002:a25:dcd4:0:b0:690:6ce9:7bb5 with SMTP id y203-20020a25dcd4000000b006906ce97bb5mr9476909ybe.516.1660942332708; Fri, 19 Aug 2022 13:52:12 -0700 (PDT) Date: Fri, 19 Aug 2022 13:51:57 -0700 In-Reply-To: <20220819205201.658693-1-axelrasmussen@google.com> Message-Id: <20220819205201.658693-2-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220819205201.658693-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 1/5] selftests: vm: add hugetlb_shared userfaultfd test to run_vmtests.sh From: Axel Rasmussen To: Alexander Viro , Andrew Morton , 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-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Shuah Khan ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660942333; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cU7/6qINBgFAfTXfLa178LOY1yjmrSCVESmZNe7FU4M=; b=IYFktYhZK/WWoJHIEpbvJ+88BblNBheFt3DPlCO8QGgeg0nBhdt11XG1tgOk+TG1aq2euQ prZUZHW8UmSfKUQI7pGN/eawQUdWIU/ZTkNKdbPmIt4cuLPuB6JidmHv688tIxIGqdeU40 krAa6uiLXqJ/w3+ZUhj9fCcm1s7hETs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bC4lMI8h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3_Pf_Yg0KCAsl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3_Pf_Yg0KCAsl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660942333; a=rsa-sha256; cv=none; b=bjKJ/IA0NFmpATKR/bbDljUhDPMaxj6LS6Z5emHK+WHVryL3qQ0v/8ke/0VC3Kf+n3hAn5 7xZ6ROra513jlzvDd72K8SdruG/uLct0Kk6doQ/aVWGNKRF9fdSOzgBGiU3E+EbtbtaSOg x1tGWc52Tb0JB4sqZXUH6aY+VBuocAM= X-Stat-Signature: ntr8d6kn1c7acbxx5xrj3kq5jjh1tw8b X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 65D0B80006 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=bC4lMI8h; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3_Pf_Yg0KCAsl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3_Pf_Yg0KCAsl8pw2l3x533pyrzzrwp.nzxwty58-xxv6lnv.z2r@flex--axelrasmussen.bounces.google.com X-Rspam-User: X-HE-Tag: 1660942333-863144 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. Reviewed-by: Shuah Khan Reviewed-by: Peter Xu Signed-off-by: Axel Rasmussen --- 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 de86983b8a0f..b8e7f6f38d64 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 Aug 19 20:51:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12949224 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 C34E7C32771 for ; Fri, 19 Aug 2022 20:53:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47A946B0073; Fri, 19 Aug 2022 16:53:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 429E68E0001; Fri, 19 Aug 2022 16:53:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F2798D0003; Fri, 19 Aug 2022 16:53:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 221FE6B0073 for ; Fri, 19 Aug 2022 16:53:43 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EE111803E0 for ; Fri, 19 Aug 2022 20:53:42 +0000 (UTC) X-FDA: 79817543484.18.274CA98 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf07.hostedemail.com (Postfix) with ESMTP id CB267400C4 for ; Fri, 19 Aug 2022 20:52:15 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-333b218f2cbso94412997b3.0 for ; Fri, 19 Aug 2022 13:52:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=Idvj8+mYb4IUUixNLqVBka+slWZ3wfKGTMlkwxnvXbY=; b=RTR1sKOG3HS4oRmW+To22+xxQ9cPl1t5H7t3If9jYPgGFCxKjrjIRLLoyJf0Zqg9M3 A68g/+ubMGpKBNXnLo6zhHo/1eHFuYJrnYeyg/B+LL/j3oRRKJMIs5DFXTmcwbaJrwWd NTpoSvxhWDlytbbQ9xrbhKWMpDVCd3Oob9QI+i3wrCh1GopvhF7R6B0QLj/RqxrAs58J 8q4wfbQX1lJcncPCvsneCOALWmwYNq5jrsRjdSvOTXXOb4hM2qdEwiCb1k5qZQlSeHbc fbntD2eqsTowh9hXEYMRTZC2dYPzaKx7C0TBU1DmNY2A2XmhlIUs9xienW1M2adUTMDE j/lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=Idvj8+mYb4IUUixNLqVBka+slWZ3wfKGTMlkwxnvXbY=; b=e/XUbNlZ454TwmcvdlYE35NMKGdNXurfHpwZQezZ9e9Doy+YZLeAUuEc1YQW2y2OhA vBc1LB/MQfUJU6m/r9iQqeznwBO3N/SI87BD2YrF1GnhKMS+gvBkTRxSZYrPQwOlfuOd b69puH/D7V0+6VFnX7lidItKzruN306QN5OFhxVFKLxUmRVLC/9FqiR9XyzulFfpOGJw 3aX/H+MYDqbCcHv5mXy6jSMypQTAfCK20hWJydIbozMgXkKMiS0hpIuRk+AZkT8KHKMH 79fTYk/XHHL7vYGBX66TVIhyeM9v/5lPYw6thJoeXhayxAbJiAGK3UXoLiN3fKlnvq8H Bm5w== X-Gm-Message-State: ACgBeo2qZck1q59DywxKR1i6fvQSMOo+t60hAHpUl6M687mSicSYHk1+ QJkORf4iN9BYxO5ckVGe0PxfLUF89/LIiXdPu4/I X-Google-Smtp-Source: AA6agR6EvZXCKnp/iA4CQcQxWJXCi/R8jfuoo5HJrCckmL50W1T2bKUISNLxRbusQZ/m1ZhpIN+ZdYhFaJ8pMV/EVH6B X-Received: from ajr0.svl.corp.google.com ([2620:15c:2d4:203:baf:4c5:18b:2c4b]) (user=axelrasmussen job=sendgmr) by 2002:a81:1117:0:b0:336:45a1:221b with SMTP id 23-20020a811117000000b0033645a1221bmr9778987ywr.229.1660942335151; Fri, 19 Aug 2022 13:52:15 -0700 (PDT) Date: Fri, 19 Aug 2022 13:51:58 -0700 In-Reply-To: <20220819205201.658693-1-axelrasmussen@google.com> Message-Id: <20220819205201.658693-3-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220819205201.658693-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 2/5] userfaultfd: add /dev/userfaultfd for fine grained access control From: Axel Rasmussen To: Alexander Viro , Andrew Morton , 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-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Mike Rapoport ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660942335; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Idvj8+mYb4IUUixNLqVBka+slWZ3wfKGTMlkwxnvXbY=; b=ICb9sWWkKrR6T4ETHnRYFFO7tjGNZurzuVM82NLZ8eew8cZ1dGqDK0NmvDOrSdouTdkkLK OmQ3y03dF0eFPXR4MJGFrW42+owoupBjGM/l5iEqXiBaLov5hZuYPk1gPDvlAC6BVj0NEW Msb4R0WBhNB9URbsx1ht0IFeZpGy/sg= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RTR1sKOG; spf=pass (imf07.hostedemail.com: domain of 3__f_Yg0KCA4oBsz5o60866s1u22uzs.q20zw18B-00y9oqy.25u@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3__f_Yg0KCA4oBsz5o60866s1u22uzs.q20zw18B-00y9oqy.25u@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660942335; a=rsa-sha256; cv=none; b=Stw+U4298wHylEPcfwXLuk9sDlXb/HZkw1SuRNx+57NoRg01W1P0yedV9cDfEEcbVCcHGs sqY09l2agdtS6FcvGI0V6JS04ZKTGMuthmUUVYhUHOLXfc/CO8x+k6baxlpZtog63+AiC3 mnEY51Nk/G+CHU7FHNmAVcGI5Y5g1RE= X-Stat-Signature: i98wb7ajq558y4foff14fmrxx5qwkyxt X-Rspam-User: Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=RTR1sKOG; spf=pass (imf07.hostedemail.com: domain of 3__f_Yg0KCA4oBsz5o60866s1u22uzs.q20zw18B-00y9oqy.25u@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3__f_Yg0KCA4oBsz5o60866s1u22uzs.q20zw18B-00y9oqy.25u@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: CB267400C4 X-HE-Tag: 1660942335-400720 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. Acked-by: Mike Rapoport Acked-by: Nadav Amit Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- fs/userfaultfd.c | 71 +++++++++++++++++++++++++------- include/uapi/linux/userfaultfd.h | 4 ++ 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 1c44bf75f916..03ba23e144ec 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -30,6 +30,7 @@ #include #include #include +#include int sysctl_unprivileged_userfaultfd __read_mostly; @@ -415,13 +416,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->flags & UFFD_USER_MODE_ONLY)) goto out; - } /* * If it's already released don't get it. This avoids to loop @@ -2052,20 +2048,11 @@ 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 int new_userfaultfd(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"); - return -EPERM; - } - BUG_ON(!current->mm); /* Check the UFFD_* constants for consistency. */ @@ -2098,8 +2085,60 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) return fd; } +static inline bool userfaultfd_syscall_allowed(int flags) +{ + /* Userspace-only page faults are always allowed */ + if (flags & UFFD_USER_MODE_ONLY) + return true; + + /* + * The user is requesting a userfaultfd which can handle kernel faults. + * Privileged users are always allowed to do this. + */ + if (capable(CAP_SYS_PTRACE)) + return true; + + /* Otherwise, access to kernel fault handling is sysctl controlled. */ + return sysctl_unprivileged_userfaultfd; +} + +SYSCALL_DEFINE1(userfaultfd, int, flags) +{ + if (!userfaultfd_syscall_allowed(flags)) + return -EPERM; + + return new_userfaultfd(flags); +} + +static long userfaultfd_dev_ioctl(struct file *file, unsigned int cmd, unsigned long flags) +{ + if (cmd != USERFAULTFD_IOC_NEW) + return -EINVAL; + + return new_userfaultfd(flags); +} + +static const struct file_operations userfaultfd_dev_fops = { + .unlocked_ioctl = userfaultfd_dev_ioctl, + .compat_ioctl = userfaultfd_dev_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) { + int ret; + + ret = misc_register(&userfaultfd_misc); + if (ret) + return ret; + 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 7d32b1e797fb..005e5e306266 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 _IO(USERFAULTFD_IOC, 0x00) + /* * 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 Aug 19 20:51:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12949223 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 1D227C32771 for ; Fri, 19 Aug 2022 20:53:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A951D8D0002; Fri, 19 Aug 2022 16:53:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A44B06B0074; Fri, 19 Aug 2022 16:53:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E5608D0002; Fri, 19 Aug 2022 16:53:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 81E6D6B0073 for ; Fri, 19 Aug 2022 16:53:37 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3DBFC1C740C for ; Fri, 19 Aug 2022 20:53:37 +0000 (UTC) X-FDA: 79817543274.17.25A7F4F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf30.hostedemail.com (Postfix) with ESMTP id 4230C800DA for ; Fri, 19 Aug 2022 20:52:18 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-3363b1dffa0so94470817b3.23 for ; Fri, 19 Aug 2022 13:52:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=yddoa+559b8nrUIn5RkzA6LuoLnD/Z+9mfZhmJLmujQ=; b=my883Qr7q2OON0CpEqhRtrz+PY7LyVlLJ+7+aSX7VIF9RVV7BlPeiuj3wj+mjCAYIn Jt7/4r32l/sCdhNl/q8xImhpxvOxLuwRwtgTRqopu3usT49UakMT3ENQ0iTPChAtg7F/ oV4OMoasRm/AdrVp2QGXhzEu2mTj7C+36zgp//kcZDryGVvgEn+HuBa1Ma/wx0xu10Tq nzLu7LXykuXplpaZt3l2c8WILxS84SFBAAk/Xi8eLEfFWdG0MHvP0BxwMtw2WAmOVTd3 GpmzVIoKjIOwDVQwDtinBx3EJEsKh5Ihc1ucblvRei20+EQiATKTGCEaWV5qGtxp+Snr wdEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=yddoa+559b8nrUIn5RkzA6LuoLnD/Z+9mfZhmJLmujQ=; b=xo8qG+HsVtCNfoozM8JMAtjq2zEWMDv68quE4T0LjQLKnlv13Y+M67J1DrRhtUWYUY Thd+vu1UBK+QqwelyGTKWB0AcN6vxd2fcAVEceMWfnW6CCvjTK719+jqvCzjXzjYnf9M +tpfenAa5lp7wE/U/tT/0vBigt9S6HJFjIJh6Z3BqrRIj2uZmC2kkHPRJuz0y05QUyDL +7zPxplQ27fwFCkpAFVxBBxlI/v77WjT2rV3Oih+5vvJelql//ytLcb7u1RBAmYhouHs 4MX1imK6/AASKoZ4l5PeEEYfRMd/yoE3ofv5lXjAghIhf+5PmuESMJzszYmEU1O/o4Sf cQ0g== X-Gm-Message-State: ACgBeo1iAdvFoB/yXBL4eHZaeOQfRKrrXGUYRY/jROIHkSzO/4ebUsvI 7ptWj9K2rOCsnnWgXMUhBhUQNMvQUVEaNb8LY1xS X-Google-Smtp-Source: AA6agR4Fv0u8xVoXAMEjH9yxjeVIHVb8ARI/qPczAvOkzx8U62q5pbP/NrE4LQys48mtCcOHPPKtPCRR2kMtOa8mqzPJ X-Received: from ajr0.svl.corp.google.com ([2620:15c:2d4:203:baf:4c5:18b:2c4b]) (user=axelrasmussen job=sendgmr) by 2002:a81:1444:0:b0:335:c517:6ec with SMTP id 65-20020a811444000000b00335c51706ecmr9651721ywu.57.1660942337474; Fri, 19 Aug 2022 13:52:17 -0700 (PDT) Date: Fri, 19 Aug 2022 13:51:59 -0700 In-Reply-To: <20220819205201.658693-1-axelrasmussen@google.com> Message-Id: <20220819205201.658693-4-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220819205201.658693-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 3/5] userfaultfd: selftests: modify selftest to use /dev/userfaultfd From: Axel Rasmussen To: Alexander Viro , Andrew Morton , 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-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Mike Rapoport ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660942338; a=rsa-sha256; cv=none; b=YusSmxml0ICtb0Rkag+Up84QlyhHE5o57jo6JubHs05fCMUGnU7S8GFJhAHz3cDuF+GOc0 v7rRMpzDpklwXapFXeISlM3AFMek9Xa1YXn4eVMq89d5SpjQQHauG0tUkA0e7qCRbiwLVa DHkDUG8KlGxPLdsvAVWn+E5B+1Im2Z0= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=my883Qr7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3Afj_Yg0KCBAqDu17q82A88u3w44w1u.s421y3AD-220Bqs0.47w@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Afj_Yg0KCBAqDu17q82A88u3w44w1u.s421y3AD-220Bqs0.47w@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660942338; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yddoa+559b8nrUIn5RkzA6LuoLnD/Z+9mfZhmJLmujQ=; b=5KM7vg+JLVIdWpKAt0d0/gxeZ1XEiaKmiSIQ0wylDXOOX6W/xfWgc8/qwYRyKs1+UBHOoS LCMe2CyynQZQV3s1Xl+ItUYYp61/mMU08M9d2FdEVJ+9xJY1ozFcLY/xDlyVuJSvduoi3H vOVAXlHinfv3gCMqKDm1e1J43DgpciE= X-Stat-Signature: hhhub6ui5qwwf5835kbq94yejddhxmth X-Rspamd-Queue-Id: 4230C800DA Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=my883Qr7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3Afj_Yg0KCBAqDu17q82A88u3w44w1u.s421y3AD-220Bqs0.47w@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Afj_Yg0KCBAqDu17q82A88u3w44w1u.s421y3AD-220Bqs0.47w@flex--axelrasmussen.bounces.google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-HE-Tag: 1660942338-323739 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. 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/ Acked-by: Mike Rapoport Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/userfaultfd.c | 76 ++++++++++++++++++++---- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/vm/userfaultfd.c b/tools/testing/selftests/vm/userfaultfd.c index 7c3f1b0ab468..7be709d9eed0 100644 --- a/tools/testing/selftests/vm/userfaultfd.c +++ b/tools/testing/selftests/vm/userfaultfd.c @@ -77,6 +77,11 @@ static int bounces; #define TEST_SHMEM 3 static int test_type; +#define UFFD_FLAGS (O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY) + +/* 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; @@ -125,6 +130,8 @@ struct uffd_stats { const char *examples = "# Run anonymous memory test on 100MiB region with 99999 bounces:\n" "./userfaultfd anon 100 99999\n\n" + "# Run the same anonymous memory test, but using /dev/userfaultfd:\n" + "./userfaultfd anon:dev 100 99999\n\n" "# Run share memory test on 1GiB region with 99 bounces:\n" "./userfaultfd shmem 1000 99\n\n" "# Run hugetlb memory test on 256MiB region with 50 bounces:\n" @@ -141,6 +148,14 @@ static void usage(void) "[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, "\tsyscall - Use userfaultfd(2) (default)\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); @@ -154,12 +169,14 @@ static void usage(void) ret, __LINE__); \ } while (0) -#define err(fmt, ...) \ +#define errexit(exitcode, fmt, ...) \ do { \ _err(fmt, ##__VA_ARGS__); \ - exit(1); \ + exit(exitcode); \ } while (0) +#define err(fmt, ...) errexit(1, fmt, ##__VA_ARGS__) + static void uffd_stats_reset(struct uffd_stats *uffd_stats, unsigned long n_cpus) { @@ -383,13 +400,34 @@ static void assert_expected_ioctls_present(uint64_t mode, uint64_t ioctls) } } +static int __userfaultfd_open_dev(void) +{ + int fd, _uffd; + + fd = open("/dev/userfaultfd", O_RDWR | O_CLOEXEC); + if (fd < 0) + errexit(KSFT_SKIP, "opening /dev/userfaultfd failed"); + + _uffd = ioctl(fd, USERFAULTFD_IOC_NEW, UFFD_FLAGS); + if (_uffd < 0) + errexit(errno == ENOTTY ? KSFT_SKIP : 1, + "creating userfaultfd failed"); + close(fd); + return _uffd; +} + 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 (uffd < 0) - err("userfaultfd syscall not available in this kernel"); + if (test_dev_userfaultfd) + uffd = __userfaultfd_open_dev(); + else { + uffd = syscall(__NR_userfaultfd, UFFD_FLAGS); + if (uffd < 0) + errexit(errno == ENOSYS ? KSFT_SKIP : 1, + "creating userfaultfd failed"); + } uffd_flags = fcntl(uffd, F_GETFD, NULL); uffdio_api.api = UFFD_API; @@ -1584,8 +1622,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; @@ -1603,9 +1639,29 @@ 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 if (!strcmp(token, "syscall")) + test_dev_userfaultfd = false; + 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(); @@ -1653,7 +1709,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 / From patchwork Fri Aug 19 20:52:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12949221 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 DC077C32772 for ; Fri, 19 Aug 2022 20:52:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DEC88E0001; Fri, 19 Aug 2022 16:52:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 38EDA8D0003; Fri, 19 Aug 2022 16:52:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27C2D8E0001; Fri, 19 Aug 2022 16:52:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1A6FF8D0003 for ; Fri, 19 Aug 2022 16:52:21 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E6B11C0341 for ; Fri, 19 Aug 2022 20:52:20 +0000 (UTC) X-FDA: 79817540040.28.F25DADF Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf23.hostedemail.com (Postfix) with ESMTP id 886C1140008 for ; Fri, 19 Aug 2022 20:52:20 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-337ed9110c2so51034067b3.15 for ; Fri, 19 Aug 2022 13:52:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=jMyZorWE6E22hV3NDdjIMErSuhZAX9bJlMRQ4lfwoQk=; b=IpWUwfsPRKJE7OaJsJdB2L08XMnLW2ICg+R0QoqCfLw/E4APvPDrTfhcUirY+CZND6 evb3tGyOoymzo6FCUW8oF7Syaupj6TQjDhLih1PVQf/Rio67h8m/TNR8mc+mNIFCccFO XDR3/ttkxX+6+uyZ0L00oWOJw7Abd3WlwNN0nzKmbpMcBzrOLmiIY2XAchs5p9aaKPyV 19dMLz80LBUqjO9wA3XLdqK8TlQm9heL8kJXn7tdZEh0h1An+wd9Oxwp+/xzzATJG9Wb jFJZoo5Q77AwoC/iF70h0EENSO63wwRgUEvWmas1zzZOsbJjVnw2hhoS71ceZe4QoIX8 uB7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=jMyZorWE6E22hV3NDdjIMErSuhZAX9bJlMRQ4lfwoQk=; b=w8lwzwgMt+TKuFyk+/r6W9/izWPLKsV0otNZWkOM+UV6La3oCNMuCEl/qPY8POYXv1 8a5k5Li9a4QWe5msMrUZkHS2R2AS8qFR2lc2srZvS9bhY3QxpdJLkAsAtWQepANDGS8H /1X7LHhb9uwoRyjIuzU+8obRLzWnBvdj3ZZuuUhZcDr0tvqi3qKIzUdzgnAo+zAB+ZCh L/psw27NzdDmatYjIgAgoc6h7nEXdxswmWU+RacFJf+rTDz7DaljD4lwEzTMc9r6oQLC tZI53kNHYgedJtRW886S99RxJ95j/ANJs5SboPFE+8yypcFC3Dzn/IBep7zSQgFspKj8 OdEA== X-Gm-Message-State: ACgBeo3H+IZTQeQrNNqD7y0J3Lw3R9fxA7KWVEExioW3FLcHKSVw31oE w1KEF006qGZig8AClHYiC8+4rEVXFJm9uvydD51C X-Google-Smtp-Source: AA6agR5HOAyxwvEDm7V3pQX2zZvc4IogiVRAS3GDNSIX/0PRNm7ftLReSWlEyTi49ZVZs+XnFcamtJitzIArA/qTOKgk X-Received: from ajr0.svl.corp.google.com ([2620:15c:2d4:203:baf:4c5:18b:2c4b]) (user=axelrasmussen job=sendgmr) by 2002:a0d:eace:0:b0:332:1c2a:6d7c with SMTP id t197-20020a0deace000000b003321c2a6d7cmr9325489ywe.481.1660942339850; Fri, 19 Aug 2022 13:52:19 -0700 (PDT) Date: Fri, 19 Aug 2022 13:52:00 -0700 In-Reply-To: <20220819205201.658693-1-axelrasmussen@google.com> Message-Id: <20220819205201.658693-5-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220819205201.658693-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 4/5] userfaultfd: update documentation to describe /dev/userfaultfd From: Axel Rasmussen To: Alexander Viro , Andrew Morton , 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-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=IpWUwfsP; spf=pass (imf23.hostedemail.com: domain of 3A_j_Yg0KCBIsFw39sA4CAAw5y66y3w.u64305CF-442Dsu2.69y@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3A_j_Yg0KCBIsFw39sA4CAAw5y66y3w.u64305CF-442Dsu2.69y@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660942340; a=rsa-sha256; cv=none; b=w3oUvFWjHsHaUwKj9KNtedPKaS4VSKx7PJxToc2uV5TpBsETL5NFkQJNVNJ4emuTg8QyLv 1rR6nhRCzz3oOpROHkJ2dTPQvoCYkBz/aPbqMzuCZ35JaB1gvb67QQN+sdIKwKXhjB1gUq K99Ay9240kZpOO2hmB09ZI7DwKIZ34w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660942340; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jMyZorWE6E22hV3NDdjIMErSuhZAX9bJlMRQ4lfwoQk=; b=mhWVlF7Rr8iFSEKFXRijcQM+MjHzLDwqaY5JjTAFH7XHLhuj0NBULmOx7Q1G6NfsWWcWuk drEm63ev3BFC+kLyRkS6ptWOe9nbsafRboXHgMA7h1VVV6ICtxpkN1JgGmcarpSSwI8I/G 9CQo8JR3bEtZdFfVNBTO79Go4VXqg20= X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=IpWUwfsP; spf=pass (imf23.hostedemail.com: domain of 3A_j_Yg0KCBIsFw39sA4CAAw5y66y3w.u64305CF-442Dsu2.69y@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3A_j_Yg0KCBIsFw39sA4CAAw5y66y3w.u64305CF-442Dsu2.69y@flex--axelrasmussen.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam02 X-Stat-Signature: pb3er98mx78h9ucge6sx83j4qp6rymhi X-Rspamd-Queue-Id: 886C1140008 X-HE-Tag: 1660942340-598758 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. Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- Documentation/admin-guide/mm/userfaultfd.rst | 41 ++++++++++++++++++-- Documentation/admin-guide/sysctl/vm.rst | 3 ++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Documentation/admin-guide/mm/userfaultfd.rst b/Documentation/admin-guide/mm/userfaultfd.rst index 6528036093e1..83f31919ebb3 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: @@ -34,12 +37,11 @@ The real advantage of userfaults if compared to regular virtual memory management of mremap/mprotect is that the userfaults in all their operations never involve heavyweight structures like vmas (in fact the ``userfaultfd`` runtime load never takes the mmap_lock for writing). - 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 +52,39 @@ is a corner case that would currently return ``-EBUSY``). API === +Creating a userfaultfd +---------------------- + +There are two ways to create a new userfaultfd, each of which provide ways to +restrict access to this functionality (since historically userfaultfds which +handle kernel page faults have been a useful tool for exploiting the kernel). + +The first way, supported since userfaultfd was introduced, is the +userfaultfd(2) syscall. Access to this is controlled in several ways: + +- Any user can always create a userfaultfd which traps userspace page faults + only. Such a userfaultfd can be created using the userfaultfd(2) syscall + with the flag UFFD_USER_MODE_ONLY. + +- In order to also trap kernel page faults for the address space, either the + process needs the CAP_SYS_PTRACE capability, or the system must have + vm.unprivileged_userfaultfd set to 1. By default, vm.unprivileged_userfaultfd + is set to 0. + +The second way, added to the kernel more recently, is by opening +/dev/userfaultfd and issuing a USERFAULTFD_IOC_NEW ioctl to it. This method +yields equivalent userfaultfds to the userfaultfd(2) syscall. + +Unlike userfaultfd(2), access to /dev/userfaultfd is controlled via normal +filesystem permissions (user/group/mode), which gives fine grained access to +userfaultfd specifically, without also granting other unrelated privileges at +the same time (as e.g. granting CAP_SYS_PTRACE would do). Users who have access +to /dev/userfaultfd can always create userfaultfds that trap kernel page faults; +vm.unprivileged_userfaultfd is not considered. + +Initializing 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 9b833e439f09..988f6a4c8084 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -926,6 +926,9 @@ calls without any restrictions. The default value is 0. +Another way to control permissions for userfaultfd is to use +/dev/userfaultfd instead of userfaultfd(2). See +Documentation/admin-guide/mm/userfaultfd.rst. user_reserve_kbytes =================== From patchwork Fri Aug 19 20:52:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 12949222 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 E6557C32771 for ; Fri, 19 Aug 2022 20:52:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7FCB38D0003; Fri, 19 Aug 2022 16:52:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AC058E0002; Fri, 19 Aug 2022 16:52:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 673EC8D0005; Fri, 19 Aug 2022 16:52:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 575C88D0003 for ; Fri, 19 Aug 2022 16:52:23 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 34234AC392 for ; Fri, 19 Aug 2022 20:52:23 +0000 (UTC) X-FDA: 79817540166.31.CB7173A Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf27.hostedemail.com (Postfix) with ESMTP id DFA344000C for ; Fri, 19 Aug 2022 20:52:22 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-335cf0fd1a4so94353617b3.11 for ; Fri, 19 Aug 2022 13:52:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc; bh=jnCzi2soCjCrdoYyE26okeRANWMjNTuMb/vBG8/63Ug=; b=dIpcC3duR2F4UdEnpnHe2+B2ki05YIFseJ6XkifI1ovJCzemg2dHLOQR3Hcrc13yAL Lpy72mYkw+OVc8KXup1+UJK4XLguOgG5HwNBjH22RGBbJMIcWlulfNwQfWuP3eCH1lzl b6B/y6zPicu82sY/bgXGd+sZ7zR21n9PVfNhwwMc7sFL1dgEFNdgDl4r6Df+26JDnzoX D/QTc2Uc0q9CmmICpeKvhxd1UkQnOMlXqy1sHiVX7JddGHfDA7XMLC1N6qy4z3fF6Ds0 wKDnL40gO8YfOaMBnPWMebrtf+ROzhZfFnwNXLkDKPFG0O28730Y0sG1orQLamNZ7Y7E ZYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc; bh=jnCzi2soCjCrdoYyE26okeRANWMjNTuMb/vBG8/63Ug=; b=Vxs2pK4/9e4lzoL6G5ivMpDKw3TuAtPqt3+jJ+8ghRCcIA5NVKQS8uMht9sz93wwHi A5x3Xk8MpQAZHei4CuH0XKZ3PfI5RkNTWZGpSE043TpOhaVngV66zqbH1xt6vTe2VFuy jMIi+kC4WTAcwu1H77ch0/GR/0h9BG3nMNRXXb8M+fjG/+wmgKuemPkqzWKYTESJ2Nj2 xvIfVLMv9wtRqY+U+873m0ZRN50NuFtGnklr7ddjmJDhhwm/J1Dzowyp8bwV8pswYulK yKUH+CdAkcspwnLiia+UkCU+PkrINKzaVofw2xKAZkxL9nkSBqt/cUaq46AWahNdTG0g SgvQ== X-Gm-Message-State: ACgBeo3Ob0VZ5OiK4P6Sboo288258d6QbbzPKdZAhwidyLjGs/kCFmv1 RZCF467xGpGst3RJ+ydpjFKkveatt73sWQAK5Vcq X-Google-Smtp-Source: AA6agR6tYdLSyLary3+Q5VuCsv3LcSs1JihCOYeNOoWw3HYFbFr1DaLds4QXVaepr+bVaJJ9uL2y6ztcYfkdMgBc8C6B X-Received: from ajr0.svl.corp.google.com ([2620:15c:2d4:203:baf:4c5:18b:2c4b]) (user=axelrasmussen job=sendgmr) by 2002:a81:eb04:0:b0:329:94ef:3e68 with SMTP id n4-20020a81eb04000000b0032994ef3e68mr9476724ywm.309.1660942342250; Fri, 19 Aug 2022 13:52:22 -0700 (PDT) Date: Fri, 19 Aug 2022 13:52:01 -0700 In-Reply-To: <20220819205201.658693-1-axelrasmussen@google.com> Message-Id: <20220819205201.658693-6-axelrasmussen@google.com> Mime-Version: 1.0 References: <20220819205201.658693-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.37.1.595.g718a3a8f04-goog Subject: [PATCH v7 5/5] selftests: vm: add /dev/userfaultfd test cases to run_vmtests.sh From: Axel Rasmussen To: Alexander Viro , Andrew Morton , 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-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, Shuah Khan ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660942342; a=rsa-sha256; cv=none; b=6KFUoolrf3NxfSNUiR6u55R2MmUoHhtBN6BfGACnac5FZcbSoz2WyJ/YsbrNImlnDK5/ky R17QVSB/BetWkRDYcNVA4MEKloDacGWf5acGJ0srbGW6hDBrYJVqb+jqmWqCC/SC6jecoe WhGQ7sPY0woz9VINiXDTcm7PMtR0Be8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dIpcC3du; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3Bvj_Yg0KCBUvIz6CvD7FDDz819916z.x97638FI-775Gvx5.9C1@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Bvj_Yg0KCBUvIz6CvD7FDDz819916z.x97638FI-775Gvx5.9C1@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660942342; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jnCzi2soCjCrdoYyE26okeRANWMjNTuMb/vBG8/63Ug=; b=v8rDJzirWAIAR9fEa2BXw41MoQEyT3kdY2+CvIvT/lYlHnKSNS62dmPjsQjY5I+YNFhK4R EwJ7qD8wr16W1gB9EY63c8MN9UgvJ1zxdppEdERvtGWNoKWJZCVZa9HOtU1iG0kVIASOD7 ymz9pKDAKpQCE2iNYGTwW63/phWFGaI= X-Stat-Signature: onfyuxkunnkx86hirf3eibat7bpqgfah X-Rspamd-Queue-Id: DFA344000C X-Rspam-User: Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=dIpcC3du; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3Bvj_Yg0KCBUvIz6CvD7FDDz819916z.x97638FI-775Gvx5.9C1@flex--axelrasmussen.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3Bvj_Yg0KCBUvIz6CvD7FDDz819916z.x97638FI-775Gvx5.9C1@flex--axelrasmussen.bounces.google.com X-Rspamd-Server: rspam11 X-HE-Tag: 1660942342-496922 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. Reviewed-by: Shuah Khan Acked-by: Peter Xu Signed-off-by: Axel Rasmussen --- tools/testing/selftests/vm/run_vmtests.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/vm/run_vmtests.sh b/tools/testing/selftests/vm/run_vmtests.sh index b8e7f6f38d64..e780e76c26b8 100755 --- a/tools/testing/selftests/vm/run_vmtests.sh +++ b/tools/testing/selftests/vm/run_vmtests.sh @@ -120,13 +120,16 @@ run_test ./gup_test -a # Dump pages 0, 19, and 4096, using pin_user_pages: run_test ./gup_test -ct -F 0x1 0 19 0x1000 -run_test ./userfaultfd anon 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_shared "$half_ufd_size_MB" 32 "$mnt"/uffd-test -rm -f "$mnt"/uffd-test -run_test ./userfaultfd shmem 20 16 +uffd_mods=("" ":dev") +for mod in "${uffd_mods[@]}"; do + run_test ./userfaultfd anon${mod} 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${mod} "$half_ufd_size_MB" 32 + run_test ./userfaultfd hugetlb_shared${mod} "$half_ufd_size_MB" 32 "$mnt"/uffd-test + rm -f "$mnt"/uffd-test + run_test ./userfaultfd shmem${mod} 20 16 +done #cleanup umount "$mnt"