lkml.org 
[lkml]   [2022]   [Apr]   [14]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] staging: media: atomisp: Use kmap_local_page() in hmm_store()
On Thu, Apr 14, 2022 at 12:55:31AM +0200, 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.
>
> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@gmail.com>

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.

Reviewed-by: Ira Weiny <ira.weiny@intel.com>

> ---
> drivers/staging/media/atomisp/pci/hmm/hmm.c | 14 ++------------
> 1 file changed, 2 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/staging/media/atomisp/pci/hmm/hmm.c b/drivers/staging/media/atomisp/pci/hmm/hmm.c
> index 46ac082cd3f1..54188197c3dc 100644
> --- a/drivers/staging/media/atomisp/pci/hmm/hmm.c
> +++ b/drivers/staging/media/atomisp/pci/hmm/hmm.c
> @@ -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
>

\
 
 \ /
  Last update: 2022-04-15 02:38    [W:0.132 / U:0.944 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site