From patchwork Mon Mar 6 22:50:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Axel Rasmussen X-Patchwork-Id: 13162505 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 2463FC6FD1B for ; Mon, 6 Mar 2023 22:50:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B850D6B007D; Mon, 6 Mar 2023 17:50:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ABF056B007E; Mon, 6 Mar 2023 17:50:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C45B6B0080; Mon, 6 Mar 2023 17:50:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 776286B007D for ; Mon, 6 Mar 2023 17:50:46 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 47F44140570 for ; Mon, 6 Mar 2023 22:50:46 +0000 (UTC) X-FDA: 80539969692.10.4C1B3E1 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf30.hostedemail.com (Postfix) with ESMTP id 8F6758000E for ; Mon, 6 Mar 2023 22:50:44 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Pi5j39Dn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3Q24GZA0KCGA8VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3Q24GZA0KCGA8VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678143044; 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=fCs2kW6NCF2FQxv/16BmAeybjQmvN98hsAEGZhfLc7o=; b=sTTWnTbLSohxoIWNtNqp29Ih+41sryQkSF9oVfcaVjICrdN125jn5v8tWrwk7NDOONQAKR u1wI4coJ9vTd6LLqtFh7K0QgseUpyNlVDwk3CyCgPRqQtessFlROrfIUuHz8kXV6cv2Mpb acG/tIelpWkDEdUwE5WNqC2IX4s4N1s= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Pi5j39Dn; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf30.hostedemail.com: domain of 3Q24GZA0KCGA8VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3Q24GZA0KCGA8VCJP8QKSQQCLEMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--axelrasmussen.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678143044; a=rsa-sha256; cv=none; b=ws/9XCYYF4RzKMJnvuP8rk4TeJp0JOi9MwOmoD5NOHYv3Ld9URon0wdxR0c4ltSUHAtuaW sj7rJyDKky4BVPDTgNuWkc2seBcLQPW9EU0VrpvomA+oNx0Gi4xpr89QiAXUgOZQbzv3+j GguXu+lJDV1EmHVMIiqVtgQormozw0k= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-536c8bcae3bso117098737b3.2 for ; Mon, 06 Mar 2023 14:50:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678143043; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fCs2kW6NCF2FQxv/16BmAeybjQmvN98hsAEGZhfLc7o=; b=Pi5j39DnN746rr0nyyIrU+OZA7nt9gA5kRLY8WZEitzZEw84ouQnj7Kd1iOl0qWchx Fsfg00plnVHHS60F/Wc6+ccaXAKfjxGnfVpaBAT4bywVH+N2CtSx0nGbHxMpiXZcYn5c 7dAjjL04g0p4w20e8rC2VpO0wlJPz5IUuzfsclgAC8hVwzJ5khrb/9zdoYrT6XFU5d8P Xg98zuA4dN5mdtwBlCeLZLx6kuC0ltkIK4z5Tpescuyukxc37xVqtTe5BPuMka6BKArx gCevuBjXleSJjazrchDJ2vVWI3749RNQVNQa4QX8dpKCYRGDb+nHDma40VUPhUcgCdpU Yi8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678143043; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=fCs2kW6NCF2FQxv/16BmAeybjQmvN98hsAEGZhfLc7o=; b=qhPgqKQ/pCcc4Ld5bEHl2iGWDAfNuxVkrc3JNTXEcq8UdTqXBXGIiah4PgnzIX8+3c /zeS/4u4ulNebw7eKbZmY93CeyuIINjilMAKhSI62Q86X5SCGW9DGX04Hd07LPK2HGP0 9mJs4I520J1wAiVMvQ8PRAL/mj82THqFAn4afCisrp4xfUsI9dRdhJtyQlzppqKaae3c 4T60SCyme3em/fyS/4Q9Xs1yJ3TNp1HXREsHkQ9QTVKPGVm73PxxVbjDVHIS+zXyIJkW v5DWAKMH4YevmGAE1UfMEhjSCTHWUNQDktAxNwvq4cbZmOvs4k7bo+bb0GtfeJOU1n4t rdYw== X-Gm-Message-State: AO0yUKUjomQ67xaH5MxoTFRfGluSQR/tVFRZa9h7GIPI+yzEReAGMe9B jQ6n11/hN4KFt612WE+eZFWFMgKaBaeKuwkrxsms X-Google-Smtp-Source: AK7set+7tWiFs0DhgMxSdJYHm9bwvHmkVPC2lzv1VN4ZzMhIYlRNSGRdQY5JfD8Ssp705gy5eOt8xeE+y9dIHikuTzWR X-Received: from axel.svl.corp.google.com ([2620:15c:2d4:203:17e9:c330:41ce:6b08]) (user=axelrasmussen job=sendgmr) by 2002:a05:6902:208:b0:a98:bd27:91de with SMTP id j8-20020a056902020800b00a98bd2791demr7381210ybs.7.1678143043755; Mon, 06 Mar 2023 14:50:43 -0800 (PST) Date: Mon, 6 Mar 2023 14:50:24 -0800 In-Reply-To: <20230306225024.264858-1-axelrasmussen@google.com> Mime-Version: 1.0 References: <20230306225024.264858-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230306225024.264858-6-axelrasmussen@google.com> Subject: [PATCH v3 5/5] mm: userfaultfd: add UFFDIO_CONTINUE_MODE_WP to install WP PTEs From: Axel Rasmussen To: Alexander Viro , Andrew Morton , Hugh Dickins , Jan Kara , "Liam R. Howlett" , Matthew Wilcox , Mike Kravetz , Mike Rapoport , Muchun Song , Nadav Amit , Peter Xu , Shuah Khan Cc: James Houghton , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Axel Rasmussen X-Rspamd-Queue-Id: 8F6758000E X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: e79nozqowjjm48odxjacp3yuy7pzee6s X-HE-Tag: 1678143044-415632 X-HE-Meta: U2FsdGVkX18Ddd+gpcTD15sEV4U+9jGR6Po18FI+G7AK3bormmTbB6u+G7/vUxPcKua1aLrK0518awNryWgEgtwxPsCfxV0Z7bMjuK8nvCV//WxIB9La67O7EwUnKOblwDaqtkLZ8MaF+P3Ysa7g5bXlfpAMYCYDnRRDURG4qIo6LWE2CNLDMVaCRdzseKxF2cLkgF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YdKl8dRO7Hu3ExycTmOqKcjZnZulvb/IX1xjCWdoMcH3GVULGisaoAsv6S68xI9ziQfxBjEyLVFBgKm9QJyNTNyJU+RQlb8Nj2+UiY6U8jUBQiIPSCk8mQM7+5ygriKWij/cR6Fa0UVy54gtJ+WSmcYLBZV5oIn/jZtCWhYTRdDF504if2z0Ur+3xo2N+HcTa8L0PU/+uUs9gH8agbzKFC6ujkACm6O9QxJLJDpd21x1Fh1K0cqFbMXGWBxuLp953rVM9usi1a7hALtnxi/bqoIIw1FHTeiox1uolB5ffP1qP0IjGWtYyIhisOjzIGG9tOy+lwwVh8hUCTO5Fw5sP20DHIgqhA71tUA73C84LqW4yiXR5IkpudGuCdRmjDdzR6vmOfEke54xYAo0M5LAQBmMGAkxsSTozVAhKv/eBg7+k3EasvF2v+W17YksUYDpMgauWOfnmpM9/oiCwV3nap4QDL9x/4ib 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: UFFDIO_COPY already has UFFDIO_COPY_MODE_WP, so when installing a new PTE to resolve a missing fault, one can install a write-protected one. This is useful when using UFFDIO_REGISTER_MODE_{MISSING,WP} in combination. So, add an analogous UFFDIO_CONTINUE_MODE_WP, which does the same thing but for *minor* faults. Update the selftest to do some very basic exercising of the new flag. Signed-off-by: Axel Rasmussen Acked-by: Peter Xu --- fs/userfaultfd.c | 8 ++++++-- include/linux/userfaultfd_k.h | 2 +- include/uapi/linux/userfaultfd.h | 7 +++++++ mm/userfaultfd.c | 5 +++-- tools/testing/selftests/mm/userfaultfd.c | 4 ++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 984b63b0fc75..b5750e20ae00 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1859,6 +1859,7 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) struct uffdio_continue uffdio_continue; struct uffdio_continue __user *user_uffdio_continue; struct uffdio_range range; + int flags = 0; user_uffdio_continue = (struct uffdio_continue __user *)arg; @@ -1881,12 +1882,15 @@ static int userfaultfd_continue(struct userfaultfd_ctx *ctx, unsigned long arg) /* double check for wraparound just in case. */ if (range.start + range.len <= range.start) goto out; - if (uffdio_continue.mode & ~UFFDIO_CONTINUE_MODE_DONTWAKE) + if (uffdio_continue.mode & ~(UFFDIO_CONTINUE_MODE_DONTWAKE | + UFFDIO_CONTINUE_MODE_WP)) goto out; + if (uffdio_continue.mode & UFFDIO_CONTINUE_MODE_WP) + flags |= MFILL_ATOMIC_WP; if (mmget_not_zero(ctx->mm)) { ret = mfill_atomic_continue(ctx->mm, &range, - &ctx->mmap_changing); + &ctx->mmap_changing, flags); mmput(ctx->mm); } else { return -ESRCH; diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index fcd95e3d3dcd..d691f898bae2 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -71,7 +71,7 @@ extern ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, atomic_t *mmap_changing); extern ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, const struct uffdio_range *dst, - atomic_t *mmap_changing); + atomic_t *mmap_changing, int flags); extern int mwriteprotect_range(struct mm_struct *dst_mm, const struct uffdio_range *range, bool enable_wp, atomic_t *mmap_changing); diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index 005e5e306266..14059a0861bf 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -297,6 +297,13 @@ struct uffdio_writeprotect { struct uffdio_continue { struct uffdio_range range; #define UFFDIO_CONTINUE_MODE_DONTWAKE ((__u64)1<<0) + /* + * UFFDIO_CONTINUE_MODE_WP will map the page write protected on + * the fly. UFFDIO_CONTINUE_MODE_WP is available only if the + * write protected ioctl is implemented for the range + * according to the uffdio_register.ioctls. + */ +#define UFFDIO_CONTINUE_MODE_WP ((__u64)1<<1) __u64 mode; /* diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 870e7489e8d1..6adbfc8dc277 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -691,10 +691,11 @@ ssize_t mfill_atomic_zeropage(struct mm_struct *dst_mm, ssize_t mfill_atomic_continue(struct mm_struct *dst_mm, const struct uffdio_range *dst, - atomic_t *mmap_changing) + atomic_t *mmap_changing, + int flags) { return mfill_atomic(dst_mm, 0, dst, - mmap_changing, MFILL_ATOMIC_CONTINUE); + mmap_changing, flags | MFILL_ATOMIC_CONTINUE); } long uffd_wp_range(struct vm_area_struct *dst_vma, diff --git a/tools/testing/selftests/mm/userfaultfd.c b/tools/testing/selftests/mm/userfaultfd.c index 7f22844ed704..41c1f9abc481 100644 --- a/tools/testing/selftests/mm/userfaultfd.c +++ b/tools/testing/selftests/mm/userfaultfd.c @@ -585,6 +585,8 @@ static void continue_range(int ufd, __u64 start, __u64 len) req.range.start = start; req.range.len = len; req.mode = 0; + if (test_uffdio_wp) + req.mode |= UFFDIO_CONTINUE_MODE_WP; if (ioctl(ufd, UFFDIO_CONTINUE, &req)) err("UFFDIO_CONTINUE failed for address 0x%" PRIx64, @@ -1332,6 +1334,8 @@ static int userfaultfd_minor_test(void) uffdio_register.range.start = (unsigned long)area_dst_alias; uffdio_register.range.len = nr_pages * page_size; uffdio_register.mode = UFFDIO_REGISTER_MODE_MINOR; + if (test_uffdio_wp) + uffdio_register.mode |= UFFDIO_REGISTER_MODE_WP; if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register)) err("register failure");