Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : #include "cache.h"
3 : #include "backing_dev.h"
4 : #include "cache_dev.h"
5 : #include "dm_pcache.h"
6 :
7 : /**
8 : * cache_key_gc - Releases the reference of a cache key segment.
9 : * @cache: Pointer to the pcache_cache structure.
10 : * @key: Pointer to the cache key to be garbage collected.
11 : *
12 : * This function decrements the reference count of the cache segment
13 : * associated with the given key. If the reference count drops to zero,
14 : * the segment may be invalidated and reused.
15 : */
16 34149130 : static void cache_key_gc(struct pcache_cache *cache, struct pcache_cache_key *key)
17 : {
18 34149130 : cache_seg_put(key->cache_pos.cache_seg);
19 : }
20 :
21 4871264 : static bool need_gc(struct pcache_cache *cache, struct pcache_cache_pos *dirty_tail, struct pcache_cache_pos *key_tail)
22 : {
23 4871264 : struct dm_pcache *pcache = CACHE_TO_PCACHE(cache);
24 4871264 : struct pcache_cache_kset_onmedia *kset_onmedia;
25 4871264 : void *dirty_addr, *key_addr;
26 4871264 : u32 segs_used, segs_gc_threshold, to_copy;
27 4871264 : int ret;
28 :
29 4871264 : dirty_addr = cache_pos_addr(dirty_tail);
30 4871264 : key_addr = cache_pos_addr(key_tail);
31 4871264 : if (dirty_addr == key_addr) {
32 13819 : pcache_dev_debug(pcache, "key tail is equal to dirty tail: %u:%u\n",
33 : dirty_tail->cache_seg->cache_seg_id,
34 : dirty_tail->seg_off);
35 13819 : return false;
36 : }
37 :
38 4857445 : kset_onmedia = (struct pcache_cache_kset_onmedia *)cache->gc_kset_onmedia_buf;
39 :
40 4857445 : to_copy = min(PCACHE_KSET_ONMEDIA_SIZE_MAX, PCACHE_SEG_SIZE - key_tail->seg_off);
41 4857445 : ret = copy_mc_to_kernel(kset_onmedia, key_addr, to_copy);
42 4857409 : if (ret) {
43 0 : pcache_dev_err(pcache, "error to read kset: %d", ret);
44 0 : return false;
45 : }
46 :
47 : /* Check if kset_onmedia is corrupted */
48 4857409 : if (kset_onmedia->magic != PCACHE_KSET_MAGIC) {
49 1 : pcache_dev_debug(pcache, "gc error: magic is not as expected. key_tail: %u:%u magic: %llx, expected: %llx\n",
50 : key_tail->cache_seg->cache_seg_id, key_tail->seg_off,
51 : kset_onmedia->magic, PCACHE_KSET_MAGIC);
52 1 : return false;
53 : }
54 :
55 : /* Verify the CRC of the kset_onmedia */
56 4857408 : if (kset_onmedia->crc != cache_kset_crc(kset_onmedia)) {
57 0 : pcache_dev_debug(pcache, "gc error: crc is not as expected. crc: %x, expected: %x\n",
58 : cache_kset_crc(kset_onmedia), kset_onmedia->crc);
59 0 : return false;
60 : }
61 :
62 4857345 : segs_used = bitmap_weight(cache->seg_map, cache->n_segs);
63 4857342 : segs_gc_threshold = cache->n_segs * pcache_cache_get_gc_percent(cache) / 100;
64 4857342 : if (segs_used < segs_gc_threshold) {
65 5054 : pcache_dev_debug(pcache, "segs_used: %u, segs_gc_threshold: %u\n", segs_used, segs_gc_threshold);
66 5054 : return false;
67 : }
68 :
69 : return true;
70 : }
71 :
72 : /**
73 : * last_kset_gc - Advances the garbage collection for the last kset.
74 : * @cache: Pointer to the pcache_cache structure.
75 : * @kset_onmedia: Pointer to the kset_onmedia structure for the last kset.
76 : */
77 76 : static void last_kset_gc(struct pcache_cache *cache, struct pcache_cache_kset_onmedia *kset_onmedia)
78 : {
79 76 : struct dm_pcache *pcache = CACHE_TO_PCACHE(cache);
80 76 : struct pcache_cache_segment *cur_seg, *next_seg;
81 :
82 76 : cur_seg = cache->key_tail.cache_seg;
83 :
84 76 : next_seg = &cache->segments[kset_onmedia->next_cache_seg_id];
85 :
86 76 : mutex_lock(&cache->key_tail_lock);
87 76 : cache->key_tail.cache_seg = next_seg;
88 76 : cache->key_tail.seg_off = 0;
89 76 : cache_encode_key_tail(cache);
90 76 : mutex_unlock(&cache->key_tail_lock);
91 :
92 76 : pcache_dev_debug(pcache, "gc advance kset seg: %u\n", cur_seg->cache_seg_id);
93 :
94 76 : spin_lock(&cache->seg_map_lock);
95 76 : __clear_bit(cur_seg->cache_seg_id, cache->seg_map);
96 76 : spin_unlock(&cache->seg_map_lock);
97 76 : }
98 :
99 18892 : void pcache_cache_gc_fn(struct work_struct *work)
100 : {
101 18892 : struct pcache_cache *cache = container_of(work, struct pcache_cache, gc_work.work);
102 18892 : struct dm_pcache *pcache = CACHE_TO_PCACHE(cache);
103 18892 : struct pcache_cache_pos dirty_tail, key_tail;
104 18892 : struct pcache_cache_kset_onmedia *kset_onmedia;
105 18892 : struct pcache_cache_key_onmedia *key_onmedia;
106 18892 : struct pcache_cache_key *key;
107 18892 : int ret;
108 18892 : int i;
109 :
110 18892 : kset_onmedia = (struct pcache_cache_kset_onmedia *)cache->gc_kset_onmedia_buf;
111 :
112 4871397 : while (true) {
113 4871397 : if (pcache_is_stopping(pcache) || atomic_read(&cache->gc_errors))
114 20 : return;
115 :
116 : /* Get new tail positions */
117 4871208 : mutex_lock(&cache->dirty_tail_lock);
118 4871305 : cache_pos_copy(&dirty_tail, &cache->dirty_tail);
119 4871055 : mutex_unlock(&cache->dirty_tail_lock);
120 :
121 4871371 : mutex_lock(&cache->key_tail_lock);
122 4871382 : cache_pos_copy(&key_tail, &cache->key_tail);
123 4871273 : mutex_unlock(&cache->key_tail_lock);
124 :
125 4871380 : if (!need_gc(cache, &dirty_tail, &key_tail))
126 : break;
127 :
128 4852264 : if (kset_onmedia->flags & PCACHE_KSET_FLAGS_LAST) {
129 : /* Don't move to the next segment if dirty_tail has not moved */
130 76 : if (dirty_tail.cache_seg == key_tail.cache_seg)
131 : break;
132 :
133 76 : last_kset_gc(cache, kset_onmedia);
134 76 : continue;
135 : }
136 :
137 39006832 : for (i = 0; i < kset_onmedia->key_num; i++) {
138 34154472 : struct pcache_cache_key key_tmp = { 0 };
139 :
140 34154472 : key_onmedia = &kset_onmedia->data[i];
141 :
142 34154472 : key = &key_tmp;
143 34154472 : cache_key_init(&cache->req_key_tree, key);
144 :
145 34151393 : ret = cache_key_decode(cache, key_onmedia, key);
146 34149130 : if (ret) {
147 : /* return without re-arm gc work, and prevent future
148 : * gc, because we can't retry the partial-gc-ed kset
149 : */
150 0 : atomic_inc(&cache->gc_errors);
151 0 : pcache_dev_err(pcache, "failed to decode cache key in gc\n");
152 0 : return;
153 : }
154 :
155 34149130 : cache_key_gc(cache, key);
156 : }
157 :
158 4852360 : pcache_dev_debug(pcache, "gc advance: %u:%u %u\n",
159 : key_tail.cache_seg->cache_seg_id,
160 : key_tail.seg_off,
161 : get_kset_onmedia_size(kset_onmedia));
162 :
163 4852360 : mutex_lock(&cache->key_tail_lock);
164 4852406 : cache_pos_advance(&cache->key_tail, get_kset_onmedia_size(kset_onmedia));
165 4852269 : cache_encode_key_tail(cache);
166 4850277 : mutex_unlock(&cache->key_tail_lock);
167 : }
168 :
169 18873 : queue_delayed_work(cache_get_wq(cache), &cache->gc_work, PCACHE_CACHE_GC_INTERVAL);
170 : }
|