From patchwork Tue Jan 24 08:43:21 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: 13113663 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 6593BC38142 for ; Tue, 24 Jan 2023 08:44:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9ECD6B0075; Tue, 24 Jan 2023 03:44:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D263A6B0078; Tue, 24 Jan 2023 03:44:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA03F6B007B; Tue, 24 Jan 2023 03:44:49 -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 A5D7F6B0075 for ; Tue, 24 Jan 2023 03:44:49 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7074114024F for ; Tue, 24 Jan 2023 08:44:49 +0000 (UTC) X-FDA: 80389057098.19.8C185C8 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by imf25.hostedemail.com (Postfix) with ESMTP id 9DF86A0004 for ; Tue, 24 Jan 2023 08:44:47 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=bUm+6PNe; spf=pass (imf25.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com; dmarc=pass (policy=none) header.from=collabora.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674549887; 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:in-reply-to:references:references:dkim-signature; bh=8XDZXniAcAgjhqKkiQQErqKZlBpPaa4nd4EHYjCKxLU=; b=ZDUjlpefMQNSH/kbnuKemPc187KWWQ96kGgdQshWvedBi2evBzHEyNfxVdwUyLvIH+XKRE CK4k3fFDO7+awxAsu9fH1/nIuw3GGyyk/3eBH87xIQXv+/SXhllXRZ3yJNKaGBGkkXhrr1 hxdH3fBkZA5AngjFyB5NySnOMo1eAwc= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=bUm+6PNe; spf=pass (imf25.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com; dmarc=pass (policy=none) header.from=collabora.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674549887; a=rsa-sha256; cv=none; b=wTQeSt/JXf+zKG/kJvopib3NxRSfwB+LP3aH96D3mZ3F3IDqZOalKN7iq0i/YCxuYP6LE+ iZw0n8iuGmatq32ec1QFGDDE8tRiF1L7IYjnXvrwDf1AMU8eBhEhJh4APm9dvI1ZPZT/6z OVfDj4EyaBpDrLoSfGR4c7s/ENTol7E= Received: from localhost.localdomain (unknown [39.45.186.163]) (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 7B4876602E2C; Tue, 24 Jan 2023 08:44:39 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1674549886; bh=RhznnELLur/OFCYFz9nqeAiw2g/ULL6wGU1+TD5rlvc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bUm+6PNenlG1HTsei/j6rwysWnI8TegYAR6iAvi9zzJwK6VPMMr+FplOGi/L1HfUa r3Ll8oFgrop7lH0ZC3hz82Yt7dgmbpgU3SHB6LbQ6Nn13bC5vOs/tYeQ3eJjSxy/UL 2Lk/jmcy1sxNNMrKJbiUjDSGXaLeIQHCZuieQZwZpXivOBPp4FL/YB69rofnMAB0F8 opwMmSz2c6XWhw9SkuJD+GJoN9zUdGWBBlbDpz2dxAExQrB1XT16iZudardQesIqQV ltsLSpi+vDXhWc+g7NihNtxOqQn7gp6UoaljbWkIzyLMuotx5aXbAnXXBQNyxQOGaY SaDJV/V9WQkHw== From: Muhammad Usama Anjum To: Peter Xu , David Hildenbrand , Andrew Morton , =?utf-8?b?TWljaGHFgiBNaXJvc8WC?= =?utf-8?b?YXc=?= , Andrei Vagin , Danylo Mocherniuk , Paul Gofman , Cyrill Gorcunov Cc: Alexander Viro , Shuah Khan , Christian Brauner , Yang Shi , Vlastimil Babka , "Liam R . Howlett" , Yun Zhou , Suren Baghdasaryan , Alex Sierra , Muhammad Usama Anjum , Matthew Wilcox , Pasha Tatashin , Mike Rapoport , Nadav Amit , Axel Rasmussen , "Gustavo A . R . Silva" , Dan Williams , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Greg KH , kernel@collabora.com Subject: [PATCH v8 2/4] userfaultfd: split mwriteprotect_range() Date: Tue, 24 Jan 2023 13:43:21 +0500 Message-Id: <20230124084323.1363825-3-usama.anjum@collabora.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230124084323.1363825-1-usama.anjum@collabora.com> References: <20230124084323.1363825-1-usama.anjum@collabora.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9DF86A0004 X-Rspam-User: X-Stat-Signature: ntco8887bywx1mq9ad7yxrbmafdqmdfe X-HE-Tag: 1674549887-413712 X-HE-Meta: U2FsdGVk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VhQYZ+OezBOOPovgFQ1Sru9pdT5eUPbeLS7xfHV1xnO9mQnClkOQLk1UpUC1Ime5L/zOb+o4WTqN6POKzAnRchHaSPw9Xu/XBJYzIcrziK/rrWnWsXXruxV1hOAb0CGCjefI+WyO/vnfU4Yai1qe7MuZR6tcBvBhRwbUlPjPYAHwcEASSjlIHktPor97DVFT/R15SioEvv5o4ZbQ== 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: Split mwriteprotect_range() to create a unlocked version. This will be used in the next patch to write protect a memory area. Add a helper function, wp_range_async() as well. Signed-off-by: Muhammad Usama Anjum --- Changes in v7: - Remove async being set in the PAGEMAP_IOCTL --- fs/userfaultfd.c | 10 +++++++++ include/linux/userfaultfd_k.h | 10 +++++++++ mm/userfaultfd.c | 40 ++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index b82af02092ce..cd52c9ee8db9 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1961,6 +1961,16 @@ int userfaultfd_wp_async(struct vm_area_struct *vma) return (ctx && (ctx->features & UFFD_FEATURE_WP_ASYNC)); } +int wp_range_async(struct vm_area_struct *vma, unsigned long start, unsigned long len) +{ + struct userfaultfd_ctx *ctx = vma->vm_userfaultfd_ctx.ctx; + + if (!ctx) + return -1; + + return __mwriteprotect_range(ctx->mm, start, len, true, &ctx->mmap_changing); +} + static inline unsigned int uffd_ctx_features(__u64 user_features) { /* diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 5db51fccae1d..40d1d9e8f34d 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -73,6 +73,9 @@ extern ssize_t mcopy_continue(struct mm_struct *dst_mm, unsigned long dst_start, extern int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, unsigned long len, bool enable_wp, atomic_t *mmap_changing); +extern int __mwriteprotect_range(struct mm_struct *dst_mm, + unsigned long start, unsigned long len, + bool enable_wp, atomic_t *mmap_changing); extern void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *vma, unsigned long start, unsigned long len, bool enable_wp); @@ -180,6 +183,8 @@ extern int userfaultfd_unmap_prep(struct mm_struct *mm, unsigned long start, extern void userfaultfd_unmap_complete(struct mm_struct *mm, struct list_head *uf); extern int userfaultfd_wp_async(struct vm_area_struct *vma); +extern int wp_range_async(struct vm_area_struct *vma, unsigned long start, + unsigned long len); #else /* CONFIG_USERFAULTFD */ @@ -280,6 +285,11 @@ int userfaultfd_wp_async(struct vm_area_struct *vma) return false; } +int wp_range_async(struct vm_area_struct *vma, unsigned long start, unsigned long len) +{ + return -1; +} + #endif /* CONFIG_USERFAULTFD */ static inline bool pte_marker_entry_uffd_wp(swp_entry_t entry) diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 65ad172add27..9d8a43faf764 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -734,25 +734,13 @@ void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma, tlb_finish_mmu(&tlb); } -int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, - unsigned long len, bool enable_wp, - atomic_t *mmap_changing) +int __mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, + unsigned long len, bool enable_wp, + atomic_t *mmap_changing) { struct vm_area_struct *dst_vma; unsigned long page_mask; int err; - - /* - * Sanitize the command parameters: - */ - BUG_ON(start & ~PAGE_MASK); - BUG_ON(len & ~PAGE_MASK); - - /* Does the address range wrap, or is the span zero-sized? */ - BUG_ON(start + len <= start); - - mmap_read_lock(dst_mm); - /* * If memory mappings are changing because of non-cooperative * operation (e.g. mremap) running in parallel, bail out and @@ -783,6 +771,28 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, err = 0; out_unlock: + return err; +} + +int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start, + unsigned long len, bool enable_wp, + atomic_t *mmap_changing) +{ + int err; + + /* + * Sanitize the command parameters: + */ + BUG_ON(start & ~PAGE_MASK); + BUG_ON(len & ~PAGE_MASK); + + /* Does the address range wrap, or is the span zero-sized? */ + BUG_ON(start + len <= start); + + mmap_read_lock(dst_mm); + + err = __mwriteprotect_range(dst_mm, start, len, enable_wp, mmap_changing); + mmap_read_unlock(dst_mm); return err; }