From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alex Williamson Subject: Re: [PATCH] vfio/type1: Limit DMA mappings per container Date: Tue, 2 Apr 2019 08:40:40 -0600 Message-ID: <20190402084040.72c3ceb5@x1.home> References: <155414977872.12780.13728555131525362206.stgit@gimli.home> <20190402024115.GA11008@xz-x1> <20190401223413.3783af5f@x1.home> <20190402051802.GB11008@xz-x1> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, eric.auger@redhat.com, cohuck@redhat.com To: Peter Xu Return-path: In-Reply-To: <20190402051802.GB11008@xz-x1> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org On Tue, 2 Apr 2019 13:18:02 +0800 Peter Xu wrote: > On Mon, Apr 01, 2019 at 10:34:13PM -0600, Alex Williamson wrote: > > On Tue, 2 Apr 2019 10:41:15 +0800 > > Peter Xu wrote: > > > > > On Mon, Apr 01, 2019 at 02:16:52PM -0600, Alex Williamson wrote: > > > > > > [...] > > > > > > > @@ -1081,8 +1088,14 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, > > > > goto out_unlock; > > > > } > > > > > > > > + if (!atomic_add_unless(&iommu->dma_avail, -1, 0)) { > > > > + ret = -ENOSPC; > > > > + goto out_unlock; > > > > + } > > > > + > > > > dma = kzalloc(sizeof(*dma), GFP_KERNEL); > > > > if (!dma) { > > > > + atomic_inc(&iommu->dma_avail); > > > > > > This should be the only special path to revert the change. Not sure > > > whether this can be avoided by simply using atomic_read() or even > > > READ_ONCE() (I feel like we don't need atomic ops with dma_avail > > > because we've had the mutex but it of course it doesn't hurt...) to > > > replace atomic_add_unless() above to check against zero then we do > > > +1/-1 in vfio_[un]link_dma() only. But AFAICT this patch is correct. > > > > Thanks for the review, you're right, we're only twiddling this atomic > > while holding the iommu->lock mutex, so it appears unnecessary. Since > > we're within the mutex, I think we don't even need a READ_ONCE. We can > > simple test it before alloc and decrement after. Am I missing something > > that would specifically require READ_ONCE within our mutex critical > > section? Thanks, > > I don't know very clear on this and I'd be glad to learn about that. > My understanding is that [READ|WRITE]_ONCE() is the same as volatile > mem operation and will make sure we don't keep variables in the > registers. So if the mutex semantics can support that (say, a "*addr > = val" following with a mutex_unlock will make sure "val" will > definitely land into memory of "&addr") then I do think it's fine even > without it (which corresponds to WRITE_ONCE(&addr, val) in this case). The READ/WRITE_ONCE macros add memory barriers, but we have the mutex for protecting concurrent access to the data. I don't see that there's anything special about a counter on the iommu object that needs special attention vs any other elements that might get modified in these sections. Thanks, Alex