Skip to contentSkip to conversation

lib: Replace kmap() with kmap_local_page()

PATCH Applied Sumitra Sharma 3 participants 4 messages Jun 10, 2023 – Jun 19, 2023

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

PATCH v2 1/1 lib: 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 mappings are kept thread local in the functions “dmirror_do_read” and “dmirror_do_write” in test_hmm.c

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

Remove the unused variable “tmp”.

---

Changes in v2:
- Change commit subject and description.
- Remove unnecessary type casting (char *).

 lib/test_hmm.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
lib/test_hmm.c +2 -8
index 67e6f83fe0f8..717dcb830127 100644
@@ -368,16 +368,13 @@ static int dmirror_do_read(struct dmirror *dmirror, unsigned long start,
 	for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
 		void *entry;
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(ptr, tmp, PAGE_SIZE);
-		kunmap(page);
+		memcpy_from_page(ptr, page, 0, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
@@ -437,16 +434,13 @@ static int dmirror_do_write(struct dmirror *dmirror, unsigned long start,
 	for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
 		void *entry;
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(tmp, ptr, PAGE_SIZE);
-		kunmap(page);
+		memcpy_to_page(page, 0, ptr, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 18 lines · Sumitra Sharma

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 mappings are kept thread local in the functions “dmirror_do_read” and “dmirror_do_write” in test_hmm.c

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

Quoted 6 lines

Remove the unused variable “tmp”.

LGTM

Reviewed-by on v2 1/1
#2 lore raw
In reply to Sumitra Sharma
Quoted 14 lines · On sabato 10 giugno 2023 19:57:12 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 mappings are kept thread local in the functions “dmirror_do_read” and “dmirror_do_write” in test_hmm.c

Quoted 8 lines

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

Remove the unused variable “tmp”.

LGTM, so...

Thanks,

Fabio

P.S.: The answer to an old question from you is that "LGTM" stands for "[It] Looks Good To Me".

It's just a way to introduce the "Reviewed-by" tag itself. However, "LGTM" is not required, whereas the tag is required for a valid review and only the tag line will be added by maintainers in the log when your patch gets applied.

While here... Please don't put unnecessary blank lines between the tags.They are not required and instead may worsen readability (obviously, I'm not requiring a new version for this).

Quoted 54 lines

---

Changes in v2:
- Change commit subject and description.
- Remove unnecessary type casting (char *).

 lib/test_hmm.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
lib/test_hmm.c +0 -0
index 67e6f83fe0f8..717dcb830127 100644
@@ -368,16 +368,13 @@ static int dmirror_do_read(struct dmirror *dmirror,

unsigned long start, for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { void *entry;

lib/test_hmm.c +1 -4
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(ptr, tmp, PAGE_SIZE);
-		kunmap(page);
+		memcpy_from_page(ptr, page, 0, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
@@ -437,16 +434,13 @@ static int dmirror_do_write(struct dmirror *dmirror,

unsigned long start, for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { void *entry;

lib/test_hmm.c +1 -5
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(tmp, ptr, PAGE_SIZE);
-		kunmap(page);
+		memcpy_to_page(page, 0, ptr, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
--

2.25.1

#3 lore raw
Quoted 35 lines · Fabio M. De Francesco
Quoted 14 lines · On sabato 10 giugno 2023 19:57:12 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 mappings are kept thread local in the functions “dmirror_do_read” and “dmirror_do_write” in test_hmm.c

Quoted 8 lines

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

Remove the unused variable “tmp”.

LGTM, so...

Thanks,

Fabio

P.S.: The answer to an old question from you is that "LGTM" stands for "[It] Looks Good To Me".

Quoted 8 lines

It's just a way to introduce the "Reviewed-by" tag itself. However, "LGTM" is not required, whereas the tag is required for a valid review and only the tag line will be added by maintainers in the log when your patch gets applied.

While here... Please don't put unnecessary blank lines between the tags.They are not required and instead may worsen readability (obviously, I'm not requiring a new version for this).

Hi Fabio,

Thank you for your explanation and clarification regarding the meaning of "LGTM." I will take care of the blank lines.

Thanks & regards
Sumitra

Quoted 58 lines
Quoted 54 lines

---

Changes in v2:
- Change commit subject and description.
- Remove unnecessary type casting (char *).

 lib/test_hmm.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
lib/test_hmm.c +0 -0
index 67e6f83fe0f8..717dcb830127 100644
@@ -368,16 +368,13 @@ static int dmirror_do_read(struct dmirror *dmirror,

unsigned long start, for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { void *entry;

lib/test_hmm.c +1 -4
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(ptr, tmp, PAGE_SIZE);
-		kunmap(page);
+		memcpy_from_page(ptr, page, 0, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
@@ -437,16 +434,13 @@ static int dmirror_do_write(struct dmirror *dmirror,

unsigned long start, for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { void *entry;

lib/test_hmm.c +1 -5
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(tmp, ptr, PAGE_SIZE);
-		kunmap(page);
+		memcpy_to_page(page, 0, ptr, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
--

2.25.1