From patchwork Mon Feb 13 10:43:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Usama Anjum X-Patchwork-Id: 13138238 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 28BD7C636D4 for ; Mon, 13 Feb 2023 10:43:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CA176B0073; Mon, 13 Feb 2023 05:43:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 57B876B0074; Mon, 13 Feb 2023 05:43:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 442486B0075; Mon, 13 Feb 2023 05:43:45 -0500 (EST) 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 318966B0073 for ; Mon, 13 Feb 2023 05:43:45 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id F41DD160348 for ; Mon, 13 Feb 2023 10:43:44 +0000 (UTC) X-FDA: 80461932768.06.9587444 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 42B4F40004 for ; Mon, 13 Feb 2023 10:43:43 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=GdtkrSRB; dmarc=pass (policy=reject) header.from=collabora.com; spf=pass (imf07.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676285023; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=iwj1I4jVejjxZxGfashrJnMQT2Qz/9U16mBoavklPnc=; b=YDK4VX9bLOLYS/D6SjN34Ao+u1CXAYO+rO4UuGBwwA4AsFb+TogeBeLT7qBHdWkfovuI2U JfG8mjY/x2DCgR3P9rph+GsXoe+WLDd+1lS13eTShIP9czyZeIPb2JeEAxS2v/5ljQ/wFP v0QyZu/8GgxZksuagwklEyw1ipgLIrA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=GdtkrSRB; dmarc=pass (policy=reject) header.from=collabora.com; spf=pass (imf07.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676285023; a=rsa-sha256; cv=none; b=43lHv8ojJmJgYjcoe4oQD/FP3IJRCC8kshkvxfxZ7d7q/3TyP/4f3b/SlxllN1y/cOoXtG 28nbXBDCW5NUQGm3WcMd+tpgd+Jtr+oR2bDESIOGRP5xPiXqR+MLGCzRhE9v8a+m6eN3PD dJ1PNx/tw/aL/jWTfSwS1LeBqlYrsxY= Received: from localhost.localdomain (unknown [39.45.179.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id B98E56602136; Mon, 13 Feb 2023 10:43:39 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1676285021; bh=IaIVSmG6X9VC7e6/KLtDU/fP5htn4wfc9h00W5AtIpM=; h=From:To:Cc:Subject:Date:From; b=GdtkrSRBBs1/Emf4QAYjrg/iXvHP/AaGkG48ruTL9p/pcfpOBcoz+r93o8qPA495S S8CKQnEHlefPEo08PrcElHSpuXkjpDjiM0wNpWVZ/UCe6QHqFyNsRfjNaBo7+5w0io C4FfowgNK17yCVThqRFQy/IH3Ia82sKuWNkxTU0Sep9JvCWVFNfApRtF7RvlsK8jqY /y5TFv5bdK0Bxp4Gf70OX/zQL9yRmBIH53rAYJlAyDHTKk3W44hSeZsHMJqdSepIhU w85GAP/up/scB1axWSlxaxp06b1KTM3B7fbxesNopOSQvTXp6gFWbpx28Ao37eNhrc gQxJgNfNZD9WQ== From: Muhammad Usama Anjum To: peterx@redhat.com, david@redhat.com, Andrew Morton Cc: Muhammad Usama Anjum , kernel@collabora.com, Paul Gofman , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/userfaultfd: Support operation on multiple VMAs Date: Mon, 13 Feb 2023 15:43:23 +0500 Message-Id: <20230213104323.1792839-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 42B4F40004 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: ih981x6igjdknxkydas5d41i1hfd7mfr X-HE-Tag: 1676285023-719208 X-HE-Meta: U2FsdGVkX1+GYbZNHoXlNBelXR3nfxthyVuoIjjFk8OSt6uM2N+CQrGIcJohKM8YEVn7GAy/Z5uqMgVEi0ES6Wb9jGPWtpqksALZ4gd6yT6Sd7FCYjFd89MmvtksMZ3UT/wM9JLgQn9akJvNzg8e4fAgzErkxxMS5yPb/3mhVMwNTVmhP1cIpL8E3f51nkc6MdHP5/40HRplhbrnRllcGnCDQKW+14bb73VKFhZh0oJ9BBowA84jC2bBgEg0D7Iin2+vXHWn7W33Ae5EXDaZ6RwCg3dfhU+rTUHR+Ynxv5/i9vFmKivL4iBuUrR/MY5AmlUoMD1lr4lHpwuTfwOcOys3R4kHDDipoGOxTFUKOg1oJoTXeP0nP6x29uGS350dtEST9bTMliW7hZcJql2dac0PWRipM/p8oE16YK+V7VfIb+McCe5d67dp0D7efb5ow8hl3myvtHYdKO5BkM6SKlbRl1Tk2HE6ymU5e4uCdtM4TXAFsWEf+/jphjlke4cuioMgryssFglmDxhae9IhNMoGTQCfL94ZELHCoPfPUjk1K0EAGSa55TF8+2a6J4ULiRssnMpXW73EZNpSRGntM5VVno68F13AYKPtokd7ZKCXNVL1BnEl1TwwK+lYAuL142S+snAXt4+TphVkPUOUdsF8im+wiUuZuHQ/ree/T/nADfPnxFB7SVvLcEyFKvyq/zaie/wFrqhVK0EtM0ldUuNBZDgbw9gRsvgXYBFxxvhdcILDtBP8Y8yWqLRXiZVGC1So7jf3P6Tb88iK4dqZP7F3urWD4JZHFnFZpB5+7dBuGS4g3vHpPZn0oTqOutkSPCN9fTQ4hwNiKoJIgAB4o2WvtgE/wctbVP1zxfVQ74kIqTfwIMCUoF62588tHh3XndisX1vDuxqXqlhmmkURuOIBXE+FJ54/DJxdtRUyFBxv73iYZrj7UurJblB/+OEILbhEvkDEskkEbtaet3M gmXzNZot 22a/ufQIoyHTL5FGInywV+jKbUYinhZ7A5N6X92hhhPEM9pI0r1t+QCX/YKmW4RaQ8158025sKHKjQ9f+Rt8/YROqjbdRN8sUdg4FchifoifxFDQNFYUdoSItH3yadzLOCrovAP3dHZW1L45Mijy/x/vB0cMAxKp++5VdK2TDrVSs2fiz/QJZ0bSThgZ9WI9VLIABWa5gN8o6z7aOxTjDmqeRMUpDNx+njHpE+v7leZlI/a1LRqb35S1bTgDomljT7RDEbhjxz0ZHR3MCWETLdJ6nQWffWYNW1LJgWSJsjp4O+btFChAz03AnCFYnj1n37lQy 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: mwriteprotect_range() errors out if [start, end) doesn't fall in one VMA. We are facing a use case where multiple VMAs are present in one range of interest. For example, the following pseudocode reproduces the error which we are trying to fix: - Allocate memory of size 16 pages with PROT_NONE with mmap - Register userfaultfd - Change protection of the first half (1 to 8 pages) of memory to PROT_READ | PROT_WRITE. This breaks the memory area in two VMAs. - Now UFFDIO_WRITEPROTECT_MODE_WP on the whole memory of 16 pages errors out. This is a simple use case where user may or may not know if the memory area has been divided into multiple VMAs. Reported-by: Paul Gofman Signed-off-by: Muhammad Usama Anjum --- mm/userfaultfd.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 65ad172add27..46e0a014af68 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -738,9 +738,11 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, unsigned long len, bool enable_wp, atomic_t *mmap_changing) { + unsigned long end = start + len; struct vm_area_struct *dst_vma; unsigned long page_mask; int err; + VMA_ITERATOR(vmi, dst_mm, start); /* * Sanitize the command parameters: @@ -762,26 +764,26 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, if (mmap_changing && atomic_read(mmap_changing)) goto out_unlock; - err = -ENOENT; - dst_vma = find_dst_vma(dst_mm, start, len); - - if (!dst_vma) - goto out_unlock; - if (!userfaultfd_wp(dst_vma)) - goto out_unlock; - if (!vma_can_userfault(dst_vma, dst_vma->vm_flags)) - goto out_unlock; + for_each_vma_range(vmi, dst_vma, end) { + err = -ENOENT; - if (is_vm_hugetlb_page(dst_vma)) { - err = -EINVAL; - page_mask = vma_kernel_pagesize(dst_vma) - 1; - if ((start & page_mask) || (len & page_mask)) - goto out_unlock; - } + if (!dst_vma->vm_userfaultfd_ctx.ctx) + break; + if (!userfaultfd_wp(dst_vma)) + break; + if (!vma_can_userfault(dst_vma, dst_vma->vm_flags)) + break; - uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp); + if (is_vm_hugetlb_page(dst_vma)) { + err = -EINVAL; + page_mask = vma_kernel_pagesize(dst_vma) - 1; + if ((start & page_mask) || (len & page_mask)) + break; + } - err = 0; + uffd_wp_range(dst_mm, dst_vma, start, len, enable_wp); + err = 0; + } out_unlock: mmap_read_unlock(dst_mm); return err;