Skip to contentSkip to conversation

dm-pcache: advance slot index before writing slot

PATCH Superseded Dongsheng Yang 3 participants 13 messages Dec 4, 2025 – Dec 6, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Dongsheng Yang

author maintainer
#0 lore raw

PATCH 1/3 dm-pcache: advance slot index before writing slot

In dm-pcache, in order to ensure crash-consistency, a dual-copy scheme is used to alternately update metadata, and there is a slot index that records the current slot. However, in the write path the current implementation writes directly to the current slot indexed by slot index, and then advances the slot — which ends up overwriting the existing slot, violating the crash-consistency guarantee.

This patch fixes that behavior, preventing metadata from being overwritten incorrectly.

---
 drivers/md/dm-pcache/cache.c         | 8 ++++----
 drivers/md/dm-pcache/cache_segment.c | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/md/dm-pcache/cache.c +4 -4
index 698697a7a73c..9289c016b7c9 100644
@@ -21,10 +21,10 @@ static void cache_info_write(struct pcache_cache *cache)
 	cache_info->header.crc = pcache_meta_crc(&cache_info->header,
 						sizeof(struct pcache_cache_info));
 
+	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
+
 	memcpy_flushcache(get_cache_info_addr(cache), cache_info,
 			sizeof(struct pcache_cache_info));
-
-	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 static void cache_info_init_default(struct pcache_cache *cache);
@@ -93,10 +93,10 @@ void cache_pos_encode(struct pcache_cache *cache,
 	pos_onmedia.header.seq = seq;
 	pos_onmedia.header.crc = cache_pos_onmedia_crc(&pos_onmedia);
 
+	*index = (*index + 1) % PCACHE_META_INDEX_MAX;
+
 	memcpy_flushcache(pos_onmedia_addr, &pos_onmedia, sizeof(struct pcache_cache_pos_onmedia));
 	pmem_wmb();
-
-	*index = (*index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 int cache_pos_decode(struct pcache_cache *cache,
drivers/md/dm-pcache/cache_segment.c +4 -4
index f0b58980806e..ae57cc261422 100644
@@ -26,11 +26,11 @@ static void cache_seg_info_write(struct pcache_cache_segment *cache_seg)
 	seg_info->header.seq++;
 	seg_info->header.crc = pcache_meta_crc(&seg_info->header, sizeof(struct pcache_segment_info));
 
+	cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
+
 	seg_info_addr = get_seg_info_addr(cache_seg);
 	memcpy_flushcache(seg_info_addr, seg_info, sizeof(struct pcache_segment_info));
 	pmem_wmb();
-
-	cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
 	mutex_unlock(&cache_seg->info_lock);
 }
 
@@ -129,10 +129,10 @@ static void cache_seg_ctrl_write(struct pcache_cache_segment *cache_seg)
 	cache_seg_gen.header.crc = pcache_meta_crc(&cache_seg_gen.header,
 						 sizeof(struct pcache_cache_seg_gen));
 
+	cache_seg->gen_index = (cache_seg->gen_index + 1) % PCACHE_META_INDEX_MAX;
+
 	memcpy_flushcache(get_cache_seg_gen_addr(cache_seg), &cache_seg_gen, sizeof(struct pcache_cache_seg_gen));
 	pmem_wmb();
-
-	cache_seg->gen_index = (cache_seg->gen_index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 static void cache_seg_ctrl_init(struct pcache_cache_segment *cache_seg)
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH 3/3 dm pcache: fix segment info indexing

From: Li Chen <[email protected]>

Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so info_index could point between slots and subsequent writes would advance incorrectly. Derive info_index from the pointer returned by the segment meta search using PCACHE_SEG_INFO_SIZE and advance to the next slot for future updates.

---
 drivers/md/dm-pcache/cache_segment.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/dm-pcache/cache_segment.c +4 -1
index ae57cc261422..9d92e2b067ed 100644
@@ -56,7 +56,10 @@ static int cache_seg_info_load(struct pcache_cache_segment *cache_seg)
 		ret = -EIO;
 		goto out;
 	}
-	cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+	cache_seg->info_index =
+		((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+		PCACHE_SEG_INFO_SIZE;
 out:
 	mutex_unlock(&cache_seg->info_lock);
 
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/3 dm pcache: fix cache info indexing

In reply to Dongsheng Yang

From: Li Chen <[email protected]>

The on-media cache_info index used sizeof(struct) instead of the 4K metadata stride, so gc_percent updates from dmsetup message were written between slots and lost after reboot. Use PCACHE_CACHE_INFO_SIZE in get_cache_info_addr() and align info_index with the slot returned by pcache_meta_find_latest().

---
 drivers/md/dm-pcache/cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/md/dm-pcache/cache.c +6 -2
index 9289c016b7c9..bcc4e509aa69 100644
@@ -8,9 +8,11 @@
 
 struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
 {
-	return cache->cache_info_addr + cache->info_index;
+	return (struct pcache_cache_info *)((char *)cache->cache_info_addr +
+						(size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
 }
 
 static void cache_info_write(struct pcache_cache *cache)
@@ -49,6 +51,8 @@ static int cache_info_init(struct pcache_cache *cache, struct pcache_cache_optio
 			return -EINVAL;
 		}
 
+		cache->info_index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) / PCACHE_CACHE_INFO_SIZE;
+
 		return 0;
 	}
 
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/3
#9 lore raw
In reply to Dongsheng Yang
Quoted 79 lines · Dongsheng Yang

In dm-pcache, in order to ensure crash-consistency, a dual-copy scheme is used to alternately update metadata, and there is a slot index that records the current slot. However, in the write path the current implementation writes directly to the current slot indexed by slot index, and then advances the slot — which ends up overwriting the existing slot, violating the crash-consistency guarantee.

This patch fixes that behavior, preventing metadata from being overwritten incorrectly.

---
 drivers/md/dm-pcache/cache.c         | 8 ++++----
 drivers/md/dm-pcache/cache_segment.c | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/md/dm-pcache/cache.c +4 -4
index 698697a7a73c..9289c016b7c9 100644
@@ -21,10 +21,10 @@ static void cache_info_write(struct pcache_cache *cache)
        cache_info->header.crc = pcache_meta_crc(&cache_info->header,
                                                sizeof(struct pcache_cache_info));
 
+       cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
+
        memcpy_flushcache(get_cache_info_addr(cache), cache_info,
                        sizeof(struct pcache_cache_info));
-
-       cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 static void cache_info_init_default(struct pcache_cache *cache);
@@ -93,10 +93,10 @@ void cache_pos_encode(struct pcache_cache *cache,
        pos_onmedia.header.seq = seq;
        pos_onmedia.header.crc = cache_pos_onmedia_crc(&pos_onmedia);
 
+       *index = (*index + 1) % PCACHE_META_INDEX_MAX;
+
        memcpy_flushcache(pos_onmedia_addr, &pos_onmedia, sizeof(struct pcache_cache_pos_onmedia));
        pmem_wmb();
-
-       *index = (*index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 int cache_pos_decode(struct pcache_cache *cache,
drivers/md/dm-pcache/cache_segment.c +4 -5
index f0b58980806e..ae57cc261422 100644
@@ -26,11 +26,11 @@ static void cache_seg_info_write(struct pcache_cache_segment *cache_seg)
        seg_info->header.seq++;
        seg_info->header.crc = pcache_meta_crc(&seg_info->header, sizeof(struct pcache_segment_info));
 
+       cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
+
        seg_info_addr = get_seg_info_addr(cache_seg);
        memcpy_flushcache(seg_info_addr, seg_info, sizeof(struct pcache_segment_info));
        pmem_wmb();
-
-       cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
        mutex_unlock(&cache_seg->info_lock);
 }
 
@@ -129,10 +129,10 @@ static void cache_seg_ctrl_write(struct pcache_cache_segment *cache_seg)
        cache_seg_gen.header.crc = pcache_meta_crc(&cache_seg_gen.header,
                                                 sizeof(struct pcache_cache_seg_gen));
 
+       cache_seg->gen_index = (cache_seg->gen_index + 1) % PCACHE_META_INDEX_MAX;
+
        memcpy_flushcache(get_cache_seg_gen_addr(cache_seg), &cache_seg_gen, sizeof(struct pcache_cache_seg_gen));
        pmem_wmb();
-
-       cache_seg->gen_index = (cache_seg->gen_index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 static void cache_seg_ctrl_init(struct pcache_cache_segment *cache_seg)
--
Quoted 2 lines

2.43.0

Nice catch!

Regards,
Gu

maintainer Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] dm pcache: fix cache info indexing

In reply to Dongsheng Yang
Quoted 43 lines · Dongsheng Yang

From: Li Chen <[email protected]>

The on-media cache_info index used sizeof(struct) instead of the 4K metadata stride, so gc_percent updates from dmsetup message were written between slots and lost after reboot. Use PCACHE_CACHE_INFO_SIZE in get_cache_info_addr() and align info_index with the slot returned by pcache_meta_find_latest().

---
 drivers/md/dm-pcache/cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/md/dm-pcache/cache.c +6 -3
index 9289c016b7c9..bcc4e509aa69 100644
@@ -8,9 +8,11 @@
 
 struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
 {
-       return cache->cache_info_addr + cache->info_index;
+       return (struct pcache_cache_info *)((char *)cache->cache_info_addr +
+                                               (size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
 }
 
 static void cache_info_write(struct pcache_cache *cache)
@@ -49,6 +51,8 @@ static int cache_info_init(struct pcache_cache *cache, struct pcache_cache_optio
                        return -EINVAL;
                }
 
+               cache->info_index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) / PCACHE_CACHE_INFO_SIZE;
+
                return 0;
        }
 
--
Quoted 2 lines

2.43.0

Regards,
Gu

maintainer Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] dm pcache: fix segment info indexing

In reply to Dongsheng Yang
Quoted 33 lines · Dongsheng Yang

From: Li Chen <[email protected]>

Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so info_index could point between slots and subsequent writes would advance incorrectly. Derive info_index from the pointer returned by the segment meta search using PCACHE_SEG_INFO_SIZE and advance to the next slot for future updates.

---
 drivers/md/dm-pcache/cache_segment.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/dm-pcache/cache_segment.c +4 -2
index ae57cc261422..9d92e2b067ed 100644
@@ -56,7 +56,10 @@ static int cache_seg_info_load(struct pcache_cache_segment *cache_seg)
                ret = -EIO;
                goto out;
        }
-       cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+       cache_seg->info_index =
+               ((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+               PCACHE_SEG_INFO_SIZE;
 out:
        mutex_unlock(&cache_seg->info_lock);
 
--
Quoted 2 lines

2.43.0

Regards,
Gu

maintainer
#7 lore raw

Re: [PATCH 3/3] dm pcache: fix segment info indexing

In reply to Dongsheng Yang
Quoted 21 lines · Dongsheng Yang

From: Li Chen <[email protected]>

Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so info_index could point between slots and subsequent writes would advance incorrectly. Derive info_index from the pointer returned by the segment meta search using PCACHE_SEG_INFO_SIZE and advance to the next slot for future updates.

---
 drivers/md/dm-pcache/cache_segment.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/dm-pcache/cache_segment.c +0 -0
index ae57cc261422..9d92e2b067ed 100644
@@ -56,7 +56,10 @@ static int cache_seg_info_load(struct pcache_cache_segment *cache_seg)
 		ret = -EIO;

drivers/md/dm-pcache/cache_segment.c static int cache_seg_info_load(struct pcache_cache_segment *cache_seg) · line 65

Quoted 10 lines
drivers/md/dm-pcache/cache_segment.c +4 -1
 		goto out;
 	}
-	cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+	cache_seg->info_index =
+		((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+		PCACHE_SEG_INFO_SIZE;
 out:
 	mutex_unlock(&cache_seg->info_lock);
 

Hi

I already staged the patch below. Which patch is valid? Your new patch or the patch that I staged?

Mikulas

(quoted diff) +0 -0

b/drivers/md/dm-pcache/cache_segment.c
index f0b58980806e..0b4bb08011ce 100644

drivers/md/dm-pcache/cache_segment.c +0 -0
@@ -56,7 +56,11 @@ static int cache_seg_info_load(struct 

pcache_cache_segment *cache_seg)

                ret = -EIO;
                goto out;
        }

- cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+ cache_seg->info_index =
+ ((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+ PCACHE_SEG_INFO_SIZE;
+ cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
out:

        mutex_unlock(&cache_seg->info_lock);
maintainer
#3 lore raw

Re: [PATCH 2/3] dm pcache: fix cache info indexing

In reply to Dongsheng Yang
Quoted 43 lines · Dongsheng Yang

From: Li Chen <[email protected]>

The on-media cache_info index used sizeof(struct) instead of the 4K metadata stride, so gc_percent updates from dmsetup message were written between slots and lost after reboot. Use PCACHE_CACHE_INFO_SIZE in get_cache_info_addr() and align info_index with the slot returned by pcache_meta_find_latest().

---
 drivers/md/dm-pcache/cache.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/md/dm-pcache/cache.c +6 -2
index 9289c016b7c9..bcc4e509aa69 100644
@@ -8,9 +8,11 @@
 
 struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
 {
-	return cache->cache_info_addr + cache->info_index;
+	return (struct pcache_cache_info *)((char *)cache->cache_info_addr +
+						(size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
 }
 
 static void cache_info_write(struct pcache_cache *cache)
@@ -49,6 +51,8 @@ static int cache_info_init(struct pcache_cache *cache, struct pcache_cache_optio
 			return -EINVAL;
 		}
 
+		cache->info_index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) / PCACHE_CACHE_INFO_SIZE;
+
 		return 0;
 	}
 
Signature · 1 line
2.43.0

I already staged the patch below? Should I revert it and stage your new patch?

Mikulas

drivers/md/dm-pcache/cache.c +5 -2
index d8e92367d947..9554d1158450 100644
@@ -8,9 +8,11 @@
 
 struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
 {
-       return cache->cache_info_addr + cache->info_index;
+       return (struct pcache_cache_info *)((char *)cache->cache_info_addr 
+
+                                               (size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
 }
 

static void cache_info_write(struct pcache_cache *cache)
@@ -40,7 +42,12 @@ static int cache_info_init(struct pcache_cache *cache, struct pcache_cache_optio

        if (IS_ERR(cache_info_addr))
                return PTR_ERR(cache_info_addr);

- if (cache_info_addr) {
+ if (cache_info_addr) {
+ int index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) /
+ PCACHE_CACHE_INFO_SIZE;
+
+ cache->info_index = (index + 1) % PCACHE_META_INDEX_MAX;
+

                if (opts->data_crc !=
                                (cache->cache_info.flags & PCACHE_CACHE_FLAGS_DATA_CRC)) {
                        pcache_dev_err(pcache, "invalid option for data_crc: %s, expected: %s",
maintainer
#10 lore raw
In reply to Dongsheng Yang
Quoted 23 lines · Dongsheng Yang

In dm-pcache, in order to ensure crash-consistency, a dual-copy scheme is used to alternately update metadata, and there is a slot index that records the current slot. However, in the write path the current implementation writes directly to the current slot indexed by slot index, and then advances the slot — which ends up overwriting the existing slot, violating the crash-consistency guarantee.

This patch fixes that behavior, preventing metadata from being overwritten incorrectly.

---
 drivers/md/dm-pcache/cache.c         | 8 ++++----
 drivers/md/dm-pcache/cache_segment.c | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)
drivers/md/dm-pcache/cache.c +0 -0
index 698697a7a73c..9289c016b7c9 100644
@@ -21,10 +21,10 @@ static void cache_info_write(struct pcache_cache *cache)
 	cache_info->header.crc = pcache_meta_crc(&cache_info->header,
 						sizeof(struct pcache_cache_info));

drivers/md/dm-pcache/cache.c static void cache_info_write(struct pcache_cache *cache) · line 30

Quoted 10 lines
drivers/md/dm-pcache/cache.c +2 -2
 
+	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
+
 	memcpy_flushcache(get_cache_info_addr(cache), cache_info,
 			sizeof(struct pcache_cache_info));
-
-	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
 }
 
 static void cache_info_init_default(struct pcache_cache *cache);

I'm not sure whether this is bug or not - but memcpy_flushcache doesn't guarantee that the cache will be flushed (despite it's name) or that writes would be ordered.

You need pmem_wmb(). I see that you are using it at other places.

Mikulas

author maintainer
#11 lore raw
Quoted 35 lines · Mikulas Patocka
Quoted 32 lines · Dongsheng Yang

In dm-pcache, in order to ensure crash-consistency, a dual-copy scheme is used to alternately update metadata, and there is a slot index that records the current slot. However, in the write path the current implementation writes directly to the current slot indexed by slot index, and then advances the slot — which ends up overwriting the existing slot, violating the crash-consistency guarantee.

This patch fixes that behavior, preventing metadata from being overwritten incorrectly.

---
  drivers/md/dm-pcache/cache.c         | 8 ++++----
  drivers/md/dm-pcache/cache_segment.c | 8 ++++----
  2 files changed, 8 insertions(+), 8 deletions(-)
drivers/md/dm-pcache/cache.c +2 -2
index 698697a7a73c..9289c016b7c9 100644
@@ -21,10 +21,10 @@ static void cache_info_write(struct pcache_cache *cache)
  	cache_info->header.crc = pcache_meta_crc(&cache_info->header,
  						sizeof(struct pcache_cache_info));
 
+	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
+
  	memcpy_flushcache(get_cache_info_addr(cache), cache_info,
  			sizeof(struct pcache_cache_info));
-
-	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
  }
 
Quoted 2 lines

drivers/md/dm-pcache/cache.c static void cache_info_write(struct pcache_cache *cache) · line 30

Quoted 1 line
drivers/md/dm-pcache/cache.c +0 -0
  static void cache_info_init_default(struct pcache_cache *cache);

I'm not sure whether this is bug or not

It's a bug, if in-place updating for slot happen, there is a potential metadata corruption while powercut happening at the same time.

so I will add

Cc [email protected]      # 6.18

in V2.

Quoted 5 lines

- but memcpy_flushcache doesn't
guarantee that the cache will be flushed (despite it's name) or that writes would be ordered.

You need pmem_wmb(). I see that you are using it at other places.

Good catch, I will send v2 to add the missing pmem_wmb().

Thanx

Quoted 2 lines

Mikulas

author maintainer
#4 lore raw

Re: [PATCH 2/3] dm pcache: fix cache info indexing

In reply to Mikulas Patocka
Quoted 44 lines · Mikulas Patocka
Quoted 41 lines · Dongsheng Yang

From: Li Chen <[email protected]>

The on-media cache_info index used sizeof(struct) instead of the 4K metadata stride, so gc_percent updates from dmsetup message were written between slots and lost after reboot. Use PCACHE_CACHE_INFO_SIZE in get_cache_info_addr() and align info_index with the slot returned by pcache_meta_find_latest().

---
  drivers/md/dm-pcache/cache.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/md/dm-pcache/cache.c +6 -2
index 9289c016b7c9..bcc4e509aa69 100644
@@ -8,9 +8,11 @@
 
  struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
  {
-	return cache->cache_info_addr + cache->info_index;
+	return (struct pcache_cache_info *)((char *)cache->cache_info_addr +
+						(size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
  }
 
  static void cache_info_write(struct pcache_cache *cache)
@@ -49,6 +51,8 @@ static int cache_info_init(struct pcache_cache *cache, struct pcache_cache_optio
  			return -EINVAL;
  		}
 
+		cache->info_index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) / PCACHE_CACHE_INFO_SIZE;
+
  		return 0;
  	}
 
Quoted 4 lines
Quoted 2 lines
Signature · 1 line
2.43.0

I already staged the patch below? Should I revert it and stage your new patch?

Yes, please revert it, I will send a v2 patchset, please pick the v2 patch.

Thanx

Quoted 39 lines

Mikulas

drivers/md/dm-pcache/cache.c +5 -2
index d8e92367d947..9554d1158450 100644
@@ -8,9 +8,11 @@
 
  struct kmem_cache *key_cache;
 
-static inline struct pcache_cache_info *get_cache_info_addr(struct pcache_cache *cache)
+static inline struct pcache_cache_info *
+get_cache_info_addr(struct pcache_cache *cache)
  {
-       return cache->cache_info_addr + cache->info_index;
+       return (struct pcache_cache_info *)((char *)cache->cache_info_addr
+
+                                               (size_t)cache->info_index * PCACHE_CACHE_INFO_SIZE);
  }
 
  static void cache_info_write(struct pcache_cache *cache)
@@ -40,7 +42,12 @@ static int cache_info_init(struct pcache_cache *cache,

struct pcache_cache_optio

         if (IS_ERR(cache_info_addr))
                 return PTR_ERR(cache_info_addr);
drivers/md/dm-pcache/cache.c +6 -1
-       if (cache_info_addr) {
+    if (cache_info_addr) {
+               int index = ((char *)cache_info_addr - (char *)cache->cache_info_addr) /
+                               PCACHE_CACHE_INFO_SIZE;
+
+               cache->info_index = (index + 1) % PCACHE_META_INDEX_MAX;
+
                 if (opts->data_crc !=
                                 (cache->cache_info.flags & PCACHE_CACHE_FLAGS_DATA_CRC)) {
                         pcache_dev_err(pcache, "invalid option for data_crc: %s, expected: %s",
author maintainer
#8 lore raw

Re: [PATCH 3/3] dm pcache: fix segment info indexing

In reply to Mikulas Patocka
Quoted 36 lines · Mikulas Patocka
Quoted 21 lines · Dongsheng Yang

From: Li Chen <[email protected]>

Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so info_index could point between slots and subsequent writes would advance incorrectly. Derive info_index from the pointer returned by the segment meta search using PCACHE_SEG_INFO_SIZE and advance to the next slot for future updates.

---
  drivers/md/dm-pcache/cache_segment.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
drivers/md/dm-pcache/cache_segment.c +0 -0
index ae57cc261422..9d92e2b067ed 100644
@@ -56,7 +56,10 @@ static int cache_seg_info_load(struct pcache_cache_segment *cache_seg)
  		ret = -EIO;

drivers/md/dm-pcache/cache_segment.c static int cache_seg_info_load(struct pcache_cache_segment *cache_seg) · line 65

Quoted 10 lines
drivers/md/dm-pcache/cache_segment.c +4 -1
  		goto out;
  	}
-	cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+	cache_seg->info_index =
+		((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+		PCACHE_SEG_INFO_SIZE;
  out:
  	mutex_unlock(&cache_seg->info_lock);
 

Hi

Quoted 2 lines

I already staged the patch below. Which patch is valid? Your new patch or the patch that I staged?

Yes, please revert it, I will send a v2 patchset, please pick the v2 patch.

Thanx

Quoted 25 lines

Mikulas

drivers/md/dm-pcache/cache_segment.c +5 -1
index f0b58980806e..0b4bb08011ce 100644
@@ -56,7 +56,11 @@ static int cache_seg_info_load(struct
 pcache_cache_segment *cache_seg)
                 ret = -EIO;
                 goto out;
         }
-       cache_seg->info_index = cache_seg_info_addr - cache_seg_info_addr_base;
+
+       cache_seg->info_index =
+               ((char *)cache_seg_info_addr - (char *)cache_seg_info_addr_base) /
+               PCACHE_SEG_INFO_SIZE;
+       cache_seg->info_index = (cache_seg->info_index + 1) % PCACHE_META_INDEX_MAX;
  out:
         mutex_unlock(&cache_seg->info_lock);
maintainer
#12 lore raw
In reply to Dongsheng Yang
Quoted 55 lines · Dongsheng Yang
Quoted 29 lines · Mikulas Patocka
Quoted 26 lines · Dongsheng Yang

In dm-pcache, in order to ensure crash-consistency, a dual-copy scheme is used to alternately update metadata, and there is a slot index that records the current slot. However, in the write path the current implementation writes directly to the current slot indexed by slot index, and then advances the slot — which ends up overwriting the existing slot, violating the crash-consistency guarantee.

This patch fixes that behavior, preventing metadata from being overwritten incorrectly.

---
  drivers/md/dm-pcache/cache.c         | 8 ++++----
  drivers/md/dm-pcache/cache_segment.c | 8 ++++----
  2 files changed, 8 insertions(+), 8 deletions(-)
drivers/md/dm-pcache/cache.c +0 -0
index 698697a7a73c..9289c016b7c9 100644
@@ -21,10 +21,10 @@ static void cache_info_write(struct pcache_cache
 *cache)
  	cache_info->header.crc = pcache_meta_crc(&cache_info->header,
  						sizeof(struct
 pcache_cache_info));
  +	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
Quoted 8 lines

drivers/md/dm-pcache/cache.c static void cache_info_write(struct pcache_cache *cache) · line 30

Quoted 7 lines
drivers/md/dm-pcache/cache.c +1 -2
+
  	memcpy_flushcache(get_cache_info_addr(cache), cache_info,
  			sizeof(struct pcache_cache_info));
-
-	cache->info_index = (cache->info_index + 1) % PCACHE_META_INDEX_MAX;
  }
    static void cache_info_init_default(struct pcache_cache *cache);

I'm not sure whether this is bug or not

It's a bug, if in-place updating for slot happen, there is a potential metadata corruption while powercut happening at the same time.

so I will add

Cc [email protected]      # 6.18

in V2.

Quoted 4 lines

- but memcpy_flushcache doesn't
guarantee that the cache will be flushed (despite it's name) or that writes would be ordered.

Quoted 7 lines
Quoted 1 line

You need pmem_wmb(). I see that you are using it at other places.

Good catch, I will send v2 to add the missing pmem_wmb().

Thanx

OK. I staged all 3 patches and I'll send to Linus soon.

Mikulas