Skip to contentSkip to conversation

lib/test_bpf: Call page_address() on page acquired with GFP_KERNEL flag

PATCH Superseded Sumitra Sharma 2 participants 2 messages Jun 22, 2023 – Jun 26, 2023

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 lib/test_bpf: Call page_address() on page acquired with GFP_KERNEL flag

generate_test_data() acquires a page with alloc_page(GFP_KERNEL). The GFP_KERNEL is typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone for direct access.

Therefore the page cannot come from ZONE_HIGHMEM. Thus there's no need to map it with kmap().

Also, the kmap() is being deprecated in favor of
kmap_local_page() [1].

Hence, use a plain page_address() directly.

Since the page passed to the page_address() is not from the highmem zone, the page_address() function will always return a valid kernel virtual address and will not return NULL. Hence, remove the check 'if (!ptr)'.

Remove the unused variable 'ptr'.

[1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny
@intel.com/

---

- Link to v1: https://lore.kernel.org/bpf/[email protected]/T/
- Link to v2: https://lore.kernel.org/lkml/3564297.R56niFO833@suse/T/

Changes in v3:
Noted by: Fabio M. De Francesco<[email protected]>
- Remove the check 'if (!ptr)'.
- Remove the unused variable 'ptr'.
- Change the commit message.

Changes in v2:
Noted by: Fabio M. De Francesco<[email protected]>
- Remove the kmap() call and call page_address() instead.
- Change the commit subject and message.

 lib/test_bpf.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
lib/test_bpf.c +1 -7
index ade9ac672adb..448bc1b0b8b5 100644
@@ -14381,18 +14381,12 @@ 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(page_address(page), test->frag_data, MAX_DATA);
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 17 lines · Sumitra Sharma

generate_test_data() acquires a page with alloc_page(GFP_KERNEL). The GFP_KERNEL is typical for kernel-internal allocations. The caller requires ZONE_NORMAL or a lower zone for direct access.

Therefore the page cannot come from ZONE_HIGHMEM. Thus there's no need to map it with kmap().

Also, the kmap() is being deprecated in favor of
kmap_local_page() [1].

Hence, use a plain page_address() directly.

Since the page passed to the page_address() is not from the highmem zone, the page_address() function will always return a valid kernel virtual address and will not return NULL. Hence, remove the check 'if (!ptr)'.

Quoted 4 lines

Remove the unused variable 'ptr'.

[1]: https://lore.kernel.org/all/20220813220034.806698-1-ira.weiny
@intel.com/

Odd line break here which makes looking at this reference harder.

Quoted 3 lines

The code looks good though.

Quoted 47 lines

---

- Link to v1: https://lore.kernel.org/bpf/[email protected]/T/
- Link to v2: https://lore.kernel.org/lkml/3564297.R56niFO833@suse/T/

Changes in v3:
Noted by: Fabio M. De Francesco<[email protected]>
- Remove the check 'if (!ptr)'.
- Remove the unused variable 'ptr'.
- Change the commit message.

Changes in v2:
Noted by: Fabio M. De Francesco<[email protected]>
- Remove the kmap() call and call page_address() instead.
- Change the commit subject and message.

 lib/test_bpf.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
lib/test_bpf.c +1 -7
index ade9ac672adb..448bc1b0b8b5 100644
@@ -14381,18 +14381,12 @@ 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(page_address(page), test->frag_data, MAX_DATA);
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
Signature · 1 line
2.25.1