dm-pcache: advance slot index before writing slot
Subsystems (2)
Dongsheng Yang
maintainerPATCH 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
@@ -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
@@ -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
Dongsheng Yang
maintainerPATCH 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
@@ -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
Dongsheng Yang
maintainerPATCH 2/3 dm pcache: fix cache info indexing
In reply to Dongsheng YangFrom: 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
@@ -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_optioreturn -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
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.
Signed-off-by Dongsheng Yang <[email protected]>--- 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@@ -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@@ -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
Re: [PATCH 2/3] dm pcache: fix cache info indexing
In reply to Dongsheng YangQuoted 43 lines · Dongsheng Yang
From: Li Chen <[email protected]>
The on-media
cache_infoindex used sizeof(struct) instead of the 4K metadata stride, sogc_percentupdates from dmsetup message were written between slots and lost after reboot. UsePCACHE_CACHE_INFO_SIZEinget_cache_info_addr()and aligninfo_indexwith the slot returned bypcache_meta_find_latest().Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/md/dm-pcache/cache.c +6
-3@@ -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_optioreturn -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
Re: [PATCH 3/3] dm pcache: fix segment info indexing
In reply to Dongsheng YangQuoted 33 lines · Dongsheng Yang
From: Li Chen <[email protected]>
Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so
info_indexcould point between slots and subsequent writes would advance incorrectly. Deriveinfo_indexfrom the pointer returned by the segment meta search usingPCACHE_SEG_INFO_SIZEand advance to the next slot for future updates.Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache_segment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/md/dm-pcache/cache_segment.c +4
-2@@ -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
Mikulas Patocka
maintainerRe: [PATCH 3/3] dm pcache: fix segment info indexing
In reply to Dongsheng YangQuoted 21 lines · Dongsheng Yang
From: Li Chen <[email protected]>
Segment info indexing also used sizeof(struct) instead of the 4K metadata stride, so
info_indexcould point between slots and subsequent writes would advance incorrectly. Deriveinfo_indexfrom the pointer returned by the segment meta search usingPCACHE_SEG_INFO_SIZEand advance to the next slot for future updates.Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache_segment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/md/dm-pcache/cache_segment.c +0
-0@@ -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
-1goto 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);
Mikulas Patocka
maintainerRe: [PATCH 2/3] dm pcache: fix cache info indexing
In reply to Dongsheng YangQuoted 43 lines · Dongsheng Yang
From: Li Chen <[email protected]>
The on-media
cache_infoindex used sizeof(struct) instead of the 4K metadata stride, sogc_percentupdates from dmsetup message were written between slots and lost after reboot. UsePCACHE_CACHE_INFO_SIZEinget_cache_info_addr()and aligninfo_indexwith the slot returned bypcache_meta_find_latest().Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/md/dm-pcache/cache.c +6
-2@@ -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_optioreturn -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
@@ -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",Mikulas Patocka
maintainerQuoted 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.
Signed-off-by Dongsheng Yang <[email protected]>--- 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@@ -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
Dongsheng Yang
maintainerQuoted 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.
Signed-off-by Dongsheng Yang <[email protected]>--- 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@@ -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
-0static 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
in V2.
Quoted 5 lines
- but
memcpy_flushcachedoesn'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
Dongsheng Yang
maintainerRe: [PATCH 2/3] dm pcache: fix cache info indexing
In reply to Mikulas PatockaQuoted 44 lines · Mikulas Patocka
Quoted 41 lines · Dongsheng Yang
From: Li Chen <[email protected]>
The on-media
cache_infoindex used sizeof(struct) instead of the 4K metadata stride, sogc_percentupdates from dmsetup message were written between slots and lost after reboot. UsePCACHE_CACHE_INFO_SIZEinget_cache_info_addr()and aligninfo_indexwith the slot returned bypcache_meta_find_latest().Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)drivers/md/dm-pcache/cache.c +6
-2@@ -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_optioreturn -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.0I 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@@ -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_optioif (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",
Dongsheng Yang
maintainerRe: [PATCH 3/3] dm pcache: fix segment info indexing
In reply to Mikulas PatockaQuoted 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_indexcould point between slots and subsequent writes would advance incorrectly. Deriveinfo_indexfrom the pointer returned by the segment meta search usingPCACHE_SEG_INFO_SIZEand advance to the next slot for future updates.Signed-off-by Li Chen <[email protected]>Signed-off-by Dongsheng Yang <[email protected]>--- drivers/md/dm-pcache/cache_segment.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)drivers/md/dm-pcache/cache_segment.c +0
-0@@ -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
-1goto 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@@ -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);
Mikulas Patocka
maintainerQuoted 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.
Signed-off-by Dongsheng Yang <[email protected]>--- 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@@ -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.18in V2.
Quoted 4 lines
- but
memcpy_flushcachedoesn'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