forked from CachyOS/kernel-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0013-zram.patch
493 lines (465 loc) · 14.1 KB
/
0013-zram.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
From 661d513d0987b55f3b00273a1411d2f43e27f2d0 Mon Sep 17 00:00:00 2001
From: Peter Jung <admin@ptr1337.dev>
Date: Mon, 6 Feb 2023 09:53:13 +0100
Subject: [PATCH 13/15] zram
Signed-off-by: Peter Jung <admin@ptr1337.dev>
---
Documentation/admin-guide/blockdev/zram.rst | 2 +
drivers/block/zram/zram_drv.c | 319 +++++++++++++++++++-
drivers/block/zram/zram_drv.h | 7 +
3 files changed, 322 insertions(+), 6 deletions(-)
diff --git a/Documentation/admin-guide/blockdev/zram.rst b/Documentation/admin-guide/blockdev/zram.rst
index e4551579cb12..a1dd202efca1 100644
--- a/Documentation/admin-guide/blockdev/zram.rst
+++ b/Documentation/admin-guide/blockdev/zram.rst
@@ -209,6 +209,7 @@ compact WO trigger memory compaction
debug_stat RO this file is used for zram debugging purposes
backing_dev RW set up backend storage for zram to write out
idle WO mark allocated slot as idle
+merge WO trigger merge identical pages
====================== ====== ===============================================
@@ -267,6 +268,7 @@ line of text and contains the following stats separated by whitespace:
pages_compacted the number of pages freed during compaction
huge_pages the number of incompressible pages
huge_pages_since the number of incompressible pages since zram set up
+ pages_merged the number of identical pages merged into single one
================ =============================================================
File /sys/block/zram<id>/bd_stat
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 03ef03e10618..4671be36c737 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -33,12 +33,15 @@
#include <linux/debugfs.h>
#include <linux/cpuhotplug.h>
#include <linux/part_stat.h>
+#include <linux/hashtable.h>
+#include <linux/xxhash.h>
#include "zram_drv.h"
static DEFINE_IDR(zram_index_idr);
/* idr index must be protected */
static DEFINE_MUTEX(zram_index_mutex);
+static DEFINE_MUTEX(zram_rbtree_mutex);
static int zram_major;
static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
@@ -57,6 +60,16 @@ static void zram_free_page(struct zram *zram, size_t index);
static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
u32 index, int offset, struct bio *bio);
+struct zram_rbtree_node {
+ struct rb_node node;
+ unsigned long key;
+ unsigned long cnt;
+};
+
+struct zram_hash_node {
+ unsigned long index;
+ struct hlist_node next;
+};
static int zram_slot_trylock(struct zram *zram, u32 index)
{
@@ -1140,7 +1153,7 @@ static ssize_t recomp_algorithm_store(struct device *dev,
while (*args) {
args = next_arg(args, ¶m, &val);
- if (!*val)
+ if (!val || !*val)
return -EINVAL;
if (!strcmp(param, "algo")) {
@@ -1184,6 +1197,30 @@ static ssize_t compact_store(struct device *dev,
return len;
}
+static int zram_do_scan(struct zram *zram);
+
+static ssize_t merge_store(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t len)
+{
+ struct zram *zram = dev_to_zram(dev);
+ int ret;
+
+ down_read(&zram->init_lock);
+ if (!init_done(zram)) {
+ up_read(&zram->init_lock);
+ return -EINVAL;
+ }
+
+ ret = zram_do_scan(zram);
+ if (ret != 0) {
+ up_read(&zram->init_lock);
+ return -ENOMEM;
+ }
+
+ up_read(&zram->init_lock);
+ return len;
+}
+
static ssize_t io_stat_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -1223,7 +1260,7 @@ static ssize_t mm_stat_show(struct device *dev,
max_used = atomic_long_read(&zram->stats.max_used_pages);
ret = scnprintf(buf, PAGE_SIZE,
- "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
+ "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu %8llu\n",
orig_size << PAGE_SHIFT,
(u64)atomic64_read(&zram->stats.compr_data_size),
mem_used << PAGE_SHIFT,
@@ -1232,7 +1269,8 @@ static ssize_t mm_stat_show(struct device *dev,
(u64)atomic64_read(&zram->stats.same_pages),
atomic_long_read(&pool_stats.pages_compacted),
(u64)atomic64_read(&zram->stats.huge_pages),
- (u64)atomic64_read(&zram->stats.huge_pages_since));
+ (u64)atomic64_read(&zram->stats.huge_pages_since),
+ (u64)atomic64_read(&zram->stats.pages_merged));
up_read(&zram->init_lock);
return ret;
@@ -1283,6 +1321,248 @@ static DEVICE_ATTR_RO(bd_stat);
#endif
static DEVICE_ATTR_RO(debug_stat);
+static bool zram_rbtree_insert(struct rb_root *root, struct zram_rbtree_node *data)
+{
+ struct rb_node **new = &(root->rb_node), *parent = NULL;
+ struct zram_rbtree_node *this;
+
+ while (*new) {
+ this = rb_entry(*new, struct zram_rbtree_node, node);
+ parent = *new;
+ if (data->key < this->key)
+ new = &((*new)->rb_left);
+ else if (data->key > this->key)
+ new = &((*new)->rb_right);
+ else
+ return false;
+ }
+
+ rb_link_node(&data->node, parent, new);
+ rb_insert_color(&data->node, root);
+ return true;
+}
+
+static struct zram_rbtree_node *zram_rbtree_search(struct rb_root *root,
+ unsigned long key)
+{
+ struct rb_node *node = root->rb_node;
+ struct zram_rbtree_node *data;
+
+ while (node) {
+ data = rb_entry(node, struct zram_rbtree_node, node);
+ if (key < data->key)
+ node = node->rb_left;
+ else if (key > data->key)
+ node = node->rb_right;
+ else
+ return data;
+ }
+
+ return NULL;
+}
+
+static unsigned long zram_calc_hash(void *src, size_t len)
+{
+ return xxhash(src, len, 0);
+}
+
+static int zram_cmp_obj_and_merge(struct zram *zram, struct hlist_head *htable,
+ size_t htable_size, size_t index)
+{
+ struct zram_rbtree_node *rb_node;
+ struct zram_hash_node *node;
+ unsigned long handle, cur_handle;
+ size_t obj_size;
+ char *src, *buf;
+ unsigned long hash;
+ int ret = 0;
+
+ handle = zram_get_handle(zram, index);
+ if (!handle)
+ return ret;
+
+ obj_size = zram_get_obj_size(zram, index);
+ buf = kmalloc(obj_size, GFP_KERNEL);
+ if (!buf) {
+ pr_err("Failed to allocate zs_map_object buffer\n");
+ return -ENOMEM;
+ }
+
+ src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
+ memcpy(buf, src, obj_size);
+ zs_unmap_object(zram->mem_pool, handle);
+ hash = zram_calc_hash(buf, obj_size);
+
+ mutex_lock(&zram_rbtree_mutex);
+ hlist_for_each_entry(node, &htable[hash % htable_size], next) {
+ int cmp;
+
+ zram_slot_lock(zram, node->index);
+
+ /*
+ * Page may change as the hash table is being formed,
+ * so the checks below are necessary.
+ */
+ cur_handle = zram_get_handle(zram, node->index);
+ if (handle == cur_handle ||
+ obj_size != zram_get_obj_size(zram, node->index)) {
+ zram_slot_unlock(zram, node->index);
+ continue;
+ }
+
+ src = zs_map_object(zram->mem_pool, cur_handle, ZS_MM_RO);
+ cmp = memcmp(buf, src, obj_size);
+ zs_unmap_object(zram->mem_pool, cur_handle);
+
+ if (!cmp) {
+ rb_node = zram_rbtree_search(&zram->sph_rbtree, handle);
+
+ /*
+ * This check is necessary in order not to zs_free an object
+ * that someone already refers to. This situation is possible
+ * when with repeated calls to zram_do_scan(). For example:
+ *
+ * [slot0] [slot1] [slot2] [slot3] [slot4]
+ * [obj0] [obj1] [obj2] [obj3] [obj4]
+ *
+ * Let's imagine that obj2 and obj3 are equal, and we called
+ * zram_do_scan() function:
+ *
+ * [slot0] [slot1] [slot2] [slot3] [slot4]
+ * [obj0] [obj1] [obj2] [obj2] [obj4]
+ *
+ * Now, slot2 and slot3 refers to obj2 zsmalloc object.
+ * Time passed, now slot0 refres to obj0_n, which is equal
+ * to obj2:
+ *
+ * [slot0] [slot1] [slot2] [slot3] [slot4]
+ * [obj0_n] [obj1] [obj2] [obj2] [obj4]
+ *
+ * Now we call zram_do_scan() function again. We get to slot2,
+ * and we understand that obj2 and obj0_n hashes are the same. We
+ * try to zs_free(obj2), but slot3 also already refers to it.
+ *
+ * This is not correct!
+ */
+ if (unlikely(rb_node))
+ if (rb_node->cnt > 1) {
+ zram_slot_unlock(zram, node->index);
+ continue;
+ }
+
+ zram_set_handle(zram, index, cur_handle);
+ zs_free(zram->mem_pool, handle);
+
+ rb_node = zram_rbtree_search(&zram->sph_rbtree, cur_handle);
+
+ if (!rb_node) {
+ rb_node = kzalloc(sizeof(struct zram_rbtree_node),
+ GFP_KERNEL);
+ if (!rb_node) {
+ pr_err("Failed to allocate rb_node\n");
+ ret = -ENOMEM;
+ zram_slot_unlock(zram, node->index);
+ mutex_unlock(&zram_rbtree_mutex);
+ goto merged_or_err;
+ }
+
+ rb_node->key = cur_handle;
+ /* Two slots refers to an zsmalloc object with cur_handle key */
+ rb_node->cnt = 2;
+ zram_rbtree_insert(&zram->sph_rbtree, rb_node);
+ } else {
+ rb_node->cnt++;
+ }
+
+ atomic64_inc(&zram->stats.pages_merged);
+ atomic64_sub(obj_size, &zram->stats.compr_data_size);
+ zram_set_flag(zram, index, ZRAM_MERGED);
+ zram_set_flag(zram, node->index, ZRAM_MERGED);
+
+ zram_slot_unlock(zram, node->index);
+ mutex_unlock(&zram_rbtree_mutex);
+ goto merged_or_err;
+ }
+
+ zram_slot_unlock(zram, node->index);
+ }
+
+ mutex_unlock(&zram_rbtree_mutex);
+
+ node = kmalloc(sizeof(struct zram_hash_node), GFP_KERNEL);
+ if (!node) {
+ ret = -ENOMEM;
+ goto merged_or_err;
+ }
+
+ node->index = index;
+ hlist_add_head(&node->next, &htable[hash % htable_size]);
+
+merged_or_err:
+ kfree(buf);
+ return ret;
+}
+
+static void zram_free_htable_entries(struct hlist_head *htable,
+ size_t htable_size)
+{
+ struct hlist_node *n;
+ struct zram_hash_node *node;
+
+ hlist_for_each_entry_safe(node, n, htable, next) {
+ hlist_del(&node->next);
+ kfree(node);
+ }
+}
+
+static int zram_do_scan(struct zram *zram)
+{
+ size_t num_pages = zram->disksize >> PAGE_SHIFT;
+ size_t htable_size = num_pages;
+ size_t index;
+ struct hlist_head *htable;
+ int i, ret = 0;
+
+ htable = vzalloc(htable_size * sizeof(struct hlist_head));
+ if (!htable) {
+ pr_err("Failed to allocate hash table\n");
+ return -ENOMEM;
+ }
+
+ for (i = 0; i < htable_size; i++)
+ INIT_HLIST_HEAD(&htable[i]);
+
+ for (index = 0; index < num_pages; index++) {
+ zram_slot_lock(zram, index);
+
+ if (!zram_allocated(zram, index)) {
+ zram_slot_unlock(zram, index);
+ continue;
+ }
+
+ if (zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
+ zram_test_flag(zram, index, ZRAM_WB) ||
+ zram_test_flag(zram, index, ZRAM_SAME)) {
+ zram_slot_unlock(zram, index);
+ continue;
+ }
+
+ /* Ignore pages that have been recompressed */
+ if (zram_get_priority(zram, index) != 0)
+ continue;
+
+ ret = zram_cmp_obj_and_merge(zram, htable, htable_size, index);
+ zram_slot_unlock(zram, index);
+ if (ret != 0)
+ goto out;
+ }
+
+out:
+ zram_free_htable_entries(htable, htable_size);
+ vfree(htable);
+ return ret;
+}
+
static void zram_meta_free(struct zram *zram, u64 disksize)
{
size_t num_pages = disksize >> PAGE_SHIFT;
@@ -1324,6 +1604,7 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize)
static void zram_free_page(struct zram *zram, size_t index)
{
unsigned long handle;
+ struct zram_rbtree_node *node;
#ifdef CONFIG_ZRAM_MEMORY_TRACKING
zram->table[index].ac_time = 0;
@@ -1361,7 +1642,28 @@ static void zram_free_page(struct zram *zram, size_t index)
if (!handle)
return;
- zs_free(zram->mem_pool, handle);
+ if (zram_test_flag(zram, index, ZRAM_MERGED)) {
+ zram_clear_flag(zram, index, ZRAM_MERGED);
+ mutex_lock(&zram_rbtree_mutex);
+
+ node = zram_rbtree_search(&zram->sph_rbtree, handle);
+ BUG_ON(!node);
+
+ node->cnt--;
+ if (node->cnt == 0) {
+ rb_erase(&node->node, &zram->sph_rbtree);
+ mutex_unlock(&zram_rbtree_mutex);
+
+ zs_free(zram->mem_pool, handle);
+ kfree(node);
+ } else {
+ mutex_unlock(&zram_rbtree_mutex);
+ }
+
+ atomic64_dec(&zram->stats.pages_merged);
+ } else {
+ zs_free(zram->mem_pool, handle);
+ }
atomic64_sub(zram_get_obj_size(zram, index),
&zram->stats.compr_data_size);
@@ -1824,7 +2126,7 @@ static ssize_t recompress_store(struct device *dev,
while (*args) {
args = next_arg(args, ¶m, &val);
- if (!*val)
+ if (!val || !*val)
return -EINVAL;
if (!strcmp(param, "type")) {
@@ -1909,7 +2211,8 @@ static ssize_t recompress_store(struct device *dev,
if (zram_test_flag(zram, index, ZRAM_WB) ||
zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
zram_test_flag(zram, index, ZRAM_SAME) ||
- zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
+ zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE) ||
+ zram_test_flag(zram, index, ZRAM_MERGED))
goto next;
err = zram_recompress(zram, index, page, threshold,
@@ -2295,6 +2598,7 @@ static const struct block_device_operations zram_devops = {
};
static DEVICE_ATTR_WO(compact);
+static DEVICE_ATTR_WO(merge);
static DEVICE_ATTR_RW(disksize);
static DEVICE_ATTR_RO(initstate);
static DEVICE_ATTR_WO(reset);
@@ -2335,6 +2639,7 @@ static struct attribute *zram_disk_attrs[] = {
#ifdef CONFIG_ZRAM_WRITEBACK
&dev_attr_bd_stat.attr,
#endif
+ &dev_attr_merge.attr,
&dev_attr_debug_stat.attr,
#ifdef CONFIG_ZRAM_MULTI_COMP
&dev_attr_recomp_algorithm.attr,
@@ -2421,6 +2726,8 @@ static int zram_add(void)
comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
+ zram->sph_rbtree = RB_ROOT;
+
zram_debugfs_register(zram);
pr_info("Added device: %s\n", zram->disk->disk_name);
return device_id;
diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h
index c5254626f051..2afdbf76a1aa 100644
--- a/drivers/block/zram/zram_drv.h
+++ b/drivers/block/zram/zram_drv.h
@@ -56,6 +56,7 @@ enum zram_pageflags {
ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */
ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */
+ ZRAM_MERGED, /* page was merged */
__NR_ZRAM_PAGEFLAGS,
};
@@ -87,6 +88,7 @@ struct zram_stats {
atomic_long_t max_used_pages; /* no. of maximum pages stored */
atomic64_t writestall; /* no. of write slow paths */
atomic64_t miss_free; /* no. of missed free */
+ atomic64_t pages_merged; /* no. of pages, which merged into single one */
#ifdef CONFIG_ZRAM_WRITEBACK
atomic64_t bd_count; /* no. of pages in backing device */
atomic64_t bd_reads; /* no. of reads from backing device */
@@ -140,5 +142,10 @@ struct zram {
#ifdef CONFIG_ZRAM_MEMORY_TRACKING
struct dentry *debugfs_dir;
#endif
+ /*
+ * This is same pages handle's rb tree, where the key is a handle
+ * to same pages and the value is a link counter
+ */
+ struct rb_root sph_rbtree;
};
#endif
--
2.40.0