Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp695355pxb; Tue, 2 Feb 2021 15:52:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJw074WX/tLIerGAe1FXQKXTGTRb3lsJ7rHIFin/pCOB4mxaJjrTgPNyhADKDFcdsFOrKhG7 X-Received: by 2002:a17:906:4013:: with SMTP id v19mr445139ejj.5.1612309973648; Tue, 02 Feb 2021 15:52:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612309973; cv=none; d=google.com; s=arc-20160816; b=KqRjOL3Ob8oA5WZc/jG0Voppn6S6vRIN//UOTtKz38pn8VhGDIXYE8NBMKWOfqPiR7 n4hx7lVc5OfK42yeYxkcvhfEdo8xoBqPUFb8V+thnvHNP2+KGtpSBetOZKtjqbIRG5kG u9rMOYcsFOhq4sfBqS9NIXGPReirdopvyK92sK5TvRhbmKty1P4b9fjE0oot++sfKpIo vpGdClGTFmCrLxUPHMW6zofZ44UmVuZQKv53HmYYKYNNhLJvyJ7sq4NSQ9fougxxDvI7 FUnUf4sxcgOypcd7NFhlubY+eFAMZGGGyYRthBBAmKW+vL+YBmmGIgrrtKt2kNiJy95o RW4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=lEa8TASFowTmr9kKp6sPUUyPmHU/46QcAZCj48yANfk=; b=OzUM5gvuQm8SfBL9Uskwrb2/z2/FjKRdrh2ip7GJEqqHPabCfhEsA8X1aVkyurrUCp NpVfl8uEDwLAsKMA6Gmi2BCH9Pzjgb/H4+mmogjH27TtRHrFP+PWcBa/NDtk7a1cQeRp cQwaSKjiLaP92CUlFjjTFeCBIFpgU/u7e8FB89mDqKMNvExUhyWc35GLM1x7UyDqWYeW DbgJ0hrhWLCDNpK3uwdHk0LVFngq5/mkoARhH+4t114eq8mB8Tf9eBtvfZmxZOd+QG9G JOLzvG6dYjze5hpQuUA/3dL1YUnBIln7VsqNUj3Acpq9S1r6UVSDRQpTCzQpIu35Dpbe pBxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=egSHzr5D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id mm7si248735ejb.575.2021.02.02.15.52.28; Tue, 02 Feb 2021 15:52:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=egSHzr5D; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S237437AbhBBRTP (ORCPT + 99 others); Tue, 2 Feb 2021 12:19:15 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26844 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236860AbhBBRQs (ORCPT ); Tue, 2 Feb 2021 12:16:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612286120; 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: in-reply-to:in-reply-to:references:references; bh=lEa8TASFowTmr9kKp6sPUUyPmHU/46QcAZCj48yANfk=; b=egSHzr5DtruvlX8eepGt/qpkEQ1BCwwQBtSxmjPOSN4/bs5DaN6vWxviX2+CKAzH/a3VN/ EFHk0aDG72/XTmgOMBeyIXA/deaX5ep0JDkRs++02Ihgz2M2YoxLX10VpU3ixTdRfsnXSp DrRwcQHTw2gNKN6AHa0vjuN+/+ZgUAI= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-16-kMjwPd2jO4O7qWu7NqaETw-1; Tue, 02 Feb 2021 12:15:19 -0500 X-MC-Unique: kMjwPd2jO4O7qWu7NqaETw-1 Received: by mail-qk1-f197.google.com with SMTP id r190so17898777qkf.19 for ; Tue, 02 Feb 2021 09:15:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=lEa8TASFowTmr9kKp6sPUUyPmHU/46QcAZCj48yANfk=; b=YsUxp12HTNe83fDmCtVw+VNbN2GSiY+ty1pq5EeN3vKEdoYl99hvdOrfIweyvL8Fvs lEVtpJs9SasdEcGqaF5mtHKV8RK3XFmy2BSGnyPiDD9pUTm++z0K/oiZoXLbsgczab2k h9zQrtpD0TzMQ7zw1vpKfN4kpJbbHIT7s3H/gPIWN2vDoJpiQRzfsyEw7NSLA7xV8DYU bhhWMp5lEp1OU5i/J08uPCeBmGTrzPvbCJRyV2NgOCpbvD8FmzbpiD9Iu33xTJkejsXc RmMcnycCgKSIInQdxSsTKoQLEbRIo8c+GKc6VIK2YertlJve0Z0p4Hj6R9HFmNg1MTjx R+Hg== X-Gm-Message-State: AOAM531XMdD3UNt8mYdYJSYBaVwLJ50WfmU8EMSUX8SNwI2j45+KeUej hFff8T99mo6LHKOSwWRDM5neyeYmBNFHBrNi/EboYz0rmoI5lCFIjqhCemWAWwFGrC0Ur0JO/I9 FqwWFedyLBroXP05QxrCiOfnW X-Received: by 2002:a37:8b81:: with SMTP id n123mr22342728qkd.242.1612286118802; Tue, 02 Feb 2021 09:15:18 -0800 (PST) X-Received: by 2002:a37:8b81:: with SMTP id n123mr22342688qkd.242.1612286118538; Tue, 02 Feb 2021 09:15:18 -0800 (PST) Received: from xz-x1 (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id p11sm17044941qtb.62.2021.02.02.09.15.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Feb 2021 09:15:17 -0800 (PST) Date: Tue, 2 Feb 2021 12:15:15 -0500 From: Peter Xu To: Axel Rasmussen Cc: Alexander Viro , Alexey Dobriyan , Andrea Arcangeli , Andrew Morton , Anshuman Khandual , Catalin Marinas , Chinwen Chang , Huang Ying , Ingo Molnar , Jann Horn , Jerome Glisse , Lokesh Gidra , "Matthew Wilcox (Oracle)" , Michael Ellerman , Michal =?utf-8?Q?Koutn=C3=BD?= , Michel Lespinasse , Mike Kravetz , Mike Rapoport , Nicholas Piggin , Shaohua Li , Shawn Anastasio , Steven Rostedt , Steven Price , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Adam Ruprecht , Cannon Matthews , "Dr . David Alan Gilbert" , David Rientjes , Oliver Upton Subject: Re: [PATCH v3 5/9] userfaultfd: add minor fault registration mode Message-ID: <20210202171515.GF6468@xz-x1> References: <20210128224819.2651899-1-axelrasmussen@google.com> <20210128224819.2651899-6-axelrasmussen@google.com> <20210201183159.GF260413@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210201183159.GF260413@xz-x1> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Feb 01, 2021 at 01:31:59PM -0500, Peter Xu wrote: > On Thu, Jan 28, 2021 at 02:48:15PM -0800, Axel Rasmussen wrote: > > This feature allows userspace to intercept "minor" faults. By "minor" > > faults, I mean the following situation: > > > > Let there exist two mappings (i.e., VMAs) to the same page(s) (shared > > memory). One of the mappings is registered with userfaultfd (in minor > > mode), and the other is not. Via the non-UFFD mapping, the underlying > > pages have already been allocated & filled with some contents. The UFFD > > mapping has not yet been faulted in; when it is touched for the first > > time, this results in what I'm calling a "minor" fault. As a concrete > > example, when working with hugetlbfs, we have huge_pte_none(), but > > find_lock_page() finds an existing page. > > > > This commit adds the new registration mode, and sets the relevant flag > > on the VMAs being registered. In the hugetlb fault path, if we find > > that we have huge_pte_none(), but find_lock_page() does indeed find an > > existing page, then we have a "minor" fault, and if the VMA has the > > userfaultfd registration flag, we call into userfaultfd to handle it. > > When re-read, now I'm thinking whether we should restrict the minor fault > scenario with shared mappings always, assuming there's one mapping with uffd > and the other one without, while the non-uffd can modify the data before an > UFFDIO_CONTINUE kicking the uffd process. > > To me, it's really more about page cache and that's all.. > > So I'm wondering whether below would be simpler and actually clearer on > defining minor faults, comparing to the above whole two paragraphs. For > example, the scemantics do not actually need two mappings: > > For shared memory, userfaultfd missing fault used to only report the event > if the page cache does not exist for the current fault process. Here we > define userfaultfd minor fault as the case where the missing page fault > does have a backing page cache (so only the pgtable entry is missing). > > It should not affect most of your code, but only one below [1]. OK it could be slightly more than that... E.g. we'd need to make UFFDIO_COPY to not install the write bit if it's UFFDIO_CONTINUE and if it's private mappings. In hugetlb_mcopy_atomic_pte() now we apply the write bit unconditionally: _dst_pte = make_huge_pte(dst_vma, page, dst_vma->vm_flags & VM_WRITE); That'll need a touch-up otherwise. It's just the change seems still very small so I'd slightly prefer to support it all. However I don't want to make your series complicated and blocking it, so please feel free to still make it shared memory if that's your preference. The worst case is if someone would like to enable this (if with a valid user scenario) we'd export a new uffd feature flag. > > [...] > > > @@ -1302,9 +1301,26 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma, > > unsigned long vm_flags) > > { > > /* FIXME: add WP support to hugetlbfs and shmem */ > > - return vma_is_anonymous(vma) || > > - ((is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) && > > - !(vm_flags & VM_UFFD_WP)); > > + if (vm_flags & VM_UFFD_WP) { > > + if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma)) > > + return false; > > + } > > + > > + if (vm_flags & VM_UFFD_MINOR) { > > + /* > > + * The use case for minor registration (intercepting minor > > + * faults) is to handle the case where a page is present, but > > + * needs to be modified before it can be used. This requires > > + * two mappings: one with UFFD registration, and one without. > > + * So, it only makes sense to do this with shared memory. > > + */ > > + /* FIXME: Add minor fault interception for shmem. */ > > + if (!(is_vm_hugetlb_page(vma) && (vma->vm_flags & VM_SHARED))) > > + return false; > > [1] > > So here we also restrict the mapping be shared. My above comment on the commit > message is also another way to ask whether we could also allow it to happen > with non-shared mappings as long as there's a page cache. If so, we could drop > the VM_SHARED check here. It won't affect your existing use case for sure, it > just gives more possibility that maybe it could also be used on non-shared > mappings due to some reason in the future. > > What do you think? > > The rest looks good to me. > > Thanks, > > -- > Peter Xu -- Peter Xu