Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp898131rwr; Wed, 3 May 2023 07:37:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ72T2bV91h5cDp8oQ/JcExiSMtB2rC3sq7KjmBGl0WAtaxcOgaz2Ha2L8BMCXssREmaINtg X-Received: by 2002:a17:903:32c6:b0:1a9:71d3:2b60 with SMTP id i6-20020a17090332c600b001a971d32b60mr22276801plr.0.1683124675780; Wed, 03 May 2023 07:37:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683124675; cv=none; d=google.com; s=arc-20160816; b=QiDSa1bA9zCZ97+MABcB2y3ow9kwH7Y0Fr0NUZpveSv7SVYTkRQT0LzNZLGPgZvETI f7kIep3P9P1T/IbGKmeI1299mq1vN2JpSmA7/cSpytgzOZuO09LwG7d96wcPmh0kjmlA Whv8oMNAxpDrxNg08uv6q1RS6o0v3iDt5H//TU3H6sC6Z5m5KDVxTtL5e5rjRPEc2GkA SLaN+TrZjoHkk6bzc6fMWacfm8l3InYzZWXB3UTr5l69lXskljbQw4Jb05V5oZ8rXOCi /zeCtNAzN1XX3wq/3BcaZazWHMbTVFqVXoOwKRX+dHLvrH3nbN/5MEnfr9xTTTdjRzx0 vCwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=jwgVjcmprHBa8NpoaXgJYiwFaH2ANM6d4RKcdmTha7o=; b=Nm52OylRP9g+qiAB8Ax7BH+fkFritq1ExgM7CKS9sx/c5CFPEah2eCwQVFX/Q+xLy9 v2tMVSuuRzzUFwT1SNj5mT1sYcBSTdqcBIfFKostQOCKEnGrEItSpaREmMFeHNzuAf2Z VIsJ679GLtL/upVtw5dbDYXIXI1L53xxEGCuzNvX3AX/QgBiO/Wa0uLkUAd2sDCZLb1p 62VC+2upY72vVXfkD6xLM3XjkSVeB4OlU6/ErNzZ5Gju2c86L9H2tNGQ51K5UG5Hylg9 /5n3WdWIkrnBPfwXsabUPFg7Aj2h5MIL5Do6aUfKNoBYkxNXxcCxBz26Ek2ltIYLgGPz iXAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q6GVcGKP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p1-20020a1709026b8100b001a2a4eb10d6si32409784plk.58.2023.05.03.07.37.43; Wed, 03 May 2023 07:37:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q6GVcGKP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230334AbjECOc2 (ORCPT + 99 others); Wed, 3 May 2023 10:32:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230254AbjECOc0 (ORCPT ); Wed, 3 May 2023 10:32:26 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BA6E30F7 for ; Wed, 3 May 2023 07:31:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683124302; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jwgVjcmprHBa8NpoaXgJYiwFaH2ANM6d4RKcdmTha7o=; b=Q6GVcGKPxt+rIoCcPh5uo49BjKX5nle7EeNLjtrzy0yxPZ3f5jPupRmoQ4sjnEjtaZtXk6 mzvojxlSlCryN10ACBb1NlaA6Et8TmHEWYQLbQVHXfyhh6E32tBTw3j9281u/PwsVJyu4u PDKMB8NxxzhVQJCVQRJ8obrTKTH8jAo= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-627--4Nvg5NhNXmEdAAQu3WfRw-1; Wed, 03 May 2023 10:31:41 -0400 X-MC-Unique: -4Nvg5NhNXmEdAAQu3WfRw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3f1754de18cso32272375e9.1 for ; Wed, 03 May 2023 07:31:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683124299; x=1685716299; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jwgVjcmprHBa8NpoaXgJYiwFaH2ANM6d4RKcdmTha7o=; b=VaOpqvYx8A5MWIFowDATYIyNXT12twFesV95R8NzlMduSLjTpd8flJHLuQ0bz22U7A 9ieos41RYn2ZUxAW5qvNPijuajgeJN6t/JRLPlr7UHyV90NpHJZ5Un7hRtqoosfSAObh BASilFK1Bt/fAOB7SDqlXFgJJhW+xwvruRP9+b0i+VHk2rfEqBq5d9aXdCFB+FSoo0Fb Mx21rrKhl2jEljKUqi+ssXBF1SlrXAeG+6Bvhg0NGSInK2kDLgo/HqE25yF+G/7pi1ks M/0MxfjsYRZ+o6t+e8GGJ3Hgi0Az01QBd4O4VdmSH9mEU7uvnyld3lsTmQhPC5UNplh4 vQbw== X-Gm-Message-State: AC+VfDxkGpl8eZ7mGCueqv0P/fOgU/kDacxCm7JchbemGF/z6ihZsnNo YSOkiRGMh/CLdi907rRX0ZJQfJr5lYrYuwnP/Ufr1W1cVyifSSfpWkwoIgDjAhprtwA3BvfkUmI 4TFcxmMIRtDyObTILbGzLkqqI X-Received: by 2002:a05:600c:2212:b0:3f1:6ebe:d598 with SMTP id z18-20020a05600c221200b003f16ebed598mr14513963wml.7.1683124299692; Wed, 03 May 2023 07:31:39 -0700 (PDT) X-Received: by 2002:a05:600c:2212:b0:3f1:6ebe:d598 with SMTP id z18-20020a05600c221200b003f16ebed598mr14513907wml.7.1683124299231; Wed, 03 May 2023 07:31:39 -0700 (PDT) Received: from ?IPV6:2003:cb:c711:6a00:9109:6424:1804:a441? (p200300cbc7116a00910964241804a441.dip0.t-ipconnect.de. [2003:cb:c711:6a00:9109:6424:1804:a441]) by smtp.gmail.com with ESMTPSA id n3-20020a7bc5c3000000b003f0b1b8cd9bsm2116716wmk.4.2023.05.03.07.31.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 May 2023 07:31:38 -0700 (PDT) Message-ID: Date: Wed, 3 May 2023 16:31:36 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Content-Language: en-US To: Lorenzo Stoakes , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Jason Gunthorpe , Jens Axboe , Matthew Wilcox , Dennis Dalessandro , Leon Romanovsky , Christian Benvenuti , Nelson Escobar , Bernard Metzler , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Bjorn Topel , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Christian Brauner , Richard Cochran , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , linux-fsdevel@vger.kernel.org, linux-perf-users@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, Oleg Nesterov , Jason Gunthorpe , John Hubbard , Jan Kara , "Kirill A . Shutemov" , Pavel Begunkov , Mika Penttila , Dave Chinner , Theodore Ts'o , Peter Xu , Matthew Rosato , "Paul E . McKenney" , Christian Borntraeger References: <7ac8bb557517bcdc9225b4e4893a2ca7f603fcc4.1683067198.git.lstoakes@gmail.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v8 1/3] mm/mmap: separate writenotify and dirty tracking logic In-Reply-To: <7ac8bb557517bcdc9225b4e4893a2ca7f603fcc4.1683067198.git.lstoakes@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03.05.23 00:51, Lorenzo Stoakes wrote: > vma_wants_writenotify() is specifically intended for setting PTE page table > flags, accounting for existing page table flag state and whether the > filesystem performs dirty tracking. > > Separate out the notions of dirty tracking and 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() and vma_is_shared_writable() > functions. This is necessary to avoid making vma_needs_dirty_tracking() > needlessly complicated (e.g. passing flags or having it assume checks were > already performed). This is small enough that it doesn't seem too > egregious. > > We check to ensure the mapping is shared writable, as any GUP caller will > be safe - MAP_PRIVATE mappings will be CoW'd and read-only file-backed > shared mappings are not permitted access, even with FOLL_FORCE. > > Signed-off-by: Lorenzo Stoakes > Reviewed-by: John Hubbard > Reviewed-by: Mika Penttilä > Reviewed-by: Jan Kara > Reviewed-by: Jason Gunthorpe > --- > include/linux/mm.h | 1 + > mm/mmap.c | 53 ++++++++++++++++++++++++++++++++++------------ > 2 files changed, 41 insertions(+), 13 deletions(-) > > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 27ce77080c79..7b1d4e7393ef 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -2422,6 +2422,7 @@ extern unsigned long move_page_tables(struct vm_area_struct *vma, > #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) > { > diff --git a/mm/mmap.c b/mm/mmap.c > index 5522130ae606..fa7442e44cc2 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -1475,6 +1475,42 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) > } > #endif /* __ARCH_WANT_SYS_OLD_MMAP */ > > +/* Do VMA operations imply write notify is required? */ Nit: comment is superfluous, this is already self-documenting code. > +static bool vm_ops_needs_writenotify(const struct vm_operations_struct *vm_ops) > +{ > + return vm_ops && (vm_ops->page_mkwrite || vm_ops->pfn_mkwrite); > +} > + > +/* Is this VMA shared and writable? */ Nit: dito > +static bool vma_is_shared_writable(struct vm_area_struct *vma) > +{ > + return (vma->vm_flags & (VM_WRITE | VM_SHARED)) == > + (VM_WRITE | VM_SHARED); > +} > + > +/* > + * Does this VMA require the underlying folios to have their dirty state > + * tracked? > + */ Nit: dito > +bool vma_needs_dirty_tracking(struct vm_area_struct *vma) > +{ > + /* Only shared, writable VMAs require dirty tracking. */ > + if (!vma_is_shared_writable(vma)) > + return false; > + > + /* 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 > @@ -1483,21 +1519,18 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg) > */ > 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))) > + if (!vma_is_shared_writable(vma)) > 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 > * won't preserve? */ > if (pgprot_val(vm_page_prot) != > - pgprot_val(vm_pgprot_modify(vm_page_prot, vm_flags))) > + pgprot_val(vm_pgprot_modify(vm_page_prot, vma->vm_flags))) > return 0; > > /* > @@ -1511,13 +1544,7 @@ int vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot) > 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); > } > > /* We now have duplicate vma_is_shared_writable() and vm_ops_needs_writenotify() checks ... Maybe move the VM_PFNMAP and "/* Can the mapping track the dirty pages? */" checks into a separate helper and call that from both, vma_wants_writenotify() and vma_needs_dirty_tracking() ? In any case Acked-by: David Hildenbrand -- Thanks, David / dhildenb