Implement MTE tag compression for swapped pages
Subsystems (2)
v8 cover Implement MTE tag compression for swapped pages
Currently, when MTE pages are swapped out, the tags are kept in the memory, occupying PAGE_SIZE/32 bytes per page. This is especially problematic for devices that use zram-backed in-memory swap, because tags stored uncompressed in the heap effectively reduce the available amount of swap memory.
The RLE-based algorithm suggested by Evgenii Stepanov and implemented in this patch series is able to efficiently compress fixed-size tag buffers, resulting in practical compression ratio of 2x. In many cases it is possible to store the compressed data in 63-bit Xarray values, resulting in no extra memory allocations.
This patch series depends on "lib/bitmap: add bitmap_{read,write}()"
(https://lore.kernel.org/linux-arm-kernel/[email protected]/T/)
that is mailed separately.
v8:
- split off the bitmap_read()/bitmap_write() series
- simplified the compression logic (only compress data if it fits into a pointer)
v7:
- fixed comments by Yury Norov, Andy Shevchenko, Rasmus Villemoes
- added perf tests for bitmap_read()/bitmap_write()
- more efficient bitmap_write() implementation (meant to be sent in v5)
v6:
- fixed comments by Yury Norov
- fixed handling of sizes divisible by MTE_GRANULES_PER_PAGE / 2 (caught while testing on a real device)
v5:
- fixed comments by Andy Shevchenko, Catalin Marinas, and Yury Norov
- added support for 16K- and 64K pages
- more efficient bitmap_write() implementation
v4:
- fixed a bunch of comments by Andy Shevchenko and Yury Norov
- added Documentation/arch/arm64/mte-tag-compression.rst
v3:
- as suggested by Andy Shevchenko, use bitmap_get_value()/bitmap_set_value() written by Syed Nayyar Waris
- switched to unsigned long to reduce typecasts
- simplified the compression code
v2:
- as suggested by Yuri Norov, replace the poorly implemented struct bitq with <linux/bitmap.h>
Alexander Potapenko (3):
arm64: mte: implement CONFIG_ARM64_MTE_COMP
arm64: mte: add a test for MTE tags compression
arm64: mte: add compression support to mteswap.c
Documentation/arch/arm64/index.rst | 1 + .../arch/arm64/mte-tag-compression.rst | 154 ++++++++ arch/arm64/Kconfig | 21 + arch/arm64/include/asm/mtecomp.h | 39 ++ arch/arm64/mm/Makefile | 2 + arch/arm64/mm/mtecomp.c | 257 +++++++++++++ arch/arm64/mm/mtecomp.h | 12 + arch/arm64/mm/mteswap.c | 88 ++++- arch/arm64/mm/test_mtecomp.c | 364 ++++++++++++++++++ 9 files changed, 934 insertions(+), 4 deletions(-) create mode 100644 Documentation/arch/arm64/mte-tag-compression.rst create mode 100644 arch/arm64/include/asm/mtecomp.h create mode 100644 arch/arm64/mm/mtecomp.c create mode 100644 arch/arm64/mm/mtecomp.h create mode 100644 arch/arm64/mm/test_mtecomp.c
Signature · 1 line
2.42.0.869.gea05f2083d-goog
PATCH v8 1/3 arm64: mte: implement CONFIG_ARM64_MTE_COMP
The config implements the algorithm compressing memory tags for ARM MTE during swapping.
The algorithm is based on RLE and specifically targets buffers of tags corresponding to a single page. In many cases a buffer can be compressed into 63 bits, making it possible to store it without additional memory allocation.
---
v8:
- As suggested by Catalin Marinas, only compress tags if they can be stored inline. This simplifies the code drastically.
- Update the documentation.
- Split off patches introducing bitmap_read()/bitmap_write().
v6:
- shuffle bits in inline handles so that they can't be confused with
canonical pointers;
- use kmem_cache_zalloc() to allocate compressed storage
- correctly handle range size overflow
- minor documentation fixes, clarify the special cases
v5:
- make code PAGE_SIZE-agnostic, remove hardcoded constants, updated the docs
- implement debugfs interface
- Address comments by Andy Shevchenko:
- update description of mtecomp.c
- remove redundant assignments, simplify mte_tags_to_ranges()
- various code simplifications
- introduce mtecomp.h
- add :export: to Documentation/arch/arm64/mte-tag-compression.rst
v4:
- Addressed comments by Andy Shevchenko:
- expanded "MTE" to "Memory Tagging Extension" in Kconfig
- fixed kernel-doc comments, moved them to C source
- changed variables to unsigned where applicable
- some code simplifications, fewer unnecessary assignments
- added the mte_largest_idx_bits() helper
- added namespace prefixes to all functions
- added missing headers (but removed bits.h)
- Addressed comments by Yury Norov:
- removed test-only functions from mtecomp.h
- dropped the algoritm name (all functions are now prefixed with "mte")
- added more comments
- got rid of MTE_RANGES_INLINE
- renamed bitmap_{get,set}_value() to bitmap_{read,write}()
- moved the big comment explaining the algorithm to Documentation/arch/arm64/mte-tag-compression.rst, expanded it, add a link to it from Documentation/arch/arm64/index.rst
- removed hardcoded ranges from mte_alloc_size()/mte_size_to_ranges()
v3:
- Addressed comments by Andy Shevchenko:
- use bitmap_{set,get}_value() writte by Syed Nayyar Waris
- switched to unsigned long everywhere (fewer casts)
- simplified the code, removed redundant checks
- dropped ea0_compress_inline()
- added bit size constants and helpers to access the bitmap
- explicitly initialize all compressed sizes in ea0_compress_to_buf()
- initialize all handle bits
v2:
- as suggested by Yury Norov, switched from struct bitq (which is not needed anymore) to <linux/bitmap.h>
- add missing symbol exports
--- Documentation/arch/arm64/index.rst | 1 + .../arch/arm64/mte-tag-compression.rst | 154 +++++++++++ arch/arm64/Kconfig | 11 + arch/arm64/include/asm/mtecomp.h | 39 +++ arch/arm64/mm/Makefile | 1 + arch/arm64/mm/mtecomp.c | 257 ++++++++++++++++++ arch/arm64/mm/mtecomp.h | 12 + 7 files changed, 475 insertions(+) create mode 100644 Documentation/arch/arm64/mte-tag-compression.rst create mode 100644 arch/arm64/include/asm/mtecomp.h create mode 100644 arch/arm64/mm/mtecomp.c create mode 100644 arch/arm64/mm/mtecomp.h
Documentation/arch/arm64/index.rst +1 -0
@@ -19,6 +19,7 @@ ARM64 Architecture legacy_instructions memory memory-tagging-extension + mte-tag-compression perf pointer-authentication ptdump
Documentation/arch/arm64/mte-tag-compression.rst new file +154 -0
154 lines not on this page · raw message
arch/arm64/Kconfig +11 -0
@@ -2073,6 +2073,17 @@ config ARM64_EPAN if the cpu does not implement the feature. endmenu # "ARMv8.7 architectural features" +config ARM64_MTE_COMP + bool "Tag compression for ARM64 Memory Tagging Extension" + default y + depends on ARM64_MTE + help + Enable tag compression support for ARM64 Memory Tagging Extension. + + Tag buffers corresponding to swapped RAM pages are compressed using + RLE to conserve heap memory. In the common case compressed tags + occupy 2.5x less memory. + config ARM64_SVE bool "ARM Scalable Vector Extension support" default y
arch/arm64/include/asm/mtecomp.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __ASM_MTECOMP_H +#define __ASM_MTECOMP_H + +#include <linux/types.h> + +/** +* mte_is_compressed() - check if the supplied pointer contains compressed tags. +* @ptr: pointer returned by kmalloc() or mte_compress(). +* +* Returns: true iff bit 0 of @ptr is 1, which is only possible if @ptr was +* returned by mte_is_compressed(). +*/ +static inline bool mte_is_compressed (void *ptr) +{ +return ((unsigned long )ptr &1 ); +} + +#if defined(CONFIG_ARM64_MTE_COMP ) + +void *mte_compress (u8 *tags); +bool mte_decompress (void *handle,u8 *tags); + +#else + +static inline void *mte_compress (u8 *tags) +{ +return NULL ; +} + +static inline bool mte_decompress (void *data,u8 *tags) +{ +return false; +} + +#endif // CONFIG_ARM64_MTE_COMP + +#endif // __ASM_MTECOMP_H
arch/arm64/mm/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-$(CONFIG_TRANS_TABLE) += trans_pgd.o obj-$(CONFIG_TRANS_TABLE) += trans_pgd-asm.o obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_ARM64_MTE) += mteswap.o +obj-$(CONFIG_ARM64_MTE_COMP) += mtecomp.o KASAN_SANITIZE_physaddr.o += n obj-$(CONFIG_KASAN) += kasan_init.o
arch/arm64/mm/mtecomp.c new file +257 -0
257 lines not on this page · raw message
arch/arm64/mm/mtecomp.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef ARCH_ARM64_MM_MTECOMP_H_ +#define ARCH_ARM64_MM_MTECOMP_H_ + +/* Functions exported from mtecomp.c for test_mtecomp.c. */ +void mte_tags_to_ranges (u8 *tags,u8 *out_tags,unsigned short *out_sizes, +size_t *out_len); +void mte_ranges_to_tags (u8 *r_tags,unsigned short *r_sizes,size_t r_len, +u8 *tags); + +#endif // ARCH_ARM64_MM_TEST_MTECOMP_H_
Signature · 1 line
2.42.0.869.gea05f2083d-goog
PATCH v8 3/3 arm64: mte: add compression support to mteswap.c
In reply to Alexander PotapenkoUpdate mteswap.c to perform inline compression of memory tags when possible.
If CONFIG_ARM64_MTE_COMP is enabled, mteswap.c will attemt to compress saved tags for a struct page and store them directly in Xarray entry instead of wasting heap space.
Soon after booting Android, tag compression saves ~2x memory previously spent by mteswap.c on tag allocations. On a moderately loaded device with ~20% tagged pages, this leads to saving several megabytes of kernel heap:
# cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368
---
v8:
- adapt to the new compression API, abandon mteswap_{no,}comp.c
- move stats collection to mteswap.c
v5:
- drop a dead variable from _mte_free_saved_tags() in mteswap_comp.c
- ensure MTE compression works with arbitrary page sizes
- update patch description
v4:
- minor code simplifications suggested by Andy Shevchenko, added missing header dependencies
- changed compression API names to reflect modifications made to memcomp.h (as suggested by Yury Norov)
v3:
- Addressed comments by Andy Shevchenko in another patch:
- fixed includes order
- replaced u64 with unsigned long
- added MODULE_IMPORT_NS(MTECOMP)
--- arch/arm64/mm/mteswap.c | 88 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 4 deletions(-)
arch/arm64/mm/mteswap.c +84 -4
@@ -1,28 +1,48 @@// SPDX-License-Identifier: GPL-2.0-only +#include <linux/debugfs.h> #include <linux/pagemap.h> #include <linux/xarray.h> #include <linux/slab.h> #include <linux/swap.h> #include <linux/swapops.h> #include <asm/mte.h> +#include <asm/mtecomp.h> +#include "mtecomp.h" + +enum mteswap_counters { +MTESWAP_CTR_INLINE =0 , +MTESWAP_CTR_NOINLINE , +MTESWAP_CTR_SIZE +}; +static atomic_long_t alloc_counters[MTESWAP_CTR_SIZE ]; +static atomic_long_t dealloc_counters[MTESWAP_CTR_SIZE ];static DEFINE_XARRAY (mte_pages);void *mte_allocate_tag_storage (void ) { -/* tags granule is 16 bytes, 2 tags stored per byte */ -return kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +void *ret; + + ret =kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +if (ret) +atomic_long_inc (&alloc_counters[MTESWAP_CTR_NOINLINE ]); +return ret; }void mte_free_tag_storage (char *storage) { -kfree (storage); +if (!mte_is_compressed (storage)) { +kfree (storage); +atomic_long_dec (&alloc_counters[MTESWAP_CTR_NOINLINE ]); + }else { +atomic_long_dec (&alloc_counters[MTESWAP_CTR_INLINE ]); + } }int mte_save_tags (struct page *page) { -void *tag_storage, *ret; +void *tag_storage, *ret, *compressed;if (!page_mte_tagged (page))return 0 ;
@@ -32,6 +52,12 @@ int mte_save_tags(struct page *page)return -ENOMEM ;mte_save_page_tags (page_address (page), tag_storage); + compressed =mte_compress (tag_storage); +if (compressed) { +mte_free_tag_storage (tag_storage); + tag_storage = (void *)compressed; +atomic_long_inc (&alloc_counters[MTESWAP_CTR_INLINE ]); + }/* lookup the swap entry.val from the page */ ret = xa_store(&mte_pages, page_swap_entry(page).val, tag_storage,
@@ -50,13 +76,20 @@ int mte_save_tags(struct page *page)void mte_restore_tags (swp_entry_t entry,struct page *page) {void *tags =xa_load (&mte_pages, entry.val); +void *tag_storage =NULL ;if (!tags)return ;if (try_page_mte_tagging (page)) { +if (mte_is_compressed (tags)) { + tag_storage =mte_allocate_tag_storage (); +mte_decompress (tags, tag_storage); + tags = tag_storage; + }mte_restore_page_tags (page_address (page), tags);set_page_mte_tagged (page); +mte_free_tag_storage (tag_storage); } }
@@ -83,3 +116,50 @@ void mte_invalidate_tags_area(int type) }xa_unlock (&mte_pages); } + +/* DebugFS interface. */ +static int stats_show (struct seq_file *seq,void *v) +{ +unsigned long total_mem_alloc =0 , total_mem_dealloc =0 ; +unsigned long total_num_alloc =0 , total_num_dealloc =0 ; +unsigned long sizes[2 ] = {8 ,MTE_PAGE_TAG_STORAGE }; +long alloc, dealloc; +unsigned long size; +int i; + +for (i =0 ; i <MTESWAP_CTR_SIZE ; i++) { + alloc =atomic_long_read (&alloc_counters[i]); + dealloc =atomic_long_read (&dealloc_counters[i]); + total_num_alloc += alloc; + total_num_dealloc += dealloc; + size = sizes[i]; +/* +* Do not count 8-byte buffers towards compressed tag storage +* size. +*/ +if (i) { + total_mem_alloc += (size * alloc); + total_mem_dealloc += (size * dealloc); + } +seq_printf (seq, +"%lu bytes: %lu allocations, %lu deallocations\n" , + size, alloc, dealloc); + } +seq_printf (seq,"uncompressed tag storage size: %lu\n" , + (total_num_alloc - total_num_dealloc) * +MTE_PAGE_TAG_STORAGE ); +seq_printf (seq,"compressed tag storage size: %lu\n" , + total_mem_alloc - total_mem_dealloc); +return 0 ; +} +DEFINE_SHOW_ATTRIBUTE (stats); + +static int mteswap_init (void ) +{ +struct dentry *mteswap_dir; + + mteswap_dir =debugfs_create_dir ("mteswap" ,NULL ); +debugfs_create_file ("stats" ,0444 , mteswap_dir,NULL , &stats_fops); +return 0 ; +} +module_init (mteswap_init);
Signature · 1 line
2.42.0.869.gea05f2083d-goog
PATCH v8 2/3 arm64: mte: add a test for MTE tags compression
In reply to Alexander PotapenkoEnsure that tag sequences containing alternating values are compressed to buffers of expected size and correctly decompressed afterwards.
---
v8:
- adapt to the simplified compression algorithm
v6:
- add test_decompress_invalid() to ensure invalid handles are ignored;
- add test_upper_bits(), which is a regression test for a case where
an inline handle looked like an out-of-line one;
- add test_compress_nonzero() to ensure a full nonzero tag array is
compressed correctly; - add test_two_ranges() to test cases when the input buffer is divided into two ranges.
v5:
- remove hardcoded constants, added test setup/teardown;
- support 16- and 64K pages;
- replace nested if-clauses with expected_size_from_ranges();
- call mte_release_handle() after tests that perform
compression/decompression;
- address comments by Andy Shevchenko:
- fix include order;
- use mtecomp.h instead of function prototypes.
v4:
- addressed comments by Andy Shevchenko:
- expanded MTE to "Memory Tagging Extension" in Kconfig
- changed signed variables to unsigned where applicable
- added missing header dependencies
- addressed comments by Yury Norov:
- moved test-only declarations from mtecomp.h into this test
- switched to the new "mte"-prefixed function names, dropped the mentions of "EA0"
- added test_tag_to_ranges_n()
v3:
- addressed comments by Andy Shevchenko in another patch:
- switched from u64 to unsigned long
- added MODULE_IMPORT_NS(MTECOMP)
- fixed includes order
--- arch/arm64/Kconfig | 10 + arch/arm64/mm/Makefile | 1 + arch/arm64/mm/test_mtecomp.c | 364 +++++++++++++++++++++++++++++++++++ 3 files changed, 375 insertions(+) create mode 100644 arch/arm64/mm/test_mtecomp.c
arch/arm64/Kconfig +10 -0
@@ -2084,6 +2084,16 @@ config ARM64_MTE_COMP RLE to conserve heap memory. In the common case compressed tags occupy 2.5x less memory. +config ARM64_MTE_COMP_KUNIT_TEST + tristate "Test tag compression for ARM64 Memory Tagging Extension" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + depends on KUNIT && ARM64_MTE_COMP + help + Test MTE compression algorithm enabled by CONFIG_ARM64_MTE_COMP. + + Ensure that tag sequences containing alternating values are compressed + to buffers of expected size and correctly decompressed afterwards. + config ARM64_SVE bool "ARM Scalable Vector Extension support" default y
arch/arm64/mm/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_TRANS_TABLE) += trans_pgd-asm.o obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o obj-$(CONFIG_ARM64_MTE) += mteswap.o obj-$(CONFIG_ARM64_MTE_COMP) += mtecomp.o +obj-$(CONFIG_ARM64_MTE_COMP_KUNIT_TEST) += test_mtecomp.o KASAN_SANITIZE_physaddr.o += n obj-$(CONFIG_KASAN) += kasan_init.o
arch/arm64/mm/test_mtecomp.c new file +364 -0
364 lines not on this page · raw message
Signature · 1 line
2.42.0.869.gea05f2083d-goog
Re: [PATCH v8 3/3] arm64: mte: add compression support to mteswap.c
In reply to Alexander PotapenkoQuoted 4 lines · Alexander Potapenko
Update
mteswap.cto perform inline compression of memory tags when possible.If
CONFIG_ARM64_MTE_COMPis enabled,mteswap.cwill attemt to compress
attempt?
Quoted 7 lines
saved tags for a struct page and store them directly in Xarray entry instead of wasting heap space.
Soon after booting Android, tag compression saves ~2x memory previously spent by
mteswap.con tag allocations. On a moderately loaded device with ~20% tagged pages, this leads to saving several megabytes of kernel heap:
Quoted 5 lines
# cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368
Can you align them like this:
# cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368
And also, can you mention a new file in the documentation?
IIRC, it was my suggestion to measure some stats... If so, can you add:
Quoted 41 lines
Signed-off-by Alexander Potapenko <[email protected]>---
v8:
- adapt to the new compression API, abandon mteswap_{no,}comp.c
- move stats collection tomteswap.cv5:
- drop a dead variable from_mte_free_saved_tags()inmteswap_comp.c
- ensure MTE compression works with arbitrary page sizes
- update patch descriptionv4:
- minor code simplifications suggested by Andy Shevchenko, added missing header dependencies
- changed compression API names to reflect modifications made tomemcomp.h(as suggested by Yury Norov)v3:
- Addressed comments by Andy Shevchenko in another patch:
- fixed includes order
- replaced u64 with unsigned long
- addedMODULE_IMPORT_NS(MTECOMP)--- arch/arm64/mm/mteswap.c | 88 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 4 deletions(-)arch/arm64/mm/mteswap.c +1
-0@@ -1,28 +1,48 @@// SPDX-License-Identifier: GPL-2.0-only +#include <linux/debugfs.h> #include <linux/pagemap.h> #include <linux/xarray.h> #include <linux/slab.h> #include <linux/swap.h> #include <linux/swapops.h> #include <asm/mte.h>
arch/arm64/mm/mteswap.c line 19
Quoted 10 lines
arch/arm64/mm/mteswap.c +10
-0+#include <asm/mtecomp.h> +#include "mtecomp.h" + +enum mteswap_counters { +MTESWAP_CTR_INLINE =0 , +MTESWAP_CTR_NOINLINE , +MTESWAP_CTR_SIZE +}; +static atomic_long_t alloc_counters[MTESWAP_CTR_SIZE ]; +static atomic_long_t dealloc_counters[MTESWAP_CTR_SIZE ];
I think it's worth to add them and all the book keeping code in a separate patch? Also can you consider making them configurable, maybe depending on CONFIG_ARM64_MTE_SWAP_STAT?..
Quoted 5 lines
arch/arm64/mm/mteswap.c +0
-1static DEFINE_XARRAY (mte_pages);void *mte_allocate_tag_storage (void ) { -/* tags granule is 16 bytes, 2 tags stored per byte */
arch/arm64/mm/mteswap.c line 33
Quoted 10 lines
arch/arm64/mm/mteswap.c +6
-1-return kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +void *ret; + + ret =kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +if (ret) +atomic_long_inc (&alloc_counters[MTESWAP_CTR_NOINLINE ]); +return ret; }void mte_free_tag_storage (char *storage)
If you use a term 'free' here, the counter name should probably reflect that.
arch/arm64/mm/mteswap.c line 37
Quoted 5 lines
arch/arm64/mm/mteswap.c +3
-1{ -kfree (storage); +if (!mte_is_compressed (storage)) { +kfree (storage); +atomic_long_dec (&alloc_counters[MTESWAP_CTR_NOINLINE ]);
s/NOINLINE/OUTLINE ?
Quoted 13 lines
arch/arm64/mm/mteswap.c +4
-1+ }else { +atomic_long_dec (&alloc_counters[MTESWAP_CTR_INLINE ]); + } }int mte_save_tags (struct page *page) { -void *tag_storage, *ret; +void *tag_storage, *ret, *compressed;if (!page_mte_tagged (page))return 0 ;
arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 58
Quoted 7 lines
arch/arm64/mm/mteswap.c +4
-0@@ -32,6 +52,12 @@ int mte_save_tags(struct page *page)return -ENOMEM ;mte_save_page_tags (page_address (page), tag_storage); + compressed =mte_compress (tag_storage); +if (compressed) { +mte_free_tag_storage (tag_storage); + tag_storage = (void *)compressed;
But 'compressed' is already 'void *', what for typecasting? Also, it's a bad naming - adjective implies bool type. I'd name it 'compressed_tag', or similar.
arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 60
Quoted 2 lines
arch/arm64/mm/mteswap.c +2
-0+atomic_long_inc (&alloc_counters[MTESWAP_CTR_INLINE ]); + }
Is it possible to move all this conditional inside the function call? I feel like it should be a single-line:
tag_storage = mte_compress(tag_storage);
So that client code will be free of housekeeping details.
Quoted 8 lines
arch/arm64/mm/mteswap.c +1
-0/* lookup the swap entry.val from the page */ ret = xa_store(&mte_pages, page_swap_entry(page).val, tag_storage,@@ -50,13 +76,20 @@ int mte_save_tags(struct page *page)void mte_restore_tags (swp_entry_t entry,struct page *page) {void *tags =xa_load (&mte_pages, entry.val); +void *tag_storage =NULL ;
arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 89
Quoted 10 lines
arch/arm64/mm/mteswap.c +5
-0if (!tags)return ;if (try_page_mte_tagging (page)) { +if (mte_is_compressed (tags)) { + tag_storage =mte_allocate_tag_storage (); +mte_decompress (tags, tag_storage); + tags = tag_storage; + }
Same here, if it's possible, I'd prefer a single line call instead of the above chunk:
tags = mte_decompress(tags);
if (!tags)
return;
And make sure that the decompress code will take care of details.
This would have a clear meaning: we add a compression/decompression option which is as simple as calling corresponding functions in mte_save/restore_tags(); and the rest of mte code is untouched
Thanks,
Yury
Quoted 59 lines
arch/arm64/mm/mteswap.c +48
-0mte_restore_page_tags (page_address (page), tags);set_page_mte_tagged (page); +mte_free_tag_storage (tag_storage); } }@@ -83,3 +116,50 @@ void mte_invalidate_tags_area(int type) }xa_unlock (&mte_pages); } + +/* DebugFS interface. */ +static int stats_show (struct seq_file *seq,void *v) +{ +unsigned long total_mem_alloc =0 , total_mem_dealloc =0 ; +unsigned long total_num_alloc =0 , total_num_dealloc =0 ; +unsigned long sizes[2 ] = {8 ,MTE_PAGE_TAG_STORAGE }; +long alloc, dealloc; +unsigned long size; +int i; + +for (i =0 ; i <MTESWAP_CTR_SIZE ; i++) { + alloc =atomic_long_read (&alloc_counters[i]); + dealloc =atomic_long_read (&dealloc_counters[i]); + total_num_alloc += alloc; + total_num_dealloc += dealloc; + size = sizes[i]; +/* +* Do not count 8-byte buffers towards compressed tag storage +* size. +*/ +if (i) { + total_mem_alloc += (size * alloc); + total_mem_dealloc += (size * dealloc); + } +seq_printf (seq, +"%lu bytes: %lu allocations, %lu deallocations\n" , + size, alloc, dealloc); + } +seq_printf (seq,"uncompressed tag storage size: %lu\n" , + (total_num_alloc - total_num_dealloc) * +MTE_PAGE_TAG_STORAGE ); +seq_printf (seq,"compressed tag storage size: %lu\n" , + total_mem_alloc - total_mem_dealloc); +return 0 ; +} +DEFINE_SHOW_ATTRIBUTE (stats); + +static int mteswap_init (void ) +{ +struct dentry *mteswap_dir; + + mteswap_dir =debugfs_create_dir ("mteswap" ,NULL ); +debugfs_create_file ("stats" ,0444 , mteswap_dir,NULL , &stats_fops); +return 0 ; +} +module_init (mteswap_init);Signature · 1 line
2.42.0.869.gea05f2083d-goog
Re: [PATCH v8 3/3] arm64: mte: add compression support to mteswap.c
Quoted 4 lines
Quoted 2 lines
If
CONFIG_ARM64_MTE_COMPis enabled,mteswap.cwill attemt to compressattempt?
Good catch!
Quoted 7 lines
Quoted 6 lines
# cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368
Quoted 7 lines
Can you align them like this:
# cat /sys/kernel/debug/mteswap/stats 8 bytes: 102496 allocations, 67302 deallocations 128 bytes: 212234 allocations, 178278 deallocations uncompressed tag storage size: 8851200 compressed tag storage size: 4346368
Ok, will do in v9.
Quoted 1 line
And also, can you mention a new file in the documentation?
Sure.
Quoted 3 lines
IIRC, it was my suggestion to measure some stats... If so, can you add:
Suggested-by Yury Norov <[email protected]> # for stats
Will do.
Quoted 6 lines
arch/arm64/mm/mteswap.c line 19
Quoted 2 lines
arch/arm64/mm/mteswap.c +2
-0+static atomic_long_t alloc_counters[MTESWAP_CTR_SIZE ]; +static atomic_long_t dealloc_counters[MTESWAP_CTR_SIZE ];I think it's worth to add them and all the book keeping code in a separate patch? Also can you consider making them configurable, maybe depending on
CONFIG_ARM64_MTE_SWAP_STAT?..
Sounds fine, I'll split this code into a separate patch.
Quoted 14 lines
Quoted 14 lines
static
DEFINE_XARRAY(mte_pages);void
*mte_allocate_tag_storage(void)
{arch/arm64/mm/mteswap.c +6
-2-/* tags granule is 16 bytes, 2 tags stored per byte */ -return kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +void *ret; + + ret =kmalloc (MTE_PAGE_TAG_STORAGE ,GFP_KERNEL ); +if (ret) +atomic_long_inc (&alloc_counters[MTESWAP_CTR_NOINLINE ]); +return ret; }
Quoted 4 lines
Quoted 1 line
void
mte_free_tag_storage(char *storage)If you use a term 'free' here, the counter name should probably reflect that.
I still want to keep the terms "allocation/deallocation" in the user-facing code (in the /sys/ file) though.
So renaming the counter here will cause a mismatch between its name and the stats output.
Quoted 8 lines
arch/arm64/mm/mteswap.c line 37
Quoted 6 lines
{
arch/arm64/mm/mteswap.c +3
-1-kfree (storage); +if (!mte_is_compressed (storage)) { +kfree (storage); +atomic_long_dec (&alloc_counters[MTESWAP_CTR_NOINLINE ]);s/NOINLINE/OUTLINE ?
Done.
Quoted 7 lines
arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 58
Quoted 5 lines
mte_save_page_tags(page_address(page), tag_storage);arch/arm64/mm/mteswap.c +4
-0+ compressed =mte_compress (tag_storage); +if (compressed) { +mte_free_tag_storage (tag_storage); + tag_storage = (void *)compressed;But 'compressed' is already 'void *', what for typecasting?
Yeah, it used to be unsigned long or something like that. Removed the cast.
Quoted 6 lines
Also, it's a bad naming - adjective implies bool type. I'd name it '
compressed_tag', or similar.arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 60
Quoted 2 lines
arch/arm64/mm/mteswap.c +2
-0+atomic_long_inc (&alloc_counters[MTESWAP_CTR_INLINE ]); + }
Quoted 6 lines
Is it possible to move all this conditional inside the function call? I feel like it should be a single-line:
tag_storage = mte_compress(tag_storage);So that client code will be free of housekeeping details.
The problem is that this assignment destroys the current value of tag_storage, which means mte_compress() will have to deal with memory deallocation.
That, in turn, will introduce an unnecessary dependency between mtecomp.c and mteswap.c (so that the former can call mte_free_tag_storage()).
Given that we still have to deal with allocations in mteswap.c, I don't think it's worth the hassle.
Quoted 23 lines
Quoted 9 lines
/* lookup the swap entry.val from the page */
ret = xa_store(&mte_pages, page_swap_entry(page).val,tag_storage,arch/arm64/mm/mteswap.c +1
-0@@ -50,13 +76,20 @@ int mte_save_tags(struct page *page)void mte_restore_tags (swp_entry_t entry,struct page *page) {void *tags =xa_load (&mte_pages, entry.val); +void *tag_storage =NULL ;arch/arm64/mm/mteswap.c int mte_save_tags(struct page *page) · line 89
Quoted 10 lines
arch/arm64/mm/mteswap.c +5
-0if (!tags)return ;if (try_page_mte_tagging (page)) { +if (mte_is_compressed (tags)) { + tag_storage =mte_allocate_tag_storage (); +mte_decompress (tags, tag_storage); + tags = tag_storage; + }Same here, if it's possible, I'd prefer a single line call instead of the above chunk:
Quoted 3 lines
tags = mte_decompress(tags);
if (!tags)
return;Same here, we'd have to make mte_decompress() allocate/deallocate memory in a way consistent with mteswap.c allocations.
Re: [PATCH v8 3/3] arm64: mte: add compression support to mteswap.c
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
void
mte_free_tag_storage(char *storage)If you use a term 'free' here, the counter name should probably reflect that.
I still want to keep the terms "allocation/deallocation" in the user-facing code (in the
/sys/file) though.
So renaming the counter here will cause a mismatch between its name and the stats output.
To move forward, I am going to send out v9 that addresses your comments except for this one and for moving the conditionals inside mte_compress() and mte_decompress().
Happy to discuss those three points further.