mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-31 10:31:33 -04:00
dma-direct: use __DMA_ATTR_ALLOC_CC_SHARED in alloc/free paths
Propagate force_dma_unencrypted() into __DMA_ATTR_ALLOC_CC_SHARED in the dma-direct allocation path and use the attribute to drive the related decisions. This updates dma_direct_alloc(), dma_direct_free(), and dma_direct_alloc_pages() to fold the forced unencrypted case into attrs. Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Tested-by: Jiri Pirko <jiri@nvidia.com> Tested-by: Michael Kelley <mhklinux@outlook.com> Tested-by: Mostafa Saleh <smostafa@google.com> Reviewed-by: Petr Tesarik <ptesarik@suse.com> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org> Link: https://lore.kernel.org/r/20260717180442.110954-11-aneesh.kumar@kernel.org Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
This commit is contained in:
committed by
Marek Szyprowski
parent
a6e40900f0
commit
b5ef0dd28e
@@ -192,16 +192,22 @@ void *dma_direct_alloc(struct device *dev, size_t size,
|
||||
dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
|
||||
{
|
||||
bool remap = false, set_uncached = false;
|
||||
bool mark_mem_decrypt = true;
|
||||
bool mark_mem_decrypt = false;
|
||||
struct page *page;
|
||||
void *ret;
|
||||
|
||||
if (force_dma_unencrypted(dev))
|
||||
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
|
||||
|
||||
if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
|
||||
mark_mem_decrypt = true;
|
||||
|
||||
size = PAGE_ALIGN(size);
|
||||
if (attrs & DMA_ATTR_NO_WARN)
|
||||
gfp |= __GFP_NOWARN;
|
||||
|
||||
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
|
||||
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev))
|
||||
if (((attrs & (DMA_ATTR_NO_KERNEL_MAPPING | __DMA_ATTR_ALLOC_CC_SHARED)) ==
|
||||
DMA_ATTR_NO_KERNEL_MAPPING) && !is_swiotlb_for_alloc(dev))
|
||||
return dma_direct_alloc_no_mapping(dev, size, dma_handle, gfp);
|
||||
|
||||
if (!dev_is_dma_coherent(dev)) {
|
||||
@@ -235,7 +241,7 @@ void *dma_direct_alloc(struct device *dev, size_t size,
|
||||
* Remapping or decrypting memory may block, allocate the memory from
|
||||
* the atomic pools instead if we aren't allowed block.
|
||||
*/
|
||||
if ((remap || force_dma_unencrypted(dev)) &&
|
||||
if ((remap || (attrs & __DMA_ATTR_ALLOC_CC_SHARED)) &&
|
||||
dma_direct_use_pool(dev, gfp)) {
|
||||
page = dma_direct_alloc_from_pool(dev, size, dma_handle,
|
||||
&ret, gfp);
|
||||
@@ -314,12 +320,22 @@ void dma_direct_free(struct device *dev, size_t size,
|
||||
void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs)
|
||||
{
|
||||
phys_addr_t phys;
|
||||
bool mark_mem_encrypted = true;
|
||||
bool mark_mem_encrypted = false;
|
||||
struct io_tlb_pool *swiotlb_pool;
|
||||
unsigned int page_order = get_order(size);
|
||||
|
||||
if ((attrs & DMA_ATTR_NO_KERNEL_MAPPING) &&
|
||||
!force_dma_unencrypted(dev) && !is_swiotlb_for_alloc(dev)) {
|
||||
/*
|
||||
* If the allocation used decrypted/shared backing pages, restore
|
||||
* the encryption state on free.
|
||||
*/
|
||||
if (force_dma_unencrypted(dev))
|
||||
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
|
||||
|
||||
if (attrs & __DMA_ATTR_ALLOC_CC_SHARED)
|
||||
mark_mem_encrypted = true;
|
||||
|
||||
if (((attrs & (DMA_ATTR_NO_KERNEL_MAPPING | __DMA_ATTR_ALLOC_CC_SHARED)) ==
|
||||
DMA_ATTR_NO_KERNEL_MAPPING) && !is_swiotlb_for_alloc(dev)) {
|
||||
/* cpu_addr is a struct page cookie, not a kernel address */
|
||||
dma_free_contiguous(dev, cpu_addr, size);
|
||||
return;
|
||||
@@ -368,10 +384,14 @@ void dma_direct_free(struct device *dev, size_t size,
|
||||
struct page *dma_direct_alloc_pages(struct device *dev, size_t size,
|
||||
dma_addr_t *dma_handle, enum dma_data_direction dir, gfp_t gfp)
|
||||
{
|
||||
unsigned long attrs = 0;
|
||||
struct page *page;
|
||||
void *ret;
|
||||
|
||||
if (force_dma_unencrypted(dev) && dma_direct_use_pool(dev, gfp))
|
||||
if (force_dma_unencrypted(dev))
|
||||
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
|
||||
|
||||
if ((attrs & __DMA_ATTR_ALLOC_CC_SHARED) && dma_direct_use_pool(dev, gfp))
|
||||
return dma_direct_alloc_from_pool(dev, size, dma_handle, &ret, gfp);
|
||||
|
||||
if (is_swiotlb_for_alloc(dev)) {
|
||||
@@ -405,7 +425,11 @@ void dma_direct_free_pages(struct device *dev, size_t size,
|
||||
phys_addr_t phys;
|
||||
void *vaddr = page_address(page);
|
||||
struct io_tlb_pool *swiotlb_pool;
|
||||
bool mark_mem_encrypted = true;
|
||||
/*
|
||||
* if the device had requested for an unencrypted buffer,
|
||||
* convert it to encrypted on free
|
||||
*/
|
||||
bool mark_mem_encrypted = force_dma_unencrypted(dev);
|
||||
|
||||
/* If page is not from an atomic pool, dma_free_from_pool_page() fails */
|
||||
if (IS_ENABLED(CONFIG_DMA_COHERENT_POOL) &&
|
||||
|
||||
@@ -638,6 +638,15 @@ void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
|
||||
if (WARN_ON_ONCE(flag & __GFP_COMP))
|
||||
return NULL;
|
||||
|
||||
if (attrs & (DMA_ATTR_CC_SHARED | __DMA_ATTR_ALLOC_CC_SHARED)) {
|
||||
trace_dma_alloc(dev, NULL, 0, size, DMA_BIDIRECTIONAL, flag,
|
||||
attrs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (force_dma_unencrypted(dev))
|
||||
attrs |= __DMA_ATTR_ALLOC_CC_SHARED;
|
||||
|
||||
if (dma_alloc_from_dev_coherent(dev, size, dma_handle, &cpu_addr)) {
|
||||
trace_dma_alloc(dev, cpu_addr, *dma_handle, size,
|
||||
DMA_BIDIRECTIONAL, flag, attrs);
|
||||
|
||||
Reference in New Issue
Block a user