fs/isofs: Replace kmap() with kmap_local_page()
RFC PATCH 1/1 fs/isofs: Replace kmap() with kmap_local_page()
The use of kmap() is being deprecated in favor of kmap_local_page().
There are two main problems with kmap(): (1) It comes with an overhead as mapping space is restricted and protected by a global lock for synchronization and (2) it also requires global TLB invalidation when the kmap’s pool wraps and it might block when the mapping space is fully utilized until a slot becomes available.
With kmap_local_page() the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Tasks can be preempted and, when scheduled to run again, the kernel virtual addresses are restored and still valid. It is faster than kmap() in kernels with HIGHMEM enabled.
Since kmap_local_page() can be safely used in compress.c, it should be called everywhere instead of kmap().
Therefore, replace kmap() with kmap_local_page() in compress.c. Where it is needed, use memzero_page() instead of open coding kmap_local_page() plus memset() to fill the pages with zeros. Delete the redundant flush_dcache_page() in the two call sites of memzero_page().
This is an RFC because these changes have not been tested (tests are welcome!), therefore I'm not entirely sure whether these conversions work properly. I'd like to hear comments from more experienced developers before sending a real patch. Suggestions about how to run tests would also be much appreciated.
--- fs/isofs/compress.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-)
fs/isofs/compress.c +13 -11
@@ -67,8 +67,7 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,for ( i =0 ; i < pcount ; i++ ) {if (!pages[i])continue ; -memset (page_address (pages[i]),0 ,PAGE_SIZE ); -flush_dcache_page (pages[i]); +memzero_page (pages[i],0 ,PAGE_SIZE );SetPageUptodate (pages[i]); }return ((loff_t )pcount) <<PAGE_SHIFT ;
@@ -120,8 +119,7 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, zerr != Z_STREAM_END) {if (!stream.avail_out) {if (pages[curpage]) { - stream.next_out =page_address (pages[curpage]) - + poffset; + stream.next_out =kmap_local_page (pages[curpage] + poffset); stream.avail_out =PAGE_SIZE - poffset; poffset =0 ; }else {
@@ -170,6 +168,12 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, } } +if (stream.next_out) +if (stream.next_out != (char *)zisofs_sink_page) { +kunmap_local (stream.next_out); + stream.next_out =NULL ; + } +if (!stream.avail_out) {/* This page completed */ if (pages[curpage]) {
@@ -183,6 +187,9 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, } inflate_out:zlib_inflateEnd (&stream); +if (stream.next_out) +if (stream.next_out != (char *)zisofs_sink_page) +kunmap_local (stream.next_out); z_eio:mutex_unlock (&zisofs_zlib_lock);
@@ -283,9 +290,7 @@ static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount, }if (poffset && *pages) { -memset (page_address (*pages) + poffset,0 , -PAGE_SIZE - poffset); -flush_dcache_page (*pages); +memzero_page (*pages, poffset,PAGE_SIZE - poffset);SetPageUptodate (*pages); }return 0 ;
@@ -343,10 +348,8 @@ static int zisofs_read_folio(struct file *file, struct folio *folio)for (i =0 ; i < pcount; i++, index++) {if (i != full_page) pages[i] =grab_cache_page_nowait (mapping, index); -if (pages[i]) { +if (pages[i])ClearPageError (pages[i]); -kmap (pages[i]); - } } err =zisofs_fill_pages (inode, full_page, pcount, pages);
@@ -357,7 +360,6 @@ static int zisofs_read_folio(struct file *file, struct folio *folio)flush_dcache_page (pages[i]);if (i == full_page && err)SetPageError (pages[i]); -kunmap (pages[i]);unlock_page (pages[i]);if (i != full_page)put_page (pages[i]);
Signature · 1 line
2.37.1
Jan Kara
maintainerQuoted 22 lines · Fabio M. De Francesco
The use of
kmap()is being deprecated in favor ofkmap_local_page().There are two main problems with
kmap(): (1) It comes with an overhead as mapping space is restricted and protected by a global lock for synchronization and (2) it also requires global TLB invalidation when the kmap’s pool wraps and it might block when the mapping space is fully utilized until a slot becomes available.With
kmap_local_page()the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Tasks can be preempted and, when scheduled to run again, the kernel virtual addresses are restored and still valid. It is faster thankmap()in kernels with HIGHMEM enabled.Since
kmap_local_page()can be safely used incompress.c, it should be called everywhere instead ofkmap().Therefore, replace
kmap()withkmap_local_page()incompress.c. Where it is needed, usememzero_page()instead of open codingkmap_local_page()plusmemset()to fill the pages with zeros. Delete the redundantflush_dcache_page()in the two call sites ofmemzero_page().
Quoted 8 lines
This is an RFC because these changes have not been tested (tests are welcome!), therefore I'm not entirely sure whether these conversions work properly. I'd like to hear comments from more experienced developers before sending a real patch. Suggestions about how to run tests would also be much appreciated.
Suggested-by Ira Weiny <[email protected]>Signed-off-by Fabio M. De Francesco <[email protected]>
What you propose makes sense to me. But the lack of testing is troublesome. You can always at least test your patch without highmem on isofs image with compression (mkisofs seems to support creating such filesystems). Even that would detect a bug in your patch ;) - see below.
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, · line 122
Quoted 11 lines
fs/isofs/compress.c +1
-2@@ -120,8 +119,7 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, zerr != Z_STREAM_END) {if (!stream.avail_out) {if (pages[curpage]) { - stream.next_out =page_address (pages[curpage]) - + poffset; + stream.next_out =kmap_local_page (pages[curpage] + poffset);
This is wrong. Most importantly because you need to add 'poffset' to the final address provided by kmap_local_page(), not to the struct page pointer.
Secondly please wrap the line to fit into 80 chars.
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, · line 176
Quoted 13 lines
fs/isofs/compress.c +6
-0stream.avail_out =PAGE_SIZE - poffset; poffset =0 ; }else {@@ -170,6 +168,12 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, } } +if (stream.next_out) +if (stream.next_out != (char *)zisofs_sink_page) { +kunmap_local (stream.next_out); + stream.next_out =NULL ; + } +
This looks buggy as well. If we mapped page above, we'll unmap it here even if stream.avail_out > 0 and we want to still write to it. I think you should unmap the page here only if stream.avail_out == 0 and we are going to switch to the next page...
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, · line 192
Quoted 10 lines
fs/isofs/compress.c +3
-0if (!stream.avail_out) {/* This page completed */ if (pages[curpage]) {@@ -183,6 +187,9 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, } inflate_out:zlib_inflateEnd (&stream); +if (stream.next_out) +if (stream.next_out != (char *)zisofs_sink_page) +kunmap_local (stream.next_out);
This is correct but I'd simplify it to:
if (stream.next_out && stream.next_out != (char *)zisofs_sink_page) kunmap_local(stream.next_out); Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 4 lines · On martedì 26 luglio 2022 16:50:24 CEST Jan Kara wrote:
Quoted 3 lines · Fabio M. De Francesco
The use of
kmap()is being deprecated in favor ofkmap_local_page().There are two main problems with
kmap(): (1) It comes with an overhead
as
Quoted 2 lines
Quoted 2 lines
mapping space is restricted and protected by a global lock for synchronization and (2) it also requires global TLB invalidation when
the
Quoted 7 lines
Quoted 7 lines
kmap’s pool wraps and it might block when the mapping space is fully utilized until a slot becomes available.
With
kmap_local_page()the mappings are per thread, CPU local, can take page faults, and can be called from any context (including interrupts). Tasks can be preempted and, when scheduled to run again, the kernel virtual addresses are restored and still valid. It is faster than
kmap()
Quoted 6 lines
Quoted 6 lines
in kernels with HIGHMEM enabled.
Since
kmap_local_page()can be safely used incompress.c, it should be called everywhere instead ofkmap().Therefore, replace
kmap()withkmap_local_page()incompress.c. Where
it
Quoted 6 lines
Quoted 6 lines
is needed, use
memzero_page()instead of open codingkmap_local_page()plusmemset()to fill the pages with zeros. Delete the redundantflush_dcache_page()in the two call sites ofmemzero_page().This is an RFC because these changes have not been tested (tests are welcome!), therefore I'm not entirely sure whether these conversions
work
Quoted 8 lines
Quoted 6 lines
properly. I'd like to hear comments from more experienced developers before sending a real patch. Suggestions about how to run tests would also be much appreciated.
Suggested-by Ira Weiny <[email protected]>Signed-off-by Fabio M. De Francesco <[email protected]>What you propose makes sense to me. But the lack of testing is
troublesome.
Quoted 1 line
You can always at least test your patch without highmem on isofs image
with
Quoted 1 line
compression (mkisofs seems to support creating such filesystems).
I understand your concerns about the lack of testing. I'll find a way to do those tests on a QEMU/KVM x86_32 VM with a kernel with HIGHMEM64GB enabled. (this is what I did for other filesystem, Btrfs to start with).
I have no experience with this particular fs and saw that xfstests have no special tests for isofs. However, you are talking about using mkisofs and I suppose it will be enough for this purpose.
Quoted 8 lines
Even that
would detect a bug in your patch ;) - see below.fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode
Quoted 5 lines
fs/isofs/compress.c +0
-0@@ -120,8 +119,7 @@ static loff_t zisofs_uncompress_block(struct inode
*inode, loff_t block_start,
Quoted 4 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
Quoted 4 lines
fs/isofs/compress.c +0
-1zerr != Z_STREAM_END) {if (!stream.avail_out) {if (pages[curpage]) { - stream.next_out =
page_address(pages[curpage])
Quoted 2 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode · line 122
Quoted 2 lines
fs/isofs/compress.c +1
-1- + poffset; + stream.next_out =
kmap_local_page(pages[curpage] + poffset);
Quoted 3 lines
This is wrong. Most importantly because you need to add 'poffset' to the final address provided by
kmap_local_page(), not to the struct page
pointer.
Sorry, this is only a typo.
I've already made dozens of conversions to kmap_local_page(), so I know that I should add the offset to the kernel virtual address:
stream.next_out = kmap_local_page(pages[curpage]) + poffset;
Quoted 4 lines
Secondly please wrap the line to fit into 80 chars.
Quoted 1 line
fs/isofs/compress.c +0
-0stream.avail_out = PAGE_SIZE -
poffset;
Quoted 3 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode
Quoted 3 lines
fs/isofs/compress.c +0
-0poffset =0 ; }else {@@ -170,6 +168,12 @@ static loff_t zisofs_uncompress_block(struct inode
*inode, loff_t block_start,
Quoted 5 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
Quoted 5 lines
fs/isofs/compress.c +2
-0} } +if (stream.next_out) + if (stream.next_out != (char
*)zisofs_sink_page) {
Quoted 6 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, · line 176
Quoted 4 lines
fs/isofs/compress.c +4
-0+kunmap_local (stream.next_out); + stream.next_out =NULL ; + } +This looks buggy as well. If we mapped page above, we'll unmap it here
even
Quoted 3 lines
if stream.avail_out > 0 and we want to still write to it. I think you should unmap the page here only if stream.avail_out == 0 and we are going to switch to the next page...
I need to look carefully at this because I'm not 100% sure of what you are talking about. At this moment I cannot look at the code, however I suppose that when I can, I'll have no problems to figure out what I missed.
Quoted 4 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode
Quoted 4 lines
fs/isofs/compress.c +0
-0if (!stream.avail_out) {/* This page completed */ if (pages[curpage]) {@@ -183,6 +187,9 @@ static loff_t zisofs_uncompress_block(struct inode
*inode, loff_t block_start,
Quoted 10 lines
fs/isofs/compress.c static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, · line 192
Quoted 6 lines
fs/isofs/compress.c +3
-0} inflate_out:zlib_inflateEnd (&stream); +if (stream.next_out) +if (stream.next_out != (char *)zisofs_sink_page) +kunmap_local (stream.next_out);This is correct but I'd simplify it to:
if (stream.next_out && stream.next_out != (char
*)zisofs_sink_page)
Quoted 1 line
kunmap_local(stream.next_out);
It's shorter and more readable. I'll rewrite the code as you suggest.
Thanks,
Fabio
Quoted 1 line
Honza
Quoted 4 lines
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR