Skip to contentSkip to conversation

staging: media: atomisp: Use kmap_local_page() in hmm_store()

PATCH In review Fabio M. De Francesco 5 participants 12 messages Apr 13, 2022 – Apr 29, 2022

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

PATCH 1/1 staging: media: atomisp: Use kmap_local_page() in hmm_store()

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -12
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 15 lines · Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

Quoted 9 lines

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works or do we need to test each one. If the latter, then how?

Quoted 41 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -12
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#2 lore raw
Quoted 21 lines · Alison Schofield
Quoted 20 lines · Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Quoted 7 lines
Quoted 4 lines

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works

Just code inspection. Most of them that I have done have been compile tested only. Part of the key is that des is a local variable and is not aliased by anything outside this function.

Quoted 1 line

or do we need to test each one. If the latter, then how?

Generally there is no test if we don't have the hardware. Some of the more difficult conversions will probably need to have some testing done but that will need to be discussed with the subsystem maintainers at the time.

Ira

Quoted 43 lines
Quoted 43 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -12
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#3 lore raw
Quoted 27 lines · Ira Weiny
Quoted 26 lines · Alison Schofield
Quoted 24 lines · Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Quoted 6 lines
Quoted 2 lines

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works

Just code inspection. Most of them that I have done have been compile tested only. Part of the key is that des is a local variable and is not aliased by anything outside this function.

Typically, the concern about being in atomic context has to do with whether GFP_KERNEL or GFP_ATOMIC should be used, ie whether allocation can sleep. It doesn't have to do with whether some data can be shared. Is that the concern here?

julia

Quoted 54 lines
Quoted 2 lines

or do we need to test each one. If the latter, then how?

Generally there is no test if we don't have the hardware. Some of the more difficult conversions will probably need to have some testing done but that will need to be discussed with the subsystem maintainers at the time.

Ira

Quoted 43 lines
Quoted 43 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -13
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
--

2.34.1

#4 lore raw
Quoted 5 lines · On gioved? 14 aprile 2022 09:03:40 CEST Julia Lawall wrote:
Quoted 2 lines · Ira Weiny
Quoted 1 line · Alison Schofield

On Thu, Apr 14, 2022 at 12:55:31AM +0200, Fabio M. De Francesco

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in

atomic

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

context and, if so, it calls kmap_atomic(), if not, it calls

kmap().

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to

care

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping

/

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and

not

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

globally visible. Therefore, hmm_store()() is a function where the

use

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works

Just code inspection. Most of them that I have done have been compile

tested

Quoted 1 line
Quoted 1 line

only. Part of the key is that des is a local variable and is not

aliased by

Quoted 5 lines
Quoted 1 line

anything outside this function.

Typically, the concern about being in atomic context has to do with whether GFP_KERNEL or GFP_ATOMIC should be used, ie whether allocation can sleep.

I'd add that the concern about being in atomic context has mainly to do with calling whatever function that may sleep.

Some time ago I analyzed a calls chain which, under spinlocks and with IRQ's disabled, led to console_lock() which is annotated with might_sleep(). It took about 8000 ms to recover when executing in a 4 CPU / 8 SMT System. Linus T. suggested to make this work asynchronous (commit 1ee33b1ca2b8 ("tty: n_hdlc: make n_hdlc_tty_wakeup() asynchronous")).

Quoted 1 line

It doesn't have to do with whether some data can be shared.

Yes, FWIW I agree with you.

Quoted 1 line

Is that the concern here?

The concern here is about the locality of the pointer variable to which the struct page has been mapped to. In atomic context we are not allowed to kmap() (this is why in the code we had that in_atomic() test), instead we can kmap_local_page() or kmap_atomic(). The latter is strongly discouraged in favor of the former.

Furthermore, Alison was asking if we can prove that these kinds of conversions can actually work when we have not the hardware for testing. As Ira wrote, code inspection is sufficient to prove it.

Thanks,

Fabio M. De Francesco

#5 lore raw
Quoted 64 lines · Fabio M. De Francesco
Quoted 5 lines · On gioved? 14 aprile 2022 09:03:40 CEST Julia Lawall wrote:
Quoted 2 lines · Ira Weiny
Quoted 1 line · Alison Schofield

On Thu, Apr 14, 2022 at 12:55:31AM +0200, Fabio M. De Francesco

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in

atomic

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

context and, if so, it calls kmap_atomic(), if not, it calls

kmap().

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to

care

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping

/

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and

not

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

globally visible. Therefore, hmm_store()() is a function where the

use

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works

Just code inspection. Most of them that I have done have been compile

tested

Quoted 1 line
Quoted 1 line

only. Part of the key is that des is a local variable and is not

aliased by

Quoted 5 lines
Quoted 1 line

anything outside this function.

Typically, the concern about being in atomic context has to do with whether GFP_KERNEL or GFP_ATOMIC should be used, ie whether allocation can sleep.

I'd add that the concern about being in atomic context has mainly to do with calling whatever function that may sleep.

Some time ago I analyzed a calls chain which, under spinlocks and with IRQ's disabled, led to console_lock() which is annotated with might_sleep(). It took about 8000 ms to recover when executing in a 4 CPU / 8 SMT System. Linus T. suggested to make this work asynchronous (commit 1ee33b1ca2b8 ("tty: n_hdlc: make n_hdlc_tty_wakeup() asynchronous")).

Quoted 1 line

It doesn't have to do with whether some data can be shared.

Yes, FWIW I agree with you.

Quoted 7 lines
Quoted 1 line

Is that the concern here?

The concern here is about the locality of the pointer variable to which the struct page has been mapped to. In atomic context we are not allowed to kmap() (this is why in the code we had that in_atomic() test), instead we can kmap_local_page() or kmap_atomic(). The latter is strongly discouraged in favor of the former.

I have the impression that you are first agreeing with me and then contradicting me :). Is your point that in general a concern about atomic context has to do with whether sleeping is allowed, but that the concern is something else here? I'm not familiar with these kmap functions.

thanks,
julia

Quoted 12 lines

Furthermore, Alison was asking if we can prove that these kinds of conversions can actually work when we have not the hardware for testing. As Ira wrote, code inspection is sufficient to prove it.

Thanks,

Fabio M. De Francesco

#6 lore raw
Quoted 11 lines · On gioved? 14 aprile 2022 11:12:39 CEST Julia Lawall wrote:
Quoted 8 lines · Fabio M. De Francesco
Quoted 5 lines · On gioved? 14 aprile 2022 09:03:40 CEST Julia Lawall wrote:
Quoted 2 lines · Ira Weiny
Quoted 1 line · Alison Schofield

On Thu, Apr 14, 2022 at 12:55:31AM +0200, Fabio M. De Francesco

Quoted 1 line · wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line

The use of kmap() is being deprecated in favor of

kmap_local_page()

Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in

atomic

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

context and, if so, it calls kmap_atomic(), if not, it calls

kmap().

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

First of all, in_atomic() shouldn't be used in drivers. This

macro

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

cannot always detect atomic context; in particular, it cannot

know

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need

to

Quoted 2 lines
Quoted 2 lines

care

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

whether or not it is executing in atomic context. It can simply

use

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

kmap_local_page() / kunmap_local() that can instead do the

mapping

Quoted 4 lines
Quoted 4 lines

/

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local

and

Quoted 2 lines
Quoted 2 lines

not

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

globally visible. Therefore, hmm_store()() is a function where

the

Quoted 2 lines
Quoted 2 lines

use

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

of kmap_local_page() in place of both kmap() and kmap_atomic()

is

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop

the

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

unnecessary tests which test if the code is in atomic context.

Not specifically about this patch, but more generally about all such conversions - is there a 'proof' that shows this just works

Just code inspection. Most of them that I have done have been

compile

Quoted 7 lines
Quoted 7 lines

tested

Quoted 1 line
Quoted 1 line

only. Part of the key is that des is a local variable and is not

aliased by

Quoted 4 lines
Quoted 1 line

anything outside this function.

Typically, the concern about being in atomic context has to do with whether GFP_KERNEL or GFP_ATOMIC should be used, ie whether

allocation

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

can sleep.

I'd add that the concern about being in atomic context has mainly to do with calling whatever function that may sleep.

Some time ago I analyzed a calls chain which, under spinlocks and with IRQ's disabled, led to console_lock() which is annotated with might_sleep(). It took about 8000 ms to recover when executing in a 4

CPU /

Quoted 10 lines
Quoted 10 lines

8 SMT System. Linus T. suggested to make this work asynchronous (commit 1ee33b1ca2b8 ("tty: n_hdlc: make n_hdlc_tty_wakeup() asynchronous")).

Quoted 1 line

It doesn't have to do with whether some data can be shared.

Yes, FWIW I agree with you.

Quoted 1 line

Is that the concern here?

The concern here is about the locality of the pointer variable to which

the

Quoted 2 lines
Quoted 2 lines

struct page has been mapped to. In atomic context we are not allowed to kmap() (this is why in the code we had that in_atomic() test), instead

we

Quoted 1 line
Quoted 1 line

can kmap_local_page() or kmap_atomic(). The latter is strongly

discouraged

Quoted 4 lines
Quoted 1 line

in favor of the former.

I have the impression that you are first agreeing with me and then contradicting me :). Is your point that in general a concern about

atomic

Quoted 2 lines

context has to do with whether sleeping is allowed, but that the concern is something else here? I'm not familiar with these kmap functions.

Yes the concern is something else here (sorry for my poor English). It was not my intention to contradict you :)

The concern for "locality" here is that if the variable is shared between different functions there are cases where we cannot use kmap_local_page(). For example, those mappings with kmap_local_page() are per thread and CPU local.

I think that the best means to convey what I want to express is pointing to my patch to Documentation/vm/highmem.rst:

https://lore.kernel.org/lkml/[email protected]/

I hope that this can help to understand why we care of "locality" in this specific case where we use kmap_local_page().

Again, sorry for not being clear.

Thanks,

Fabio M. De Francesco

Quoted 7 lines

thanks,
julia

Quoted 3 lines

Furthermore, Alison was asking if we can prove that these kinds of conversions can actually work when we have not the hardware for

testing. As

Quoted 5 lines
Quoted 5 lines

Ira wrote, code inspection is sufficient to prove it.

Thanks,

Fabio M. De Francesco

Reviewed-by on v1 1/1
#7 lore raw
In reply to Fabio M. De Francesco
Quoted 20 lines · Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Quoted 5 lines

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

I've had to research this a bit myself because I've not seen this pattern before.

kmap_atomic() had 2 uses:

1) a situation where the operation on the page requires pagefaults and/or preemption to be disabled
2) in a situation where one knows the code can't sleep

kmap_local_page() removes the second use case because kmap() is no longer the only alternative; from the kdoc for kmap_atomic():

...
* kmap_atomic - Atomically map a page for temporary usage - Deprecated!
...
* Effectively a wrapper around kmap_local_page() which disables pagefaults
* and preemption.
...

The deprecation is because any pagefault/preemption disabling should probably be done explicitly from now on but allows for existing kmap_atomic() callers to live on.

In this case, I suspect the original driver writer wanted to use kmap() but hmm_store() was called from various contexts. So they incorrectly tried to protect against the (potentially) sleeping kmap() call. In reality, I think they could have simply called kmap_atomic() and skipped 'in_atomic()' check altogether.

Regardless now that kmap_local_page() exists, this is correct.

Quoted 39 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -12
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#8 lore raw
Quoted 3 lines · On venerd? 15 aprile 2022 02:37:27 CEST Ira Weiny wrote:

Regardless now that kmap_local_page() exists, this is correct.

I'm pretty sure this particular patch would have had a hard time getting accepted without any authoritative "Reviewed-by" tag from you or other people more experienced than me.

Thank you very much!

Fabio M. De Francesco

Tested-by on v1 1/1
#9 lore raw
In reply to Fabio M. De Francesco

Hi,

Quoted 20 lines · Fabio M. De Francesco

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Quoted 5 lines

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

I've successfully tested this on both the front and back cams of a chuwi hi8 tablet:

Regards,

Hans

Quoted 36 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +2 -12
index 46ac082cd3f1..54188197c3dc 100644
@@ -482,10 +482,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 		idx = (virt - bo->start) >> PAGE_SHIFT;
 		offset = (virt - bo->start) - (idx << PAGE_SHIFT);
 
-		if (in_atomic())
-			des = (char *)kmap_atomic(bo->page_obj[idx].page);
-		else
-			des = (char *)kmap(bo->page_obj[idx].page);
+		des = (char *)kmap_local_page(bo->page_obj[idx].page);
 
 		if (!des) {
 			dev_err(atomisp_dev,
@@ -512,14 +509,7 @@ int hmm_store(ia_css_ptr virt, const void *data, unsigned int bytes)
 
 		clflush_cache_range(des, len);
 
-		if (in_atomic())
-			/*
-			 * Note: kunmap_atomic requires return addr from
-			 * kmap_atomic, not the page. See linux/highmem.h
-			 */
-			kunmap_atomic(des - offset);
-		else
-			kunmap(bo->page_obj[idx].page);
+		kunmap_local(des);
 	}
 
 	return 0;
#10 lore raw
In reply to Fabio M. De Francesco
Quoted 20 lines · On giovedì 14 aprile 2022 00:55:31 CEST Fabio M. De Francesco wrote:

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Quoted 8 lines

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

Hi Mauro,

I'm writing for just a gentle ping for this and two other staging/atomisp/ patches that still seem to be waiting to be applied.

In the meantime I would like to remind you that Hans de Goede has successfully tested this patch and the other two on both the front and back cams of a chuwi hi8 tablet.

Please let me know if there is anything else that is required to be done in order to accept the three patches.

Thanks,

Fabio M. De Francesco

#11 lore raw
Quoted 25 lines · On lunedì 25 aprile 2022 20:29:03 CEST Fabio M. De Francesco wrote:
Quoted 24 lines · On giovedì 14 aprile 2022 00:55:31 CEST Fabio M. De Francesco wrote:

The use of kmap() is being deprecated in favor of kmap_local_page() where it is feasible. The same is true for kmap_atomic().

In file pci/hmm/hmm.c, function hmm_store() test if we are in atomic context and, if so, it calls kmap_atomic(), if not, it calls kmap().

First of all, in_atomic() shouldn't be used in drivers. This macro cannot always detect atomic context; in particular, it cannot know about held spinlocks in non-preemptible kernels.

Notwithstanding what it is said above, this code doesn't need to care whether or not it is executing in atomic context. It can simply use kmap_local_page() / kunmap_local() that can instead do the mapping / unmapping regardless of the context.

With kmap_local_page(), the mapping is per thread, CPU local and not globally visible. Therefore, hmm_store()() is a function where the use of kmap_local_page() in place of both kmap() and kmap_atomic() is correctly suited.

Convert the calls of kmap() / kunmap() and kmap_atomic() / kunmap_atomic() to kmap_local_page() / kunmap_local() and drop the unnecessary tests which test if the code is in atomic context.

Quoted 8 lines
Quoted 4 lines
---
 drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

Hi Mauro,

I'm writing for just a gentle ping for this and two other staging/

atomisp/

Quoted 4 lines

patches that still seem to be waiting to be applied.

In the meantime I would like to remind you that Hans de Goede has successfully tested this patch and the other two on both the front and

back

Quoted 1 line

cams of a chuwi hi8 tablet.

Hi Mauro,

In my previous message I forgot to remind you that two of these three patches have also been reviewed by Ira Weiny (Intel).

I'd like to ask you again if there is still something left which prevents these three patches from being accepted. The first of the three patches was submitted on April 9, 2022.

For you convenience here are the links to the patches, the "Reviewed-by:" and "Tested-by:" tags:

[PATCH] staging: media: atomisp: Use kmap_local_page() in hmm_store()
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/Yli+R7iLZKqO8kVP@iweiny-desk3/
https://lore.kernel.org/lkml/[email protected]/

[PATCH] staging: media: atomisp: Use kmap_local_page() in hmm_set()
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/YldNhErgt53RqYp7@iweiny-desk3/
https://lore.kernel.org/lkml/[email protected]/

[PATCH] staging: media: atomisp: Convert kmap() to kmap_local_page()
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Thanks,

Fabio M. De Francesco

P.S.: Do you want me to resend them all and add the above-mentioned tags?