[RFC,v1,1/4] dma-mapping: introduce the DMA_ATTR_MAY_SLEEP attribute

Message ID ea0646e0e63380bb8595fbac81c23aeca30feae9.1679309810.git.petr.tesarik.ext@huawei.com
State New
Headers
Series Allow dynamic allocation of software IO TLB bounce buffers |

Commit Message

Petr Tesarik March 20, 2023, 12:28 p.m. UTC
  From: Petr Tesarik <petr.tesarik.ext@huawei.com>

Introduce a DMA attribute to tell the DMA-mapping subsystem that
the operation is allowed to sleep.

This patch merely adds the flag, which is not used for anything at
the moment. It should be used by users who can sleep (e.g. dma-buf
ioctls) to allow page reclaim and/or allocations from CMA.

Signed-off-by: Petr Tesarik <petr.tesarik.ext@huawei.com>
---
 Documentation/core-api/dma-attributes.rst | 10 ++++++++++
 include/linux/dma-mapping.h               |  6 ++++++
 2 files changed, 16 insertions(+)
  

Comments

Christoph Hellwig March 28, 2023, 3:57 a.m. UTC | #1
On Mon, Mar 20, 2023 at 01:28:13PM +0100, Petr Tesarik wrote:
> From: Petr Tesarik <petr.tesarik.ext@huawei.com>
> 
> Introduce a DMA attribute to tell the DMA-mapping subsystem that
> the operation is allowed to sleep.
> 
> This patch merely adds the flag, which is not used for anything at
> the moment. It should be used by users who can sleep (e.g. dma-buf
> ioctls) to allow page reclaim and/or allocations from CMA.

So what drivers would call this?  As-is it doesn't have any users in
the series.
  
Petr Tesarik March 28, 2023, 7:21 a.m. UTC | #2
On 3/28/2023 5:57 AM, Christoph Hellwig wrote:
> On Mon, Mar 20, 2023 at 01:28:13PM +0100, Petr Tesarik wrote:
>> From: Petr Tesarik <petr.tesarik.ext@huawei.com>
>>
>> Introduce a DMA attribute to tell the DMA-mapping subsystem that
>> the operation is allowed to sleep.
>>
>> This patch merely adds the flag, which is not used for anything at
>> the moment. It should be used by users who can sleep (e.g. dma-buf
>> ioctls) to allow page reclaim and/or allocations from CMA.
> 
> So what drivers would call this?  As-is it doesn't have any users in
> the series.

Yes, I removed one patch from the RFC series to reduce the Cc list while
I wasn't sure if the proposal would be considered at all.

The full series in my local tree added it to the implementation of
DRM_IOCTL_PRIME_FD_TO_HANDLE:

diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index f924b8b4ab6b..f32e12445570 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -635,7 +635,7 @@ struct sg_table *drm_gem_map_dma_buf(struct
dma_buf_attachment *attach,
 		return sgt;

 	ret = dma_map_sgtable(attach->dev, sgt, dir,
-			      DMA_ATTR_SKIP_CPU_SYNC);
+			      DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MAY_SLEEP);
 	if (ret) {
 		sg_free_table(sgt);
 		kfree(sgt);

I also noticed a similar place in udmabuf, but since I don't have a use
case ATM, I haven't added the flag there (yet).

Petr T
  
Bagas Sanjaya March 31, 2023, 1:06 p.m. UTC | #3
On 3/20/23 19:28, Petr Tesarik wrote:
> +
> +DMA_ATTR_MAY_SLEEP
> +------------------
> +
> +This tells the DMA-mapping subsystem that it is allowed to sleep. For example,
> +if mapping needs a bounce buffer, software IO TLB may use CMA for the
> +allocation if this flag is given.
> +
> +This attribute is not used for dma_alloc_* functions. Instead, the provided
                                  dma_alloc_\* (escape wildcard in order to
                                                not confuse Sphinx for emphasis).
> +GFP flags are used to determine whether the allocation may sleep.

Otherwise the doc LGTM.
  
Christoph Hellwig April 7, 2023, 5:52 a.m. UTC | #4
On Tue, Mar 28, 2023 at 09:21:10AM +0200, Petr Tesarik wrote:
> The full series in my local tree added it to the implementation of
> DRM_IOCTL_PRIME_FD_TO_HANDLE:

Umm, an all these are callers that absolutely never should even
end up in swiotlb.  If we have large buffers allocated by media
subsystems, we need to make sure they are fully addressable.
  

Patch

diff --git a/Documentation/core-api/dma-attributes.rst b/Documentation/core-api/dma-attributes.rst
index 1887d92e8e92..6481ce2acf5d 100644
--- a/Documentation/core-api/dma-attributes.rst
+++ b/Documentation/core-api/dma-attributes.rst
@@ -130,3 +130,13 @@  accesses to DMA buffers in both privileged "supervisor" and unprivileged
 subsystem that the buffer is fully accessible at the elevated privilege
 level (and ideally inaccessible or at least read-only at the
 lesser-privileged levels).
+
+DMA_ATTR_MAY_SLEEP
+------------------
+
+This tells the DMA-mapping subsystem that it is allowed to sleep. For example,
+if mapping needs a bounce buffer, software IO TLB may use CMA for the
+allocation if this flag is given.
+
+This attribute is not used for dma_alloc_* functions. Instead, the provided
+GFP flags are used to determine whether the allocation may sleep.
diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h
index 0ee20b764000..7a75c503ac38 100644
--- a/include/linux/dma-mapping.h
+++ b/include/linux/dma-mapping.h
@@ -61,6 +61,12 @@ 
  */
 #define DMA_ATTR_PRIVILEGED		(1UL << 9)
 
+/*
+ * DMA_ATTR_MAY_SLEEP: This tells the DMA-mapping subsystem that it is allowed
+ * to sleep.
+ */
+#define DMA_ATTR_MAY_SLEEP		(1UL << 10)
+
 /*
  * A dma_addr_t can hold any valid DMA or bus address for the platform.  It can
  * be given to a device to use as a DMA source or target.  It is specific to a