From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E52DC77B7C for ; Tue, 2 May 2023 01:11:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230202AbjEBBLS (ORCPT ); Mon, 1 May 2023 21:11:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229863AbjEBBLS (ORCPT ); Mon, 1 May 2023 21:11:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A61C3584 for ; Mon, 1 May 2023 18:11:16 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 153F76112A for ; Tue, 2 May 2023 01:11:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52165C433EF; Tue, 2 May 2023 01:11:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1682989875; bh=HdNvNrrFLUJeMsafQPA1EnNDP1yh9FkSvgTaAoWwaFQ=; h=Date:To:From:Subject:From; b=qDNry1411ZmYlZu3oOzn+IjSHcpW4Lyw6+05drGPnRBcv4RF+XTJSONMXSaBESpBa i6pVZXOHDd25vE2NRv7uwOqFU2eEE7XMVlPo1LS2L1NChkcwU7iKrrjaMmhL0LJmWY cRcDx6bk+UyX8JhJfRIbAUOxO/Cq91tZiVyStx9c= Date: Mon, 01 May 2023 18:11:14 -0700 To: mm-commits@vger.kernel.org, willy@infradead.org, tytso@mit.edu, richardcochran@gmail.com, peterz@infradead.org, peterx@redhat.com, pabeni@redhat.com, oleg@redhat.com, neescoba@cisco.com, namhyung@kernel.org, mpenttil@redhat.com, mingo@redhat.com, mark.rutland@arm.com, magnus.karlsson@intel.com, maciej.fijalkowski@intel.com, leon@kernel.org, kuba@kernel.org, kirill@shutemov.name, jonathan.lemon@gmail.com, jolsa@kernel.org, john.fastabend@gmail.com, jhubbard@nvidia.com, jgg@nvidia.com, jack@suse.cz, irogers@google.com, hawk@kernel.org, edumazet@google.com, dennis.dalessandro@cornelisnetworks.com, david@redhat.com, david@fromorbit.com, davem@davemloft.net, daniel@iogearbox.net, brauner@kernel.org, bmt@zurich.ibm.com, bjorn@kernel.org, benve@cisco.com, axboe@kernel.dk, ast@kernel.org, asml.silence@gmail.com, alexander.shishkin@linux.intel.com, adrian.hunter@intel.com, acme@kernel.org, lstoakes@gmail.com, akpm@linux-foundation.org From: Andrew Morton Subject: + mm-mmap-separate-writenotify-and-dirty-tracking-logic.patch added to mm-unstable branch Message-Id: <20230502011115.52165C433EF@smtp.kernel.org> Precedence: bulk Reply-To: linux-kernel@vger.kernel.org List-ID: X-Mailing-List: mm-commits@vger.kernel.org The patch titled Subject: mm/mmap: separate writenotify and dirty tracking logic has been added to the -mm mm-unstable branch. Its filename is mm-mmap-separate-writenotify-and-dirty-tracking-logic.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-mmap-separate-writenotify-and-dirty-tracking-logic.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Lorenzo Stoakes Subject: mm/mmap: separate writenotify and dirty tracking logic Date: Tue, 2 May 2023 00:11:47 +0100 Patch series "mm/gup: disallow GUP writing to file-backed mappings by default", v6. Writing to file-backed mappings which require folio dirty tracking using GUP is a fundamentally broken operation, as kernel write access to GUP mappings do not adhere to the semantics expected by a file system. A GUP caller uses the direct mapping to access the folio, which does not cause write notify to trigger, nor does it enforce that the caller marks the folio dirty. The problem arises when, after an initial write to the folio, writeback results in the folio being cleaned and then the caller, via the GUP interface, writes to the folio again. As a result of the use of this secondary, direct, mapping to the folio no write notify will occur, and if the caller does mark the folio dirty, this will be done so unexpectedly. For example, consider the following scenario:- 1. A folio is written to via GUP which write-faults the memory, notifying the file system and dirtying the folio. 2. Later, writeback is triggered, resulting in the folio being cleaned and the PTE being marked read-only. 3. The GUP caller writes to the folio, as it is mapped read/write via the direct mapping. 4. The GUP caller, now done with the page, unpins it and sets it dirty (though it does not have to). This change updates both the PUP FOLL_LONGTERM slow and fast APIs. As pin_user_pages_fast_only() does not exist, we can rely on a slightly imperfect whitelisting in the PUP-fast case and fall back to the slow case should this fail. This patch (of 3): vma_wants_writenotify() is specifically intended for setting PTE page table flags, accounting for existing PTE flag state and whether that might already be read-only while mixing this check with a check whether the filesystem performs dirty tracking. Separate out the notions of dirty tracking and a PTE write notify checking in order that we can invoke the dirty tracking check from elsewhere. Note that this change introduces a very small duplicate check of the separated out vm_ops_needs_writenotify(). This is necessary to avoid making vma_needs_dirty_tracking() needlessly complicated (e.g. passing a check_writenotify flag or having it assume this check was already performed). This is such a small check that it doesn't seem too egregious to do this. Link: https://lkml.kernel.org/r/cover.1682981880.git.lstoakes@gmail.com Link: https://lkml.kernel.org/r/72a90af5a9e4445a33ae44efa710f112c2694cb1.1682981880.git.lstoakes@gmail.com Signed-off-by: Lorenzo Stoakes Reviewed-by: John Hubbard Reviewed-by: Mika Penttilä Reviewed-by: Jan Kara Reviewed-by: Jason Gunthorpe Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Alexei Starovoitov Cc: Arnaldo Carvalho de Melo Cc: Bernard Metzler Cc: Björn Töpel Cc: Christian Benvenuti Cc: Christian Brauner Cc: Daniel Borkmann Cc: Dave Chinner Cc: David Hildenbrand Cc: David S. Miller Cc: Dennis Dalessandro Cc: Eric Dumazet Cc: Ian Rogers Cc: Ingo Molnar Cc: Jakub Kicinski Cc: Jens Axboe Cc: Jesper Dangaard Brouer Cc: Jiri Olsa Cc: John Fastabend Cc: Jonathan Lemon Cc: "Kirill A. Shutemov" Cc: Leon Romanovsky Cc: Maciej Fijalkowski Cc: Magnus Karlsson Cc: Mark Rutland Cc: Matthew Wilcox (Oracle) Cc: Namhyung Kim Cc: Nelson Escobar Cc: Oleg Nesterov Cc: Paolo Abeni Cc: Pavel Begunkov Cc: Peter Xu Cc: Peter Zijlstra Cc: Richard Cochran Cc: Theodore Ts'o Signed-off-by: Andrew Morton --- include/linux/mm.h | 1 + mm/mmap.c | 36 +++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) --- a/include/linux/mm.h~mm-mmap-separate-writenotify-and-dirty-tracking-logic +++ a/include/linux/mm.h @@ -2422,6 +2422,7 @@ extern unsigned long move_page_tables(st #define MM_CP_UFFD_WP_ALL (MM_CP_UFFD_WP | \ MM_CP_UFFD_WP_RESOLVE) +bool vma_needs_dirty_tracking(struct vm_area_struct *vma); int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot); static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struct *vma) { --- a/mm/mmap.c~mm-mmap-separate-writenotify-and-dirty-tracking-logic +++ a/mm/mmap.c @@ -1475,6 +1475,31 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_ar } #endif /* __ARCH_WANT_SYS_OLD_MMAP */ +/* Do VMA operations imply write notify is required? */ +static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops) +{ + return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite); +} + +/* + * Does this VMA require the underlying folios to have their dirty state + * tracked? + */ +bool vma_needs_dirty_tracking(struct vm_area_struct *vma) +{ + /* Does the filesystem need to be notified? */ + if (vm_ops_needs_writenotify(vma->vm_ops)) + return true; + + /* Specialty mapping? */ + if (vma->vm_flags & VM_PFNMAP) + return false; + + /* Can the mapping track the dirty pages? */ + return vma->vm_file && vma->vm_file->f_mapping && + mapping_can_writeback(vma->vm_file->f_mapping); +} + /* * Some shared mappings will want the pages marked read-only * to track write events. If so, we'll downgrade vm_page_prot @@ -1484,14 +1509,13 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_ar int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) { vm_flags_t vm_flags = vma->vm_flags; - const struct vm_operations_struct *vm_ops = vma->vm_ops; /* If it was private or non-writable, the write bit is already clear */ if ((vm_flags & (VM_WRITE|VM_SHARED)) != ((VM_WRITE|VM_SHARED))) return 0; /* The backer wishes to know when pages are first written to? */ - if (vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite)) + if (vm_ops_needs_writenotify(vma->vm_ops)) return 1; /* The open routine did something to the protections that pgprot_modify @@ -1511,13 +1535,7 @@ int vma_wants_writenotify(struct vm_area if (userfaultfd_wp(vma)) return 1; - /* Specialty mapping? */ - if (vm_flags & VM_PFNMAP) - return 0; - - /* Can the mapping track the dirty pages? */ - return vma->vm_file && vma->vm_file->f_mapping && - mapping_can_writeback(vma->vm_file->f_mapping); + return vma_needs_dirty_tracking(vma); } /* _ Patches currently in -mm which might be from lstoakes@gmail.com are mm-mempolicy-correctly-update-prev-when-policy-is-equal-on-mbind.patch mm-mmap-separate-writenotify-and-dirty-tracking-logic.patch mm-gup-disallow-foll_longterm-gup-nonfast-writing-to-file-backed-mappings.patch mm-gup-disallow-foll_longterm-gup-fast-writing-to-file-backed-mappings.patch