linux-coco.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: "Petr Tesařík" <petr@tesarici.cz>
To: Michael Kelley <mhklinux@outlook.com>
Cc: "mhkelley58@gmail.com" <mhkelley58@gmail.com>,
	"kbusch@kernel.org" <kbusch@kernel.org>,
	"axboe@kernel.dk" <axboe@kernel.dk>,
	"sagi@grimberg.me" <sagi@grimberg.me>,
	"James.Bottomley@HansenPartnership.com"
	<James.Bottomley@HansenPartnership.com>,
	"martin.petersen@oracle.com" <martin.petersen@oracle.com>,
	"kys@microsoft.com" <kys@microsoft.com>,
	"haiyangz@microsoft.com" <haiyangz@microsoft.com>,
	"wei.liu@kernel.org" <wei.liu@kernel.org>,
	"decui@microsoft.com" <decui@microsoft.com>,
	"robin.murphy@arm.com" <robin.murphy@arm.com>,
	"hch@lst.de" <hch@lst.de>,
	"m.szyprowski@samsung.com" <m.szyprowski@samsung.com>,
	"iommu@lists.linux.dev" <iommu@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-nvme@lists.infradead.org" <linux-nvme@lists.infradead.org>,
	"linux-scsi@vger.kernel.org" <linux-scsi@vger.kernel.org>,
	"linux-hyperv@vger.kernel.org" <linux-hyperv@vger.kernel.org>,
	"linux-coco@lists.linux.dev" <linux-coco@lists.linux.dev>
Subject: Re: [RFC 2/7] dma: Handle swiotlb throttling for SGLs
Date: Sat, 24 Aug 2024 21:56:37 +0200	[thread overview]
Message-ID: <20240824215637.2bbacb50@meshulam.tesarici.cz> (raw)
In-Reply-To: <SN6PR02MB41577D08C73A93C32F836E17D4882@SN6PR02MB4157.namprd02.prod.outlook.com>

On Fri, 23 Aug 2024 20:42:08 +0000
Michael Kelley <mhklinux@outlook.com> wrote:

> From: Petr Tesařík <petr@tesarici.cz> Sent: Friday, August 23, 2024 1:03 AM
> > 
> > On Thu, 22 Aug 2024 11:37:13 -0700
> > mhkelley58@gmail.com wrote:
> >   
> > > From: Michael Kelley <mhklinux@outlook.com>
> > >
> > > When a DMA map request is for a SGL, each SGL entry results in an
> > > independent mapping operation. If the mapping requires a bounce buffer
> > > due to running in a CoCo VM or due to swiotlb=force on the boot line,
> > > swiotlb is invoked. If swiotlb throttling is enabled for the request,
> > > each SGL entry results in a separate throttling operation. This is
> > > problematic because a thread may be holding swiotlb memory while waiting
> > > for memory to become free.
> > >
> > > Resolve this problem by only allowing throttling on the 0th SGL
> > > entry. When unmapping the SGL, unmap entries 1 thru N-1 first, then
> > > unmap entry 0 so that the throttle isn't released until all swiotlb
> > > memory has been freed.
> > >
> > > Signed-off-by: Michael Kelley <mhklinux@outlook.com>
> > > ---
> > > This approach to SGLs muddies the line between DMA direct and swiotlb
> > > throttling functionality. To keep the MAY_BLOCK attr fully generic, it
> > > should propagate to the mapping of all SGL entries.
> > >
> > > An alternate approach is to define an additional DMA attribute that
> > > is internal to the DMA layer. Instead of clearing MAX_BLOCK, this
> > > attr is added by dma_direct_map_sg() when mapping SGL entries other
> > > than the 0th entry. swiotlb would do throttling only when MAY_BLOCK
> > > is set and this new attr is not set.
> > >
> > > This approach has a modest amount of additional complexity. Given
> > > that we currently have no other users of the MAY_BLOCK attr, the
> > > conceptual cleanliness may not be warranted until we do.
> > >
> > > Thoughts?  
> > 
> > If we agree to change the unthrottling logic (see my comment to your
> > RFC 1/7), we'll need an additional attribute to delay unthrottling when
> > unmapping sg list entries 1 to N-1. This attribute could convey that
> > the mapping is the non-initial segment of an sg list and it could then
> > be also used to disable blocking in swiotlb_tbl_map_single().
> >   
> > >
> > >  kernel/dma/direct.c | 35 ++++++++++++++++++++++++++++++-----
> > >  1 file changed, 30 insertions(+), 5 deletions(-)
> > >
> > > diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
> > > index 4480a3cd92e0..80e03c0838d4 100644
> > > --- a/kernel/dma/direct.c
> > > +++ b/kernel/dma/direct.c
> > > @@ -438,6 +438,18 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
> > >  		arch_sync_dma_for_cpu_all();
> > >  }
> > >
> > > +static void dma_direct_unmap_sgl_entry(struct device *dev,
> > > +		struct scatterlist *sgl, enum dma_data_direction dir,  
> > 
> > Nitpick: This parameter should probably be called "sg", because it is
> > never used to do any operation on the whole list. Similarly, the
> > function could be called dma_direct_unmap_sg_entry(), because there is
> > no dma_direct_unmap_sgl() either...  
> 
> OK.  I agree.
> 
> >   
> > > +		unsigned long attrs)
> > > +
> > > +{
> > > +	if (sg_dma_is_bus_address(sgl))
> > > +		sg_dma_unmark_bus_address(sgl);
> > > +	else
> > > +		dma_direct_unmap_page(dev, sgl->dma_address,
> > > +				      sg_dma_len(sgl), dir, attrs);
> > > +}
> > > +
> > >  /*
> > >   * Unmaps segments, except for ones marked as pci_p2pdma which do not
> > >   * require any further action as they contain a bus address.
> > > @@ -449,12 +461,20 @@ void dma_direct_unmap_sg(struct device *dev, struct  
> > scatterlist *sgl,  
> > >  	int i;
> > >
> > >  	for_each_sg(sgl,  sg, nents, i) {
> > > -		if (sg_dma_is_bus_address(sg))
> > > -			sg_dma_unmark_bus_address(sg);
> > > -		else
> > > -			dma_direct_unmap_page(dev, sg->dma_address,
> > > -					      sg_dma_len(sg), dir, attrs);
> > > +		/*
> > > +		 * Skip the 0th SGL entry in case this SGL consists of
> > > +		 * throttled swiotlb mappings. In such a case, any other
> > > +		 * entries should be unmapped first since unmapping the
> > > +		 * 0th entry will release the throttle semaphore.
> > > +		 */
> > > +		if (!i)
> > > +			continue;
> > > +		dma_direct_unmap_sgl_entry(dev, sg, dir, attrs);
> > >  	}
> > > +
> > > +	/* Now do the 0th SGL entry */
> > > +	if (nents)  
> > 
> > I wonder if nents can ever be zero here, but it's nowhere enforced and
> > dma_map_sg_attrs() is exported, so I agree, let's play it safe.  
> 
> Yep -- my thinking exactly.
> 
> >   
> > > +		dma_direct_unmap_sgl_entry(dev, sgl, dir, attrs);
> > >  }
> > >  #endif
> > >
> > > @@ -492,6 +512,11 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist  
> > *sgl, int nents,  
> > >  			ret = -EIO;
> > >  			goto out_unmap;
> > >  		}
> > > +
> > > +		/* Allow only the 0th SGL entry to block */
> > > +		if (!i)  
> > 
> > Are you sure? I think the modified value of attrs is first used in the
> > next loop iteration, so the conditional should be removed, or else both
> > segment index 0 and 1 will keep the flag.  
> 
> I don't understand your comment. If it's present, the MAY_BLOCK flag
> is used for the index 0 SGL entry, and then is cleared before the loop is
> run again for the index 1 and subsequent SGL entries. But it would
> still work with the conditional removed, and maybe the CPU overhead
> of always clearing the flag is the same as doing the conditional.

Yes, this was my original thinking, but then I somehow got caught up in
it and went on to thinking that the condition was not only unnecessary,
but wrong. You're right, the code is perfectly fine as it is.

Sorry for the noise.

Petr T

  reply	other threads:[~2024-08-24 19:56 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-22 18:37 [RFC 0/7] Introduce swiotlb throttling mhkelley58
2024-08-22 18:37 ` [RFC 1/7] swiotlb: " mhkelley58
2024-08-23  7:41   ` Petr Tesařík
2024-08-23 20:41     ` Michael Kelley
2024-08-27 15:55       ` Petr Tesařík
2024-08-27 17:30         ` Michael Kelley
2024-08-28  5:15           ` Petr Tesařík
2024-08-28  6:14             ` Michael Kelley
2024-08-22 18:37 ` [RFC 2/7] dma: Handle swiotlb throttling for SGLs mhkelley58
2024-08-23  8:02   ` Petr Tesařík
2024-08-23 20:42     ` Michael Kelley
2024-08-24 19:56       ` Petr Tesařík [this message]
2024-08-22 18:37 ` [RFC 3/7] dma: Add function for drivers to know if allowing blocking is useful mhkelley58
2024-08-23  8:07   ` Petr Tesařík
2024-08-22 18:37 ` [RFC 4/7] scsi_lib_dma: Add _attrs variant of scsi_dma_map() mhkelley58
2024-08-23  8:08   ` Petr Tesařík
2024-08-22 18:37 ` [RFC 5/7] scsi: storvsc: Enable swiotlb throttling mhkelley58
2024-08-23  8:19   ` Petr Tesařík
2024-08-23 20:42     ` Michael Kelley
2024-08-22 18:37 ` [RFC 6/7] nvme: Move BLK_MQ_F_BLOCKING indicator to struct nvme_ctrl mhkelley58
2024-08-23  8:22   ` Petr Tesařík
2024-08-22 18:37 ` [RFC 7/7] nvme: Enable swiotlb throttling for NVMe PCI devices mhkelley58
2024-08-23  8:26   ` Petr Tesařík
2024-08-22 19:29 ` [RFC 0/7] Introduce swiotlb throttling Bart Van Assche
2024-08-23  2:20   ` Michael Kelley
2024-08-23  5:46     ` Petr Tesařík
2024-08-24  8:05     ` hch
2024-08-23  6:44 ` Petr Tesařík
2024-08-23 20:40   ` Michael Kelley
2024-08-24 20:05     ` Petr Tesařík
2024-08-26 16:24       ` Michael Kelley
2024-08-26 19:28         ` Petr Tesařík
2024-08-27  0:26           ` Michael Kelley
2024-08-27  8:00             ` Petr Tesařík
2024-08-24  8:16 ` Christoph Hellwig
2024-08-26 15:27   ` Michael Kelley
2024-08-27  7:14     ` Christoph Hellwig
2024-08-28 12:02 ` Robin Murphy
2024-08-28 13:03   ` Petr Tesařík
2024-08-28 16:30     ` Michael Kelley
2024-08-28 16:41       ` Petr Tesařík
2024-08-28 19:50     ` Robin Murphy
2024-08-30  3:58       ` Michael Kelley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240824215637.2bbacb50@meshulam.tesarici.cz \
    --to=petr@tesarici.cz \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=axboe@kernel.dk \
    --cc=decui@microsoft.com \
    --cc=haiyangz@microsoft.com \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux.dev \
    --cc=kbusch@kernel.org \
    --cc=kys@microsoft.com \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=martin.petersen@oracle.com \
    --cc=mhkelley58@gmail.com \
    --cc=mhklinux@outlook.com \
    --cc=robin.murphy@arm.com \
    --cc=sagi@grimberg.me \
    --cc=wei.liu@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).