TEE: improve shared buffer registration compatibility

-----BEGIN PGP SIGNATURE-----
 
 iQJOBAABCgA4FiEEFV+gSSXZJY9ZyuB5LinzTIcAHJcFAmV7AjYaHGplbnMud2lr
 bGFuZGVyQGxpbmFyby5vcmcACgkQLinzTIcAHJd5yA//STz2/LtQta0ucC9h1w3X
 +7mU8rWA2Ccmb7yQCyLGG9yXQNXzVn8bcRUsaLCU4YSKoLZdjeFgOD7dwz5uvtsN
 SwRVUoQpo02YZWjs+0WxKV+PuPBcJS6/Yiw8iomuk+3IE4I+V+hcJSiYLxfVZwWZ
 602VDtyEzkFgaLHQZkIf9F0uZKfzPY6F2VJYTD9bipFw3D6rDjIgWP89tuk5RgBi
 pcKBZry2F95/mesJFXGF6jlYcCpD1OZJZ9nk/SbyoVB/2koviAUnMMMfe9MQPDgs
 JSGx1ycF6DwShl5o4SyD36uS3gi93iaVzQpHF5XDRfhoX+pyvqAmt4hbOIjOgcj8
 mbTg39vYUVikOM741cYfkjvydIOwTi3jQrFmVBWSIHy529h4CJPmNXhFhSGYKm4Y
 SNIsZgGnU6VeKtgNgaQuSlg1/t/WwOIHtJWGtlRhj6g8PKWL1PIUK3ujCYM7ad8b
 fZA4eCwFFESrv3WmbGqAxjSZdb+pMlTk6/71j5d3MraLOM9KKOw5hcPgnnZrDyhg
 31GK8ZbqISkIX+m24wkBkjhD7DijRhx2C8qPZwAXC5SPUgZXcP3ps/hMkME7qcTl
 H9H/fe8R+ivb3FlyuS+tT9wRN6b0pK1yF2giVqyexDrEv8qgK/qlwmtZNlsYU9aZ
 yTUZmXfZZ+ZyFFIECFUog/4=
 =0HCH
 -----END PGP SIGNATURE-----
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEiK/NIGsWEZVxh/FrYKtH/8kJUicFAmWFdRsACgkQYKtH/8kJ
 Uic1vQ/+N9wpDXyDIgg3sJgh2O/tTOQOHISlSvbM8PBMjIHprCoAFQ3V9+tacqtk
 H0hNuJrJTf5+9OobUGHyj52yPR70Xy3eumpmWSXLrQWJ3fFmu+NtPQuDWXkKNJ8b
 Y3DRrgxW3OYDInNT4+KRIXdrPzbP1jICswRnd5gTFkULQSCwsCyZFxq9+Gln2cIU
 by5lGoGG3HcD1P6pl/ikRibQzKq5KR5E3LhvaMLjOQL11kl/gez74tWyW9o3yFpw
 AgZBnuP3hh5qV6EN1p84jYUYXUvAGQuIpvuw4ONMhXRxToR7aXeFNtqei9n+JeKo
 79ovvsZQJHa0Ro888fLqM0tk73MVtn4HX/GoxfcG9ujLvY42sH1Ml11w+2VfGmDS
 P+ovO21xwx1Glo5aXZoc0bUlIzOAJZlbrWGu3MF6qcstLM8QVRdLfZt5sZ/dxj7U
 hH79ZMwYHx2qlRU9x9Q05MnR6ik4YZ+KPwyMB+PhgaeX+r4HEn21qjX8EBqZiK23
 2u/DxRUjWNmnARPIym5xU6PRpUsfQLGUHl7qzT58FLazWDEU/5BbpXFWccwFzE3g
 gdzcFD3u/Uf5xQUSmkAHdqzFGTHXt/IxvGcvhiei7fFuv1798VDpbw3iiEqTHVCR
 Dj9D6GClwj5bqx3aK6c67CGSlEYa8XsuUpi+xJi6nNxCLtel20w=
 =v/vB
 -----END PGP SIGNATURE-----

Merge tag 'tee-iov-iter-for-v6.8' of https://git.linaro.org/people/jens.wiklander/linux-tee into soc/drivers

TEE: improve shared buffer registration compatibility

* tag 'tee-iov-iter-for-v6.8' of https://git.linaro.org/people/jens.wiklander/linux-tee:
  tee: Use iov_iter to better support shared buffer registration

Link: https://lore.kernel.org/r/20231214134139.GA3098718@rayden
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
This commit is contained in:
Arnd Bergmann 2023-12-22 11:38:02 +00:00
commit db0a7c09b2

View File

@ -22,23 +22,12 @@ static void shm_put_kernel_pages(struct page **pages, size_t page_count)
put_page(pages[n]);
}
static int shm_get_kernel_pages(unsigned long start, size_t page_count,
struct page **pages)
static void shm_get_kernel_pages(struct page **pages, size_t page_count)
{
struct page *page;
size_t n;
if (WARN_ON_ONCE(is_vmalloc_addr((void *)start) ||
is_kmap_addr((void *)start)))
return -EINVAL;
page = virt_to_page((void *)start);
for (n = 0; n < page_count; n++) {
pages[n] = page + n;
for (n = 0; n < page_count; n++)
get_page(pages[n]);
}
return page_count;
}
static void release_registered_pages(struct tee_shm *shm)
@ -214,13 +203,14 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
static struct tee_shm *
register_shm_helper(struct tee_context *ctx, unsigned long addr,
size_t length, u32 flags, int id)
register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
int id)
{
struct tee_device *teedev = ctx->teedev;
struct tee_shm *shm;
unsigned long start;
size_t num_pages;
unsigned long start, addr;
size_t num_pages, off;
ssize_t len;
void *ret;
int rc;
@ -245,31 +235,38 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr,
shm->flags = flags;
shm->ctx = ctx;
shm->id = id;
addr = untagged_addr(addr);
addr = untagged_addr((unsigned long)iter_iov_addr(iter));
start = rounddown(addr, PAGE_SIZE);
shm->offset = addr - start;
shm->size = length;
num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
num_pages = iov_iter_npages(iter, INT_MAX);
if (!num_pages) {
ret = ERR_PTR(-ENOMEM);
goto err_ctx_put;
}
shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
if (!shm->pages) {
ret = ERR_PTR(-ENOMEM);
goto err_free_shm;
}
if (flags & TEE_SHM_USER_MAPPED)
rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
shm->pages);
else
rc = shm_get_kernel_pages(start, num_pages, shm->pages);
if (rc > 0)
shm->num_pages = rc;
if (rc != num_pages) {
if (rc >= 0)
rc = -ENOMEM;
ret = ERR_PTR(rc);
goto err_put_shm_pages;
len = iov_iter_extract_pages(iter, &shm->pages, LONG_MAX, num_pages, 0,
&off);
if (unlikely(len <= 0)) {
ret = len ? ERR_PTR(len) : ERR_PTR(-ENOMEM);
goto err_free_shm_pages;
}
/*
* iov_iter_extract_kvec_pages does not get reference on the pages,
* get a reference on them.
*/
if (iov_iter_is_kvec(iter))
shm_get_kernel_pages(shm->pages, num_pages);
shm->offset = off;
shm->size = len;
shm->num_pages = num_pages;
rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
shm->num_pages, start);
if (rc) {
@ -279,10 +276,11 @@ register_shm_helper(struct tee_context *ctx, unsigned long addr,
return shm;
err_put_shm_pages:
if (flags & TEE_SHM_USER_MAPPED)
if (!iov_iter_is_kvec(iter))
unpin_user_pages(shm->pages, shm->num_pages);
else
shm_put_kernel_pages(shm->pages, shm->num_pages);
err_free_shm_pages:
kfree(shm->pages);
err_free_shm:
kfree(shm);
@ -307,6 +305,7 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
u32 flags = TEE_SHM_USER_MAPPED | TEE_SHM_DYNAMIC;
struct tee_device *teedev = ctx->teedev;
struct tee_shm *shm;
struct iov_iter iter;
void *ret;
int id;
@ -319,7 +318,8 @@ struct tee_shm *tee_shm_register_user_buf(struct tee_context *ctx,
if (id < 0)
return ERR_PTR(id);
shm = register_shm_helper(ctx, addr, length, flags, id);
iov_iter_ubuf(&iter, ITER_DEST, (void __user *)addr, length);
shm = register_shm_helper(ctx, &iter, flags, id);
if (IS_ERR(shm)) {
mutex_lock(&teedev->mutex);
idr_remove(&teedev->idr, id);
@ -352,8 +352,14 @@ struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
void *addr, size_t length)
{
u32 flags = TEE_SHM_DYNAMIC;
struct kvec kvec;
struct iov_iter iter;
return register_shm_helper(ctx, (unsigned long)addr, length, flags, -1);
kvec.iov_base = addr;
kvec.iov_len = length;
iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, length);
return register_shm_helper(ctx, &iter, flags, -1);
}
EXPORT_SYMBOL_GPL(tee_shm_register_kernel_buf);