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

Li Chen posted 2 patches 2 months, 1 week ago
[PATCH 1/2] dm pcache: fix cache info indexing
Posted by Li Chen 2 months, 1 week ago
From: Li Chen <chenl311@chinatelecom.cn>

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().

Signed-off-by: Li Chen <chenl311@chinatelecom.cn>
---
 drivers/md/dm-pcache/cache.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/md/dm-pcache/cache.c b/drivers/md/dm-pcache/cache.c
index 698697a7a73c..6d5001548628 100644
--- a/drivers/md/dm-pcache/cache.c
+++ b/drivers/md/dm-pcache/cache.c
@@ -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",
-- 
2.51.0
Re: [PATCH 1/2] dm pcache: fix cache info indexing
Posted by Dongsheng Yang 2 months, 1 week ago
在 12/2/2025 8:11 PM, Li Chen 写道:
> From: Li Chen <chenl311@chinatelecom.cn>
>
> 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().
>
> Signed-off-by: Li Chen <chenl311@chinatelecom.cn>
> ---
>   drivers/md/dm-pcache/cache.c | 13 ++++++++++---
>   1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/md/dm-pcache/cache.c b/drivers/md/dm-pcache/cache.c
> index 698697a7a73c..6d5001548628 100644
> --- a/drivers/md/dm-pcache/cache.c
> +++ b/drivers/md/dm-pcache/cache.c
> @@ -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) {


this is unrelated change, and it introduce error in checkpatch.pl

# ./scripts/checkpatch.pl drivers/md/dm-pcache/cache.c
WARNING: please, no spaces at the start of a line
#45: FILE: drivers/md/dm-pcache/cache.c:45:
+    if (cache_info_addr) {$

WARNING: suspect code indent for conditional statements (4, 16)
#45: FILE: drivers/md/dm-pcache/cache.c:45:
+    if (cache_info_addr) {
+               int index = ((char *)cache_info_addr - (char 
*)cache->cache_info_addr) /

total: 0 errors, 2 warnings, 452 lines checked

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


Don't advance info_index at init stage, cache->info_index means the 
current index, not the next index.

It will be advanced in cache_info_write() after write cache_info into 
next slot.

In addition, cache->info_index initialization should be after the 
data_crc checking.

Thanx

> +
>   		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",
Re: [PATCH 1/2] dm pcache: fix cache info indexing
Posted by Dongsheng Yang 2 months, 1 week ago
在 12/3/2025 1:56 PM, Dongsheng Yang 写道:
>
> 在 12/2/2025 8:11 PM, Li Chen 写道:
>> From: Li Chen <chenl311@chinatelecom.cn>

>>
>
>> +        int index = ((char *)cache_info_addr - (char 
>> *)cache->cache_info_addr) /
>> +                PCACHE_CACHE_INFO_SIZE;
>> +
>> +        cache->info_index = (index + 1) % PCACHE_META_INDEX_MAX;
>
>
> Don't advance info_index at init stage, cache->info_index means the 
> current index, not the next index.
>
> It will be advanced in cache_info_write() after write cache_info into 
> next slot.


My bad, ->info_index means next slot, so advance it is correct.

>
> In addition, cache->info_index initialization should be after the 
> data_crc checking.
>
> Thanx
>
>> +
>>           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",
>