1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * DMABUF System heap exporter 4 * 5 * Copyright (C) 2011 Google, Inc. 6 * Copyright (C) 2019, 2020 Linaro Ltd. 7 * 8 * Portions based off of Andrew Davis' SRAM heap: 9 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/ 10 * Andrew F. Davis <afd@ti.com> 11 */ 12 13 #include <linux/cc_platform.h> 14 #include <linux/dma-buf.h> 15 #include <linux/dma-mapping.h> 16 #include <linux/dma-heap.h> 17 #include <linux/err.h> 18 #include <linux/highmem.h> 19 #include <linux/mem_encrypt.h> 20 #include <linux/mm.h> 21 #include <linux/set_memory.h> 22 #include <linux/module.h> 23 #include <linux/pgtable.h> 24 #include <linux/scatterlist.h> 25 #include <linux/slab.h> 26 #include <linux/vmalloc.h> 27 28 struct system_heap_priv { 29 bool cc_shared; 30 }; 31 32 struct system_heap_buffer { 33 struct dma_heap *heap; 34 struct list_head attachments; 35 struct mutex lock; 36 unsigned long len; 37 struct sg_table sg_table; 38 int vmap_cnt; 39 void *vaddr; 40 bool cc_shared; 41 }; 42 43 struct dma_heap_attachment { 44 struct device *dev; 45 struct sg_table table; 46 struct list_head list; 47 bool mapped; 48 bool cc_shared; 49 }; 50 51 #define cc_shared_buffer(b) (IS_ENABLED(CONFIG_DMABUF_HEAPS_SYSTEM_CC_SHARED) && \ 52 (b)->cc_shared) 53 54 #define LOW_ORDER_GFP (GFP_HIGHUSER | __GFP_ZERO) 55 #define HIGH_ORDER_GFP (((GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN \ 56 | __GFP_NORETRY) & ~__GFP_RECLAIM) \ 57 | __GFP_COMP) 58 static gfp_t order_flags[] = {HIGH_ORDER_GFP, HIGH_ORDER_GFP, LOW_ORDER_GFP}; 59 /* 60 * The selection of the orders used for allocation (1MB, 64K, 4K) is designed 61 * to match with the sizes often found in IOMMUs. Using order 4 pages instead 62 * of order 0 pages can significantly improve the performance of many IOMMUs 63 * by reducing TLB pressure and time spent updating page tables. 64 */ 65 static const unsigned int orders[] = {8, 4, 0}; 66 #define NUM_ORDERS ARRAY_SIZE(orders) 67 68 static int system_heap_set_page_decrypted(struct page *page) 69 { 70 unsigned long addr = (unsigned long)page_address(page); 71 unsigned int nr_pages = 1 << compound_order(page); 72 int ret; 73 74 ret = set_memory_decrypted(addr, nr_pages); 75 if (ret) 76 pr_warn_ratelimited("dma-buf system heap: failed to decrypt page at %p\n", 77 page_address(page)); 78 79 return ret; 80 } 81 82 static int system_heap_set_page_encrypted(struct page *page) 83 { 84 unsigned long addr = (unsigned long)page_address(page); 85 unsigned int nr_pages = 1 << compound_order(page); 86 int ret; 87 88 ret = set_memory_encrypted(addr, nr_pages); 89 if (ret) 90 pr_warn_ratelimited("dma-buf system heap: failed to re-encrypt page at %p, leaking memory\n", 91 page_address(page)); 92 93 return ret; 94 } 95 96 static int dup_sg_table(struct sg_table *from, struct sg_table *to) 97 { 98 struct scatterlist *sg, *new_sg; 99 int ret, i; 100 101 ret = sg_alloc_table(to, from->orig_nents, GFP_KERNEL); 102 if (ret) 103 return ret; 104 105 new_sg = to->sgl; 106 for_each_sgtable_sg(from, sg, i) { 107 sg_set_page(new_sg, sg_page(sg), sg->length, sg->offset); 108 new_sg = sg_next(new_sg); 109 } 110 111 return 0; 112 } 113 114 static int system_heap_attach(struct dma_buf *dmabuf, 115 struct dma_buf_attachment *attachment) 116 { 117 struct system_heap_buffer *buffer = dmabuf->priv; 118 struct dma_heap_attachment *a; 119 int ret; 120 121 a = kzalloc_obj(*a); 122 if (!a) 123 return -ENOMEM; 124 125 ret = dup_sg_table(&buffer->sg_table, &a->table); 126 if (ret) { 127 kfree(a); 128 return ret; 129 } 130 131 a->dev = attachment->dev; 132 INIT_LIST_HEAD(&a->list); 133 a->mapped = false; 134 a->cc_shared = buffer->cc_shared; 135 136 attachment->priv = a; 137 138 mutex_lock(&buffer->lock); 139 list_add(&a->list, &buffer->attachments); 140 mutex_unlock(&buffer->lock); 141 142 return 0; 143 } 144 145 static void system_heap_detach(struct dma_buf *dmabuf, 146 struct dma_buf_attachment *attachment) 147 { 148 struct system_heap_buffer *buffer = dmabuf->priv; 149 struct dma_heap_attachment *a = attachment->priv; 150 151 mutex_lock(&buffer->lock); 152 list_del(&a->list); 153 mutex_unlock(&buffer->lock); 154 155 sg_free_table(&a->table); 156 kfree(a); 157 } 158 159 static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment, 160 enum dma_data_direction direction) 161 { 162 struct dma_heap_attachment *a = attachment->priv; 163 struct sg_table *table = &a->table; 164 unsigned long attrs; 165 int ret; 166 167 attrs = cc_shared_buffer(a) ? DMA_ATTR_CC_SHARED : 0; 168 ret = dma_map_sgtable(attachment->dev, table, direction, attrs); 169 if (ret) 170 return ERR_PTR(ret); 171 172 a->mapped = true; 173 return table; 174 } 175 176 static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment, 177 struct sg_table *table, 178 enum dma_data_direction direction) 179 { 180 struct dma_heap_attachment *a = attachment->priv; 181 182 a->mapped = false; 183 dma_unmap_sgtable(attachment->dev, table, direction, 0); 184 } 185 186 static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, 187 enum dma_data_direction direction) 188 { 189 struct system_heap_buffer *buffer = dmabuf->priv; 190 struct dma_heap_attachment *a; 191 192 mutex_lock(&buffer->lock); 193 194 if (buffer->vmap_cnt) 195 invalidate_kernel_vmap_range(buffer->vaddr, buffer->len); 196 197 list_for_each_entry(a, &buffer->attachments, list) { 198 if (!a->mapped) 199 continue; 200 dma_sync_sgtable_for_cpu(a->dev, &a->table, direction); 201 } 202 mutex_unlock(&buffer->lock); 203 204 return 0; 205 } 206 207 static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf, 208 enum dma_data_direction direction) 209 { 210 struct system_heap_buffer *buffer = dmabuf->priv; 211 struct dma_heap_attachment *a; 212 213 mutex_lock(&buffer->lock); 214 215 if (buffer->vmap_cnt) 216 flush_kernel_vmap_range(buffer->vaddr, buffer->len); 217 218 list_for_each_entry(a, &buffer->attachments, list) { 219 if (!a->mapped) 220 continue; 221 dma_sync_sgtable_for_device(a->dev, &a->table, direction); 222 } 223 mutex_unlock(&buffer->lock); 224 225 return 0; 226 } 227 228 static int system_heap_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) 229 { 230 struct system_heap_buffer *buffer = dmabuf->priv; 231 struct sg_table *table = &buffer->sg_table; 232 unsigned long addr = vma->vm_start; 233 unsigned long pgoff = vma->vm_pgoff; 234 struct scatterlist *sg; 235 pgprot_t prot; 236 int i, ret; 237 238 prot = vma->vm_page_prot; 239 if (cc_shared_buffer(buffer)) 240 prot = pgprot_decrypted(prot); 241 242 for_each_sgtable_sg(table, sg, i) { 243 unsigned long n = sg->length >> PAGE_SHIFT; 244 245 if (pgoff < n) 246 break; 247 pgoff -= n; 248 } 249 250 for (; sg && addr < vma->vm_end; sg = sg_next(sg)) { 251 unsigned long n = (sg->length >> PAGE_SHIFT) - pgoff; 252 struct page *page = sg_page(sg) + pgoff; 253 unsigned long size = n << PAGE_SHIFT; 254 255 if (addr + size > vma->vm_end) 256 size = vma->vm_end - addr; 257 258 ret = remap_pfn_range(vma, addr, page_to_pfn(page), size, prot); 259 if (ret) 260 return ret; 261 262 addr += size; 263 pgoff = 0; 264 } 265 266 return 0; 267 } 268 269 static void *system_heap_do_vmap(struct system_heap_buffer *buffer) 270 { 271 struct sg_table *table = &buffer->sg_table; 272 int npages = PAGE_ALIGN(buffer->len) / PAGE_SIZE; 273 struct page **pages = vmalloc(sizeof(struct page *) * npages); 274 struct page **tmp = pages; 275 struct sg_page_iter piter; 276 pgprot_t prot; 277 void *vaddr; 278 279 if (!pages) 280 return ERR_PTR(-ENOMEM); 281 282 for_each_sgtable_page(table, &piter, 0) { 283 WARN_ON(tmp - pages >= npages); 284 *tmp++ = sg_page_iter_page(&piter); 285 } 286 287 prot = PAGE_KERNEL; 288 if (cc_shared_buffer(buffer)) 289 prot = pgprot_decrypted(prot); 290 vaddr = vmap(pages, npages, VM_MAP, prot); 291 vfree(pages); 292 293 if (!vaddr) 294 return ERR_PTR(-ENOMEM); 295 296 return vaddr; 297 } 298 299 static int system_heap_vmap(struct dma_buf *dmabuf, struct iosys_map *map) 300 { 301 struct system_heap_buffer *buffer = dmabuf->priv; 302 void *vaddr; 303 int ret = 0; 304 305 mutex_lock(&buffer->lock); 306 if (buffer->vmap_cnt) { 307 buffer->vmap_cnt++; 308 iosys_map_set_vaddr(map, buffer->vaddr); 309 goto out; 310 } 311 312 vaddr = system_heap_do_vmap(buffer); 313 if (IS_ERR(vaddr)) { 314 ret = PTR_ERR(vaddr); 315 goto out; 316 } 317 318 buffer->vaddr = vaddr; 319 buffer->vmap_cnt++; 320 iosys_map_set_vaddr(map, buffer->vaddr); 321 out: 322 mutex_unlock(&buffer->lock); 323 324 return ret; 325 } 326 327 static void system_heap_vunmap(struct dma_buf *dmabuf, struct iosys_map *map) 328 { 329 struct system_heap_buffer *buffer = dmabuf->priv; 330 331 mutex_lock(&buffer->lock); 332 if (!--buffer->vmap_cnt) { 333 vunmap(buffer->vaddr); 334 buffer->vaddr = NULL; 335 } 336 mutex_unlock(&buffer->lock); 337 iosys_map_clear(map); 338 } 339 340 static void system_heap_dma_buf_release(struct dma_buf *dmabuf) 341 { 342 struct system_heap_buffer *buffer = dmabuf->priv; 343 struct sg_table *table; 344 struct scatterlist *sg; 345 int i; 346 347 table = &buffer->sg_table; 348 for_each_sgtable_sg(table, sg, i) { 349 struct page *page = sg_page(sg); 350 351 /* 352 * Intentionally leak pages that cannot be re-encrypted 353 * to prevent shared memory from being reused. 354 */ 355 if (cc_shared_buffer(buffer) && 356 system_heap_set_page_encrypted(page)) 357 continue; 358 359 __free_pages(page, compound_order(page)); 360 } 361 sg_free_table(table); 362 kfree(buffer); 363 } 364 365 static const struct dma_buf_ops system_heap_buf_ops = { 366 .attach = system_heap_attach, 367 .detach = system_heap_detach, 368 .map_dma_buf = system_heap_map_dma_buf, 369 .unmap_dma_buf = system_heap_unmap_dma_buf, 370 .begin_cpu_access = system_heap_dma_buf_begin_cpu_access, 371 .end_cpu_access = system_heap_dma_buf_end_cpu_access, 372 .mmap = system_heap_mmap, 373 .vmap = system_heap_vmap, 374 .vunmap = system_heap_vunmap, 375 .release = system_heap_dma_buf_release, 376 }; 377 378 static struct page *alloc_largest_available(unsigned long size, 379 unsigned int max_order) 380 { 381 struct page *page; 382 int i; 383 gfp_t flags; 384 385 for (i = 0; i < NUM_ORDERS; i++) { 386 if (size < (PAGE_SIZE << orders[i])) 387 continue; 388 if (max_order < orders[i]) 389 continue; 390 flags = order_flags[i]; 391 if (mem_accounting) 392 flags |= __GFP_ACCOUNT; 393 page = alloc_pages(flags, orders[i]); 394 if (!page) 395 continue; 396 return page; 397 } 398 return NULL; 399 } 400 401 static struct dma_buf *system_heap_allocate(struct dma_heap *heap, 402 unsigned long len, 403 u32 fd_flags, 404 u64 heap_flags) 405 { 406 struct system_heap_buffer *buffer; 407 DEFINE_DMA_BUF_EXPORT_INFO(exp_info); 408 unsigned long size_remaining = len; 409 unsigned int max_order = orders[0]; 410 struct system_heap_priv *priv = dma_heap_get_drvdata(heap); 411 bool cc_shared = priv->cc_shared; 412 struct dma_buf *dmabuf; 413 struct sg_table *table; 414 struct scatterlist *sg; 415 struct list_head pages; 416 struct page *page, *tmp_page; 417 int i, ret = -ENOMEM; 418 419 buffer = kzalloc_obj(*buffer); 420 if (!buffer) 421 return ERR_PTR(-ENOMEM); 422 423 INIT_LIST_HEAD(&buffer->attachments); 424 mutex_init(&buffer->lock); 425 buffer->heap = heap; 426 buffer->len = len; 427 buffer->cc_shared = cc_shared; 428 429 INIT_LIST_HEAD(&pages); 430 i = 0; 431 while (size_remaining > 0) { 432 /* 433 * Avoid trying to allocate memory if the process 434 * has been killed by SIGKILL 435 */ 436 if (fatal_signal_pending(current)) { 437 ret = -EINTR; 438 goto free_buffer; 439 } 440 441 page = alloc_largest_available(size_remaining, max_order); 442 if (!page) 443 goto free_buffer; 444 445 list_add_tail(&page->lru, &pages); 446 size_remaining -= page_size(page); 447 max_order = compound_order(page); 448 i++; 449 } 450 451 table = &buffer->sg_table; 452 if (sg_alloc_table(table, i, GFP_KERNEL)) 453 goto free_buffer; 454 455 sg = table->sgl; 456 list_for_each_entry_safe(page, tmp_page, &pages, lru) { 457 sg_set_page(sg, page, page_size(page), 0); 458 sg = sg_next(sg); 459 list_del(&page->lru); 460 } 461 462 if (cc_shared_buffer(buffer)) { 463 for_each_sgtable_sg(table, sg, i) { 464 ret = system_heap_set_page_decrypted(sg_page(sg)); 465 if (ret) 466 goto free_pages; 467 } 468 } 469 470 /* create the dmabuf */ 471 exp_info.exp_name = dma_heap_get_name(heap); 472 exp_info.ops = &system_heap_buf_ops; 473 exp_info.size = buffer->len; 474 exp_info.flags = fd_flags; 475 exp_info.priv = buffer; 476 dmabuf = dma_buf_export(&exp_info); 477 if (IS_ERR(dmabuf)) { 478 ret = PTR_ERR(dmabuf); 479 goto free_pages; 480 } 481 return dmabuf; 482 483 free_pages: 484 for_each_sgtable_sg(table, sg, i) { 485 struct page *p = sg_page(sg); 486 487 /* 488 * Intentionally leak pages that cannot be re-encrypted 489 * to prevent shared memory from being reused. 490 */ 491 if (cc_shared_buffer(buffer) && 492 system_heap_set_page_encrypted(p)) 493 continue; 494 __free_pages(p, compound_order(p)); 495 } 496 sg_free_table(table); 497 free_buffer: 498 list_for_each_entry_safe(page, tmp_page, &pages, lru) 499 __free_pages(page, compound_order(page)); 500 kfree(buffer); 501 502 return ERR_PTR(ret); 503 } 504 505 static const struct dma_heap_ops system_heap_ops = { 506 .allocate = system_heap_allocate, 507 }; 508 509 static struct system_heap_priv system_heap_priv = { 510 .cc_shared = false, 511 }; 512 513 static struct system_heap_priv system_heap_cc_shared_priv = { 514 .cc_shared = true, 515 }; 516 517 static int __init system_heap_create(void) 518 { 519 struct dma_heap_export_info exp_info; 520 struct dma_heap *sys_heap; 521 522 exp_info.name = "system"; 523 exp_info.ops = &system_heap_ops; 524 exp_info.priv = &system_heap_priv; 525 526 sys_heap = dma_heap_add(&exp_info); 527 if (IS_ERR(sys_heap)) 528 return PTR_ERR(sys_heap); 529 530 if (IS_ENABLED(CONFIG_HIGHMEM) || 531 !IS_ENABLED(CONFIG_DMABUF_HEAPS_SYSTEM_CC_SHARED) || 532 !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) 533 return 0; 534 535 exp_info.name = "system_cc_shared"; 536 exp_info.priv = &system_heap_cc_shared_priv; 537 sys_heap = dma_heap_add(&exp_info); 538 if (IS_ERR(sys_heap)) 539 return PTR_ERR(sys_heap); 540 541 return 0; 542 } 543 module_init(system_heap_create); 544 545 MODULE_DESCRIPTION("DMA-BUF System Heap"); 546 MODULE_LICENSE("GPL"); 547 MODULE_IMPORT_NS("DMA_BUF"); 548 MODULE_IMPORT_NS("DMA_BUF_HEAP"); 549