Skip to contentSkip to conversation

lib/test_bpf: Replace kmap() with kmap_local_page()

PATCH In review Sumitra Sharma 2 participants 3 messages Jun 12, 2023 – Jun 13, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/test_bpf: Replace kmap() with kmap_local_page()

kmap() has been deprecated in favor of the kmap_local_page() due to high cost, restricted mapping space, the overhead of a global lock for synchronization, and making the process sleep in the absence of free slots.

kmap_local_page() is faster than kmap() and offers thread-local and CPU-local mappings, take pagefaults in a local kmap region and preserves preemption by saving the mappings of outgoing tasks and restoring those of the incoming one during a context switch.

The mapping is kept thread local in the function
“generate_test_data” in test_bpf.c

Therefore, replace kmap() with kmap_local_page() and use memcpy_to_page() to avoid open coding kmap_local_page()
+ memcpy() + kunmap_local().

Remove the unused variable “ptr”.

---
 lib/test_bpf.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
lib/test_bpf.c +1 -9
index ade9ac672adb..3bb94727d83b 100644
@@ -14381,25 +14381,17 @@ static void *generate_test_data(struct bpf_test *test, int sub)
 		 * single fragment to the skb, filled with
 		 * test->frag_data.
 		 */
-		void *ptr;
-
 		page = alloc_page(GFP_KERNEL);
 
 		if (!page)
 			goto err_kfree_skb;
 
-		ptr = kmap(page);
-		if (!ptr)
-			goto err_free_page;
-		memcpy(ptr, test->frag_data, MAX_DATA);
-		kunmap(page);
+		memcpy_to_page(page, 0, test->frag_data, MAX_DATA);
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
 	return skb;
 
-err_free_page:
-	__free_page(page);
 err_kfree_skb:
 	kfree_skb(skb);
 	return NULL;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 11 lines · On lunedì 12 giugno 2023 12:33:41 CEST Sumitra Sharma wrote:

kmap() has been deprecated in favor of the kmap_local_page() due to high cost, restricted mapping space, the overhead of a global lock for synchronization, and making the process sleep in the absence of free slots.

kmap_local_page() is faster than kmap() and offers thread-local and CPU-local mappings, take pagefaults in a local kmap region and preserves preemption by saving the mappings of outgoing tasks and restoring those of the incoming one during a context switch.

Quoted 8 lines

The mapping is kept thread local in the function
“generate_test_data” in test_bpf.c

Therefore, replace kmap() with kmap_local_page() and use memcpy_to_page() to avoid open coding kmap_local_page()
+ memcpy() + kunmap_local().

Remove the unused variable “ptr”.

Sumitra,

Please Cc your mentors while in the internship.
It's not mandatory but it would expedite comments and reviews :-)

Quoted 17 lines
---
 lib/test_bpf.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
lib/test_bpf.c +0 -2
index ade9ac672adb..3bb94727d83b 100644
@@ -14381,25 +14381,17 @@ static void *generate_test_data(struct bpf_test
 *test, int sub) * single fragment to the skb, filled with
 		 * test->frag_data.
 		 */
-		void *ptr;
-
 		page = alloc_page(GFP_KERNEL);

lib/test_bpf.c static void *generate_test_data(struct bpf_test · line 14389

Quoted 10 lines
lib/test_bpf.c +1 -5
 
 		if (!page)
 			goto err_kfree_skb;
 
-		ptr = kmap(page);
-		if (!ptr)
-			goto err_free_page;
-		memcpy(ptr, test->frag_data, MAX_DATA);
-		kunmap(page);
+		memcpy_to_page(page, 0, test->frag_data, MAX_DATA);

Why are you temporary mapping a page allocated with the GFP_KERNEL flag? It cannot come from ZONE_HIGHMEM.

Fabio

Quoted 12 lines
lib/test_bpf.c +0 -3
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
 	return skb;
 
-err_free_page:
-	__free_page(page);
 err_kfree_skb:
 	kfree_skb(skb);
 	return NULL;
--

2.25.1

#2 lore raw
Quoted 19 lines · Fabio M. De Francesco
Quoted 18 lines · On lunedì 12 giugno 2023 12:33:41 CEST Sumitra Sharma wrote:

kmap() has been deprecated in favor of the kmap_local_page() due to high cost, restricted mapping space, the overhead of a global lock for synchronization, and making the process sleep in the absence of free slots.

kmap_local_page() is faster than kmap() and offers thread-local and CPU-local mappings, take pagefaults in a local kmap region and preserves preemption by saving the mappings of outgoing tasks and restoring those of the incoming one during a context switch.

The mapping is kept thread local in the function
“generate_test_data” in test_bpf.c

Therefore, replace kmap() with kmap_local_page() and use memcpy_to_page() to avoid open coding kmap_local_page()
+ memcpy() + kunmap_local().

Quoted 7 lines
Quoted 1 line

Remove the unused variable “ptr”.

Sumitra,

Please Cc your mentors while in the internship.
It's not mandatory but it would expedite comments and reviews :-)

Hi Fabio,

I will take care of it.

Quoted 21 lines
Quoted 21 lines
---
 lib/test_bpf.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
lib/test_bpf.c +0 -2
index ade9ac672adb..3bb94727d83b 100644
@@ -14381,25 +14381,17 @@ static void *generate_test_data(struct bpf_test
 *test, int sub) * single fragment to the skb, filled with
 		 * test->frag_data.
 		 */
-		void *ptr;
-
 		page = alloc_page(GFP_KERNEL);
 
 		if (!page)
 			goto err_kfree_skb;
 
Quoted 10 lines

lib/test_bpf.c static void *generate_test_data(struct bpf_test *test, int sub) · line 14389

Quoted 6 lines
lib/test_bpf.c +1 -5
-		ptr = kmap(page);
-		if (!ptr)
-			goto err_free_page;
-		memcpy(ptr, test->frag_data, MAX_DATA);
-		kunmap(page);
+		memcpy_to_page(page, 0, test->frag_data, MAX_DATA);

Why are you temporary mapping a page allocated with the GFP_KERNEL flag? It cannot come from ZONE_HIGHMEM.

Fabio, I somewhere _wrongly_ read that "GFP_KERNEL may allocate pages from highmem". However, with further readings and checking the GFP_KERNEL definition in gfp_types.h

#define GFP_KERNEL (__GFP_RECLAIM | __GFP_IO | __GFP_FS)

I have got that with the GFP_KERNEL flag, pages can never come from ZONE_HIGHMEM.

Apologise.

Thanks & regards
Sumitra

Quoted 17 lines

Fabio

Quoted 12 lines
lib/test_bpf.c +0 -3
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
 	return skb;
 
-err_free_page:
-	__free_page(page);
 err_kfree_skb:
 	kfree_skb(skb);
 	return NULL;
--

2.25.1