Skip to contentSkip to conversation

tee: convert get_user_pages() --> pin_user_pages()

PATCH Superseded John Hubbard 2 participants 2 messages May 25, 2020 – May 26, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tee: convert get_user_pages() --> pin_user_pages()

This code was using get_user_pages*(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages*() + put_page() calls to pin_user_pages*() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

    https://lwn.net/Articles/807108/

---

Hi,

This fixes the typo ("convert convert") in the subject line, but otherwise no changes.

thanks,
John Hubbard
NVIDIA

 drivers/tee/tee_shm.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/tee/tee_shm.c +3 -9
index bd679b72bd05..7dffc42d8d5a 100644
@@ -31,16 +31,13 @@ static void tee_shm_release(struct tee_shm *shm)
 
 		poolm->ops->free(poolm, shm);
 	} else if (shm->flags & TEE_SHM_REGISTER) {
-		size_t n;
 		int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
 
 		if (rc)
 			dev_err(teedev->dev.parent,
 				"unregister shm %p failed: %d", shm, rc);
 
-		for (n = 0; n < shm->num_pages; n++)
-			put_page(shm->pages[n]);
-
+		unpin_user_pages(shm->pages, shm->num_pages);
 		kfree(shm->pages);
 	}
 
@@ -226,7 +223,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 		goto err;
 	}
 
-	rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
+	rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
 	if (rc > 0)
 		shm->num_pages = rc;
 	if (rc != num_pages) {
@@ -271,16 +268,13 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
 	return shm;
 err:
 	if (shm) {
-		size_t n;
-
 		if (shm->id >= 0) {
 			mutex_lock(&teedev->mutex);
 			idr_remove(&teedev->idr, shm->id);
 			mutex_unlock(&teedev->mutex);
 		}
 		if (shm->pages) {
-			for (n = 0; n < shm->num_pages; n++)
-				put_page(shm->pages[n]);
+			unpin_user_pages(shm->pages, shm->num_pages);
 			kfree(shm->pages);
 		}
 	}
Signature · 1 line
2.26.2
#1 lore raw

Hi John,

Quoted 29 lines · John Hubbard

This code was using get_user_pages*(), in a "Case 2" scenario (DMA/RDMA), using the categorization from [1]. That means that it's time to convert the get_user_pages*() + put_page() calls to pin_user_pages*() + unpin_user_pages() calls.

There is some helpful background in [2]: basically, this is a small part of fixing a long-standing disconnect between pinning pages, and file systems' use of those pages.

[1] Documentation/core-api/pin_user_pages.rst

[2] "Explicit pinning of user-space pages":

    https://lwn.net/Articles/807108/

---

Hi,

This fixes the typo ("convert convert") in the subject line, but otherwise no changes.

Quoted 7 lines

thanks,
John Hubbard
NVIDIA

 drivers/tee/tee_shm.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

Looks good. I've tested this on a HiKey 620 board, no regressions. I'm picking up this patch.

Thanks,
Jens

Quoted 53 lines
drivers/tee/tee_shm.c +3 -10
index bd679b72bd05..7dffc42d8d5a 100644
@@ -31,16 +31,13 @@ static void tee_shm_release(struct tee_shm *shm)
 
                poolm->ops->free(poolm, shm);
        } else if (shm->flags & TEE_SHM_REGISTER) {
-               size_t n;
                int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
 
                if (rc)
                        dev_err(teedev->dev.parent,
                                "unregister shm %p failed: %d", shm, rc);
 
-               for (n = 0; n < shm->num_pages; n++)
-                       put_page(shm->pages[n]);
-
+               unpin_user_pages(shm->pages, shm->num_pages);
                kfree(shm->pages);
        }
 
@@ -226,7 +223,7 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
                goto err;
        }
 
-       rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
+       rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
        if (rc > 0)
                shm->num_pages = rc;
        if (rc != num_pages) {
@@ -271,16 +268,13 @@ struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
        return shm;
 err:
        if (shm) {
-               size_t n;
-
                if (shm->id >= 0) {
                        mutex_lock(&teedev->mutex);
                        idr_remove(&teedev->idr, shm->id);
                        mutex_unlock(&teedev->mutex);
                }
                if (shm->pages) {
-                       for (n = 0; n < shm->num_pages; n++)
-                               put_page(shm->pages[n]);
+                       unpin_user_pages(shm->pages, shm->num_pages);
                        kfree(shm->pages);
                }
        }
--

2.26.2