From: "Petr Tesařík" <petr@tesarici.cz>
To: Michael Kelley <mhklinux@outlook.com>
Cc: Petr Tesarik <petrtesarik@huaweicloud.com>,
Christoph Hellwig <hch@lst.de>,
Marek Szyprowski <m.szyprowski@samsung.com>,
Robin Murphy <robin.murphy@arm.com>,
Will Deacon <will@kernel.org>, Nicolin Chen <nicolinc@nvidia.com>,
"open list:DMA MAPPING HELPERS" <iommu@lists.linux.dev>,
open list <linux-kernel@vger.kernel.org>,
Roberto Sassu <roberto.sassu@huaweicloud.com>,
Petr Tesarik <petr.tesarik1@huawei-partners.com>
Subject: Re: [PATCH 1/1] swiotlb: extend buffer pre-padding to alloc_align_mask if necessary
Date: Fri, 15 Mar 2024 16:09:31 +0100 [thread overview]
Message-ID: <20240315160931.48879f5c@meshulam.tesarici.cz> (raw)
In-Reply-To: <SN6PR02MB41574FB8FF2FA08C26682EC1D4282@SN6PR02MB4157.namprd02.prod.outlook.com>
On Fri, 15 Mar 2024 14:59:08 +0000
Michael Kelley <mhklinux@outlook.com> wrote:
> From: Petr Tesařík <petr@tesarici.cz> Sent: Friday, March 15, 2024 5:26 AM
> >
> > On Fri, 15 Mar 2024 02:53:10 +0000
> > Michael Kelley <mhklinux@outlook.com> wrote:
> >
> > > From: Petr Tesarik <petrtesarik@huaweicloud.com> Sent: Tuesday, March 12, 2024 6:42 AM
> > > >
>
> [snip]
>
> > > > @@ -1349,6 +1353,15 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
> > > > return (phys_addr_t)DMA_MAPPING_ERROR;
> > > > }
> > > >
> > > > + /*
> > > > + * Calculate buffer pre-padding within the allocated space. Use it to
> > > > + * preserve the low bits of the original address according to device's
> > > > + * min_align_mask. Limit the padding to alloc_align_mask or slot size
> > > > + * (whichever is bigger); higher bits of the original address are
> > > > + * preserved by selecting a suitable IO TLB slot.
> > > > + */
> > > > + offset = orig_addr & dma_get_min_align_mask(dev) &
> > > > + (alloc_align_mask | (IO_TLB_SIZE - 1));
> > > > index = swiotlb_find_slots(dev, orig_addr,
> > > > alloc_size + offset, alloc_align_mask, &pool);
> > > > if (index == -1) {
> > > > @@ -1364,6 +1377,12 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
> > > > * This is needed when we sync the memory. Then we sync the buffer if
> > > > * needed.
> > > > */
> > > > + padding = 0;
> > > > + while (offset >= IO_TLB_SIZE) {
> > > > + pool->slots[index++].orig_addr = INVALID_PHYS_ADDR;
> > > > + pool->slots[index].padding = ++padding;
> > > > + offset -= IO_TLB_SIZE;
> > > > + }
> > >
> > > Looping to fill in the padding slots seems unnecessary.
> > > The orig_addr field should already be initialized to
> > > INVALID_PHYS_ADDR, and the padding field should already
> > > be initialized to zero.
> >
> > Ack.
> >
> > > The value of "padding" should be just
> > > (offset / IO_TLB_SIZE), and it only needs to be stored in the
> > > first non-padding slot where swiotlb_release_slots() will
> > > find it.
> >
> > This is also right. I asked myself the question what should happen if
> > somebody passes a padding slot's address to swiotlb_tbl_unmap_single().
> > Of course, it's an invalid address, but as a proponent of defensive
> > programming, I still asked what would be the best response? If I store
> > padding in each slot, the whole block is released. If I store it only
> > in the first non-padding slot, some slots may leak.
> >
> > On a second thought, the resulting SWIOTLB state is consistent either
> > way, and we don't to care about leaking some slots if a driver is
> > buggy. Maybe it's even better, because the leak will be noticed.
> >
> > In short, I agree, let's keep the code simple.
> >
>
> One other thought: Fundamentally, fixing the core problem
> requires swiotlb_tbl_unmap_single() to have some information
> it doesn't have in the current code. It needs to know the
> number of padding slots, so that it can free them correctly.
> Your solution is to store the # of padding slots in the
> io_tlb_slot array.
>
> Another approach would be to have callers pass the
> alloc_align_mask as an argument to swiotlb_tbl_unmap_single().
> It can then calculate the offset and the number of padding
> slots just like swiotlb_tbl_map_single() does. Nothing
> additional would need to be stored in the io_tlb_slot array.
> The IOMMU code is the only caller than uses a non-zero
> alloc_align_mask. From a quick look at that code, the
> unmap path has the iova_mask() available, so that would
> work. Other callers would pass zero, just like they do for
> swiotlb_tbl_map_single().
>
> I don't immediately have a strong opinion either way, but
> it's something to think about a bit more.
I believe it's slightly more robust to store how the buffer was
actually allocated than to rely on the caller. It seems to me that this
was also a design goal of the original author. For example, note that
swiotlb_tbl_unmap_single() uses mapping_size only to do the final
buffer sync, but not to determine how many slots should be released.
This information is taken from struct io_tlb_slot.alloc_size.
Petr T
next prev parent reply other threads:[~2024-03-15 15:09 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-12 13:41 Petr Tesarik
2024-03-15 2:53 ` Michael Kelley
2024-03-15 12:26 ` Petr Tesařík
2024-03-15 14:59 ` Michael Kelley
2024-03-15 15:09 ` Petr Tesařík [this message]
2024-03-18 13:05 ` Robin Murphy
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=20240315160931.48879f5c@meshulam.tesarici.cz \
--to=petr@tesarici.cz \
--cc=hch@lst.de \
--cc=iommu@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=mhklinux@outlook.com \
--cc=nicolinc@nvidia.com \
--cc=petr.tesarik1@huawei-partners.com \
--cc=petrtesarik@huaweicloud.com \
--cc=roberto.sassu@huaweicloud.com \
--cc=robin.murphy@arm.com \
--cc=will@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
all inboxes | Powered by JetHome®