Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp4033655pxb; Mon, 1 Feb 2021 10:36:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJz1wBUACTgt2tJ7Zdd7Dl9ylf//VaVYYPg0TdKAh23eA6J6XzFxGvcgM7JQpcMMlkgxOfGE X-Received: by 2002:aa7:d649:: with SMTP id v9mr20130040edr.383.1612204608868; Mon, 01 Feb 2021 10:36:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612204608; cv=none; d=google.com; s=arc-20160816; b=YUkoco+sEewBHOgL3pYtqxA+b4ZciK1ntcbgSX/+Q2LdFhZxct1/yAo3eHhbyQiei8 Qdo4jyAmEtAt+dZvE4AZAgJ8atEoD4iMrgRZYLxbsMsxQ1Jj0s1mgwql0JWjm9mlfvzI iOlqzIeKQyTsVmP8AIZnjcv+nS4eDZp3ld9EPeMlqOmz7tcMlUy+8RKqhS0STIa7wNHc kPxAxyL4adqh6aHyRCQGyJ3Fp2xgOsZYVx9ZpAcegJ9Owwkr7MLjuxxFG1kA524Y9FaF I2yu6lA5kySBxsuzlaOuMgBAXOiDcdy66yJn7t6VqtIyxCWSNJhjj+8oar0IPjr5uOJr 4F7w== 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=eGnMVrjYoL74O1671GF2bpk9Mg2hYvXBiXlmuuE5Fo4=; b=r/xyWXNF+2qvyLIwa0KkUoKXyO+JZrKjlwO9wMlqTpsiB7GYnJXVCjdeli6xuYeFzy MeIDOom5QMx+dImPf6pSdvPRrDA8tsAA8X3rDagA09bdAtJrwyPj+2StT7bfdk3plj1X si4k9HUuAdOy68v66TZpVEGttB8TVrSUQ+Voe20IIGE/itNHEkl41KifKTvEWHqcjOOK z5BX7ONbB87/hJFoFMTz89XtK4NWz9VH0U+TpFGfWbzbCj8pqwrYznPhmAyEcpH9xuNT I/CwATnVG672gwMIRGATTHpR2mfnfixTD/9Nm4ZapS5Wuq835Qsp5eNuDBzU3QyEbd8h UXDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jIwvncTv; 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 p5si11833014edj.169.2021.02.01.10.36.24; Mon, 01 Feb 2021 10:36:48 -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=jIwvncTv; 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 S232749AbhBASfg (ORCPT + 99 others); Mon, 1 Feb 2021 13:35:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:49533 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232999AbhBASdb (ORCPT ); Mon, 1 Feb 2021 13:33:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612204324; 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=eGnMVrjYoL74O1671GF2bpk9Mg2hYvXBiXlmuuE5Fo4=; b=jIwvncTvqton9+fvjufPpBQgYUvljB8lTYpbZHoO/X2He0N7ZDz6YLuurKQ+QIeFaqMHE3 B6IvGKSbZPReN8YDLz6LlJwdmKNxipqUdzD/DWEsHsZuzFSvksrYwKFeWhCs6HGYZVk0MV CTk0Ex6VXwNSgkFX0OZ/OIdWNiXVTX0= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-217-q6uz6CwNOLq3R9dfUrfoag-1; Mon, 01 Feb 2021 13:32:03 -0500 X-MC-Unique: q6uz6CwNOLq3R9dfUrfoag-1 Received: by mail-qk1-f199.google.com with SMTP id e5so13966533qkn.2 for ; Mon, 01 Feb 2021 10:32:03 -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=eGnMVrjYoL74O1671GF2bpk9Mg2hYvXBiXlmuuE5Fo4=; b=qUTVKrcykhwf+B39l+bOFqBVPW5nCTFsofjBCiBwSoFMBPpC7LhtLEU1bNi6iymEoa ZjvgM2o5MYelV5GsKK7WQ8RuTGAEKWh//JeG3d/f3qQbvLDYzBNvc8VX7OQ/iw3Hv7N7 T80GpgY2+hIR9TGJo58xDoYVkgWAqWv0Zj40+2xhR0H5a4o8zWhtImzEb01+sE37HizZ pqzQl92Bprg3FjvVZD/fcRQmOpWbAhYabYYewyMbOddnrd5R//BhH8vJlqJ8IXhgmVWv LTyREQg6fhHAnB6rXF0qiGGYGtZThnaasO28EC5WH0E2NfqMdf3qd1p5b7THfH5++YQW 7hFg== X-Gm-Message-State: AOAM531ij8qJd8e+lLiPo4QAmRdv/KlJ96/aqYR4U5TppHo0VX3A2HdX vMW2g2M5OaKJ2oJhyv91xJ6bs7MuMR2IgO1/QyZ3C2rsPahEMuO9Sz6LRV6b3e7Lx4gIuf9/FUb m+8BI+DwP7P6GWnvaSnlSIK67 X-Received: by 2002:a05:622a:28d:: with SMTP id z13mr16655646qtw.87.1612204322668; Mon, 01 Feb 2021 10:32:02 -0800 (PST) X-Received: by 2002:a05:622a:28d:: with SMTP id z13mr16655618qtw.87.1612204322354; Mon, 01 Feb 2021 10:32:02 -0800 (PST) Received: from xz-x1 ([142.126.83.202]) by smtp.gmail.com with ESMTPSA id m21sm14447098qtq.52.2021.02.01.10.32.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 10:32:01 -0800 (PST) Date: Mon, 1 Feb 2021 13:31:59 -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: <20210201183159.GF260413@xz-x1> References: <20210128224819.2651899-1-axelrasmussen@google.com> <20210128224819.2651899-6-axelrasmussen@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20210128224819.2651899-6-axelrasmussen@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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]. [...] > @@ -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