dm-compress: introduce the dm-compress target
Subsystems (3)
Mikulas Patocka
maintainerPATCH 1/1 dm-compress: introduce the dm-compress target
This commit introduces a new target dm-compress that provides transparent compression and decompression of block devices.
--- Documentation/admin-guide/device-mapper/dm-compress.rst | 61 + drivers/md/Kconfig | 2 drivers/md/Makefile | 1 drivers/md/dm-bufio.c | 16 drivers/md/dm-compress/Kconfig | 17 drivers/md/dm-compress/Makefile | 10 drivers/md/dm-compress/alloc.c | 480 ++++++++ drivers/md/dm-compress/cc.c | 251 ++++ drivers/md/dm-compress/chunk.c | 440 +++++++ drivers/md/dm-compress/dm-compress.h | 217 +++ drivers/md/dm-compress/io.c | 652 ++++++++++ drivers/md/dm-compress/struct.h | 128 ++ drivers/md/dm-compress/target.c | 959 ++++++++++++++++ include/linux/dm-bufio.h | 14 14 files changed, 3248 insertions(+)
drivers/md/Kconfig +2 -0
@@ -719,4 +719,6 @@ source "drivers/md/dm-vdo/Kconfig" source "drivers/md/dm-pcache/Kconfig" +source "drivers/md/dm-compress/Kconfig" + endif # MD
drivers/md/dm-compress/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# dm-compress configuration +# + +config DM_COMPRESS + tristate "Compression target" + depends on BLK_DEV_DM + select DM_BUFIO + select CRYPTO_DEFLATE + select CRYPTO_LZO + select CRYPTO_842 + select CRYPTO_LZ4 + select CRYPTO_LZ4HC + select CRYPTO_ZSTD + help + This target stores compressed data.
drivers/md/dm-compress/dm-compress.h new file +217 -0
217 lines not on this page · raw message
drivers/md/dm-compress/target.c new file +959 -0
959 lines not on this page · raw message
drivers/md/Makefile +1 -0
@@ -85,6 +85,7 @@ obj-$(CONFIG_DM_INTEGRITY) += dm-integri obj-$(CONFIG_DM_ZONED) += dm-zoned.o obj-$(CONFIG_DM_WRITECACHE) += dm-writecache.o obj-$(CONFIG_SECURITY_LOADPIN_VERITY) += dm-verity-loadpin.o +obj-$(CONFIG_DM_COMPRESS) += dm-compress/ ifeq ($(CONFIG_DM_INIT),y) dm-mod-objs += dm-init.o
drivers/md/dm-compress/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_DM_COMPRESS) += dm-compress.o + +dm-compress-objs := \ + alloc.o \ + cc.o \ + chunk.o \ + io.o \ + target.o
drivers/md/dm-compress/alloc.c new file +480 -0
480 lines not on this page · raw message
drivers/md/dm-compress/cc.c new file +251 -0
251 lines not on this page · raw message
drivers/md/dm-compress/io.c new file +652 -0
652 lines not on this page · raw message
Documentation/admin-guide/device-mapper/dm-compress.rst new file +61 -0
61 lines not on this page · raw message
drivers/md/dm-compress/chunk.c new file +440 -0
340 lines not on this page · raw message
drivers/md/dm-compress/struct.h new file +128 -0
128 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 11 potential issues to consider: - [Critical] dm-compress: uncommitted metadata flushed and sb->cc rewound on error - [High] dm-compress…
Thank you for your contribution! Sashiko AI review found 11 potential issues to consider:
- [Critical] dm-compress: uncommitted metadata flushed and sb->cc rewound on error
- [High] dm-compress: scomp scratch page overflow via page-split scatterlists
- [High] dm-compress: unallocated bitmap in dm_compress_resize_bitmaps()
- [High] dm-compress: stale n_allocated_sectors in dm_compress_resize_bitmaps()
- [High] dm-compress: missing cc->failed check in dm_compress_chunk_decompress()
- [High] dm-compress: premature chunk release in dm_compress_read_failed()
- [High] dm-compress: bio reordering and blocking in dm_compress_pick_dio_for_chunk()
- [High] dm-compress: active work_struct re-initialized in dm_compress_chunk_work()
- [Medium] dm-compress: prepare_bitmap() dirties bitmaps before allocation check
- [Medium] dm-compress: metadata size checked against raw sectors in initialize_device()
- [Low] dm-compress: missing index.rst entry and commit_time documentation
--
commit aeac9b542cd6769324ef487c13c874a11f0ef7b9
Author: Mikulas Patocka <[email protected]>
dm-compress: introduce the dm-compress target
This commit adds the dm-compress device-mapper target for transparent block device compression and decompression, along with a forced buffer eviction helper in dm-bufio.
Quoted 5 lines
Documentation/admin-guide/device-mapper/dm-compress.rst new file +0
-0
[ ... ]
Documentation/admin-guide/device-mapper/dm-compress.rst line 55
Quoted 9 lines
Documentation/admin-guide/device-mapper/dm-compress.rst new file +9
-0+Additional arguments: + +meta_device:device + Store metadata on a separate device + +n_cached_chunks:number (default 128) + The number of cached decompressed chunks that is kept in memory. The + cached chunks are preallocated when the target is loaded. The memory + consumption of the target is n_cached_chunks*chunk_size*3.
[Severity: Low]
Should Documentation/admin-guide/device-mapper/dm-compress.rst be added to Documentation/admin-guide/device-mapper/index.rst to avoid a Sphinx warning during htmldocs builds, and should the optional commit_time:<msec> argument parsed in dm_compress_ctr() also be documented here?
Quoted 6 lines
drivers/md/dm-compress/alloc.c new file +0
-0@@ -0,0 +1,480 @@
[ ... ]
Quoted 2 lines
drivers/md/dm-compress/alloc.c new file +2
-0+static int prepare_bitmap (struct dm_compress *c,sector_t bmp_block,__u64 **working_copy,__u64 **stable_copy,struct dm_buffer **bp) +{
[ ... ]
Quoted 11 lines
drivers/md/dm-compress/alloc.c new file +11
-0+ idx = !CC_VALID (c, &bmp->cc_txc); +if (unlikely (!CC_CURRENT (c, &bmp->cc_txc))) { +CC_SET_CURRENT (c, &bmp->cc_txc); +//WARN_ON(idx != CC_VALID(c, &bmp->cc_txc)); +if (!idx) +memcpy (bmp->bmp1, bmp->bmp2,BITS_PER_BITMAP /8 ); +else +memcpy (bmp->bmp2, bmp->bmp1,BITS_PER_BITMAP /8 ); + idx = !idx; +dm_bufio_mark_buffer_dirty (*bp); + }
[ ... ]
Quoted 1 line
+}
[ ... ]
Quoted 2 lines
drivers/md/dm-compress/alloc.c new file +2
-0+sector_t dm_compress_allocate_space (struct dm_compress *c,sector_t preferred_block,unsigned n_blocks) +{
[ ... ]
Quoted 8 lines
drivers/md/dm-compress/alloc.c new file +8
-0+mutex_lock (&group->mutex); +if (unlikely (prepare_bitmap (c, bmp_block, &working_copy, &stable_copy, &bp))) { +mutex_unlock (&group->mutex); +return ALLOCATE_ERROR ; + } + +find_again: + bit =find_next_zero_bit_le (working_copy,BITS_PER_BITMAP , bmp_offset);
[ ... ]
drivers/md/dm-compress/alloc.c line 132
Quoted 7 lines
drivers/md/dm-compress/alloc.c new file +7
-0+dm_bufio_release (bp); +mutex_unlock (&group->mutex); + +if (++n_retries <= c->n_groups) { + preferred_block +=BITS_PER_BITMAP ; + goto try_next_bitmap; + }
[Severity: Medium]
Does calling prepare_bitmap() before checking whether the bitmap has enough free bits dirty every bitmap visited during an allocation scan?
When dm_compress_allocate_space() steps past full or fragmented bitmaps, prepare_bitmap() updates bmp->cc_txc, copies 2040 bytes between bmp1 and bmp2, and marks the buffer dirty even when no bits are allocated. In addition, struct block_group_descriptor has a free field initialized to -1 in dm_compress_ctr() that is never maintained or consulted, so failed contiguous allocations can linearly scan and dirty every bitmap on the device before commit.
[ ... ]
Quoted 2 lines
drivers/md/dm-compress/alloc.c new file +2
-0+int dm_compress_resize_bitmaps (struct dm_compress *c,sector_t current_size,sector_t new_size,bool *need_sync) +{
[ ... ]
Quoted 15 lines
drivers/md/dm-compress/alloc.c new file +15
-0+ need_sectors =0 ; +if (!bmp_offset && !dir_offset) + need_sectors =BUFFER_SECTORS *2 ; +else if (!bmp_offset) + need_sectors =BUFFER_SECTORS ; +if (n_allocated_sectors < need_sectors) { +if (c->meta_dev) { +dm_compress_error (c,"The metadata device is too small" , -ENOSPC ); +return -ENOSPC ; + } +align_allocated: + allocated_sector = current_size << c->log2_sectors_per_block; +if (allocated_sector & (BUFFER_SECTORS -1 )) { +if (current_size >= new_size) + goto finish;
drivers/md/dm-compress/alloc.c line 362
Quoted 10 lines
drivers/md/dm-compress/alloc.c new file +10
-0+ current_size++; + goto align_allocated; + } +while (n_allocated_sectors < need_sectors) { +if (current_size >= new_size) + goto finish; + current_size++; + n_allocated_sectors +=1 << c->log2_sectors_per_block; + } + }
[Severity: High]
When align_allocated sets a new allocated_sector because n_allocated_sectors < need_sectors, should n_allocated_sectors be reset to 0?
With !c->meta_dev and a block size larger than BUFFER_SIZE (e.g. 8 KiB blocks, 16 sectors), reserving one block adds 16 sectors to n_allocated_sectors while allocating a 4 KiB bitmap consumes 8 sectors, leaving n_allocated_sectors == 8. Because DIRECTORY_ENTRIES (679) is odd, reaching a directory boundary requires need_sectors == 16 while n_allocated_sectors is 8. Jumping to align_allocated moves allocated_sector to current_size without zeroing n_allocated_sectors, so the loop only reserves one 8 KiB block instead of two, leaving allocated_sector pointing at the next block while that block is marked free in the bitmap and later overwritten by the next metadata bitmap.
Quoted 2 lines
drivers/md/dm-compress/alloc.c new file +2
-0+ + did_something = true;
[ ... ]
Quoted 15 lines
drivers/md/dm-compress/alloc.c new file +15
-0+finish: +if (did_something) { +bool b; + *need_sync = true; + b = !CC_VALID (c, &sb->n_cc_txc); +if (unlikely (!CC_CURRENT (c, &sb->n_cc_txc))) { +CC_SET_CURRENT (c, &sb->n_cc_txc); +WARN_ON (b !=CC_VALID (c, &sb->n_cc_txc)); + sb->n_blocks[!b] = sb->n_blocks[b]; + sb->n_chunks[!b] = sb->n_chunks[b]; + sb->n_used_metadata_blocks[!b] = sb->n_used_metadata_blocks[b]; + b = !b; + } + sb->n_blocks[b] =cpu_to_le64 (current_size); +if (c->meta_dev)
drivers/md/dm-compress/alloc.c line 480
Quoted 10 lines
drivers/md/dm-compress/alloc.c new file +10
-0+ sb->n_used_metadata_blocks[b] =cpu_to_le64 (allocated_sector /BUFFER_SECTORS ); + r =dm_compress_sync_rw_metadata (c,REQ_OP_WRITE ,DM_IO_KMEM ,0 , c->sb,SB_SECTORS ); +if (r) { +dm_compress_error (c,"Error writing superblock" , r); +return r; + } + c->n_blocks = current_size; + } +return 0 ; +}
[Severity: High]
What happens in dm_compress_resize_bitmaps() if current_size reaches new_size inside the align_allocated or n_allocated_sectors < need_sectors loops and jumps to finish?
Because current_size is incremented in place before checking whether enough new blocks exist to satisfy need_sectors, jumping to finish leaves current_size == new_size without allocating the new bitmap or directory. If did_something is true from an earlier iteration, finish writes sb->n_blocks[b] = new_size to the superblock; if did_something is false, c->n_blocks still remains at new_size from dm_compress_ctr(). In both cases dm_compress_preresume() then tries to load or scan the unallocated bitmap/directory at block 0 (the superblock) and fails with -EUCLEAN.
Quoted 6 lines
drivers/md/dm-compress/cc.c new file +0
-0@@ -0,0 +1,251 @@
[ ... ]
Quoted 17 lines
drivers/md/dm-compress/cc.c new file +17
-0+static struct scatterlist *dm_compress_alloc_sglist (void *data,unsigned len,unsigned offset) +{ +struct scatterlist *sg; +if (!is_vmalloc_addr (data)) { + sg =kmalloc (sizeof (struct scatterlist ),GFP_KERNEL ); +if (!sg) +return NULL ; +sg_init_one (sg, data + offset, len - offset); + }else { +unsigned i; +unsigned n_entries =DIV_ROUND_UP (len,PAGE_SIZE ); +size_t s =array_size (sizeof (struct scatterlist ), n_entries); + sg =kmalloc (s,GFP_KERNEL ); +if (!sg) +return NULL ; +sg_init_table (sg, n_entries); +for (i =0 ; i < n_entries; i++) {
drivers/md/dm-compress/cc.c line 37
Quoted 10 lines
drivers/md/dm-compress/cc.c new file +10
-0+struct page *pg =vmalloc_to_page (data); +unsigned pg_len =min (len,PAGE_SIZE ); +sg_set_page (&sg[i], pg, pg_len - offset, offset); + data +=PAGE_SIZE ; + len -=PAGE_SIZE ; + offset =0 ; + } + } +return sg; +}
[Severity: High]
Can passing PAGE_SIZE-split scatterlists via acomp_request_set_params() overflow scomp's order-0 scratch->src page or fail with -ENOSYS?
Four of the six supported algorithms (lzo, 842, lz4, lz4hc) are scomp algorithms bridged through scomp_acomp_comp_decomp(). When virtual pointers are not used, scomp_acomp_comp_decomp() requires dlen <= req->dst->length (only the first scatterlist entry, which is PAGE_SIZE - 4 on compress and PAGE_SIZE on decompress) and copies linear source data into scratch->src allocated as a single order-0 page by scomp_alloc_scratch():
crypto/scompress.c:scomp_acomp_comp_decomp() {
...
if (!dst_isvirt) {
if (dlen <= req->dst->length) {
...
} else
return -ENOSYS;
}
if (src_isvirt || slen <= req->src->length) {
...
} else {
memcpy_from_sglist(scratch->src, req->src, 0, slen);
src = scratch->src;
}
...
}When chunk_size is 2 * PAGE_SIZE (8192) and block_size is PAGE_SIZE (4096), len_out is 4092 == cc->compressed_sglist[0].length, so the destination check passes and memcpy_from_sglist() copies slen (8192 bytes) into the 4096-byte scratch->src page, overflowing it by 4096 bytes. For larger chunk sizes, compression fails with -ENOSYS and silently falls back to ALGORITHM_UNCOMPRESSED, and decompression of any scomp-compressed chunk with chunk_size > PAGE_SIZE fails with -ENOSYS. Since the buffers come from __vmalloc(), passing them via acomp_request_set_src_nondma() and acomp_request_set_dst_nondma() would avoid the scatterlist path entirely.
[ ... ]
Quoted 31 lines
drivers/md/dm-compress/cc.c new file +31
-0+void dm_compress_wait_for_cached_chunk (struct dm_compress_io *dio) +{ +struct dm_compress *c = dio->c; +struct rb_node *parent, **node; + +list_add (&dio->waiting_list_entry, &c->waiting_bio_lru); + + parent =NULL ; + node = &c->waiting_bio_tree.rb_node; + +while (*node) { + struct dm_compress_io *tdio = container_of(*node, struct dm_compress_io, tree_entry); + parent = &tdio->tree_entry; +if (tdio->chunk > dio->chunk) { + node = &parent->rb_left; + }else { + node = &parent->rb_right; + } + } + +rb_link_node (&dio->tree_entry, parent, node); +rb_insert_color (&dio->tree_entry, &c->waiting_bio_tree); +} + +struct dm_compress_io *dm_compress_pick_dio_for_chunk (struct dm_compress *c,sector_t chunk) +{ +struct rb_node *node = c->waiting_bio_tree.rb_node; + +while (node) { + struct dm_compress_io *tdio = container_of(node, struct dm_compress_io, tree_entry); +if (tdio->chunk == chunk) {
drivers/md/dm-compress/cc.c line 219
Quoted 10 lines
drivers/md/dm-compress/cc.c new file +10
-0+return tdio; + } +if (tdio->chunk > chunk) + node = tdio->tree_entry.rb_left; +else + node = tdio->tree_entry.rb_right; + } + +return NULL ; +}
[Severity: High]
Can inserting duplicate chunk keys into c->waiting_bio_tree reorder or block waiting bios in dm_compress_pick_dio_for_chunk()?
When multiple bios wait on the same chunk, rb_insert_color() and rb_erase() rotations can place a newer dio above an older dio with the same chunk key. dm_compress_pick_dio_for_chunk() returns the first matching node closest to the root, so waiting writes to the same chunk can execute out of FIFO order and overwrite newer data with older data.
In addition, if the topmost node matching chunk cannot run yet (for example, a write bio when cc->intent >= INTENT_WRITE in dm_compress_chunk_decompress(), or a waiting flush bio which dm_compress_setup_flush() inserts with fake key dio->chunk = 0), dm_compress_process_bios_on_chunk() stops immediately without serving runnable read bios for that chunk located lower in the tree.
Quoted 6 lines
drivers/md/dm-compress/io.c new file +0
-0@@ -0,0 +1,652 @@
[ ... ]
Quoted 2 lines
drivers/md/dm-compress/io.c new file +2
-0+static void dm_compress_chunk_work (struct work_struct *w) +{
[ ... ]
Quoted 28 lines
drivers/md/dm-compress/io.c new file +28
-0+INIT_WORK (&cc->work, dm_compress_chunk_work_2); +if (unlikely (!dm_compress_allocate_chunk (cc, preferred_block, &new_cp))) { +complete (&cc->completion); +dm_compress_chunk_work_2 (&cc->work); +return ; + } + + cp =dm_compress_prepare_chunk (c, cc->chunk, &bp); +if (unlikely (!cp)) + goto cont; + + old_cp = *cp; + *cp = new_cp; + +dm_bufio_mark_buffer_dirty (bp); +dm_bufio_release (bp); + +dm_compress_release_chunk (c, &old_cp); + +cont: +complete (&cc->completion); +} + +static void dm_compress_chunk_work_2 (struct work_struct *w) +{ + struct cached_chunk *cc = container_of(w, struct cached_chunk, work); +struct dm_compress *c = cc->c; +
drivers/md/dm-compress/io.c line 421
Quoted 10 lines
drivers/md/dm-compress/io.c new file +10
-0+wait_for_completion (&cc->completion); + +mutex_lock (&c->mutex); +if (cc->intent >=INTENT_FREE ) { +dm_compress_release_cached_chunk (cc); + }else { + cc->intent =INTENT_IDLE ; + } +dm_compress_process_pending_ios (c); +}
[Severity: High]
Is it safe to call INIT_WORK(&cc->work, ...) and queue &cc->work while a work callback on &cc->work is still running?
kworker/0 kworker/1process_one_work(&cc->work)
dm_compress_chunk_work(&cc->work)
INIT_WORK(&cc->work,
dm_compress_chunk_work_2)
dm_compress_allocate_chunk()
...
queue_work(..., &cc->work)
process_one_work(&cc->work)
dm_compress_chunk_work_2()
wait_for_completion()
dm_compress_prepare_chunk()
dm_compress_release_chunk()
complete(&cc->completion)
cc->intent = INTENT_IDLE
dm_compress_process_pending_ios()
dm_compress_enqueue_chunk()
INIT_WORK(&cc->work, ...)Calling INIT_WORK() inside dm_compress_chunk_work() (and similarly in dm_compress_read_and_decompress() and via dm_compress_enqueue_chunk() before the worker returns) overwrites cc->work.data and cc->work.lockdep_map while process_one_work() is still tracking the work item, breaking workqueue non-reentrancy tracking and allowing concurrent execution on the same work_struct.
[ ... ]
Quoted 6 lines
drivers/md/dm-compress/io.c new file +6
-0+static void dm_compress_read_failed (struct cached_chunk *cc) +{ +struct dm_compress *c = cc->c; +sector_t chunk = cc->chunk; +mutex_lock (&c->mutex); +if (unlikely (cc->dirty)) {
drivers/md/dm-compress/io.c line 473
Quoted 10 lines
drivers/md/dm-compress/io.c new file +10
-0+ cc->dirty = false; +dm_compress_error (c,"Could not read a chunk with pending writes" , -EIO ); + } +if (cc->intent >=INTENT_FREE ) { +dm_compress_release_cached_chunk (cc); + }else { + cc->intent =INTENT_IDLE ; + } +dm_compress_process_bios_on_chunk (c, chunk); +}
[Severity: High]
If cc->intent was upgraded to INTENT_FREE while a read was in flight, does releasing cc before dm_compress_process_bios_on_chunk() prevent waiting read bios from seeing cc->failed?
When c->free_chunks is empty, another bio can select cc from c->lru.prev and upgrade cc->intent from INTENT_READ to INTENT_FREE. If the read or decompression then fails, dm_compress_read_failed() calls dm_compress_release_cached_chunk(cc) before dm_compress_process_bios_on_chunk(c, chunk), removing cc from c->chunks. When dm_compress_process_bios_on_chunk() then runs the waiting read bio for chunk, dm_compress_find_cached_chunk() returns NULL instead of finding cc->failed, and dm_compress_new_cached_chunk() re-allocates cc with cc->failed reset to 0, retrying the failed read in an unbounded loop.
Quoted 2 lines
drivers/md/dm-compress/io.c new file +2
-0+ +static void dm_compress_read_and_decompress (struct cached_chunk *cc)
[ ... ]
drivers/md/dm-compress/io.c line 513
Quoted 8 lines
drivers/md/dm-compress/io.c new file +8
-0+static void dm_compress_chunk_decompress (struct work_struct *w) +{ + struct cached_chunk *cc = container_of(w, struct cached_chunk, work); +struct dm_compress *c = cc->c; +unsigned i; +unsigned intent; + +if (cc->algorithm ==ALGORITHM_ZERO ) {
[Severity: High]
Should dm_compress_chunk_decompress() check cc->failed at entry before copying or decompressing cc->compressed_data?
When a non-split read bio fails, dm_compress_read_endio() records the error in cc->failed via dm_compress_error_chunk() and queues cc->work, which runs dm_compress_chunk_decompress(). Because dm_compress_chunk_decompress() does not check cc->failed, it proceeds to copy or decompress stale data from cc->compressed_data into cc->uncompressed_data, marks all cc->sector_tags as TAG_VALID, and skips dm_compress_read_failed(cc). If cc->dirty was set from an earlier partial write, skipping dm_compress_read_failed(cc) also skips disabling the device and allows a subsequent write to reset cc->dirty and cc->sector_tags, silently losing the earlier write.
Quoted 6 lines
drivers/md/dm-compress/target.c new file +0
-0@@ -0,0 +1,959 @@
[ ... ]
Quoted 25 lines
drivers/md/dm-compress/target.c new file +25
-0+void dm_compress_sync (struct dm_compress *c) +{ +struct block_device *bdev = c->meta_dev ? c->meta_dev->bdev : c->dev->bdev; +__u16 cc; +unsigned granularity; +unsigned offset; +int r; + + r =dm_bufio_write_dirty_buffers (c->bufio); +if (unlikely (r)) { +dm_compress_error (c,"Error flushing buffers" , r); + goto skip_commit; + } + +if (unlikely (c->split_bufio_dirty)) { + c->split_bufio_dirty = false; + r =dm_bufio_write_dirty_buffers (c->split_bufio); +if (unlikely (r)) { +dm_compress_error (c,"Error flushing split chunk buffers" , r); + goto skip_commit; + } + goto skip_flush_dev; + } + +if (c->meta_dev) {
drivers/md/dm-compress/target.c line 95
Quoted 10 lines
drivers/md/dm-compress/target.c new file +10
-0+ r =blkdev_issue_flush (c->dev->bdev); +if (unlikely (r)) { +dm_compress_error (c,"Error flushing the data device" , r); + goto skip_commit; + } + } +skip_flush_dev: + +if (unlikely (c->failed)) + goto skip_commit;
[Severity: Critical]
When c->failed is set, does flushing dirty buffers in dm_compress_sync() and rewinding c->sb->cc in dm_compress_postsuspend() cause uncommitted metadata from a failed transaction to be treated as valid on the next resume?
In dm_compress_preresume(), c->sb->cc is incremented on disk from K to K + 1 and c->cc_txc is advanced to transaction T + 0x10000, while on-disk c->cct->entries[K] remains at T until commit. If a runtime error (such as -ENOSPC mid-allocation in dm_compress_allocate_chunk() or a write error) sets c->failed, dirty metadata buffers in c->bufio are already stamped with (K, T + 0x10000). When dm_compress_postsuspend() runs:
drivers/md/dm-compress/target.c:dm_compress_postsuspend() {
...
dm_compress_sync(c);
mutex_unlock(&c->mutex);
cancel_delayed_work_sync(&c->autocommit_work);
c->cct->entries[c->cc_txc & 0xffff] = cpu_to_le64(le64_to_cpup(&c->cct->entries[c->cc_txc & 0xffff]) - 0x10000);
c->sb->cc = cpu_to_le16(le16_to_cpup(&c->sb->cc) - 1);
c->cc_txc -= 0x10000;
r = dm_compress_sync_rw_metadata(c, REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA, DM_IO_KMEM, 0, c->sb, SB_SECTORS);
...
}dm_compress_sync() writes the dirty metadata buffers to disk before checking c->failed and skipping the c->cct update, and dm_compress_postsuspend() then unconditionally decrements c->sb->cc back to K on disk. On the next resume, dm_compress_preresume() reads sb->cc == K and cct->entries[K] == T, starting the new session with the same (K, T + 0x10000) stamp as the aborted transaction so CC_VALID() treats the partially updated metadata as committed instead of rolling it back.
[ ... ]
Quoted 2 lines
drivers/md/dm-compress/target.c new file +2
-0+static int initialize_device (struct dm_compress *c) +{
[ ... ]
Quoted 14 lines
drivers/md/dm-compress/target.c new file +14
-0+ meta_size =bdev_nr_sectors (c->meta_dev ? c->meta_dev->bdev : c->dev->bdev); + meta_size = meta_size >= c->start ? meta_size - c->start :0 ; + meta_size >>= log2_buffer_sectors; + + used_meta_size = position + n_bitmap_blocks + n_bitmap_directories + n_chunk_blocks + n_chunk_directories; +if (c->meta_dev) + sb->n_used_metadata_blocks[0 ] =cpu_to_le64 (used_meta_size); +if (used_meta_size > meta_size) { +if (c->meta_dev) + c->ti->error ="The metadata device is too small" ; +else + c->ti->error ="The device is too small" ; +return -ENOSPC ; + }
[ ... ]
drivers/md/dm-compress/target.c line 183
Quoted 4 lines
drivers/md/dm-compress/target.c new file +4
-0+if (c->log2_sectors_per_block < log2_buffer_sectors) + used_meta_size <<= log2_buffer_sectors - c->log2_sectors_per_block; +else if (c->log2_sectors_per_block > log2_buffer_sectors) + used_meta_size =dm_sector_div_up (used_meta_size,1U << (c->log2_sectors_per_block - log2_buffer_sectors));
[Severity: Medium]
When !c->meta_dev, should initialize_device() verify that used_meta_size (after converting to data blocks) is strictly less than data_blocks?
dm_compress_ctr() computes c->n_blocks (data_blocks) by shifting the device sector count right by c->log2_sectors_per_block, which truncates any trailing sectors that do not form a full data block. Because initialize_device() checks used_meta_size against meta_size in 4 KiB buffer units before converting used_meta_size to data block units, a device with block_size > 4096 and trailing sectors can place metadata buffers in the partial block at index data_blocks (or format with 0 free data blocks). If the device is later grown, dm_compress_resize_bitmaps() clears the bitmap bit starting at current_size == data_blocks, freeing the block holding those metadata buffers for data allocation.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 7 lines · Mikulas Patocka
drivers/md/dm-compress/target.c new file +7
-0+static int __init dm_compress_init (void ) +{ +int r; +unsigned i; + +for (i =0 ; i < dm_compress_n_algorithms; i++) { +unsigned request_size;
drivers/md/dm-compress/target.c line 928
Quoted 10 lines
drivers/md/dm-compress/target.c new file +10
-0+ dm_compress_algorithms[i].cc =crypto_alloc_acomp (dm_compress_algorithms[i].name,0 ,0 ); +if (IS_ERR (dm_compress_algorithms[i].cc)) { + r =PTR_ERR (dm_compress_algorithms[i].cc); + dm_compress_algorithms[i].cc =NULL ; +DMERR ("Could not initialize algorithm %s" , dm_compress_algorithms[i].name); + goto bad; + } + request_size =ALIGN (sizeof (struct acomp_req ) +crypto_acomp_reqsize (dm_compress_algorithms[i].cc),CRYPTO_MINALIGN ); + dm_compress_max_request_size =max (dm_compress_max_request_size, request_size); + }
I would strongly suggest avoiding crypto_acomp for now. Instead, it would be better to simplify the set of supported algorithms to ZSTD and LZ4 (the ones that are well known to do the best on the space/time tradeoff curve) and call those library APIs directly.
That avoids all the scatterlist nonsense and other crypto_acomp issues, especially when it seems the caller in this module has linear buffers already and waits synchronously for each one to complete. (acomp does now support virtual addresses too, but it's still an extra layer that provides no benefit for a synchronous software-only user like this.)
As-is, it looks like it also doesn't work correctly with the scomp-based algorithms (lz4, lz4hc, lzo, 842) when chunk_size > PAGE_SIZE. In that case scomp either returns -ENOSYS (making dm-compress silently store everything uncompressed) or overflows its internal scratch buffer.
There's plenty of precedent for just using the compression libraries
directly: zram, dm-vdo, btrfs, f2fs, etc. If hardware offload is ever wanted, it can be added later as an optional path, like erofs does.
- Eric