@@ -109,29 +109,30 @@ static inline bool dma_should_free_from_pool(struct device *dev,
109
109
return false;
110
110
}
111
111
112
- struct page * __dma_direct_alloc_pages (struct device * dev , size_t size ,
112
+ static struct page * __dma_direct_alloc_pages (struct device * dev , size_t size ,
113
113
gfp_t gfp , unsigned long attrs )
114
114
{
115
- size_t alloc_size = PAGE_ALIGN (size );
116
115
int node = dev_to_node (dev );
117
116
struct page * page = NULL ;
118
117
u64 phys_limit ;
119
118
119
+ WARN_ON_ONCE (!PAGE_ALIGNED (size ));
120
+
120
121
if (attrs & DMA_ATTR_NO_WARN )
121
122
gfp |= __GFP_NOWARN ;
122
123
123
124
/* we always manually zero the memory once we are done: */
124
125
gfp &= ~__GFP_ZERO ;
125
126
gfp |= dma_direct_optimal_gfp_mask (dev , dev -> coherent_dma_mask ,
126
127
& phys_limit );
127
- page = dma_alloc_contiguous (dev , alloc_size , gfp );
128
+ page = dma_alloc_contiguous (dev , size , gfp );
128
129
if (page && !dma_coherent_ok (dev , page_to_phys (page ), size )) {
129
- dma_free_contiguous (dev , page , alloc_size );
130
+ dma_free_contiguous (dev , page , size );
130
131
page = NULL ;
131
132
}
132
133
again :
133
134
if (!page )
134
- page = alloc_pages_node (node , gfp , get_order (alloc_size ));
135
+ page = alloc_pages_node (node , gfp , get_order (size ));
135
136
if (page && !dma_coherent_ok (dev , page_to_phys (page ), size )) {
136
137
dma_free_contiguous (dev , page , size );
137
138
page = NULL ;
@@ -157,9 +158,12 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
157
158
{
158
159
struct page * page ;
159
160
void * ret ;
161
+ int err ;
162
+
163
+ size = PAGE_ALIGN (size );
160
164
161
165
if (dma_should_alloc_from_pool (dev , gfp , attrs )) {
162
- ret = dma_alloc_from_pool (dev , PAGE_ALIGN ( size ) , & page , gfp );
166
+ ret = dma_alloc_from_pool (dev , size , & page , gfp );
163
167
if (!ret )
164
168
return NULL ;
165
169
goto done ;
@@ -183,14 +187,20 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
183
187
dma_alloc_need_uncached (dev , attrs )) ||
184
188
(IS_ENABLED (CONFIG_DMA_REMAP ) && PageHighMem (page ))) {
185
189
/* remove any dirty cache lines on the kernel alias */
186
- arch_dma_prep_coherent (page , PAGE_ALIGN ( size ) );
190
+ arch_dma_prep_coherent (page , size );
187
191
188
192
/* create a coherent mapping */
189
- ret = dma_common_contiguous_remap (page , PAGE_ALIGN ( size ) ,
193
+ ret = dma_common_contiguous_remap (page , size ,
190
194
dma_pgprot (dev , PAGE_KERNEL , attrs ),
191
195
__builtin_return_address (0 ));
192
196
if (!ret )
193
197
goto out_free_pages ;
198
+ if (force_dma_unencrypted (dev )) {
199
+ err = set_memory_decrypted ((unsigned long )ret ,
200
+ 1 << get_order (size ));
201
+ if (err )
202
+ goto out_free_pages ;
203
+ }
194
204
memset (ret , 0 , size );
195
205
goto done ;
196
206
}
@@ -207,8 +217,12 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
207
217
}
208
218
209
219
ret = page_address (page );
210
- if (force_dma_unencrypted (dev ))
211
- set_memory_decrypted ((unsigned long )ret , 1 << get_order (size ));
220
+ if (force_dma_unencrypted (dev )) {
221
+ err = set_memory_decrypted ((unsigned long )ret ,
222
+ 1 << get_order (size ));
223
+ if (err )
224
+ goto out_free_pages ;
225
+ }
212
226
213
227
memset (ret , 0 , size );
214
228
@@ -217,14 +231,23 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size,
217
231
arch_dma_prep_coherent (page , size );
218
232
ret = arch_dma_set_uncached (ret , size );
219
233
if (IS_ERR (ret ))
220
- goto out_free_pages ;
234
+ goto out_encrypt_pages ;
221
235
}
222
236
done :
223
237
if (force_dma_unencrypted (dev ))
224
238
* dma_handle = __phys_to_dma (dev , page_to_phys (page ));
225
239
else
226
240
* dma_handle = phys_to_dma (dev , page_to_phys (page ));
227
241
return ret ;
242
+
243
+ out_encrypt_pages :
244
+ if (force_dma_unencrypted (dev )) {
245
+ err = set_memory_encrypted ((unsigned long )page_address (page ),
246
+ 1 << get_order (size ));
247
+ /* If memory cannot be re-encrypted, it must be leaked */
248
+ if (err )
249
+ return NULL ;
250
+ }
228
251
out_free_pages :
229
252
dma_free_contiguous (dev , page , size );
230
253
return NULL ;
@@ -459,7 +482,6 @@ int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
459
482
return ret ;
460
483
}
461
484
462
- #ifdef CONFIG_MMU
463
485
bool dma_direct_can_mmap (struct device * dev )
464
486
{
465
487
return dev_is_dma_coherent (dev ) ||
@@ -485,19 +507,6 @@ int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
485
507
return remap_pfn_range (vma , vma -> vm_start , pfn + vma -> vm_pgoff ,
486
508
user_count << PAGE_SHIFT , vma -> vm_page_prot );
487
509
}
488
- #else /* CONFIG_MMU */
489
- bool dma_direct_can_mmap (struct device * dev )
490
- {
491
- return false;
492
- }
493
-
494
- int dma_direct_mmap (struct device * dev , struct vm_area_struct * vma ,
495
- void * cpu_addr , dma_addr_t dma_addr , size_t size ,
496
- unsigned long attrs )
497
- {
498
- return - ENXIO ;
499
- }
500
- #endif /* CONFIG_MMU */
501
510
502
511
int dma_direct_supported (struct device * dev , u64 mask )
503
512
{
0 commit comments