Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp133018ybj; Fri, 8 May 2020 08:08:03 -0700 (PDT) X-Google-Smtp-Source: APiQypKEVtKfyeGW7RYcQN4bA1wh/WkgX/vYL1U3p+lg7HxriNtZl0RaNathQApZApXoi2C/bw+W X-Received: by 2002:a05:6402:1841:: with SMTP id v1mr2412229edy.182.1588950483677; Fri, 08 May 2020 08:08:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588950483; cv=none; d=google.com; s=arc-20160816; b=uwFWSnufcZ7VqhpjPKsQArlUzN+/bkoDUIn9e/FO4PsJyZ9+l7PZQgwnk0c0jlGPBy CVvmHIslNt0NW44pvMXDI5ZDZsXEW8IDLK/AYkPDKdBGWAvibQ08dwsb2bHflsPVQK0r OK3glQ/RNggHiPNUKzdTKwKxJTY6ucNpPSutNgjT96tpageJcg+iTTLEmcOttJA60qNo T9gM1+52XGIYPot+jZ4BzzxH/EArfsA1anCs3eU9g81kLm8BEdfk2rrrgcUMjnvsmwgM NkghqjnFhywciO5EpmGhRA94IeBUYXLPXS1wGvCViociLly9WYB8UFPE3hdEyD07FpRS pfkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=IhwHM3vWsZyNiLOz6qrYkX84URnnDzt9jte52kOygqQ=; b=wW2qm6dFjvecjhDtrl7lla07sXJH5eqsmJ9TRirY/zJfk5d/n9HxoPM4o51a38ryVI uhEt98SnFVjbj8FpWZW5cnpxrQZ+xjYDApgpUF04f+SDzaMbBQUzu1E6FmoLFNcTNOGO /RpS/wF1yPtacj4PZeo7gQHe9hB94GTduETBN/P0MBJ+B63jmtgZdGhKfssHpHAXXuaB LxZf1RwQkXEcG3IDJwSBGjW9sJoY7+pIia7Zf3QlmQRfYQ0YztziVNwsQcLE83ndkIhb 6vmeAokZ987bvdQAUNS+br82JLR0SLKZ/oJGStmRmhFAP8M6ZrUbm7DxnjbAr2wXrrTQ uAIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b="TFzl/YNS"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bz1si1037583ejc.368.2020.05.08.08.07.41; Fri, 08 May 2020 08:08:03 -0700 (PDT) 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=@ziepe.ca header.s=google header.b="TFzl/YNS"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728034AbgEHPFn (ORCPT + 99 others); Fri, 8 May 2020 11:05:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726767AbgEHPFn (ORCPT ); Fri, 8 May 2020 11:05:43 -0400 Received: from mail-qt1-x844.google.com (mail-qt1-x844.google.com [IPv6:2607:f8b0:4864:20::844]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C974AC061A0C for ; Fri, 8 May 2020 08:05:42 -0700 (PDT) Received: by mail-qt1-x844.google.com with SMTP id b1so689606qtt.1 for ; Fri, 08 May 2020 08:05:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=IhwHM3vWsZyNiLOz6qrYkX84URnnDzt9jte52kOygqQ=; b=TFzl/YNStMC7c/QO4v5za8XO+KdWc8V3Wd8lfoEEFivJ45eAabfZwazGMN+/jcEkzo xig3oNkFG8G1NbfhmPaNtCG7zmj7A96+13gjzJL3s7roQ6Jtzmvmjnwa6pChGt8KdQDP iwopRVy6jwdAuPvbQzWXoxHzTzwTXFf0OEqHSEvgVAvxLhm4G+cQ/0RkGRzXst6FSjX1 G9hBAZSNHljo8WNWLgNMwT+rfJipymy6Qhjis4QaVTLGf+f2YhrKWyPi4CXnwnuYA6ha 8MCYs3K1Sg84Iu3kbMKf7OnoyGDX4bvdPrb6yoBat/Bm3hlBKYOCIZk7/EWsQAaIgVFz tltw== 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:user-agent; bh=IhwHM3vWsZyNiLOz6qrYkX84URnnDzt9jte52kOygqQ=; b=N0/j5PNIdcgFB5pOMP3JANBjGIRbJt8CKB/Uj4FQ2/DWVcYXJL/88xhZCO7SH42L1J 8QfGWS0SprczgR2Ty5Tru1iE35lsVBuXD1IQE+ngIWJrQRSoL0zoOudt+TRmr4dwKbIf AyYzoxumeR/vBNBX6dwB+YPQHJXSJ/ilhAGOTQdB46NIuh+wtHtbJTEII0B7y5Gtq0mA 9Y1lNgxquGamwwZXqNJnw+lCGImz1fKRmCGix4Bu6w4GM9LN6cjJf6gBbKMlCH7gkfUR l8iN2cB6ME9sb5Kkf4FRh8D2IfqMqKItzzHqHgTtjVQskyRtU7xwVVMRIXH/wrmvbvOn /dEA== X-Gm-Message-State: AGi0PuZESdL+3oRG9vxzOvt9rkUOlpmDmGs8jhlwTSaxNFi2y3uQjzbU Jvvbp3M2HdXvOu6mJdbHf8S5jA== X-Received: by 2002:ac8:7758:: with SMTP id g24mr3275992qtu.85.1588950341927; Fri, 08 May 2020 08:05:41 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-68-57-212.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.57.212]) by smtp.gmail.com with ESMTPSA id z18sm1598288qti.47.2020.05.08.08.05.40 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 May 2020 08:05:41 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1jX4ZI-0006Yk-Fu; Fri, 08 May 2020 12:05:40 -0300 Date: Fri, 8 May 2020 12:05:40 -0300 From: Jason Gunthorpe To: Peter Xu Cc: Alex Williamson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, cohuck@redhat.com Subject: Re: [PATCH v2 1/3] vfio/type1: Support faulting PFNMAP vmas Message-ID: <20200508150540.GP26002@ziepe.ca> References: <158871401328.15589.17598154478222071285.stgit@gimli.home> <158871568480.15589.17339878308143043906.stgit@gimli.home> <20200507212443.GO228260@xz-x1> <20200507235421.GK26002@ziepe.ca> <20200508021939.GT228260@xz-x1> <20200508121013.GO26002@ziepe.ca> <20200508143042.GY228260@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200508143042.GY228260@xz-x1> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 08, 2020 at 10:30:42AM -0400, Peter Xu wrote: > On Fri, May 08, 2020 at 09:10:13AM -0300, Jason Gunthorpe wrote: > > On Thu, May 07, 2020 at 10:19:39PM -0400, Peter Xu wrote: > > > On Thu, May 07, 2020 at 08:54:21PM -0300, Jason Gunthorpe wrote: > > > > On Thu, May 07, 2020 at 05:24:43PM -0400, Peter Xu wrote: > > > > > On Tue, May 05, 2020 at 03:54:44PM -0600, Alex Williamson wrote: > > > > > > With conversion to follow_pfn(), DMA mapping a PFNMAP range depends on > > > > > > the range being faulted into the vma. Add support to manually provide > > > > > > that, in the same way as done on KVM with hva_to_pfn_remapped(). > > > > > > > > > > > > Signed-off-by: Alex Williamson > > > > > > drivers/vfio/vfio_iommu_type1.c | 36 +++++++++++++++++++++++++++++++++--- > > > > > > 1 file changed, 33 insertions(+), 3 deletions(-) > > > > > > > > > > > > diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c > > > > > > index cc1d64765ce7..4a4cb7cd86b2 100644 > > > > > > +++ b/drivers/vfio/vfio_iommu_type1.c > > > > > > @@ -317,6 +317,32 @@ static int put_pfn(unsigned long pfn, int prot) > > > > > > return 0; > > > > > > } > > > > > > > > > > > > +static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm, > > > > > > + unsigned long vaddr, unsigned long *pfn, > > > > > > + bool write_fault) > > > > > > +{ > > > > > > + int ret; > > > > > > + > > > > > > + ret = follow_pfn(vma, vaddr, pfn); > > > > > > + if (ret) { > > > > > > + bool unlocked = false; > > > > > > + > > > > > > + ret = fixup_user_fault(NULL, mm, vaddr, > > > > > > + FAULT_FLAG_REMOTE | > > > > > > + (write_fault ? FAULT_FLAG_WRITE : 0), > > > > > > + &unlocked); > > > > > > + if (unlocked) > > > > > > + return -EAGAIN; > > > > > > > > > > Hi, Alex, > > > > > > > > > > IIUC this retry is not needed too because fixup_user_fault() will guarantee the > > > > > fault-in is done correctly with the valid PTE as long as ret==0, even if > > > > > unlocked==true. > > > > > > > > It is true, and today it is fine, but be careful when reworking this > > > > to use notifiers as unlocked also means things like the vma pointer > > > > are invalidated. > > > > > > Oh right, thanks for noticing that. Then we should probably still keep the > > > retry logic... because otherwise the latter follow_pfn() could be referencing > > > an invalid vma already... > > > > I looked briefly and thought this flow used the vma only once? > > ret = follow_pfn(vma, vaddr, pfn); > if (ret) { > bool unlocked = false; > > ret = fixup_user_fault(NULL, mm, vaddr, > FAULT_FLAG_REMOTE | > (write_fault ? FAULT_FLAG_WRITE : 0), > &unlocked); > if (unlocked) > return -EAGAIN; > > if (ret) > return ret; > > ret = follow_pfn(vma, vaddr, pfn); <--------------- [1] > } > > So imo the 2nd follow_pfn() [1] could be racy if without the unlocked check. Ah yes, I didn't notice that, you can't touch vma here if unlocked is true. Jason