Skip to contentSkip to conversation

dm-pcache: built-in support and metadata hardening

PATCH In review Li Chen 4 participants 10 messages Nov 11, 2025 – Nov 18, 2025

Subsystems (2)
lore
Share & tools
lore

Li Chen

author
#0 lore raw

v2 cover dm-pcache: built-in support and metadata hardening

From: Li Chen <[email protected]>

This three-patch series tidies dm-pcache’s build glue and tightens the metadata scan.

Patch 1 allow dm-pcache to be linked into vmlinux and avoids clashing with the sunrpc cache_flush() by using obj-$(CONFIG_DM_PCACHE) and renaming the helper across the tree.

Patch 2 drops a redundant recomputation of the metadata slot pointer while walking headers.

Patch 3 Ensure dm-pcache initializes cache_info from a zeroed state so metadata CRC failures can’t leak stale flags into the new-cache path.

Thanks for your review.

Li Chen (3):
dm-pcache: allow built-in build and rename flush helper
dm-pcache: reuse meta_addr in pcache_meta_find_latest
dm-pcache: zero cache_info before default init

 drivers/md/dm-pcache/Makefile          | 2 +-
 drivers/md/dm-pcache/cache.c           | 4 ++--
 drivers/md/dm-pcache/cache.h           | 2 +-
 drivers/md/dm-pcache/cache_req.c       | 6 +++---
 drivers/md/dm-pcache/pcache_internal.h | 2 +-
 5 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.51.0
author
#1 lore raw

PATCH v2 1/3 dm-pcache: allow built-in build and rename flush helper

From: Li Chen <[email protected]>

CONFIG_BCACHE is tristate, so dm-pcache can also be built-in. Switch the Makefile to use obj-$(CONFIG_DM_PCACHE) so the target can be linked into vmlinux instead of always being a loadable module.

Also rename cache_flush() to pcache_cache_flush() to avoid a global symbol clash with sunrpc/cache.c's cache_flush().

---
 drivers/md/dm-pcache/Makefile    | 2 +-
 drivers/md/dm-pcache/cache.c     | 2 +-
 drivers/md/dm-pcache/cache.h     | 2 +-
 drivers/md/dm-pcache/cache_req.c | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)
drivers/md/dm-pcache/Makefile +1 -1
index 86776e4acad2..cedfd38854f6 100644
@@ -1,3 +1,3 @@
 dm-pcache-y := dm_pcache.o cache_dev.o segment.o backing_dev.o cache.o cache_gc.o cache_writeback.o cache_segment.o cache_key.o cache_req.o
 
-obj-m += dm-pcache.o
+obj-$(CONFIG_DM_PCACHE) += dm-pcache.o
drivers/md/dm-pcache/cache.c +1 -1
index d8e92367d947..d516d4904227 100644
@@ -411,7 +411,7 @@ void pcache_cache_stop(struct dm_pcache *pcache)
 {
 	struct pcache_cache *cache = &pcache->cache;
 
-	cache_flush(cache);
+	pcache_cache_flush(cache);
 
 	cancel_delayed_work_sync(&cache->gc_work);
 	flush_work(&cache->clean_work);
drivers/md/dm-pcache/cache.h +1 -1
index 1136d86958c8..27613b56be54 100644
@@ -339,7 +339,7 @@ void cache_seg_put(struct pcache_cache_segment *cache_seg);
 void cache_seg_set_next_seg(struct pcache_cache_segment *cache_seg, u32 seg_id);
 
 /* cache request*/
-int cache_flush(struct pcache_cache *cache);
+int pcache_cache_flush(struct pcache_cache *cache);
 void miss_read_end_work_fn(struct work_struct *work);
 int pcache_cache_handle_req(struct pcache_cache *cache, struct pcache_request *pcache_req);
 
drivers/md/dm-pcache/cache_req.c +3 -3
index 27f94c1fa968..7854a30e07b7 100644
@@ -790,7 +790,7 @@ static int cache_write(struct pcache_cache *cache, struct pcache_request *pcache
 }
 
 /**
- * cache_flush - Flush all ksets to persist any pending cache data
+ * pcache_cache_flush - Flush all ksets to persist any pending cache data
  * @cache: Pointer to the cache structure
  *
  * This function iterates through all ksets associated with the provided `cache`
@@ -802,7 +802,7 @@ static int cache_write(struct pcache_cache *cache, struct pcache_request *pcache
  * the respective error code, preventing the flush operation from proceeding to
  * subsequent ksets.
  */
-int cache_flush(struct pcache_cache *cache)
+int pcache_cache_flush(struct pcache_cache *cache)
 {
 	struct pcache_cache_kset *kset;
 	int ret;
@@ -827,7 +827,7 @@ int pcache_cache_handle_req(struct pcache_cache *cache, struct pcache_request *p
 	struct bio *bio = pcache_req->bio;
 
 	if (unlikely(bio->bi_opf & REQ_PREFLUSH))
-		return cache_flush(cache);
+		return pcache_cache_flush(cache);
 
 	if (bio_data_dir(bio) == READ)
 		return cache_read(cache, pcache_req);
Signature · 1 line
2.51.0
maintainer
#2 lore raw

Re: [PATCH v2 1/3] dm-pcache: allow built-in build and rename flush helper

Quoted 10 lines · Li Chen

From: Li Chen <[email protected]>

CONFIG_BCACHE is tristate, so dm-pcache can also be built-in. Switch the Makefile to use obj-$(CONFIG_DM_PCACHE) so the target can be linked into vmlinux instead of always being a loadable module.

Also rename cache_flush() to pcache_cache_flush() to avoid a global symbol clash with sunrpc/cache.c's cache_flush().

Reviewed-by: Dongsheng Yang <[email protected]>

Quoted 73 lines
---
  drivers/md/dm-pcache/Makefile    | 2 +-
  drivers/md/dm-pcache/cache.c     | 2 +-
  drivers/md/dm-pcache/cache.h     | 2 +-
  drivers/md/dm-pcache/cache_req.c | 6 +++---
  4 files changed, 6 insertions(+), 6 deletions(-)
drivers/md/dm-pcache/Makefile +1 -1
index 86776e4acad2..cedfd38854f6 100644
@@ -1,3 +1,3 @@
  dm-pcache-y := dm_pcache.o cache_dev.o segment.o backing_dev.o cache.o cache_gc.o cache_writeback.o cache_segment.o cache_key.o cache_req.o
 
-obj-m += dm-pcache.o
+obj-$(CONFIG_DM_PCACHE) += dm-pcache.o
drivers/md/dm-pcache/cache.c +1 -1
index d8e92367d947..d516d4904227 100644
@@ -411,7 +411,7 @@ void pcache_cache_stop(struct dm_pcache *pcache)
  {
  	struct pcache_cache *cache = &pcache->cache;
 
-	cache_flush(cache);
+	pcache_cache_flush(cache);
 
  	cancel_delayed_work_sync(&cache->gc_work);
  	flush_work(&cache->clean_work);
drivers/md/dm-pcache/cache.h +1 -1
index 1136d86958c8..27613b56be54 100644
@@ -339,7 +339,7 @@ void cache_seg_put(struct pcache_cache_segment *cache_seg);
  void cache_seg_set_next_seg(struct pcache_cache_segment *cache_seg, u32 seg_id);
 
  /* cache request*/
-int cache_flush(struct pcache_cache *cache);
+int pcache_cache_flush(struct pcache_cache *cache);
  void miss_read_end_work_fn(struct work_struct *work);
  int pcache_cache_handle_req(struct pcache_cache *cache, struct pcache_request *pcache_req);
 
drivers/md/dm-pcache/cache_req.c +3 -3
index 27f94c1fa968..7854a30e07b7 100644
@@ -790,7 +790,7 @@ static int cache_write(struct pcache_cache *cache, struct pcache_request *pcache
  }
 
  /**
- * cache_flush - Flush all ksets to persist any pending cache data
+ * pcache_cache_flush - Flush all ksets to persist any pending cache data
   * @cache: Pointer to the cache structure
   *
   * This function iterates through all ksets associated with the provided `cache`
@@ -802,7 +802,7 @@ static int cache_write(struct pcache_cache *cache, struct pcache_request *pcache
   * the respective error code, preventing the flush operation from proceeding to
   * subsequent ksets.
   */
-int cache_flush(struct pcache_cache *cache)
+int pcache_cache_flush(struct pcache_cache *cache)
  {
  	struct pcache_cache_kset *kset;
  	int ret;
@@ -827,7 +827,7 @@ int pcache_cache_handle_req(struct pcache_cache *cache, struct pcache_request *p
  	struct bio *bio = pcache_req->bio;
 
  	if (unlikely(bio->bi_opf & REQ_PREFLUSH))
-		return cache_flush(cache);
+		return pcache_cache_flush(cache);
 
  	if (bio_data_dir(bio) == READ)
  		return cache_read(cache, pcache_req);
author
#3 lore raw

PATCH v2 2/3 dm-pcache: reuse meta_addr in pcache_meta_find_latest

In reply to Li Chen

From: Li Chen <[email protected]>

pcache_meta_find_latest() already computes the metadata address as meta_addr. Reuse that instead of recomputing.

---
 drivers/md/dm-pcache/pcache_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-pcache/pcache_internal.h +1 -1
index d427e534727c..b7a3319d2bd3 100644
@@ -99,7 +99,7 @@ static inline void __must_check *pcache_meta_find_latest(struct pcache_meta_head
 		/* Update latest if a more recent sequence is found */
 		if (!latest || pcache_meta_seq_after(meta->seq, seq_latest)) {
 			seq_latest = meta->seq;
-			latest = (void *)header + (i * meta_max_size);
+			latest = meta_addr;
 		}
 	}
 
Signature · 1 line
2.51.0
maintainer
#4 lore raw

Re: [PATCH v2 2/3] dm-pcache: reuse meta_addr in pcache_meta_find_latest

Quoted 6 lines · Li Chen

From: Li Chen <[email protected]>

pcache_meta_find_latest() already computes the metadata address as meta_addr. Reuse that instead of recomputing.

Reviewed-by: Dongsheng Yang <[email protected]

Quoted 17 lines
---
  drivers/md/dm-pcache/pcache_internal.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-pcache/pcache_internal.h +1 -1
index d427e534727c..b7a3319d2bd3 100644
@@ -99,7 +99,7 @@ static inline void __must_check *pcache_meta_find_latest(struct pcache_meta_head
  		/* Update latest if a more recent sequence is found */
  		if (!latest || pcache_meta_seq_after(meta->seq, seq_latest)) {
  			seq_latest = meta->seq;
-			latest = (void *)header + (i * meta_max_size);
+			latest = meta_addr;
  		}
  	}
 
author
#5 lore raw

PATCH v2 3/3 dm-pcache: zero cache_info before default init

In reply to Li Chen

From: Li Chen <[email protected]>

pcache_meta_find_latest() leaves whatever it last copied into the caller’s buffer even when it returns NULL. For cache_info_init(), that meant cache->cache_info could still contain CRC-bad garbage when no valid metadata exists, leading later initialization paths to read bogus flags.

Explicitly memset cache->cache_info in cache_info_init_default() so new-cache paths start from a clean slate. The default sequence number assignment becomes redundant with this reset, so it drops out.

---
 drivers/md/dm-pcache/cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-pcache/cache.c +1 -1
index d516d4904227..698697a7a73c 100644
@@ -181,7 +181,7 @@ static void cache_info_init_default(struct pcache_cache *cache)
 {
 	struct pcache_cache_info *cache_info = &cache->cache_info;
 
-	cache_info->header.seq = 0;
+	memset(cache_info, 0, sizeof(*cache_info));
 	cache_info->n_segs = cache->cache_dev->seg_num;
 	cache_info_set_gc_percent(cache_info, PCACHE_CACHE_GC_PERCENT_DEFAULT);
 }
Signature · 1 line
2.51.0
maintainer Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] dm-pcache: zero cache_info before default init

Quoted 32 lines · Li Chen

From: Li Chen <[email protected]>

pcache_meta_find_latest() leaves whatever it last copied into the caller’s buffer even when it returns NULL. For cache_info_init(), that meant cache->cache_info could still contain CRC-bad garbage when no valid metadata exists, leading later initialization paths to read bogus flags.

Explicitly memset cache->cache_info in cache_info_init_default() so new-cache paths start from a clean slate. The default sequence number assignment becomes redundant with this reset, so it drops out.

---
 drivers/md/dm-pcache/cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-pcache/cache.c +1 -2
index d516d4904227..698697a7a73c 100644
@@ -181,7 +181,7 @@ static void cache_info_init_default(struct pcache_cache *cache)
 {
        struct pcache_cache_info *cache_info = &cache->cache_info;
 
-       cache_info->header.seq = 0;
+       memset(cache_info, 0, sizeof(*cache_info));
        cache_info->n_segs = cache->cache_dev->seg_num;
        cache_info_set_gc_percent(cache_info, PCACHE_CACHE_GC_PERCENT_DEFAULT);
 }
--
Quoted 2 lines

2.51.0

It looks good to me.

maintainer
#7 lore raw

Re: [PATCH v2 3/3] dm-pcache: zero cache_info before default init

In reply to Li Chen
Quoted 8 lines · Li Chen

From: Li Chen <[email protected]>

pcache_meta_find_latest() leaves whatever it last copied into the caller’s buffer even when it returns NULL. For cache_info_init(), that meant cache->cache_info could still contain CRC-bad garbage when no valid metadata exists, leading later initialization paths to read bogus flags.

Quoted 5 lines

Explicitly memset cache->cache_info in cache_info_init_default() so new-cache paths start from a clean slate. The default sequence number assignment becomes redundant with this reset, so it drops out.

Reviewed-by: Dongsheng Yang <[email protected]>

Quoted 17 lines
---
  drivers/md/dm-pcache/cache.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/dm-pcache/cache.c +1 -1
index d516d4904227..698697a7a73c 100644
@@ -181,7 +181,7 @@ static void cache_info_init_default(struct pcache_cache *cache)
  {
  	struct pcache_cache_info *cache_info = &cache->cache_info;
 
-	cache_info->header.seq = 0;
+	memset(cache_info, 0, sizeof(*cache_info));
  	cache_info->n_segs = cache->cache_dev->seg_num;
  	cache_info_set_gc_percent(cache_info, PCACHE_CACHE_GC_PERCENT_DEFAULT);
  }
maintainer
#8 lore raw
In reply to Li Chen

Hi Mikulas,

    Please consider taking these three patches.

Thanx

Dongsheng

Quoted 26 lines · Li Chen

From: Li Chen <[email protected]>

This three-patch series tidies dm-pcache’s build glue and tightens the metadata scan.

Patch 1 allow dm-pcache to be linked into vmlinux and avoids clashing with the sunrpc cache_flush() by using obj-$(CONFIG_DM_PCACHE) and renaming the helper across the tree.

Patch 2 drops a redundant recomputation of the metadata slot pointer while walking headers.

Patch 3 Ensure dm-pcache initializes cache_info from a zeroed state so metadata CRC failures can’t leak stale flags into the new-cache path.

Thanks for your review.

Li Chen (3):
dm-pcache: allow built-in build and rename flush helper
dm-pcache: reuse meta_addr in pcache_meta_find_latest
dm-pcache: zero cache_info before default init

  drivers/md/dm-pcache/Makefile          | 2 +-
  drivers/md/dm-pcache/cache.c           | 4 ++--
  drivers/md/dm-pcache/cache.h           | 2 +-
  drivers/md/dm-pcache/cache_req.c       | 6 +++---
  drivers/md/dm-pcache/pcache_internal.h | 2 +-
  5 files changed, 8 insertions(+), 8 deletions(-)
maintainer
#9 lore raw
Quoted 7 lines · Dongsheng Yang

Hi Mikulas,

    Please consider taking these three patches.

Thanx

Dongsheng

OK, thanks. I accepted the patches and I staged them for the kernel 6.18.

Mikulas

Quoted 33 lines · Li Chen
Quoted 31 lines

From: Li Chen <[email protected]>

This three-patch series tidies dm-pcache’s build glue and tightens the metadata scan.

Patch 1 allow dm-pcache to be linked into vmlinux and avoids clashing with the sunrpc
cache_flush() by using obj-$(CONFIG_DM_PCACHE) and renaming the helper across the tree.

Patch 2 drops a redundant recomputation of the metadata slot pointer while walking headers.

Patch 3 Ensure dm-pcache initializes cache_info from a zeroed state so metadata CRC
failures can’t leak stale flags into the new-cache path.

Thanks for your review.

Li Chen (3):
dm-pcache: allow built-in build and rename flush helper
dm-pcache: reuse meta_addr in pcache_meta_find_latest
dm-pcache: zero cache_info before default init

  drivers/md/dm-pcache/Makefile          | 2 +-
  drivers/md/dm-pcache/cache.c           | 4 ++--
  drivers/md/dm-pcache/cache.h           | 2 +-
  drivers/md/dm-pcache/cache_req.c       | 6 +++---
  drivers/md/dm-pcache/pcache_internal.h | 2 +-
  5 files changed, 8 insertions(+), 8 deletions(-)