Blame view
drivers/tee/tee_shm.c
11 KB
9c92ab619 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
967c9cca2 tee: generic TEE ... |
2 3 |
/* * Copyright (c) 2015-2016, Linaro Limited |
967c9cca2 tee: generic TEE ... |
4 5 6 7 8 9 10 11 |
*/ #include <linux/device.h> #include <linux/dma-buf.h> #include <linux/fdtable.h> #include <linux/idr.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/tee_drv.h> |
2a6ba3f79 tee: enable suppo... |
12 |
#include <linux/uio.h> |
967c9cca2 tee: generic TEE ... |
13 |
#include "tee_private.h" |
4300cd637 tee: convert get_... |
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
static void release_registered_pages(struct tee_shm *shm) { if (shm->pages) { if (shm->flags & TEE_SHM_USER_MAPPED) { unpin_user_pages(shm->pages, shm->num_pages); } else { size_t n; for (n = 0; n < shm->num_pages; n++) put_page(shm->pages[n]); } kfree(shm->pages); } } |
967c9cca2 tee: generic TEE ... |
29 30 |
static void tee_shm_release(struct tee_shm *shm) { |
5271b2011 tee: remove redun... |
31 |
struct tee_device *teedev = shm->ctx->teedev; |
967c9cca2 tee: generic TEE ... |
32 |
|
f1bbacedb tee: don't assign... |
33 34 35 36 37 |
if (shm->flags & TEE_SHM_DMA_BUF) { mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, shm->id); mutex_unlock(&teedev->mutex); } |
967c9cca2 tee: generic TEE ... |
38 |
|
033ddf12b tee: add register... |
39 40 41 42 43 44 45 46 47 48 |
if (shm->flags & TEE_SHM_POOL) { struct tee_shm_pool_mgr *poolm; if (shm->flags & TEE_SHM_DMA_BUF) poolm = teedev->pool->dma_buf_mgr; else poolm = teedev->pool->private_mgr; poolm->ops->free(poolm, shm); } else if (shm->flags & TEE_SHM_REGISTER) { |
033ddf12b tee: add register... |
49 50 51 52 53 |
int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm); if (rc) dev_err(teedev->dev.parent, "unregister shm %p failed: %d", shm, rc); |
4300cd637 tee: convert get_... |
54 |
release_registered_pages(shm); |
033ddf12b tee: add register... |
55 |
} |
967c9cca2 tee: generic TEE ... |
56 |
|
5271b2011 tee: remove redun... |
57 |
teedev_ctx_put(shm->ctx); |
217e0250c tee: use referenc... |
58 |
|
967c9cca2 tee: generic TEE ... |
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
kfree(shm); tee_device_put(teedev); } static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment *attach, enum dma_data_direction dir) { return NULL; } static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach, struct sg_table *table, enum dma_data_direction dir) { } static void tee_shm_op_release(struct dma_buf *dmabuf) { struct tee_shm *shm = dmabuf->priv; tee_shm_release(shm); } |
967c9cca2 tee: generic TEE ... |
82 83 84 85 |
static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) { struct tee_shm *shm = dmabuf->priv; size_t size = vma->vm_end - vma->vm_start; |
033ddf12b tee: add register... |
86 |
/* Refuse sharing shared memory provided by application */ |
758ecf13a tee: tee_shm_op_m... |
87 |
if (shm->flags & TEE_SHM_USER_MAPPED) |
033ddf12b tee: add register... |
88 |
return -EINVAL; |
967c9cca2 tee: generic TEE ... |
89 90 91 |
return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT, size, vma->vm_page_prot); } |
53e3ca5ce tee: tee_shm: Con... |
92 |
static const struct dma_buf_ops tee_shm_dma_buf_ops = { |
967c9cca2 tee: generic TEE ... |
93 94 95 |
.map_dma_buf = tee_shm_op_map_dma_buf, .unmap_dma_buf = tee_shm_op_unmap_dma_buf, .release = tee_shm_op_release, |
967c9cca2 tee: generic TEE ... |
96 97 |
.mmap = tee_shm_op_mmap, }; |
c180f9bbe tee: remove unuse... |
98 |
struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) |
967c9cca2 tee: generic TEE ... |
99 |
{ |
c180f9bbe tee: remove unuse... |
100 |
struct tee_device *teedev = ctx->teedev; |
967c9cca2 tee: generic TEE ... |
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
struct tee_shm_pool_mgr *poolm = NULL; struct tee_shm *shm; void *ret; int rc; if (!(flags & TEE_SHM_MAPPED)) { dev_err(teedev->dev.parent, "only mapped allocations supported "); return ERR_PTR(-EINVAL); } if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) { dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags); return ERR_PTR(-EINVAL); } if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); if (!teedev->pool) { /* teedev has been detached from driver */ ret = ERR_PTR(-EINVAL); goto err_dev_put; } shm = kzalloc(sizeof(*shm), GFP_KERNEL); if (!shm) { ret = ERR_PTR(-ENOMEM); goto err_dev_put; } |
033ddf12b tee: add register... |
132 |
shm->flags = flags | TEE_SHM_POOL; |
967c9cca2 tee: generic TEE ... |
133 134 |
shm->ctx = ctx; if (flags & TEE_SHM_DMA_BUF) |
e2aca5d89 tee: flexible sha... |
135 |
poolm = teedev->pool->dma_buf_mgr; |
967c9cca2 tee: generic TEE ... |
136 |
else |
e2aca5d89 tee: flexible sha... |
137 |
poolm = teedev->pool->private_mgr; |
967c9cca2 tee: generic TEE ... |
138 139 140 141 142 143 |
rc = poolm->ops->alloc(poolm, shm, size); if (rc) { ret = ERR_PTR(rc); goto err_kfree; } |
967c9cca2 tee: generic TEE ... |
144 145 146 |
if (flags & TEE_SHM_DMA_BUF) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
f1bbacedb tee: don't assign... |
147 148 149 150 151 152 153 |
mutex_lock(&teedev->mutex); shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); mutex_unlock(&teedev->mutex); if (shm->id < 0) { ret = ERR_PTR(shm->id); goto err_pool_free; } |
967c9cca2 tee: generic TEE ... |
154 155 156 157 158 159 160 161 162 163 164 |
exp_info.ops = &tee_shm_dma_buf_ops; exp_info.size = shm->size; exp_info.flags = O_RDWR; exp_info.priv = shm; shm->dmabuf = dma_buf_export(&exp_info); if (IS_ERR(shm->dmabuf)) { ret = ERR_CAST(shm->dmabuf); goto err_rem; } } |
033ddf12b tee: add register... |
165 |
|
1115899e7 tee: remove unnec... |
166 |
teedev_ctx_get(ctx); |
967c9cca2 tee: generic TEE ... |
167 168 169 |
return shm; err_rem: |
f1bbacedb tee: don't assign... |
170 171 172 173 174 |
if (flags & TEE_SHM_DMA_BUF) { mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, shm->id); mutex_unlock(&teedev->mutex); } |
967c9cca2 tee: generic TEE ... |
175 176 177 178 179 180 181 182 183 |
err_pool_free: poolm->ops->free(poolm, shm); err_kfree: kfree(shm); err_dev_put: tee_device_put(teedev); return ret; } EXPORT_SYMBOL_GPL(tee_shm_alloc); |
033ddf12b tee: add register... |
184 185 186 187 |
struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, size_t length, u32 flags) { struct tee_device *teedev = ctx->teedev; |
2a6ba3f79 tee: enable suppo... |
188 189 |
const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED; const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED; |
033ddf12b tee: add register... |
190 191 192 193 194 |
struct tee_shm *shm; void *ret; int rc; int num_pages; unsigned long start; |
2a6ba3f79 tee: enable suppo... |
195 |
if (flags != req_user_flags && flags != req_kernel_flags) |
033ddf12b tee: add register... |
196 197 198 199 200 201 202 203 204 205 |
return ERR_PTR(-ENOTSUPP); if (!tee_device_get(teedev)) return ERR_PTR(-EINVAL); if (!teedev->desc->ops->shm_register || !teedev->desc->ops->shm_unregister) { tee_device_put(teedev); return ERR_PTR(-ENOTSUPP); } |
217e0250c tee: use referenc... |
206 |
teedev_ctx_get(ctx); |
033ddf12b tee: add register... |
207 208 209 210 211 212 213 |
shm = kzalloc(sizeof(*shm), GFP_KERNEL); if (!shm) { ret = ERR_PTR(-ENOMEM); goto err; } shm->flags = flags | TEE_SHM_REGISTER; |
033ddf12b tee: add register... |
214 215 |
shm->ctx = ctx; shm->id = -1; |
78063a9dd tee/shm: untag us... |
216 |
addr = untagged_addr(addr); |
033ddf12b tee: add register... |
217 218 219 220 221 222 223 224 225 |
start = rounddown(addr, PAGE_SIZE); shm->offset = addr - start; shm->size = length; num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE; shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); if (!shm->pages) { ret = ERR_PTR(-ENOMEM); goto err; } |
2a6ba3f79 tee: enable suppo... |
226 |
if (flags & TEE_SHM_USER_MAPPED) { |
4300cd637 tee: convert get_... |
227 |
rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE, |
2a6ba3f79 tee: enable suppo... |
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
shm->pages); } else { struct kvec *kiov; int i; kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL); if (!kiov) { ret = ERR_PTR(-ENOMEM); goto err; } for (i = 0; i < num_pages; i++) { kiov[i].iov_base = (void *)(start + i * PAGE_SIZE); kiov[i].iov_len = PAGE_SIZE; } rc = get_kernel_pages(kiov, num_pages, 0, shm->pages); kfree(kiov); } |
033ddf12b tee: add register... |
247 248 249 |
if (rc > 0) shm->num_pages = rc; if (rc != num_pages) { |
2490cdf64 tee: shm: Potenti... |
250 |
if (rc >= 0) |
033ddf12b tee: add register... |
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
rc = -ENOMEM; ret = ERR_PTR(rc); goto err; } mutex_lock(&teedev->mutex); shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); mutex_unlock(&teedev->mutex); if (shm->id < 0) { ret = ERR_PTR(shm->id); goto err; } rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages, |
95ffe4ca4 tee: add start ar... |
266 |
shm->num_pages, start); |
033ddf12b tee: add register... |
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
if (rc) { ret = ERR_PTR(rc); goto err; } if (flags & TEE_SHM_DMA_BUF) { DEFINE_DMA_BUF_EXPORT_INFO(exp_info); exp_info.ops = &tee_shm_dma_buf_ops; exp_info.size = shm->size; exp_info.flags = O_RDWR; exp_info.priv = shm; shm->dmabuf = dma_buf_export(&exp_info); if (IS_ERR(shm->dmabuf)) { ret = ERR_CAST(shm->dmabuf); teedev->desc->ops->shm_unregister(ctx, shm); goto err; } } |
033ddf12b tee: add register... |
287 288 289 |
return shm; err: if (shm) { |
033ddf12b tee: add register... |
290 291 292 293 294 |
if (shm->id >= 0) { mutex_lock(&teedev->mutex); idr_remove(&teedev->idr, shm->id); mutex_unlock(&teedev->mutex); } |
4300cd637 tee: convert get_... |
295 |
release_registered_pages(shm); |
033ddf12b tee: add register... |
296 297 |
} kfree(shm); |
217e0250c tee: use referenc... |
298 |
teedev_ctx_put(ctx); |
033ddf12b tee: add register... |
299 300 301 302 |
tee_device_put(teedev); return ret; } EXPORT_SYMBOL_GPL(tee_shm_register); |
967c9cca2 tee: generic TEE ... |
303 304 305 306 307 308 309 |
/** * tee_shm_get_fd() - Increase reference count and return file descriptor * @shm: Shared memory handle * @returns user space file descriptor to shared memory */ int tee_shm_get_fd(struct tee_shm *shm) { |
967c9cca2 tee: generic TEE ... |
310 |
int fd; |
033ddf12b tee: add register... |
311 |
if (!(shm->flags & TEE_SHM_DMA_BUF)) |
967c9cca2 tee: generic TEE ... |
312 |
return -EINVAL; |
bb765d1c3 tee: shm: fix use... |
313 |
get_dma_buf(shm->dmabuf); |
967c9cca2 tee: generic TEE ... |
314 |
fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC); |
bb765d1c3 tee: shm: fix use... |
315 316 |
if (fd < 0) dma_buf_put(shm->dmabuf); |
967c9cca2 tee: generic TEE ... |
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
return fd; } /** * tee_shm_free() - Free shared memory * @shm: Handle to shared memory to free */ void tee_shm_free(struct tee_shm *shm) { /* * dma_buf_put() decreases the dmabuf reference counter and will * call tee_shm_release() when the last reference is gone. * * In the case of driver private memory we call tee_shm_release * directly instead as it doesn't have a reference counter. */ if (shm->flags & TEE_SHM_DMA_BUF) dma_buf_put(shm->dmabuf); else tee_shm_release(shm); } EXPORT_SYMBOL_GPL(tee_shm_free); /** * tee_shm_va2pa() - Get physical address of a virtual address * @shm: Shared memory handle * @va: Virtual address to tranlsate * @pa: Returned physical address * @returns 0 on success and < 0 on failure */ int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa) { |
033ddf12b tee: add register... |
349 350 |
if (!(shm->flags & TEE_SHM_MAPPED)) return -EINVAL; |
967c9cca2 tee: generic TEE ... |
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
/* Check that we're in the range of the shm */ if ((char *)va < (char *)shm->kaddr) return -EINVAL; if ((char *)va >= ((char *)shm->kaddr + shm->size)) return -EINVAL; return tee_shm_get_pa( shm, (unsigned long)va - (unsigned long)shm->kaddr, pa); } EXPORT_SYMBOL_GPL(tee_shm_va2pa); /** * tee_shm_pa2va() - Get virtual address of a physical address * @shm: Shared memory handle * @pa: Physical address to tranlsate * @va: Returned virtual address * @returns 0 on success and < 0 on failure */ int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va) { |
033ddf12b tee: add register... |
371 372 |
if (!(shm->flags & TEE_SHM_MAPPED)) return -EINVAL; |
967c9cca2 tee: generic TEE ... |
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
/* Check that we're in the range of the shm */ if (pa < shm->paddr) return -EINVAL; if (pa >= (shm->paddr + shm->size)) return -EINVAL; if (va) { void *v = tee_shm_get_va(shm, pa - shm->paddr); if (IS_ERR(v)) return PTR_ERR(v); *va = v; } return 0; } EXPORT_SYMBOL_GPL(tee_shm_pa2va); /** * tee_shm_get_va() - Get virtual address of a shared memory plus an offset * @shm: Shared memory handle * @offs: Offset from start of this shared memory * @returns virtual address of the shared memory + offs if offs is within * the bounds of this shared memory, else an ERR_PTR */ void *tee_shm_get_va(struct tee_shm *shm, size_t offs) { |
033ddf12b tee: add register... |
399 400 |
if (!(shm->flags & TEE_SHM_MAPPED)) return ERR_PTR(-EINVAL); |
967c9cca2 tee: generic TEE ... |
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
if (offs >= shm->size) return ERR_PTR(-EINVAL); return (char *)shm->kaddr + offs; } EXPORT_SYMBOL_GPL(tee_shm_get_va); /** * tee_shm_get_pa() - Get physical address of a shared memory plus an offset * @shm: Shared memory handle * @offs: Offset from start of this shared memory * @pa: Physical address to return * @returns 0 if offs is within the bounds of this shared memory, else an * error code. */ int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa) { if (offs >= shm->size) return -EINVAL; if (pa) *pa = shm->paddr + offs; return 0; } EXPORT_SYMBOL_GPL(tee_shm_get_pa); /** * tee_shm_get_from_id() - Find shared memory object and increase reference * count * @ctx: Context owning the shared memory * @id: Id of shared memory object * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure */ struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id) { struct tee_device *teedev; struct tee_shm *shm; if (!ctx) return ERR_PTR(-EINVAL); teedev = ctx->teedev; mutex_lock(&teedev->mutex); shm = idr_find(&teedev->idr, id); if (!shm || shm->ctx != ctx) shm = ERR_PTR(-EINVAL); else if (shm->flags & TEE_SHM_DMA_BUF) get_dma_buf(shm->dmabuf); mutex_unlock(&teedev->mutex); return shm; } EXPORT_SYMBOL_GPL(tee_shm_get_from_id); /** |
967c9cca2 tee: generic TEE ... |
453 454 455 456 457 458 459 460 461 |
* tee_shm_put() - Decrease reference count on a shared memory handle * @shm: Shared memory handle */ void tee_shm_put(struct tee_shm *shm) { if (shm->flags & TEE_SHM_DMA_BUF) dma_buf_put(shm->dmabuf); } EXPORT_SYMBOL_GPL(tee_shm_put); |