Skip to contentSkip to conversation

btrfs: Replace kmap() with kmap_local_page() in zstd.c

PATCH In review Fabio M. De Francesco 4 participants 13 messages Jun 11, 2022 – Jul 14, 2022

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

PATCH 1/1 btrfs: Replace kmap() with kmap_local_page() in zstd.c

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.

---

Thanks to Ira Weiny for his invaluable help and persevering support. Thanks also to Filipe Manana for identifying a fundamental detail I had overlooked:
https://lore.kernel.org/lkml/20220611093411.GA3779054@falcondesktop/

tweed32:/usr/lib/xfstests # ./check -g compress

FSTYP         -- btrfs
PLATFORM      -- Linux/i686 tweed32 5.19.0-rc1-vanilla-debug+ #22 SMP PREEMPT_DYNAMIC Sat Jun 11 14:31:39 CEST 2022
MKFS_OPTIONS  -- /dev/loop1

MOUNT_OPTIONS -- /dev/loop1 /mnt/scratch

btrfs/024 1s ...  0s
btrfs/026 3s ...  3s
btrfs/037 1s ...  1s
btrfs/038 1s ...  0s
btrfs/041 0s ...  1s
btrfs/062 34s ...  35s
btrfs/063 18s ...  18s
btrfs/067 33s ...  32s
btrfs/068 10s ...  10s
btrfs/070       [not run] btrfs and this test needs 5 or more disks in SCRATCH_DEV_POOL
btrfs/071       [not run] btrfs and this test needs 5 or more disks in SCRATCH_DEV_POOL
btrfs/072 33s ...  33s
btrfs/073 17s ...  15s
btrfs/074 36s ...  32s
btrfs/076 0s ...  0s
btrfs/103 1s ...  1s
btrfs/106 1s ...  1s
btrfs/109 1s ...  0s
btrfs/113 0s ...  1s
btrfs/138 43s ...  46s
btrfs/149 1s ...  1s
btrfs/183 1s ...  1s
btrfs/205 1s ...  1s
btrfs/234 3s ...  3s
btrfs/246 0s ...  0s
btrfs/251 1s ...  1s

Ran: btrfs/024 btrfs/026 btrfs/037 btrfs/038 btrfs/041 btrfs/062 btrfs/063 btrfs/067 btrfs/068 btrfs/070 btrfs/071 btrfs/072 btrfs/073 btrfs/074 btrfs/076 btrfs/103 btrfs/106 btrfs/109 btrfs/113 btrfs/138 btrfs/149 btrfs/183 btrfs/205 btrfs/234 btrfs/246 btrfs/251 Not run: btrfs/070 btrfs/071
Passed all 26 tests

 fs/btrfs/zstd.c | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)
fs/btrfs/zstd.c +23 -19
index 0fe31a6f6e68..4d50eb3edce5 100644
@@ -391,6 +391,8 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 	*out_pages = 0;
 	*total_out = 0;
 	*total_in = 0;
+	workspace->in_buf.src = NULL;
+	workspace->out_buf.dst = NULL;
 
 	/* Initialize the stream */
 	stream = zstd_init_cstream(&params, len, workspace->mem,
@@ -403,7 +405,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 
 	/* map in the first page of input data */
 	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
-	workspace->in_buf.src = kmap(in_page);
+	workspace->in_buf.src = kmap_local_page(in_page);
 	workspace->in_buf.pos = 0;
 	workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
 
@@ -415,7 +417,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 		goto out;
 	}
 	pages[nr_pages++] = out_page;
-	workspace->out_buf.dst = kmap(out_page);
+	workspace->out_buf.dst = kmap_local_page(out_page);
 	workspace->out_buf.pos = 0;
 	workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
 
@@ -450,9 +452,9 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 		if (workspace->out_buf.pos == workspace->out_buf.size) {
 			tot_out += PAGE_SIZE;
 			max_out -= PAGE_SIZE;
-			kunmap(out_page);
+			kunmap_local(workspace->out_buf.dst);
 			if (nr_pages == nr_dest_pages) {
-				out_page = NULL;
+				workspace->out_buf.dst = NULL;
 				ret = -E2BIG;
 				goto out;
 			}
@@ -462,7 +464,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 				goto out;
 			}
 			pages[nr_pages++] = out_page;
-			workspace->out_buf.dst = kmap(out_page);
+			workspace->out_buf.dst = kmap_local_page(out_page);
 			workspace->out_buf.pos = 0;
 			workspace->out_buf.size = min_t(size_t, max_out,
 							PAGE_SIZE);
@@ -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);
 			put_page(in_page);
-
 			start += PAGE_SIZE;
 			len -= PAGE_SIZE;
 			in_page = find_get_page(mapping, start >> PAGE_SHIFT);
-			workspace->in_buf.src = kmap(in_page);
+			workspace->in_buf.src = kmap_local_page(in_page);
 			workspace->in_buf.pos = 0;
 			workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
+			workspace->out_buf.dst = kmap_local_page(out_page);
 		}
 	}
 	while (1) {
@@ -510,9 +513,9 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 
 		tot_out += PAGE_SIZE;
 		max_out -= PAGE_SIZE;
-		kunmap(out_page);
+		kunmap_local(workspace->out_buf.dst);
 		if (nr_pages == nr_dest_pages) {
-			out_page = NULL;
+			workspace->out_buf.dst = NULL;
 			ret = -E2BIG;
 			goto out;
 		}
@@ -522,7 +525,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 			goto out;
 		}
 		pages[nr_pages++] = out_page;
-		workspace->out_buf.dst = kmap(out_page);
+		workspace->out_buf.dst = kmap_local_page(out_page);
 		workspace->out_buf.pos = 0;
 		workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
 	}
@@ -538,12 +541,12 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 out:
 	*out_pages = nr_pages;
 	/* Cleanup */
-	if (in_page) {
-		kunmap(in_page);
+	if (workspace->out_buf.dst)
+		kunmap_local(workspace->out_buf.dst);
+	if (workspace->in_buf.src) {
+		kunmap_local((void *)workspace->in_buf.src);
 		put_page(in_page);
 	}
-	if (out_page)
-		kunmap(out_page);
 	return ret;
 }
 
@@ -567,7 +570,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 		goto done;
 	}
 
-	workspace->in_buf.src = kmap(pages_in[page_in_index]);
+	workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
 	workspace->in_buf.pos = 0;
 	workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
 
@@ -603,14 +606,15 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 			break;
 
 		if (workspace->in_buf.pos == workspace->in_buf.size) {
-			kunmap(pages_in[page_in_index++]);
+			kunmap_local((void *)workspace->in_buf.src);
+			page_in_index++;
 			if (page_in_index >= total_pages_in) {
 				workspace->in_buf.src = NULL;
 				ret = -EIO;
 				goto done;
 			}
 			srclen -= PAGE_SIZE;
-			workspace->in_buf.src = kmap(pages_in[page_in_index]);
+			workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
 			workspace->in_buf.pos = 0;
 			workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
 		}
@@ -619,7 +623,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 	zero_fill_bio(cb->orig_bio);
 done:
 	if (workspace->in_buf.src)
-		kunmap(pages_in[page_in_index]);
+		kunmap_local((void *)workspace->in_buf.src);
 	return ret;
 }
 
Signature · 1 line
2.36.1
#1 lore raw
Quoted 18 lines · Fabio M. De Francesco

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.

---

fs/btrfs/zstd.c int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · line 483

Quoted 6 lines
fs/btrfs/zstd.c +2 -1
@@ -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.

#2 lore raw
Quoted 2 lines · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:
Quoted 1 line · Fabio M. De Francesco

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

With

Quoted 1 line
Quoted 1 line

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

globally

Quoted 5 lines
Quoted 5 lines

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

Quoted 11 lines

Review int zstd_compress_pages(struct list_head *ws,

Quoted 11 lines

local mapping / unmapping ordering rules.

Tested with xfstests on a QEMU + KVM 32 bits VM with 4GB of RAM and HIGHMEM64G enabled.

---

(quoted diff) +0 -0
@@ -477,15 +479,16 @@ int zstd_compress_pages(struct list_head *ws, 

struct address_space *mapping,

Quoted 8 lines

fs/btrfs/zstd.c int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · line 483

Quoted 6 lines
fs/btrfs/zstd.c +2 -1
 		/* 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?

As I wrote in an email I sent some days ago ("[RFC PATCH] btrfs: Replace kmap() with kmap_local_page() in zstd.c")[1] I get a series of errors like the following:

/usr/src/git/kernels/linux/fs/btrfs/zstd.c:547:33: warning: passing argument 1 of '__kunmap_local' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

  547 |   kunmap_local(workspace->in_buf.src);
      |                ~~~~~~~~~~~~~~~~~^~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:284:17: note: 
in definition of macro 'kunmap_local'
  284 |  __kunmap_local(__addr);     \
      |                 ^~~~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:92:41: note: 
expected 'void *' but argument is of type 'const void *'
   92 | static inline void __kunmap_local(void *vaddr)
      |                                   ~~~~~~^~~~~

Therefore, this is a (bad?) hack to make these changes compile. A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to const void to kunmap_local(). Therefore, I was not sure if changing the API were better suited (however I have already discussed this with Ira).

Quoted 4 lines

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

Yes, correct, kunmap_local() does _not_ touch the buffer.

Quoted 2 lines

and 'const
void*' would work too, then it should be fixed.

I'll send an RFC patch for changing __kunmap_local() and the other functions of the calls chain down to kunmap_local_indexed(). Furthermore, changes to kunmap_local_indexed() prototype require also changes to __kunmap_atomic() (if I recall correctly...).

Thanks for your review,

Fabio

#3 lore raw
Quoted 9 lines · On martedì 14 giugno 2022 01:22:50 CEST Fabio M. De Francesco wrote:
Quoted 2 lines · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:
Quoted 1 line · Fabio M. De Francesco

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

With

Quoted 1 line
Quoted 1 line

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

globally

Quoted 3 lines
Quoted 3 lines

visible.

Therefore, use kmap_local_page() / kunmap_local() in zstd.c because

in

Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

this file the mappings are per thread and are not visible in other contexts; meanwhile refactor zstd_compress_pages() to comply with

nested

Quoted 11 lines

Review int zstd_compress_pages(struct list_head *ws,

Quoted 11 lines

local mapping / unmapping ordering rules.

Tested with xfstests on a QEMU + KVM 32 bits VM with 4GB of RAM and HIGHMEM64G enabled.

---

(quoted diff) +0 -0
@@ -477,15 +479,16 @@ int zstd_compress_pages(struct list_head *ws, 

struct address_space *mapping,

Quoted 8 lines

fs/btrfs/zstd.c int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · line 483

Quoted 6 lines
fs/btrfs/zstd.c +2 -1
 		/* 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?

Quoted 2 lines

As I wrote in an email I sent some days ago ("[RFC PATCH] btrfs: Replace kmap() with kmap_local_page() in zstd.c")[1] I get a series of errors

like

Quoted 16 lines

the following:

/usr/src/git/kernels/linux/fs/btrfs/zstd.c:547:33: warning: passing argument 1 of '__kunmap_local' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

  547 |   kunmap_local(workspace->in_buf.src);
      |                ~~~~~~~~~~~~~~~~~^~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:284:17: note: 
in definition of macro 'kunmap_local'
  284 |  __kunmap_local(__addr);     \
      |                 ^~~~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:92:41: note: 
expected 'void *' but argument is of type 'const void *'
   92 | static inline void __kunmap_local(void *vaddr)
      |                                   ~~~~~~^~~~~
Quoted 8 lines

Therefore, this is a (bad?) hack to make these changes compile. A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to

const

Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the API

were

Quoted 12 lines

better suited (however I have already discussed this with Ira).

Quoted 4 lines

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

Yes, correct, kunmap_local() does _not_ touch the buffer.

Quoted 2 lines

and 'const
void*' would work too, then it should be fixed.

Quoted 9 lines

I'll send an RFC patch for changing __kunmap_local() and the other functions of the calls chain down to kunmap_local_indexed(). Furthermore, changes to kunmap_local_indexed() prototype require also changes to __kunmap_atomic() (if I recall correctly...).

Thanks for your review,

Fabio

Sorry, I forgot to paste a link:
[1] https://lore.kernel.org/lkml/[email protected]/

Fabio

#4 lore raw
In reply to Fabio M. De Francesco
Quoted 51 lines · Fabio M. De Francesco
Quoted 2 lines · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:
Quoted 1 line · Fabio M. De Francesco

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

With

Quoted 1 line
Quoted 1 line

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

globally

Quoted 5 lines
Quoted 5 lines

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

Quoted 11 lines

Review int zstd_compress_pages(struct list_head *ws,

Quoted 11 lines

local mapping / unmapping ordering rules.

Tested with xfstests on a QEMU + KVM 32 bits VM with 4GB of RAM and HIGHMEM64G enabled.

---

(quoted diff) +0 -0
@@ -477,15 +479,16 @@ int zstd_compress_pages(struct list_head *ws, 

struct address_space *mapping,

Quoted 8 lines

fs/btrfs/zstd.c int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · line 483

Quoted 6 lines
fs/btrfs/zstd.c +2 -1
 		/* 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?

As I wrote in an email I sent some days ago ("[RFC PATCH] btrfs: Replace kmap() with kmap_local_page() in zstd.c")[1] I get a series of errors like the following:

/usr/src/git/kernels/linux/fs/btrfs/zstd.c:547:33: warning: passing argument 1 of '__kunmap_local' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

  547 |   kunmap_local(workspace->in_buf.src);
      |                ~~~~~~~~~~~~~~~~~^~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:284:17: note: 
in definition of macro 'kunmap_local'
  284 |  __kunmap_local(__addr);     \
      |                 ^~~~~~
/usr/src/git/kernels/linux/include/linux/highmem-internal.h:92:41: note: 
expected 'void *' but argument is of type 'const void *'
   92 | static inline void __kunmap_local(void *vaddr)
      |                                   ~~~~~~^~~~~
Quoted 1 line

Therefore, this is a (bad?) hack to make these changes compile.

I think it's a bad practice and that API that does not modify parameters should declare the pointers const. Type casts should be used in justified cases and not to paper over fixable issues.

Quoted 9 lines

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to const void to kunmap_local(). Therefore, I was not sure if changing the API were better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

#5 lore raw
Quoted 3 lines · On martedì 14 giugno 2022 16:25:21 CEST David Sterba wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:

On Sat, Jun 11, 2022 at 03:52:03PM +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 6 lines
Quoted 6 lines

With

Quoted 1 line
Quoted 1 line

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

globally

Quoted 3 lines
Quoted 3 lines

visible.

Therefore, use kmap_local_page() / kunmap_local() in zstd.c because

in

Quoted 24 lines
Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

this file the mappings are per thread and are not visible in other contexts; meanwhile refactor zstd_compress_pages() to comply with

nested

Quoted 11 lines

Review int zstd_compress_pages(struct list_head *ws,

Quoted 11 lines

local mapping / unmapping ordering rules.

Tested with xfstests on a QEMU + KVM 32 bits VM with 4GB of RAM and HIGHMEM64G enabled.

---

(quoted diff) +0 -0
@@ -477,15 +479,16 @@ int zstd_compress_pages(struct list_head *ws, 

struct address_space *mapping,

Quoted 8 lines

fs/btrfs/zstd.c int zstd_compress_pages(struct list_head *ws, struct address_space *mapping, · line 483

Quoted 6 lines
fs/btrfs/zstd.c +2 -1
 		/* 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?

Quoted 1 line
Quoted 1 line

As I wrote in an email I sent some days ago ("[RFC PATCH] btrfs:

Replace

Quoted 1 line
Quoted 1 line

kmap() with kmap_local_page() in zstd.c")[1] I get a series of errors

like

Quoted 8 lines
Quoted 8 lines

the following:

/usr/src/git/kernels/linux/fs/btrfs/zstd.c:547:33: warning: passing argument 1 of '__kunmap_local' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

  547 |   kunmap_local(workspace->in_buf.src);
      |                ~~~~~~~~~~~~~~~~~^~~~

/usr/src/git/kernels/linux/include/linux/highmem-internal.h:284:17:

note:

Quoted 4 lines
Quoted 4 lines

in definition of macro 'kunmap_local'

  284 |  __kunmap_local(__addr);     \
      |                 ^~~~~~

/usr/src/git/kernels/linux/include/linux/highmem-internal.h:92:41:

note:

Quoted 4 lines
Quoted 4 lines

expected 'void *' but argument is of type 'const void *'
92 | static inline void __kunmap_local(void *vaddr)

      |                                   ~~~~~~^~~~~
Quoted 13 lines
Quoted 1 line

Therefore, this is a (bad?) hack to make these changes compile.

I think it's a bad practice and that API that does not modify parameters should declare the pointers const. Type casts should be used in justified cases and not to paper over fixable issues.

Quoted 7 lines

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to

const

Quoted 1 line
Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the API

were

Quoted 4 lines
Quoted 1 line

better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

I agree with you in full.

At the same time when you sent this email I submitted a patch to change kunmap_local() and kunmap_atomic().

After Andrew takes them I'll send v2 of this patch to zstd.c without those unnecessary casts.

Thanks for your review,

Fabio

#6 lore raw
Quoted 5 lines · Fabio M. De Francesco
Quoted 4 lines · On martedì 14 giugno 2022 16:25:21 CEST David Sterba wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:

On Sat, Jun 11, 2022 at 03:52:03PM +0200, Fabio M. De Francesco

[snip]

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to

const

Quoted 1 line
Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the API

were

Quoted 4 lines
Quoted 1 line

better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

Quoted 7 lines

I agree with you in full.

At the same time when you sent this email I submitted a patch to change kunmap_local() and kunmap_atomic().

After Andrew takes them I'll send v2 of this patch to zstd.c without those unnecessary casts.

David,

Would you be willing to take this through your tree as a pre-patch to the kmap changes in btrfs?

That would be easier for Fabio and probably you and Andrew in the long run.

Ira

Quoted 7 lines

Thanks for your review,

Fabio

#7 lore raw
Quoted 3 lines · On martedì 14 giugno 2022 19:07:34 CEST Ira Weiny wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On martedì 14 giugno 2022 16:25:21 CEST David Sterba wrote:

On Tue, Jun 14, 2022 at 01:22:50AM +0200, Fabio M. De Francesco

Quoted 4 lines · wrote:
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:
Quoted 1 line

On Sat, Jun 11, 2022 at 03:52:03PM +0200, Fabio M. De Francesco

Quoted 8 lines

[snip]

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15

conversions

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

across the kernel but it's the first time I had to pass a pointer

to

Quoted 2 lines
Quoted 2 lines

const

Quoted 1 line
Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the

API

Quoted 11 lines
Quoted 11 lines

were

Quoted 4 lines
Quoted 1 line

better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

I agree with you in full.

At the same time when you sent this email I submitted a patch to change kunmap_local() and kunmap_atomic().

After Andrew takes them I'll send v2 of this patch to zstd.c without

those

Quoted 5 lines
Quoted 1 line

unnecessary casts.

David,

Would you be willing to take this through your tree as a pre-patch to the

kmap

Quoted 3 lines

changes in btrfs?

That would be easier for Fabio and probably you and Andrew in the long

run.

Quoted 2 lines

Ira

David,

Please drop the first version of the changes[1] to the API and instead take my second version.[2] I've only reworked the commit message (and subject, but this was involuntary) to make explicit that the fundamental reason behind these changes are semantic correctness.

Thanks,

Fabio

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

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

#8 lore raw
In reply to Ira Weiny
Quoted 28 lines · Ira Weiny
Quoted 19 lines · Fabio M. De Francesco
Quoted 11 lines · On martedì 14 giugno 2022 16:25:21 CEST David Sterba wrote:
Quoted 10 lines · Fabio M. De Francesco
Quoted 1 line · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:

On Sat, Jun 11, 2022 at 03:52:03PM +0200, Fabio M. De Francesco

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to

const

Quoted 1 line
Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the API

were

Quoted 4 lines
Quoted 1 line

better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

Quoted 7 lines

I agree with you in full.

At the same time when you sent this email I submitted a patch to change kunmap_local() and kunmap_atomic().

After Andrew takes them I'll send v2 of this patch to zstd.c without those unnecessary casts.

Quoted 6 lines

David,

Would you be willing to take this through your tree as a pre-patch to the kmap changes in btrfs?

That would be easier for Fabio and probably you and Andrew in the long run.

Yes, no problem. I could probably send all the kmap conversions after tests so it does not need to wait until the next cycle.

#9 lore raw
In reply to Ira Weiny
Quoted 31 lines · Ira Weiny
Quoted 5 lines · Fabio M. De Francesco
Quoted 4 lines · On martedì 14 giugno 2022 16:25:21 CEST David Sterba wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On lunedì 13 giugno 2022 20:39:13 CEST David Sterba wrote:

On Sat, Jun 11, 2022 at 03:52:03PM +0200, Fabio M. De Francesco

[snip]

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines

A better solution is changing the prototype of __kunmap_local(); I suppose that Andrew won't object, but who knows?

(+Cc Andrew Morton).

I was waiting for your comments. At now I've done about 15 conversions across the kernel but it's the first time I had to pass a pointer to

const

Quoted 1 line
Quoted 1 line

void to kunmap_local(). Therefore, I was not sure if changing the API

were

Quoted 4 lines
Quoted 1 line

better suited (however I have already discussed this with Ira).

IMHO it should be fixed in the API.

Quoted 7 lines

I agree with you in full.

At the same time when you sent this email I submitted a patch to change kunmap_local() and kunmap_atomic().

After Andrew takes them I'll send v2 of this patch to zstd.c without those unnecessary casts.

Quoted 6 lines

David,

Would you be willing to take this through your tree as a pre-patch to the kmap changes in btrfs?

That would be easier for Fabio and probably you and Andrew in the long run.

Yes, no problem if the patch has an ack. I could probably send all the kmap conversions after tests so it does not need to wait until the next cycle.

#10 lore raw
In reply to Fabio M. De Francesco

Hi,

Compiler warning:

fs/btrfs/zstd.c:478:55: warning: passing argument 1 of ‘__kunmap_local’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
478 | kunmap_local(workspace->in_buf.src);
./include/linux/highmem-internal.h:284:24: note: in definition of macro ‘kunmap_local’
284 | __kunmap_local(__addr); \
| ^~~~~~
./include/linux/highmem-internal.h:200:41: note: expected ‘void *’ but argument is of type ‘const void *’
200 | static inline void __kunmap_local(void *addr)
| ~~~~~~^~~~

Best Regards
Wang Yugui ([email protected])
2022/07/14

Quoted 208 lines

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.

---

Thanks to Ira Weiny for his invaluable help and persevering support. Thanks also to Filipe Manana for identifying a fundamental detail I had overlooked:
https://lore.kernel.org/lkml/20220611093411.GA3779054@falcondesktop/

tweed32:/usr/lib/xfstests # ./check -g compress

FSTYP         -- btrfs
PLATFORM      -- Linux/i686 tweed32 5.19.0-rc1-vanilla-debug+ #22 SMP PREEMPT_DYNAMIC Sat Jun 11 14:31:39 CEST 2022
MKFS_OPTIONS  -- /dev/loop1

MOUNT_OPTIONS -- /dev/loop1 /mnt/scratch

btrfs/024 1s ...  0s
btrfs/026 3s ...  3s
btrfs/037 1s ...  1s
btrfs/038 1s ...  0s
btrfs/041 0s ...  1s
btrfs/062 34s ...  35s
btrfs/063 18s ...  18s
btrfs/067 33s ...  32s
btrfs/068 10s ...  10s
btrfs/070       [not run] btrfs and this test needs 5 or more disks in SCRATCH_DEV_POOL
btrfs/071       [not run] btrfs and this test needs 5 or more disks in SCRATCH_DEV_POOL
btrfs/072 33s ...  33s
btrfs/073 17s ...  15s
btrfs/074 36s ...  32s
btrfs/076 0s ...  0s
btrfs/103 1s ...  1s
btrfs/106 1s ...  1s
btrfs/109 1s ...  0s
btrfs/113 0s ...  1s
btrfs/138 43s ...  46s
btrfs/149 1s ...  1s
btrfs/183 1s ...  1s
btrfs/205 1s ...  1s
btrfs/234 3s ...  3s
btrfs/246 0s ...  0s
btrfs/251 1s ...  1s

Ran: btrfs/024 btrfs/026 btrfs/037 btrfs/038 btrfs/041 btrfs/062 btrfs/063 btrfs/067 btrfs/068 btrfs/070 btrfs/071 btrfs/072 btrfs/073 btrfs/074 btrfs/076 btrfs/103 btrfs/106 btrfs/109 btrfs/113 btrfs/138 btrfs/149 btrfs/183 btrfs/205 btrfs/234 btrfs/246 btrfs/251 Not run: btrfs/070 btrfs/071
Passed all 26 tests

 fs/btrfs/zstd.c | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)
fs/btrfs/zstd.c +23 -19
index 0fe31a6f6e68..4d50eb3edce5 100644
@@ -391,6 +391,8 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 	*out_pages = 0;
 	*total_out = 0;
 	*total_in = 0;
+	workspace->in_buf.src = NULL;
+	workspace->out_buf.dst = NULL;
 
 	/* Initialize the stream */
 	stream = zstd_init_cstream(&params, len, workspace->mem,
@@ -403,7 +405,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 
 	/* map in the first page of input data */
 	in_page = find_get_page(mapping, start >> PAGE_SHIFT);
-	workspace->in_buf.src = kmap(in_page);
+	workspace->in_buf.src = kmap_local_page(in_page);
 	workspace->in_buf.pos = 0;
 	workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
 
@@ -415,7 +417,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 		goto out;
 	}
 	pages[nr_pages++] = out_page;
-	workspace->out_buf.dst = kmap(out_page);
+	workspace->out_buf.dst = kmap_local_page(out_page);
 	workspace->out_buf.pos = 0;
 	workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
 
@@ -450,9 +452,9 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 		if (workspace->out_buf.pos == workspace->out_buf.size) {
 			tot_out += PAGE_SIZE;
 			max_out -= PAGE_SIZE;
-			kunmap(out_page);
+			kunmap_local(workspace->out_buf.dst);
 			if (nr_pages == nr_dest_pages) {
-				out_page = NULL;
+				workspace->out_buf.dst = NULL;
 				ret = -E2BIG;
 				goto out;
 			}
@@ -462,7 +464,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 				goto out;
 			}
 			pages[nr_pages++] = out_page;
-			workspace->out_buf.dst = kmap(out_page);
+			workspace->out_buf.dst = kmap_local_page(out_page);
 			workspace->out_buf.pos = 0;
 			workspace->out_buf.size = min_t(size_t, max_out,
 							PAGE_SIZE);
@@ -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);
 			put_page(in_page);
-
 			start += PAGE_SIZE;
 			len -= PAGE_SIZE;
 			in_page = find_get_page(mapping, start >> PAGE_SHIFT);
-			workspace->in_buf.src = kmap(in_page);
+			workspace->in_buf.src = kmap_local_page(in_page);
 			workspace->in_buf.pos = 0;
 			workspace->in_buf.size = min_t(size_t, len, PAGE_SIZE);
+			workspace->out_buf.dst = kmap_local_page(out_page);
 		}
 	}
 	while (1) {
@@ -510,9 +513,9 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 
 		tot_out += PAGE_SIZE;
 		max_out -= PAGE_SIZE;
-		kunmap(out_page);
+		kunmap_local(workspace->out_buf.dst);
 		if (nr_pages == nr_dest_pages) {
-			out_page = NULL;
+			workspace->out_buf.dst = NULL;
 			ret = -E2BIG;
 			goto out;
 		}
@@ -522,7 +525,7 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 			goto out;
 		}
 		pages[nr_pages++] = out_page;
-		workspace->out_buf.dst = kmap(out_page);
+		workspace->out_buf.dst = kmap_local_page(out_page);
 		workspace->out_buf.pos = 0;
 		workspace->out_buf.size = min_t(size_t, max_out, PAGE_SIZE);
 	}
@@ -538,12 +541,12 @@ int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
 out:
 	*out_pages = nr_pages;
 	/* Cleanup */
-	if (in_page) {
-		kunmap(in_page);
+	if (workspace->out_buf.dst)
+		kunmap_local(workspace->out_buf.dst);
+	if (workspace->in_buf.src) {
+		kunmap_local((void *)workspace->in_buf.src);
 		put_page(in_page);
 	}
-	if (out_page)
-		kunmap(out_page);
 	return ret;
 }
 
@@ -567,7 +570,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 		goto done;
 	}
 
-	workspace->in_buf.src = kmap(pages_in[page_in_index]);
+	workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
 	workspace->in_buf.pos = 0;
 	workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
 
@@ -603,14 +606,15 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 			break;
 
 		if (workspace->in_buf.pos == workspace->in_buf.size) {
-			kunmap(pages_in[page_in_index++]);
+			kunmap_local((void *)workspace->in_buf.src);
+			page_in_index++;
 			if (page_in_index >= total_pages_in) {
 				workspace->in_buf.src = NULL;
 				ret = -EIO;
 				goto done;
 			}
 			srclen -= PAGE_SIZE;
-			workspace->in_buf.src = kmap(pages_in[page_in_index]);
+			workspace->in_buf.src = kmap_local_page(pages_in[page_in_index]);
 			workspace->in_buf.pos = 0;
 			workspace->in_buf.size = min_t(size_t, srclen, PAGE_SIZE);
 		}
@@ -619,7 +623,7 @@ int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 	zero_fill_bio(cb->orig_bio);
 done:
 	if (workspace->in_buf.src)
-		kunmap(pages_in[page_in_index]);
+		kunmap_local((void *)workspace->in_buf.src);
 	return ret;
 }
 
Signature · 1 line
2.36.1
#11 lore raw
Quoted 5 lines · On giovedì 14 luglio 2022 02:25:20 CEST Wang Yugui wrote:

Hi,

Compiler warning:

fs/btrfs/zstd.c:478:55: warning: passing argument 1 of ‘__kunmap_local’

discards ‘const’ qualifier from pointer target type [-Wdiscarded- qualifiers]

Quoted 2 lines

478 | kunmap_local(workspace->in_buf.src);
./include/linux/highmem-internal.h:284:24: note: in definition of macro

‘kunmap_local’

Quoted 3 lines

284 | __kunmap_local(__addr); \
| ^~~~~~
./include/linux/highmem-internal.h:200:41: note: expected ‘void *’ but

argument is of type ‘const void *’

Quoted 6 lines

200 | static inline void __kunmap_local(void *addr)
| ~~~~~~^~~~

Best Regards
Wang Yugui ([email protected])
2022/07/14

Thanks for the build test, but you're a little late :)

This patch is superseded by a series of two: please see
[PATCH v6 0/2] btrfs: Replace kmap() with kmap_local_page() in zstd.c at https://lore.kernel.org/lkml/[email protected]/

In that series, patch 1/2 changes __kunmap_{local,atomic}() prototypes to take pointers to const void.

Regards,

Fabio

Quoted 1 line
Quoted 1 line

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

With

Quoted 1 line
Quoted 1 line

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

globally

Quoted 5 lines
Quoted 5 lines

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

Quoted 2 lines
Quoted 2 lines

local mapping / unmapping ordering rules.

#12 lore raw
In reply to Wang Yugui
Quoted 4 lines · Wang Yugui

Hi,

Compiler warning:

Quoted 8 lines

fs/btrfs/zstd.c:478:55: warning: passing argument 1 of ‘__kunmap_local’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
478 | kunmap_local(workspace->in_buf.src);
./include/linux/highmem-internal.h:284:24: note: in definition of macro ‘kunmap_local’
284 | __kunmap_local(__addr); \
| ^~~~~~
./include/linux/highmem-internal.h:200:41: note: expected ‘void *’ but argument is of type ‘const void *’
200 | static inline void __kunmap_local(void *addr)
| ~~~~~~^~~~

This requires patch
https://lore.kernel.org/all/[email protected]/
that adds const to kmap/kunmap API.