Skip to content

Commit cf44415

Browse files
balint-dobszay-armjenswi-linaro
authored andcommitted
tee: optee: Move pool_op helper functions
Move the pool alloc and free helper functions from the OP-TEE driver to the TEE subsystem, since these could be reused in other TEE drivers. This patch is not supposed to change behavior, it's only reorganizing the code. Reviewed-by: Sumit Garg <[email protected]> Suggested-by: Jens Wiklander <[email protected]> Signed-off-by: Balint Dobszay <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent 0439fcf commit cf44415

File tree

6 files changed

+82
-84
lines changed

6 files changed

+82
-84
lines changed

drivers/tee/optee/core.c

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,13 @@
99
#include <linux/crash_dump.h>
1010
#include <linux/errno.h>
1111
#include <linux/io.h>
12-
#include <linux/mm.h>
1312
#include <linux/module.h>
1413
#include <linux/slab.h>
1514
#include <linux/string.h>
1615
#include <linux/tee_core.h>
1716
#include <linux/types.h>
1817
#include "optee_private.h"
1918

20-
int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
21-
size_t size, size_t align,
22-
int (*shm_register)(struct tee_context *ctx,
23-
struct tee_shm *shm,
24-
struct page **pages,
25-
size_t num_pages,
26-
unsigned long start))
27-
{
28-
size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
29-
struct page **pages;
30-
unsigned int i;
31-
int rc = 0;
32-
33-
/*
34-
* Ignore alignment since this is already going to be page aligned
35-
* and there's no need for any larger alignment.
36-
*/
37-
shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
38-
GFP_KERNEL | __GFP_ZERO);
39-
if (!shm->kaddr)
40-
return -ENOMEM;
41-
42-
shm->paddr = virt_to_phys(shm->kaddr);
43-
shm->size = nr_pages * PAGE_SIZE;
44-
45-
pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
46-
if (!pages) {
47-
rc = -ENOMEM;
48-
goto err;
49-
}
50-
51-
for (i = 0; i < nr_pages; i++)
52-
pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
53-
54-
shm->pages = pages;
55-
shm->num_pages = nr_pages;
56-
57-
if (shm_register) {
58-
rc = shm_register(shm->ctx, shm, pages, nr_pages,
59-
(unsigned long)shm->kaddr);
60-
if (rc)
61-
goto err;
62-
}
63-
64-
return 0;
65-
err:
66-
free_pages_exact(shm->kaddr, shm->size);
67-
shm->kaddr = NULL;
68-
return rc;
69-
}
70-
71-
void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
72-
int (*shm_unregister)(struct tee_context *ctx,
73-
struct tee_shm *shm))
74-
{
75-
if (shm_unregister)
76-
shm_unregister(shm->ctx, shm);
77-
free_pages_exact(shm->kaddr, shm->size);
78-
shm->kaddr = NULL;
79-
kfree(shm->pages);
80-
shm->pages = NULL;
81-
}
82-
8319
static void optee_bus_scan(struct work_struct *work)
8420
{
8521
WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));

drivers/tee/optee/ffa_abi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
374374
static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
375375
struct tee_shm *shm, size_t size, size_t align)
376376
{
377-
return optee_pool_op_alloc_helper(pool, shm, size, align,
378-
optee_ffa_shm_register);
377+
return tee_dyn_shm_alloc_helper(shm, size, align,
378+
optee_ffa_shm_register);
379379
}
380380

381381
static void pool_ffa_op_free(struct tee_shm_pool *pool,
382382
struct tee_shm *shm)
383383
{
384-
optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister);
384+
tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
385385
}
386386

387387
static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)

drivers/tee/optee/optee_private.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
283283
int optee_enumerate_devices(u32 func);
284284
void optee_unregister_devices(void);
285285

286-
int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
287-
size_t size, size_t align,
288-
int (*shm_register)(struct tee_context *ctx,
289-
struct tee_shm *shm,
290-
struct page **pages,
291-
size_t num_pages,
292-
unsigned long start));
293-
void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
294-
int (*shm_unregister)(struct tee_context *ctx,
295-
struct tee_shm *shm));
296-
297-
298286
void optee_remove_common(struct optee *optee);
299287
int optee_open(struct tee_context *ctx, bool cap_memref_null);
300288
void optee_release(struct tee_context *ctx);

drivers/tee/optee/smc_abi.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,18 @@ static int pool_op_alloc(struct tee_shm_pool *pool,
592592
* to be registered with OP-TEE.
593593
*/
594594
if (shm->flags & TEE_SHM_PRIV)
595-
return optee_pool_op_alloc_helper(pool, shm, size, align, NULL);
595+
return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
596596

597-
return optee_pool_op_alloc_helper(pool, shm, size, align,
598-
optee_shm_register);
597+
return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
599598
}
600599

601600
static void pool_op_free(struct tee_shm_pool *pool,
602601
struct tee_shm *shm)
603602
{
604603
if (!(shm->flags & TEE_SHM_PRIV))
605-
optee_pool_op_free_helper(pool, shm, optee_shm_unregister);
604+
tee_dyn_shm_free_helper(shm, optee_shm_unregister);
606605
else
607-
optee_pool_op_free_helper(pool, shm, NULL);
606+
tee_dyn_shm_free_helper(shm, NULL);
608607
}
609608

610609
static void pool_op_destroy_pool(struct tee_shm_pool *pool)

drivers/tee/tee_shm.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/anon_inodes.h>
66
#include <linux/device.h>
77
#include <linux/idr.h>
8+
#include <linux/io.h>
89
#include <linux/mm.h>
910
#include <linux/sched.h>
1011
#include <linux/slab.h>
@@ -202,6 +203,70 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
202203
}
203204
EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
204205

206+
int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
207+
int (*shm_register)(struct tee_context *ctx,
208+
struct tee_shm *shm,
209+
struct page **pages,
210+
size_t num_pages,
211+
unsigned long start))
212+
{
213+
size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
214+
struct page **pages;
215+
unsigned int i;
216+
int rc = 0;
217+
218+
/*
219+
* Ignore alignment since this is already going to be page aligned
220+
* and there's no need for any larger alignment.
221+
*/
222+
shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
223+
GFP_KERNEL | __GFP_ZERO);
224+
if (!shm->kaddr)
225+
return -ENOMEM;
226+
227+
shm->paddr = virt_to_phys(shm->kaddr);
228+
shm->size = nr_pages * PAGE_SIZE;
229+
230+
pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
231+
if (!pages) {
232+
rc = -ENOMEM;
233+
goto err;
234+
}
235+
236+
for (i = 0; i < nr_pages; i++)
237+
pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
238+
239+
shm->pages = pages;
240+
shm->num_pages = nr_pages;
241+
242+
if (shm_register) {
243+
rc = shm_register(shm->ctx, shm, pages, nr_pages,
244+
(unsigned long)shm->kaddr);
245+
if (rc)
246+
goto err;
247+
}
248+
249+
return 0;
250+
err:
251+
free_pages_exact(shm->kaddr, shm->size);
252+
shm->kaddr = NULL;
253+
return rc;
254+
}
255+
EXPORT_SYMBOL_GPL(tee_dyn_shm_alloc_helper);
256+
257+
void tee_dyn_shm_free_helper(struct tee_shm *shm,
258+
int (*shm_unregister)(struct tee_context *ctx,
259+
struct tee_shm *shm))
260+
{
261+
if (shm_unregister)
262+
shm_unregister(shm->ctx, shm);
263+
free_pages_exact(shm->kaddr, shm->size);
264+
shm->kaddr = NULL;
265+
kfree(shm->pages);
266+
shm->pages = NULL;
267+
}
268+
EXPORT_SYMBOL_GPL(tee_dyn_shm_free_helper);
269+
205270
static struct tee_shm *
206271
register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
207272
int id)

include/linux/tee_core.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ void *tee_get_drvdata(struct tee_device *teedev);
232232
*/
233233
struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
234234

235+
int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
236+
int (*shm_register)(struct tee_context *ctx,
237+
struct tee_shm *shm,
238+
struct page **pages,
239+
size_t num_pages,
240+
unsigned long start));
241+
void tee_dyn_shm_free_helper(struct tee_shm *shm,
242+
int (*shm_unregister)(struct tee_context *ctx,
243+
struct tee_shm *shm));
244+
235245
/**
236246
* tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
237247
* @shm: Shared memory handle

0 commit comments

Comments
 (0)