Re: [PATCH] btrfs: Replace kmap() with kmap_local_page() in zstd.c

From: David Sterba
Date: Mon Jun 13 2022 - 16:09:56 EST


On Sat, Jun 11, 2022 at 03:52:03PM +0200, Fabio M. De Francesco wrote:
> The use of kmap() is being deprecated in favor of kmap_local_page(). With
> kmap_local_page(), the mapping is per thread, CPU local and not globally
> visible.
>
> Therefore, use kmap_local_page() / kunmap_local() in zstd.c because in
> this file the mappings are per thread and are not visible in other
> contexts; meanwhile refactor zstd_compress_pages() to comply with nested
> local mapping / unmapping ordering rules.
>
> Tested with xfstests on a QEMU + KVM 32 bits VM with 4GB of RAM and
> HIGHMEM64G enabled.
>
> Cc: Filipe Manana <fdmanana@xxxxxxxxxx>
> Suggested-by: Ira Weiny <ira.weiny@xxxxxxxxx>
> Signed-off-by: Fabio M. De Francesco <fmdefrancesco@xxxxxxxxx>
> ---
>
> @@ -477,15 +479,16 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
> /* Check if we need more input */
> if (workspace->in_buf.pos == workspace->in_buf.size) {
> tot_in += PAGE_SIZE;
> - kunmap(in_page);
> + kunmap_local(workspace->out_buf.dst);
> + kunmap_local((void *)workspace->in_buf.src);

Why is the cast needed? I see that it leads to a warning but we pass a
const buffer and that breaks the API contract as in kunmap it would be
accessed as non-const and potentially changed without warning or
compiler error. If kunmap_local does not touch the buffer and 'const
void*' would work too, then it should be fixed.