Skip to contentSkip to conversation

binder: Use kmap_local_page() in binder_alloc.c

PATCH In review Fabio M. De Francesco 2 participants 5 messages Apr 25, 2022 – May 1, 2022 Android drivers

lore
Share & tools
lore
#0 lore raw

v2 cover binder: Use kmap_local_page() in binder_alloc.c

Use kmap_local_page() in binder_alloc.c because kmap() and kmap_atomic() are being deprecated and kmap_local_page() is preferred where it is feasible.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Furthermore, the mapping can be acquired from any context, including interrupts.

Fabio M. De Francesco (3):
binder: Use memset_page() in binder_alloc_clear_buf()
binder: Use kmap_local_page() in binder_alloc_copy_user_to_buffer()
binder: Use memcpy_{to,from}_page() in binder_alloc_do_buffer_copy()

 drivers/android/binder_alloc.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/3 binder: Use memset_page() in binder_alloc_clear_buf()

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. With kmap_local_page(), the mapping is per thread, CPU local and not globally visible.

binder_alloc_clear_buf() is a function where the use of kmap_local_page() in place of kmap() is correctly suited because the mapping is local to the thread.

Therefore, use kmap_local_page() / kunmap_local() but, instead of open coding these two functions and adding a memset() of the virtual address of the mapping, prefer memset_page().

---

v1->v2: Add Todd Kjos's tag in the commit message (thanks!)

 drivers/android/binder_alloc.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/android/binder_alloc.c +1 -4
index 2ac1008a5f39..0b3f2f569053 100644
@@ -1175,14 +1175,11 @@ static void binder_alloc_clear_buf(struct binder_alloc *alloc,
 		unsigned long size;
 		struct page *page;
 		pgoff_t pgoff;
-		void *kptr;
 
 		page = binder_alloc_get_page(alloc, buffer,
 					     buffer_offset, &pgoff);
 		size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
-		kptr = kmap(page) + pgoff;
-		memset(kptr, 0, size);
-		kunmap(page);
+		memset_page(page, pgoff, 0, size);
 		bytes -= size;
 		buffer_offset += size;
 	}
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/3 binder: Use kmap_local_page() in binder_alloc_copy_user_to_buffer()

In reply to Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. With kmap_local_page(), the mapping is per thread, CPU local and not globally visible.

binder_alloc_copy_user_to_buffer() is a function where the use of kmap_local_page() in place of kmap() is correctly suited because the mapping is local to the thread.

Therefore, use kmap_local_page() / kunmap_local().

---

v1->v2: Add Todd Kjos's tag in the commit message (thanks!).

 drivers/android/binder_alloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/android/binder_alloc.c +2 -2
index 0b3f2f569053..0875c463c002 100644
@@ -1217,9 +1217,9 @@ binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
 		page = binder_alloc_get_page(alloc, buffer,
 					     buffer_offset, &pgoff);
 		size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
-		kptr = kmap(page) + pgoff;
+		kptr = kmap_local_page(page) + pgoff;
 		ret = copy_from_user(kptr, from, size);
-		kunmap(page);
+		kunmap_local(kptr);
 		if (ret)
 			return bytes - size + ret;
 		bytes -= size;
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/3 binder: Use memcpy_{to,from}_page() in binder_alloc_do_buffer_copy()

In reply to Fabio M. De Francesco

The use of kmap_atomic() is being deprecated in favor of kmap_local_page() where it is feasible. Each call of kmap_atomic() in the kernel creates a non-preemptible section and disable pagefaults. This could be a source of unwanted latency, so kmap_local_page() should be preferred.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Furthermore, the mapping can be acquired from any context (including interrupts). binder_alloc_do_buffer_copy() is a function where the use of kmap_local_page() in place of kmap_atomic() is correctly suited.

Use kmap_local_page() / kunmap_local() in place of kmap_atomic() / kunmap_atomic() but, instead of open coding the mappings and call memcpy() to and from the virtual addresses of the mapped pages, prefer the use of the memcpy_{to,from}_page() wrappers (as suggested by Christophe Jaillet).

---

v1->v2: Add Todd Kjos's tag in the commit message (thanks!); re-write the subject which referred to another function (my fault!); use the memcpy_{to,from}_page() (as suggested by Christophe - thanks!); extend and rework the commit message to say something about the use of the above-mentioned wrappers.

 drivers/android/binder_alloc.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/android/binder_alloc.c +2 -11
index 0875c463c002..5649a0371a1f 100644
@@ -1244,23 +1244,14 @@ static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
 		unsigned long size;
 		struct page *page;
 		pgoff_t pgoff;
-		void *tmpptr;
-		void *base_ptr;
 
 		page = binder_alloc_get_page(alloc, buffer,
 					     buffer_offset, &pgoff);
 		size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
-		base_ptr = kmap_atomic(page);
-		tmpptr = base_ptr + pgoff;
 		if (to_buffer)
-			memcpy(tmpptr, ptr, size);
+			memcpy_to_page(page, pgoff, ptr, size);
 		else
-			memcpy(ptr, tmpptr, size);
-		/*
-		 * kunmap_atomic() takes care of flushing the cache
-		 * if this device has VIVT cache arch
-		 */
-		kunmap_atomic(base_ptr);
+			memcpy_from_page(ptr, page, pgoff, size);
 		bytes -= size;
 		pgoff = 0;
 		ptr = ptr + size;
Signature · 1 line
2.34.1
Reviewed-by on the v2 series
#4 lore raw
In reply to Fabio M. De Francesco
Quoted 7 lines · Fabio M. De Francesco

Use kmap_local_page() in binder_alloc.c because kmap() and kmap_atomic() are being deprecated and kmap_local_page() is preferred where it is feasible.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Furthermore, the mapping can be acquired from any context, including interrupts.

For the series:

Quoted 12 lines

Fabio M. De Francesco (3):
binder: Use memset_page() in binder_alloc_clear_buf()
binder: Use kmap_local_page() in binder_alloc_copy_user_to_buffer()
binder: Use memcpy_{to,from}_page() in binder_alloc_do_buffer_copy()

 drivers/android/binder_alloc.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
Signature · 1 line
2.34.1