public inbox for linux-nvme@lists.infradead.org
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: Christoph Hellwig <hch@lst.de>,
	jxgao@google.com, gregkh@linuxfoundation.org
Cc: saravanak@google.com, konrad.wilk@oracle.com, marcorr@google.com,
	linux-nvme@lists.infradead.org, kbusch@kernel.org,
	iommu@lists.linux-foundation.org, erdemaktas@google.com,
	m.szyprowski@samsung.com
Subject: Re: [PATCH 5/8] swiotlb: refactor swiotlb_tbl_map_single
Date: Thu, 4 Feb 2021 22:12:31 +0000	[thread overview]
Message-ID: <f60ddc1b-94c3-32a1-06eb-b2187d56d797@arm.com> (raw)
In-Reply-To: <20210204193035.2606838-6-hch@lst.de>

On 2021-02-04 19:30, Christoph Hellwig wrote:
> Split out a bunch of a self-contained helpers to make the function easier
> to follow.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>   kernel/dma/swiotlb.c | 177 +++++++++++++++++++++----------------------
>   1 file changed, 87 insertions(+), 90 deletions(-)
> 
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index 79d5b236f25f10..e78615e3be2906 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
> @@ -468,134 +468,131 @@ static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
>   	}
>   }
>   
> -phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
> -		size_t mapping_size, size_t alloc_size,
> -		enum dma_data_direction dir, unsigned long attrs)
> +/*
> + * Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
> + */
> +static inline unsigned long get_max_slots(unsigned long boundary_mask)
>   {
> -	dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, io_tlb_start);
> -	unsigned long flags;
> -	phys_addr_t tlb_addr;
> -	unsigned int nslots, stride, index, wrap;
> -	int i;
> -	unsigned long mask;
> -	unsigned long offset_slots;
> -	unsigned long max_slots;
> -	unsigned long tmp_io_tlb_used;
> -
> -	if (no_iotlb_memory)
> -		panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
> -
> -	if (mem_encrypt_active())
> -		pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
> -
> -	if (mapping_size > alloc_size) {
> -		dev_warn_once(hwdev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
> -			      mapping_size, alloc_size);
> -		return (phys_addr_t)DMA_MAPPING_ERROR;
> -	}
> -
> -	mask = dma_get_seg_boundary(hwdev);
> +	if (boundary_mask + 1 == ~0UL)

Either "mask == ~0UL" or "mask + 1 == 0", surely?

> +		return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
> +	return nr_slots(boundary_mask + 1);
> +}
>   
> -	tbl_dma_addr &= mask;
> +static unsigned int wrap_index(unsigned int index)
> +{
> +	if (index >= io_tlb_nslabs)
> +		return 0;
> +	return index;
> +}
>   
> -	offset_slots = nr_slots(tbl_dma_addr);
> +/*
> + * Find a suitable number of IO TLB entries size that will fit this request and
> + * allocate a buffer from that IO TLB pool.
> + */
> +static int find_slots(struct device *dev, size_t alloc_size,
> +		dma_addr_t tbl_dma_addr)
> +{
> +	unsigned int max_slots = get_max_slots(dma_get_seg_boundary(dev));
> +	unsigned int nslots = nr_slots(alloc_size), stride = 1;
> +	unsigned int index, wrap, count = 0, i;
> +	unsigned long flags;
>   
> -	/*
> -	 * Carefully handle integer overflow which can occur when mask == ~0UL.
> -	 */
> -	max_slots = mask + 1
> -		    ? nr_slots(mask + 1)
> -		    : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);

...since the condition here is effectively the latter.

Robin.

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  reply	other threads:[~2021-02-04 22:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-04 19:30 preserve DMA offsets when using swiotlb Christoph Hellwig
2021-02-04 19:30 ` [PATCH 1/8] driver core: add a min_align_mask field to struct device_dma_parameters Christoph Hellwig
2021-02-04 19:44   ` Greg KH
2021-02-04 19:30 ` [PATCH 2/8] swiotlb: add a io_tlb_offset helper Christoph Hellwig
2021-02-04 19:30 ` [PATCH 3/8] swiotlb: factor out a nr_slots helper Christoph Hellwig
2021-02-04 22:09   ` Robin Murphy
2021-02-05  9:45     ` Christoph Hellwig
2021-02-04 19:30 ` [PATCH 4/8] swiotlb: clean up swiotlb_tbl_unmap_single Christoph Hellwig
2021-02-04 19:30 ` [PATCH 5/8] swiotlb: refactor swiotlb_tbl_map_single Christoph Hellwig
2021-02-04 22:12   ` Robin Murphy [this message]
2021-02-05  9:45     ` Christoph Hellwig
2021-02-04 19:30 ` [PATCH 6/8] swiotlb: don't modify orig_addr in swiotlb_tbl_sync_single Christoph Hellwig
2021-02-04 19:30 ` [PATCH 7/8] swiotlb: respect min_align_mask Christoph Hellwig
2021-02-04 23:13   ` Robin Murphy
2021-02-05 10:34     ` Christoph Hellwig
2021-02-04 19:30 ` [PATCH 8/8] nvme-pci: set min_align_mask Christoph Hellwig
2021-02-04 19:32   ` Christoph Hellwig
2021-02-05 11:50 ` preserve DMA offsets when using swiotlb Christoph Hellwig

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=f60ddc1b-94c3-32a1-06eb-b2187d56d797@arm.com \
    --to=robin.murphy@arm.com \
    --cc=erdemaktas@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux-foundation.org \
    --cc=jxgao@google.com \
    --cc=kbusch@kernel.org \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-nvme@lists.infradead.org \
    --cc=m.szyprowski@samsung.com \
    --cc=marcorr@google.com \
    --cc=saravanak@google.com \
    /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