Commit | Line | Data |
---|---|---|
306b0c95 | 1 | /* |
f1e3cfff | 2 | * Compressed RAM block device |
306b0c95 | 3 | * |
1130ebba | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
7bfb3de8 | 5 | * 2012, 2013 Minchan Kim |
306b0c95 NG |
6 | * |
7 | * This code is released using a dual license strategy: BSD/GPL | |
8 | * You can choose the licence that better fits your requirements. | |
9 | * | |
10 | * Released under the terms of 3-clause BSD License | |
11 | * Released under the terms of GNU General Public License Version 2.0 | |
12 | * | |
306b0c95 NG |
13 | */ |
14 | ||
f1e3cfff | 15 | #define KMSG_COMPONENT "zram" |
306b0c95 NG |
16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
17 | ||
18 | #include <linux/module.h> | |
19 | #include <linux/kernel.h> | |
8946a086 | 20 | #include <linux/bio.h> |
306b0c95 NG |
21 | #include <linux/bitops.h> |
22 | #include <linux/blkdev.h> | |
23 | #include <linux/buffer_head.h> | |
24 | #include <linux/device.h> | |
25 | #include <linux/genhd.h> | |
26 | #include <linux/highmem.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
b09ab054 | 28 | #include <linux/backing-dev.h> |
306b0c95 | 29 | #include <linux/string.h> |
306b0c95 | 30 | #include <linux/vmalloc.h> |
fcfa8d95 | 31 | #include <linux/err.h> |
85508ec6 | 32 | #include <linux/idr.h> |
6566d1a3 | 33 | #include <linux/sysfs.h> |
1dd6c834 | 34 | #include <linux/cpuhotplug.h> |
306b0c95 | 35 | |
16a4bfb9 | 36 | #include "zram_drv.h" |
306b0c95 | 37 | |
85508ec6 | 38 | static DEFINE_IDR(zram_index_idr); |
6566d1a3 SS |
39 | /* idr index must be protected */ |
40 | static DEFINE_MUTEX(zram_index_mutex); | |
41 | ||
f1e3cfff | 42 | static int zram_major; |
b7ca232e | 43 | static const char *default_compressor = "lzo"; |
306b0c95 | 44 | |
306b0c95 | 45 | /* Module params (documentation at end) */ |
ca3d70bd | 46 | static unsigned int num_devices = 1; |
33863c21 | 47 | |
1f7319c7 MK |
48 | static void zram_free_page(struct zram *zram, size_t index); |
49 | ||
08eee69f | 50 | static inline bool init_done(struct zram *zram) |
be2d1d56 | 51 | { |
08eee69f | 52 | return zram->disksize; |
be2d1d56 SS |
53 | } |
54 | ||
9b3bb7ab SS |
55 | static inline struct zram *dev_to_zram(struct device *dev) |
56 | { | |
57 | return (struct zram *)dev_to_disk(dev)->private_data; | |
58 | } | |
59 | ||
643ae61d MK |
60 | static unsigned long zram_get_handle(struct zram *zram, u32 index) |
61 | { | |
62 | return zram->table[index].handle; | |
63 | } | |
64 | ||
65 | static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) | |
66 | { | |
67 | zram->table[index].handle = handle; | |
68 | } | |
69 | ||
b31177f2 | 70 | /* flag operations require table entry bit_spin_lock() being held */ |
beb6602c | 71 | static int zram_test_flag(struct zram *zram, u32 index, |
522698d7 | 72 | enum zram_pageflags flag) |
99ebbd30 | 73 | { |
beb6602c | 74 | return zram->table[index].value & BIT(flag); |
522698d7 | 75 | } |
99ebbd30 | 76 | |
beb6602c | 77 | static void zram_set_flag(struct zram *zram, u32 index, |
522698d7 SS |
78 | enum zram_pageflags flag) |
79 | { | |
beb6602c | 80 | zram->table[index].value |= BIT(flag); |
522698d7 | 81 | } |
99ebbd30 | 82 | |
beb6602c | 83 | static void zram_clear_flag(struct zram *zram, u32 index, |
522698d7 SS |
84 | enum zram_pageflags flag) |
85 | { | |
beb6602c | 86 | zram->table[index].value &= ~BIT(flag); |
522698d7 | 87 | } |
99ebbd30 | 88 | |
beb6602c | 89 | static inline void zram_set_element(struct zram *zram, u32 index, |
8e19d540 | 90 | unsigned long element) |
91 | { | |
beb6602c | 92 | zram->table[index].element = element; |
8e19d540 | 93 | } |
94 | ||
643ae61d | 95 | static unsigned long zram_get_element(struct zram *zram, u32 index) |
8e19d540 | 96 | { |
643ae61d | 97 | return zram->table[index].element; |
8e19d540 | 98 | } |
99 | ||
beb6602c | 100 | static size_t zram_get_obj_size(struct zram *zram, u32 index) |
522698d7 | 101 | { |
beb6602c | 102 | return zram->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1); |
99ebbd30 AM |
103 | } |
104 | ||
beb6602c | 105 | static void zram_set_obj_size(struct zram *zram, |
522698d7 | 106 | u32 index, size_t size) |
9b3bb7ab | 107 | { |
beb6602c | 108 | unsigned long flags = zram->table[index].value >> ZRAM_FLAG_SHIFT; |
9b3bb7ab | 109 | |
beb6602c | 110 | zram->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size; |
522698d7 SS |
111 | } |
112 | ||
1f7319c7 | 113 | #if PAGE_SIZE != 4096 |
1c53e0d2 | 114 | static inline bool is_partial_io(struct bio_vec *bvec) |
522698d7 SS |
115 | { |
116 | return bvec->bv_len != PAGE_SIZE; | |
117 | } | |
1f7319c7 MK |
118 | #else |
119 | static inline bool is_partial_io(struct bio_vec *bvec) | |
120 | { | |
121 | return false; | |
122 | } | |
123 | #endif | |
522698d7 | 124 | |
b09ab054 MK |
125 | static void zram_revalidate_disk(struct zram *zram) |
126 | { | |
127 | revalidate_disk(zram->disk); | |
128 | /* revalidate_disk reset the BDI_CAP_STABLE_WRITES so set again */ | |
e1735496 | 129 | zram->disk->queue->backing_dev_info->capabilities |= |
b09ab054 MK |
130 | BDI_CAP_STABLE_WRITES; |
131 | } | |
132 | ||
522698d7 SS |
133 | /* |
134 | * Check if request is within bounds and aligned on zram logical blocks. | |
135 | */ | |
1c53e0d2 | 136 | static inline bool valid_io_request(struct zram *zram, |
522698d7 SS |
137 | sector_t start, unsigned int size) |
138 | { | |
139 | u64 end, bound; | |
140 | ||
141 | /* unaligned request */ | |
142 | if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1))) | |
1c53e0d2 | 143 | return false; |
522698d7 | 144 | if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1))) |
1c53e0d2 | 145 | return false; |
522698d7 SS |
146 | |
147 | end = start + (size >> SECTOR_SHIFT); | |
148 | bound = zram->disksize >> SECTOR_SHIFT; | |
149 | /* out of range range */ | |
150 | if (unlikely(start >= bound || end > bound || start > end)) | |
1c53e0d2 | 151 | return false; |
522698d7 SS |
152 | |
153 | /* I/O request is valid */ | |
1c53e0d2 | 154 | return true; |
522698d7 SS |
155 | } |
156 | ||
157 | static void update_position(u32 *index, int *offset, struct bio_vec *bvec) | |
158 | { | |
e86942c7 | 159 | *index += (*offset + bvec->bv_len) / PAGE_SIZE; |
522698d7 SS |
160 | *offset = (*offset + bvec->bv_len) % PAGE_SIZE; |
161 | } | |
162 | ||
163 | static inline void update_used_max(struct zram *zram, | |
164 | const unsigned long pages) | |
165 | { | |
166 | unsigned long old_max, cur_max; | |
167 | ||
168 | old_max = atomic_long_read(&zram->stats.max_used_pages); | |
169 | ||
170 | do { | |
171 | cur_max = old_max; | |
172 | if (pages > cur_max) | |
173 | old_max = atomic_long_cmpxchg( | |
174 | &zram->stats.max_used_pages, cur_max, pages); | |
175 | } while (old_max != cur_max); | |
176 | } | |
177 | ||
48ad1abe | 178 | static inline void zram_fill_page(void *ptr, unsigned long len, |
8e19d540 | 179 | unsigned long value) |
180 | { | |
8e19d540 | 181 | WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long))); |
48ad1abe | 182 | memset_l(ptr, value, len / sizeof(unsigned long)); |
8e19d540 | 183 | } |
184 | ||
185 | static bool page_same_filled(void *ptr, unsigned long *element) | |
522698d7 SS |
186 | { |
187 | unsigned int pos; | |
188 | unsigned long *page; | |
f0fe9984 | 189 | unsigned long val; |
522698d7 SS |
190 | |
191 | page = (unsigned long *)ptr; | |
f0fe9984 | 192 | val = page[0]; |
522698d7 | 193 | |
f0fe9984 SP |
194 | for (pos = 1; pos < PAGE_SIZE / sizeof(*page); pos++) { |
195 | if (val != page[pos]) | |
1c53e0d2 | 196 | return false; |
522698d7 SS |
197 | } |
198 | ||
f0fe9984 | 199 | *element = val; |
8e19d540 | 200 | |
1c53e0d2 | 201 | return true; |
522698d7 SS |
202 | } |
203 | ||
9b3bb7ab SS |
204 | static ssize_t initstate_show(struct device *dev, |
205 | struct device_attribute *attr, char *buf) | |
206 | { | |
a68eb3b6 | 207 | u32 val; |
9b3bb7ab SS |
208 | struct zram *zram = dev_to_zram(dev); |
209 | ||
a68eb3b6 SS |
210 | down_read(&zram->init_lock); |
211 | val = init_done(zram); | |
212 | up_read(&zram->init_lock); | |
9b3bb7ab | 213 | |
56b4e8cb | 214 | return scnprintf(buf, PAGE_SIZE, "%u\n", val); |
9b3bb7ab SS |
215 | } |
216 | ||
522698d7 SS |
217 | static ssize_t disksize_show(struct device *dev, |
218 | struct device_attribute *attr, char *buf) | |
219 | { | |
220 | struct zram *zram = dev_to_zram(dev); | |
221 | ||
222 | return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize); | |
223 | } | |
224 | ||
9ada9da9 MK |
225 | static ssize_t mem_limit_store(struct device *dev, |
226 | struct device_attribute *attr, const char *buf, size_t len) | |
227 | { | |
228 | u64 limit; | |
229 | char *tmp; | |
230 | struct zram *zram = dev_to_zram(dev); | |
231 | ||
232 | limit = memparse(buf, &tmp); | |
233 | if (buf == tmp) /* no chars parsed, invalid input */ | |
234 | return -EINVAL; | |
235 | ||
236 | down_write(&zram->init_lock); | |
237 | zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT; | |
238 | up_write(&zram->init_lock); | |
239 | ||
240 | return len; | |
241 | } | |
242 | ||
461a8eee MK |
243 | static ssize_t mem_used_max_store(struct device *dev, |
244 | struct device_attribute *attr, const char *buf, size_t len) | |
245 | { | |
246 | int err; | |
247 | unsigned long val; | |
248 | struct zram *zram = dev_to_zram(dev); | |
461a8eee MK |
249 | |
250 | err = kstrtoul(buf, 10, &val); | |
251 | if (err || val != 0) | |
252 | return -EINVAL; | |
253 | ||
254 | down_read(&zram->init_lock); | |
5a99e95b | 255 | if (init_done(zram)) { |
461a8eee | 256 | atomic_long_set(&zram->stats.max_used_pages, |
beb6602c | 257 | zs_get_total_pages(zram->mem_pool)); |
5a99e95b | 258 | } |
461a8eee MK |
259 | up_read(&zram->init_lock); |
260 | ||
261 | return len; | |
262 | } | |
263 | ||
013bf95a MK |
264 | #ifdef CONFIG_ZRAM_WRITEBACK |
265 | static bool zram_wb_enabled(struct zram *zram) | |
266 | { | |
267 | return zram->backing_dev; | |
268 | } | |
269 | ||
270 | static void reset_bdev(struct zram *zram) | |
271 | { | |
272 | struct block_device *bdev; | |
273 | ||
274 | if (!zram_wb_enabled(zram)) | |
275 | return; | |
276 | ||
277 | bdev = zram->bdev; | |
278 | if (zram->old_block_size) | |
279 | set_blocksize(bdev, zram->old_block_size); | |
280 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); | |
281 | /* hope filp_close flush all of IO */ | |
282 | filp_close(zram->backing_dev, NULL); | |
283 | zram->backing_dev = NULL; | |
284 | zram->old_block_size = 0; | |
285 | zram->bdev = NULL; | |
1363d466 MK |
286 | |
287 | kvfree(zram->bitmap); | |
288 | zram->bitmap = NULL; | |
013bf95a MK |
289 | } |
290 | ||
291 | static ssize_t backing_dev_show(struct device *dev, | |
292 | struct device_attribute *attr, char *buf) | |
293 | { | |
294 | struct zram *zram = dev_to_zram(dev); | |
295 | struct file *file = zram->backing_dev; | |
296 | char *p; | |
297 | ssize_t ret; | |
298 | ||
299 | down_read(&zram->init_lock); | |
300 | if (!zram_wb_enabled(zram)) { | |
301 | memcpy(buf, "none\n", 5); | |
302 | up_read(&zram->init_lock); | |
303 | return 5; | |
304 | } | |
305 | ||
306 | p = file_path(file, buf, PAGE_SIZE - 1); | |
307 | if (IS_ERR(p)) { | |
308 | ret = PTR_ERR(p); | |
309 | goto out; | |
310 | } | |
311 | ||
312 | ret = strlen(p); | |
313 | memmove(buf, p, ret); | |
314 | buf[ret++] = '\n'; | |
315 | out: | |
316 | up_read(&zram->init_lock); | |
317 | return ret; | |
318 | } | |
319 | ||
320 | static ssize_t backing_dev_store(struct device *dev, | |
321 | struct device_attribute *attr, const char *buf, size_t len) | |
322 | { | |
323 | char *file_name; | |
324 | struct file *backing_dev = NULL; | |
325 | struct inode *inode; | |
326 | struct address_space *mapping; | |
1363d466 MK |
327 | unsigned int bitmap_sz, old_block_size = 0; |
328 | unsigned long nr_pages, *bitmap = NULL; | |
013bf95a MK |
329 | struct block_device *bdev = NULL; |
330 | int err; | |
331 | struct zram *zram = dev_to_zram(dev); | |
332 | ||
333 | file_name = kmalloc(PATH_MAX, GFP_KERNEL); | |
334 | if (!file_name) | |
335 | return -ENOMEM; | |
336 | ||
337 | down_write(&zram->init_lock); | |
338 | if (init_done(zram)) { | |
339 | pr_info("Can't setup backing device for initialized device\n"); | |
340 | err = -EBUSY; | |
341 | goto out; | |
342 | } | |
343 | ||
344 | strlcpy(file_name, buf, len); | |
345 | ||
346 | backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0); | |
347 | if (IS_ERR(backing_dev)) { | |
348 | err = PTR_ERR(backing_dev); | |
349 | backing_dev = NULL; | |
350 | goto out; | |
351 | } | |
352 | ||
353 | mapping = backing_dev->f_mapping; | |
354 | inode = mapping->host; | |
355 | ||
356 | /* Support only block device in this moment */ | |
357 | if (!S_ISBLK(inode->i_mode)) { | |
358 | err = -ENOTBLK; | |
359 | goto out; | |
360 | } | |
361 | ||
362 | bdev = bdgrab(I_BDEV(inode)); | |
363 | err = blkdev_get(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL, zram); | |
364 | if (err < 0) | |
365 | goto out; | |
366 | ||
1363d466 MK |
367 | nr_pages = i_size_read(inode) >> PAGE_SHIFT; |
368 | bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long); | |
369 | bitmap = kvzalloc(bitmap_sz, GFP_KERNEL); | |
370 | if (!bitmap) { | |
371 | err = -ENOMEM; | |
372 | goto out; | |
373 | } | |
374 | ||
013bf95a MK |
375 | old_block_size = block_size(bdev); |
376 | err = set_blocksize(bdev, PAGE_SIZE); | |
377 | if (err) | |
378 | goto out; | |
379 | ||
380 | reset_bdev(zram); | |
1363d466 | 381 | spin_lock_init(&zram->bitmap_lock); |
013bf95a MK |
382 | |
383 | zram->old_block_size = old_block_size; | |
384 | zram->bdev = bdev; | |
385 | zram->backing_dev = backing_dev; | |
1363d466 MK |
386 | zram->bitmap = bitmap; |
387 | zram->nr_pages = nr_pages; | |
013bf95a MK |
388 | up_write(&zram->init_lock); |
389 | ||
390 | pr_info("setup backing device %s\n", file_name); | |
391 | kfree(file_name); | |
392 | ||
393 | return len; | |
394 | out: | |
1363d466 MK |
395 | if (bitmap) |
396 | kvfree(bitmap); | |
397 | ||
013bf95a MK |
398 | if (bdev) |
399 | blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); | |
400 | ||
401 | if (backing_dev) | |
402 | filp_close(backing_dev, NULL); | |
403 | ||
404 | up_write(&zram->init_lock); | |
405 | ||
406 | kfree(file_name); | |
407 | ||
408 | return err; | |
409 | } | |
410 | ||
1363d466 MK |
411 | static unsigned long get_entry_bdev(struct zram *zram) |
412 | { | |
413 | unsigned long entry; | |
414 | ||
415 | spin_lock(&zram->bitmap_lock); | |
416 | /* skip 0 bit to confuse zram.handle = 0 */ | |
417 | entry = find_next_zero_bit(zram->bitmap, zram->nr_pages, 1); | |
418 | if (entry == zram->nr_pages) { | |
419 | spin_unlock(&zram->bitmap_lock); | |
420 | return 0; | |
421 | } | |
422 | ||
423 | set_bit(entry, zram->bitmap); | |
424 | spin_unlock(&zram->bitmap_lock); | |
425 | ||
426 | return entry; | |
427 | } | |
428 | ||
429 | static void put_entry_bdev(struct zram *zram, unsigned long entry) | |
430 | { | |
431 | int was_set; | |
432 | ||
433 | spin_lock(&zram->bitmap_lock); | |
434 | was_set = test_and_clear_bit(entry, zram->bitmap); | |
435 | spin_unlock(&zram->bitmap_lock); | |
436 | WARN_ON_ONCE(!was_set); | |
437 | } | |
438 | ||
db8ffbd4 MK |
439 | void zram_page_end_io(struct bio *bio) |
440 | { | |
441 | struct page *page = bio->bi_io_vec[0].bv_page; | |
442 | ||
443 | page_endio(page, op_is_write(bio_op(bio)), | |
444 | blk_status_to_errno(bio->bi_status)); | |
445 | bio_put(bio); | |
446 | } | |
447 | ||
8e654f8f MK |
448 | /* |
449 | * Returns 1 if the submission is successful. | |
450 | */ | |
451 | static int read_from_bdev_async(struct zram *zram, struct bio_vec *bvec, | |
452 | unsigned long entry, struct bio *parent) | |
453 | { | |
454 | struct bio *bio; | |
455 | ||
456 | bio = bio_alloc(GFP_ATOMIC, 1); | |
457 | if (!bio) | |
458 | return -ENOMEM; | |
459 | ||
460 | bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9); | |
a0725ab0 | 461 | bio_set_dev(bio, zram->bdev); |
8e654f8f MK |
462 | if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, bvec->bv_offset)) { |
463 | bio_put(bio); | |
464 | return -EIO; | |
465 | } | |
466 | ||
467 | if (!parent) { | |
468 | bio->bi_opf = REQ_OP_READ; | |
469 | bio->bi_end_io = zram_page_end_io; | |
470 | } else { | |
471 | bio->bi_opf = parent->bi_opf; | |
472 | bio_chain(bio, parent); | |
473 | } | |
474 | ||
475 | submit_bio(bio); | |
476 | return 1; | |
477 | } | |
478 | ||
479 | struct zram_work { | |
480 | struct work_struct work; | |
481 | struct zram *zram; | |
482 | unsigned long entry; | |
483 | struct bio *bio; | |
484 | }; | |
485 | ||
486 | #if PAGE_SIZE != 4096 | |
487 | static void zram_sync_read(struct work_struct *work) | |
488 | { | |
489 | struct bio_vec bvec; | |
490 | struct zram_work *zw = container_of(work, struct zram_work, work); | |
491 | struct zram *zram = zw->zram; | |
492 | unsigned long entry = zw->entry; | |
493 | struct bio *bio = zw->bio; | |
494 | ||
495 | read_from_bdev_async(zram, &bvec, entry, bio); | |
496 | } | |
497 | ||
498 | /* | |
499 | * Block layer want one ->make_request_fn to be active at a time | |
500 | * so if we use chained IO with parent IO in same context, | |
501 | * it's a deadlock. To avoid, it, it uses worker thread context. | |
502 | */ | |
503 | static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec, | |
504 | unsigned long entry, struct bio *bio) | |
505 | { | |
506 | struct zram_work work; | |
507 | ||
508 | work.zram = zram; | |
509 | work.entry = entry; | |
510 | work.bio = bio; | |
511 | ||
512 | INIT_WORK_ONSTACK(&work.work, zram_sync_read); | |
513 | queue_work(system_unbound_wq, &work.work); | |
514 | flush_work(&work.work); | |
515 | destroy_work_on_stack(&work.work); | |
516 | ||
517 | return 1; | |
518 | } | |
519 | #else | |
520 | static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec, | |
521 | unsigned long entry, struct bio *bio) | |
522 | { | |
523 | WARN_ON(1); | |
524 | return -EIO; | |
525 | } | |
526 | #endif | |
527 | ||
528 | static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, | |
529 | unsigned long entry, struct bio *parent, bool sync) | |
530 | { | |
531 | if (sync) | |
532 | return read_from_bdev_sync(zram, bvec, entry, parent); | |
533 | else | |
534 | return read_from_bdev_async(zram, bvec, entry, parent); | |
535 | } | |
536 | ||
db8ffbd4 MK |
537 | static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, |
538 | u32 index, struct bio *parent, | |
539 | unsigned long *pentry) | |
540 | { | |
541 | struct bio *bio; | |
542 | unsigned long entry; | |
543 | ||
544 | bio = bio_alloc(GFP_ATOMIC, 1); | |
545 | if (!bio) | |
546 | return -ENOMEM; | |
547 | ||
548 | entry = get_entry_bdev(zram); | |
549 | if (!entry) { | |
550 | bio_put(bio); | |
551 | return -ENOSPC; | |
552 | } | |
553 | ||
554 | bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9); | |
a0725ab0 | 555 | bio_set_dev(bio, zram->bdev); |
db8ffbd4 MK |
556 | if (!bio_add_page(bio, bvec->bv_page, bvec->bv_len, |
557 | bvec->bv_offset)) { | |
558 | bio_put(bio); | |
559 | put_entry_bdev(zram, entry); | |
560 | return -EIO; | |
561 | } | |
562 | ||
563 | if (!parent) { | |
564 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC; | |
565 | bio->bi_end_io = zram_page_end_io; | |
566 | } else { | |
567 | bio->bi_opf = parent->bi_opf; | |
568 | bio_chain(bio, parent); | |
569 | } | |
570 | ||
571 | submit_bio(bio); | |
572 | *pentry = entry; | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | static void zram_wb_clear(struct zram *zram, u32 index) | |
578 | { | |
579 | unsigned long entry; | |
580 | ||
581 | zram_clear_flag(zram, index, ZRAM_WB); | |
582 | entry = zram_get_element(zram, index); | |
583 | zram_set_element(zram, index, 0); | |
584 | put_entry_bdev(zram, entry); | |
585 | } | |
586 | ||
013bf95a MK |
587 | #else |
588 | static bool zram_wb_enabled(struct zram *zram) { return false; } | |
589 | static inline void reset_bdev(struct zram *zram) {}; | |
db8ffbd4 MK |
590 | static int write_to_bdev(struct zram *zram, struct bio_vec *bvec, |
591 | u32 index, struct bio *parent, | |
592 | unsigned long *pentry) | |
593 | ||
594 | { | |
595 | return -EIO; | |
596 | } | |
8e654f8f MK |
597 | |
598 | static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, | |
599 | unsigned long entry, struct bio *parent, bool sync) | |
600 | { | |
601 | return -EIO; | |
602 | } | |
db8ffbd4 | 603 | static void zram_wb_clear(struct zram *zram, u32 index) {} |
013bf95a MK |
604 | #endif |
605 | ||
606 | ||
43209ea2 SS |
607 | /* |
608 | * We switched to per-cpu streams and this attr is not needed anymore. | |
609 | * However, we will keep it around for some time, because: | |
610 | * a) we may revert per-cpu streams in the future | |
611 | * b) it's visible to user space and we need to follow our 2 years | |
612 | * retirement rule; but we already have a number of 'soon to be | |
613 | * altered' attrs, so max_comp_streams need to wait for the next | |
614 | * layoff cycle. | |
615 | */ | |
522698d7 SS |
616 | static ssize_t max_comp_streams_show(struct device *dev, |
617 | struct device_attribute *attr, char *buf) | |
618 | { | |
43209ea2 | 619 | return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus()); |
522698d7 SS |
620 | } |
621 | ||
beca3ec7 SS |
622 | static ssize_t max_comp_streams_store(struct device *dev, |
623 | struct device_attribute *attr, const char *buf, size_t len) | |
624 | { | |
43209ea2 | 625 | return len; |
beca3ec7 SS |
626 | } |
627 | ||
e46b8a03 SS |
628 | static ssize_t comp_algorithm_show(struct device *dev, |
629 | struct device_attribute *attr, char *buf) | |
630 | { | |
631 | size_t sz; | |
632 | struct zram *zram = dev_to_zram(dev); | |
633 | ||
634 | down_read(&zram->init_lock); | |
635 | sz = zcomp_available_show(zram->compressor, buf); | |
636 | up_read(&zram->init_lock); | |
637 | ||
638 | return sz; | |
639 | } | |
640 | ||
641 | static ssize_t comp_algorithm_store(struct device *dev, | |
642 | struct device_attribute *attr, const char *buf, size_t len) | |
643 | { | |
644 | struct zram *zram = dev_to_zram(dev); | |
f357e345 | 645 | char compressor[ARRAY_SIZE(zram->compressor)]; |
4bbacd51 SS |
646 | size_t sz; |
647 | ||
415403be SS |
648 | strlcpy(compressor, buf, sizeof(compressor)); |
649 | /* ignore trailing newline */ | |
650 | sz = strlen(compressor); | |
651 | if (sz > 0 && compressor[sz - 1] == '\n') | |
652 | compressor[sz - 1] = 0x00; | |
653 | ||
654 | if (!zcomp_available_algorithm(compressor)) | |
1d5b43bf LH |
655 | return -EINVAL; |
656 | ||
e46b8a03 SS |
657 | down_write(&zram->init_lock); |
658 | if (init_done(zram)) { | |
659 | up_write(&zram->init_lock); | |
660 | pr_info("Can't change algorithm for initialized device\n"); | |
661 | return -EBUSY; | |
662 | } | |
4bbacd51 | 663 | |
f357e345 | 664 | strcpy(zram->compressor, compressor); |
e46b8a03 SS |
665 | up_write(&zram->init_lock); |
666 | return len; | |
667 | } | |
668 | ||
522698d7 SS |
669 | static ssize_t compact_store(struct device *dev, |
670 | struct device_attribute *attr, const char *buf, size_t len) | |
306b0c95 | 671 | { |
522698d7 | 672 | struct zram *zram = dev_to_zram(dev); |
306b0c95 | 673 | |
522698d7 SS |
674 | down_read(&zram->init_lock); |
675 | if (!init_done(zram)) { | |
676 | up_read(&zram->init_lock); | |
677 | return -EINVAL; | |
678 | } | |
306b0c95 | 679 | |
beb6602c | 680 | zs_compact(zram->mem_pool); |
522698d7 | 681 | up_read(&zram->init_lock); |
d2d5e762 | 682 | |
522698d7 | 683 | return len; |
d2d5e762 WY |
684 | } |
685 | ||
522698d7 SS |
686 | static ssize_t io_stat_show(struct device *dev, |
687 | struct device_attribute *attr, char *buf) | |
d2d5e762 | 688 | { |
522698d7 SS |
689 | struct zram *zram = dev_to_zram(dev); |
690 | ssize_t ret; | |
d2d5e762 | 691 | |
522698d7 SS |
692 | down_read(&zram->init_lock); |
693 | ret = scnprintf(buf, PAGE_SIZE, | |
694 | "%8llu %8llu %8llu %8llu\n", | |
695 | (u64)atomic64_read(&zram->stats.failed_reads), | |
696 | (u64)atomic64_read(&zram->stats.failed_writes), | |
697 | (u64)atomic64_read(&zram->stats.invalid_io), | |
698 | (u64)atomic64_read(&zram->stats.notify_free)); | |
699 | up_read(&zram->init_lock); | |
306b0c95 | 700 | |
522698d7 | 701 | return ret; |
9b3bb7ab SS |
702 | } |
703 | ||
522698d7 SS |
704 | static ssize_t mm_stat_show(struct device *dev, |
705 | struct device_attribute *attr, char *buf) | |
9b3bb7ab | 706 | { |
522698d7 | 707 | struct zram *zram = dev_to_zram(dev); |
7d3f3938 | 708 | struct zs_pool_stats pool_stats; |
522698d7 SS |
709 | u64 orig_size, mem_used = 0; |
710 | long max_used; | |
711 | ssize_t ret; | |
a539c72a | 712 | |
7d3f3938 SS |
713 | memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats)); |
714 | ||
522698d7 | 715 | down_read(&zram->init_lock); |
7d3f3938 | 716 | if (init_done(zram)) { |
beb6602c MK |
717 | mem_used = zs_get_total_pages(zram->mem_pool); |
718 | zs_pool_stats(zram->mem_pool, &pool_stats); | |
7d3f3938 | 719 | } |
9b3bb7ab | 720 | |
522698d7 SS |
721 | orig_size = atomic64_read(&zram->stats.pages_stored); |
722 | max_used = atomic_long_read(&zram->stats.max_used_pages); | |
9b3bb7ab | 723 | |
522698d7 | 724 | ret = scnprintf(buf, PAGE_SIZE, |
7d3f3938 | 725 | "%8llu %8llu %8llu %8lu %8ld %8llu %8lu\n", |
522698d7 SS |
726 | orig_size << PAGE_SHIFT, |
727 | (u64)atomic64_read(&zram->stats.compr_data_size), | |
728 | mem_used << PAGE_SHIFT, | |
729 | zram->limit_pages << PAGE_SHIFT, | |
730 | max_used << PAGE_SHIFT, | |
8e19d540 | 731 | (u64)atomic64_read(&zram->stats.same_pages), |
860c707d | 732 | pool_stats.pages_compacted); |
522698d7 | 733 | up_read(&zram->init_lock); |
9b3bb7ab | 734 | |
522698d7 SS |
735 | return ret; |
736 | } | |
737 | ||
623e47fc SS |
738 | static ssize_t debug_stat_show(struct device *dev, |
739 | struct device_attribute *attr, char *buf) | |
740 | { | |
741 | int version = 1; | |
742 | struct zram *zram = dev_to_zram(dev); | |
743 | ssize_t ret; | |
744 | ||
745 | down_read(&zram->init_lock); | |
746 | ret = scnprintf(buf, PAGE_SIZE, | |
747 | "version: %d\n%8llu\n", | |
748 | version, | |
749 | (u64)atomic64_read(&zram->stats.writestall)); | |
750 | up_read(&zram->init_lock); | |
751 | ||
752 | return ret; | |
753 | } | |
754 | ||
522698d7 SS |
755 | static DEVICE_ATTR_RO(io_stat); |
756 | static DEVICE_ATTR_RO(mm_stat); | |
623e47fc | 757 | static DEVICE_ATTR_RO(debug_stat); |
522698d7 | 758 | |
86c49814 MK |
759 | static void zram_slot_lock(struct zram *zram, u32 index) |
760 | { | |
beb6602c | 761 | bit_spin_lock(ZRAM_ACCESS, &zram->table[index].value); |
86c49814 MK |
762 | } |
763 | ||
764 | static void zram_slot_unlock(struct zram *zram, u32 index) | |
765 | { | |
beb6602c | 766 | bit_spin_unlock(ZRAM_ACCESS, &zram->table[index].value); |
86c49814 MK |
767 | } |
768 | ||
1f7319c7 MK |
769 | static bool zram_same_page_read(struct zram *zram, u32 index, |
770 | struct page *page, | |
771 | unsigned int offset, unsigned int len) | |
772 | { | |
86c49814 | 773 | zram_slot_lock(zram, index); |
643ae61d MK |
774 | if (unlikely(!zram_get_handle(zram, index) || |
775 | zram_test_flag(zram, index, ZRAM_SAME))) { | |
1f7319c7 MK |
776 | void *mem; |
777 | ||
86c49814 | 778 | zram_slot_unlock(zram, index); |
1f7319c7 | 779 | mem = kmap_atomic(page); |
643ae61d MK |
780 | zram_fill_page(mem + offset, len, |
781 | zram_get_element(zram, index)); | |
1f7319c7 MK |
782 | kunmap_atomic(mem); |
783 | return true; | |
784 | } | |
86c49814 | 785 | zram_slot_unlock(zram, index); |
1f7319c7 MK |
786 | |
787 | return false; | |
788 | } | |
789 | ||
beb6602c | 790 | static void zram_meta_free(struct zram *zram, u64 disksize) |
522698d7 SS |
791 | { |
792 | size_t num_pages = disksize >> PAGE_SHIFT; | |
793 | size_t index; | |
1fec1172 GM |
794 | |
795 | /* Free all pages that are still in this zram device */ | |
302128dc MK |
796 | for (index = 0; index < num_pages; index++) |
797 | zram_free_page(zram, index); | |
1fec1172 | 798 | |
beb6602c MK |
799 | zs_destroy_pool(zram->mem_pool); |
800 | vfree(zram->table); | |
9b3bb7ab SS |
801 | } |
802 | ||
beb6602c | 803 | static bool zram_meta_alloc(struct zram *zram, u64 disksize) |
9b3bb7ab SS |
804 | { |
805 | size_t num_pages; | |
9b3bb7ab | 806 | |
9b3bb7ab | 807 | num_pages = disksize >> PAGE_SHIFT; |
beb6602c MK |
808 | zram->table = vzalloc(num_pages * sizeof(*zram->table)); |
809 | if (!zram->table) | |
810 | return false; | |
9b3bb7ab | 811 | |
beb6602c MK |
812 | zram->mem_pool = zs_create_pool(zram->disk->disk_name); |
813 | if (!zram->mem_pool) { | |
814 | vfree(zram->table); | |
815 | return false; | |
9b3bb7ab SS |
816 | } |
817 | ||
beb6602c | 818 | return true; |
9b3bb7ab SS |
819 | } |
820 | ||
d2d5e762 WY |
821 | /* |
822 | * To protect concurrent access to the same index entry, | |
823 | * caller should hold this table index entry's bit_spinlock to | |
824 | * indicate this index entry is accessing. | |
825 | */ | |
f1e3cfff | 826 | static void zram_free_page(struct zram *zram, size_t index) |
306b0c95 | 827 | { |
db8ffbd4 MK |
828 | unsigned long handle; |
829 | ||
830 | if (zram_wb_enabled(zram) && zram_test_flag(zram, index, ZRAM_WB)) { | |
831 | zram_wb_clear(zram, index); | |
832 | atomic64_dec(&zram->stats.pages_stored); | |
833 | return; | |
834 | } | |
306b0c95 | 835 | |
8e19d540 | 836 | /* |
837 | * No memory is allocated for same element filled pages. | |
838 | * Simply clear same page flag. | |
839 | */ | |
beb6602c MK |
840 | if (zram_test_flag(zram, index, ZRAM_SAME)) { |
841 | zram_clear_flag(zram, index, ZRAM_SAME); | |
643ae61d | 842 | zram_set_element(zram, index, 0); |
8e19d540 | 843 | atomic64_dec(&zram->stats.same_pages); |
51f9f82c | 844 | atomic64_dec(&zram->stats.pages_stored); |
306b0c95 NG |
845 | return; |
846 | } | |
847 | ||
db8ffbd4 | 848 | handle = zram_get_handle(zram, index); |
8e19d540 | 849 | if (!handle) |
850 | return; | |
851 | ||
beb6602c | 852 | zs_free(zram->mem_pool, handle); |
306b0c95 | 853 | |
beb6602c | 854 | atomic64_sub(zram_get_obj_size(zram, index), |
d2d5e762 | 855 | &zram->stats.compr_data_size); |
90a7806e | 856 | atomic64_dec(&zram->stats.pages_stored); |
306b0c95 | 857 | |
643ae61d | 858 | zram_set_handle(zram, index, 0); |
beb6602c | 859 | zram_set_obj_size(zram, index, 0); |
306b0c95 NG |
860 | } |
861 | ||
8e654f8f MK |
862 | static int __zram_bvec_read(struct zram *zram, struct page *page, u32 index, |
863 | struct bio *bio, bool partial_io) | |
306b0c95 | 864 | { |
1f7319c7 | 865 | int ret; |
92967471 | 866 | unsigned long handle; |
ebaf9ab5 | 867 | unsigned int size; |
1f7319c7 | 868 | void *src, *dst; |
1f7319c7 | 869 | |
8e654f8f MK |
870 | if (zram_wb_enabled(zram)) { |
871 | zram_slot_lock(zram, index); | |
872 | if (zram_test_flag(zram, index, ZRAM_WB)) { | |
873 | struct bio_vec bvec; | |
874 | ||
875 | zram_slot_unlock(zram, index); | |
876 | ||
877 | bvec.bv_page = page; | |
878 | bvec.bv_len = PAGE_SIZE; | |
879 | bvec.bv_offset = 0; | |
880 | return read_from_bdev(zram, &bvec, | |
881 | zram_get_element(zram, index), | |
882 | bio, partial_io); | |
883 | } | |
884 | zram_slot_unlock(zram, index); | |
885 | } | |
886 | ||
1f7319c7 MK |
887 | if (zram_same_page_read(zram, index, page, 0, PAGE_SIZE)) |
888 | return 0; | |
92967471 | 889 | |
86c49814 | 890 | zram_slot_lock(zram, index); |
643ae61d | 891 | handle = zram_get_handle(zram, index); |
beb6602c | 892 | size = zram_get_obj_size(zram, index); |
306b0c95 | 893 | |
beb6602c | 894 | src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO); |
ebaf9ab5 | 895 | if (size == PAGE_SIZE) { |
1f7319c7 MK |
896 | dst = kmap_atomic(page); |
897 | memcpy(dst, src, PAGE_SIZE); | |
898 | kunmap_atomic(dst); | |
899 | ret = 0; | |
ebaf9ab5 SS |
900 | } else { |
901 | struct zcomp_strm *zstrm = zcomp_stream_get(zram->comp); | |
902 | ||
1f7319c7 MK |
903 | dst = kmap_atomic(page); |
904 | ret = zcomp_decompress(zstrm, src, size, dst); | |
905 | kunmap_atomic(dst); | |
ebaf9ab5 SS |
906 | zcomp_stream_put(zram->comp); |
907 | } | |
beb6602c | 908 | zs_unmap_object(zram->mem_pool, handle); |
86c49814 | 909 | zram_slot_unlock(zram, index); |
a1dd52af | 910 | |
8c921b2b | 911 | /* Should NEVER happen. Return bio error if it does. */ |
1f7319c7 | 912 | if (unlikely(ret)) |
8c921b2b | 913 | pr_err("Decompression failed! err=%d, page=%u\n", ret, index); |
306b0c95 | 914 | |
1f7319c7 | 915 | return ret; |
306b0c95 NG |
916 | } |
917 | ||
37b51fdd | 918 | static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec, |
8e654f8f | 919 | u32 index, int offset, struct bio *bio) |
924bd88d JM |
920 | { |
921 | int ret; | |
37b51fdd | 922 | struct page *page; |
37b51fdd | 923 | |
1f7319c7 MK |
924 | page = bvec->bv_page; |
925 | if (is_partial_io(bvec)) { | |
926 | /* Use a temporary buffer to decompress the page */ | |
927 | page = alloc_page(GFP_NOIO|__GFP_HIGHMEM); | |
928 | if (!page) | |
929 | return -ENOMEM; | |
924bd88d JM |
930 | } |
931 | ||
8e654f8f | 932 | ret = __zram_bvec_read(zram, page, index, bio, is_partial_io(bvec)); |
1f7319c7 MK |
933 | if (unlikely(ret)) |
934 | goto out; | |
7e5a5104 | 935 | |
1f7319c7 MK |
936 | if (is_partial_io(bvec)) { |
937 | void *dst = kmap_atomic(bvec->bv_page); | |
938 | void *src = kmap_atomic(page); | |
37b51fdd | 939 | |
1f7319c7 MK |
940 | memcpy(dst + bvec->bv_offset, src + offset, bvec->bv_len); |
941 | kunmap_atomic(src); | |
942 | kunmap_atomic(dst); | |
37b51fdd | 943 | } |
1f7319c7 | 944 | out: |
37b51fdd | 945 | if (is_partial_io(bvec)) |
1f7319c7 | 946 | __free_page(page); |
37b51fdd | 947 | |
37b51fdd | 948 | return ret; |
924bd88d JM |
949 | } |
950 | ||
db8ffbd4 MK |
951 | static int __zram_bvec_write(struct zram *zram, struct bio_vec *bvec, |
952 | u32 index, struct bio *bio) | |
306b0c95 | 953 | { |
ae85a807 | 954 | int ret = 0; |
1f7319c7 | 955 | unsigned long alloced_pages; |
da9556a2 | 956 | unsigned long handle = 0; |
97ec7c8b MK |
957 | unsigned int comp_len = 0; |
958 | void *src, *dst, *mem; | |
959 | struct zcomp_strm *zstrm; | |
960 | struct page *page = bvec->bv_page; | |
961 | unsigned long element = 0; | |
962 | enum zram_pageflags flags = 0; | |
db8ffbd4 | 963 | bool allow_wb = true; |
97ec7c8b MK |
964 | |
965 | mem = kmap_atomic(page); | |
966 | if (page_same_filled(mem, &element)) { | |
967 | kunmap_atomic(mem); | |
968 | /* Free memory associated with this sector now. */ | |
969 | flags = ZRAM_SAME; | |
970 | atomic64_inc(&zram->stats.same_pages); | |
971 | goto out; | |
972 | } | |
973 | kunmap_atomic(mem); | |
924bd88d | 974 | |
da9556a2 | 975 | compress_again: |
97ec7c8b | 976 | zstrm = zcomp_stream_get(zram->comp); |
1f7319c7 | 977 | src = kmap_atomic(page); |
97ec7c8b | 978 | ret = zcomp_compress(zstrm, src, &comp_len); |
1f7319c7 | 979 | kunmap_atomic(src); |
306b0c95 | 980 | |
b7ca232e | 981 | if (unlikely(ret)) { |
97ec7c8b | 982 | zcomp_stream_put(zram->comp); |
8c921b2b | 983 | pr_err("Compression failed! err=%d\n", ret); |
97ec7c8b | 984 | zs_free(zram->mem_pool, handle); |
1f7319c7 | 985 | return ret; |
8c921b2b | 986 | } |
da9556a2 | 987 | |
db8ffbd4 MK |
988 | if (unlikely(comp_len > max_zpage_size)) { |
989 | if (zram_wb_enabled(zram) && allow_wb) { | |
990 | zcomp_stream_put(zram->comp); | |
991 | ret = write_to_bdev(zram, bvec, index, bio, &element); | |
992 | if (!ret) { | |
993 | flags = ZRAM_WB; | |
994 | ret = 1; | |
995 | goto out; | |
996 | } | |
997 | allow_wb = false; | |
998 | goto compress_again; | |
999 | } | |
1f7319c7 | 1000 | comp_len = PAGE_SIZE; |
db8ffbd4 | 1001 | } |
a1dd52af | 1002 | |
da9556a2 SS |
1003 | /* |
1004 | * handle allocation has 2 paths: | |
1005 | * a) fast path is executed with preemption disabled (for | |
1006 | * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear, | |
1007 | * since we can't sleep; | |
1008 | * b) slow path enables preemption and attempts to allocate | |
1009 | * the page with __GFP_DIRECT_RECLAIM bit set. we have to | |
1010 | * put per-cpu compression stream and, thus, to re-do | |
1011 | * the compression once handle is allocated. | |
1012 | * | |
1013 | * if we have a 'non-null' handle here then we are coming | |
1014 | * from the slow path and handle has already been allocated. | |
1015 | */ | |
1016 | if (!handle) | |
beb6602c | 1017 | handle = zs_malloc(zram->mem_pool, comp_len, |
da9556a2 SS |
1018 | __GFP_KSWAPD_RECLAIM | |
1019 | __GFP_NOWARN | | |
9bc482d3 MK |
1020 | __GFP_HIGHMEM | |
1021 | __GFP_MOVABLE); | |
fd1a30de | 1022 | if (!handle) { |
2aea8493 | 1023 | zcomp_stream_put(zram->comp); |
623e47fc | 1024 | atomic64_inc(&zram->stats.writestall); |
beb6602c | 1025 | handle = zs_malloc(zram->mem_pool, comp_len, |
9bc482d3 MK |
1026 | GFP_NOIO | __GFP_HIGHMEM | |
1027 | __GFP_MOVABLE); | |
da9556a2 SS |
1028 | if (handle) |
1029 | goto compress_again; | |
1f7319c7 | 1030 | return -ENOMEM; |
8c921b2b | 1031 | } |
9ada9da9 | 1032 | |
beb6602c | 1033 | alloced_pages = zs_get_total_pages(zram->mem_pool); |
12372755 SS |
1034 | update_used_max(zram, alloced_pages); |
1035 | ||
461a8eee | 1036 | if (zram->limit_pages && alloced_pages > zram->limit_pages) { |
97ec7c8b | 1037 | zcomp_stream_put(zram->comp); |
beb6602c | 1038 | zs_free(zram->mem_pool, handle); |
1f7319c7 MK |
1039 | return -ENOMEM; |
1040 | } | |
1041 | ||
beb6602c | 1042 | dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO); |
1f7319c7 MK |
1043 | |
1044 | src = zstrm->buffer; | |
1045 | if (comp_len == PAGE_SIZE) | |
397c6066 | 1046 | src = kmap_atomic(page); |
1f7319c7 MK |
1047 | memcpy(dst, src, comp_len); |
1048 | if (comp_len == PAGE_SIZE) | |
397c6066 | 1049 | kunmap_atomic(src); |
306b0c95 | 1050 | |
2aea8493 | 1051 | zcomp_stream_put(zram->comp); |
beb6602c | 1052 | zs_unmap_object(zram->mem_pool, handle); |
4ebbe7f7 MK |
1053 | atomic64_add(comp_len, &zram->stats.compr_data_size); |
1054 | out: | |
f40ac2ae SS |
1055 | /* |
1056 | * Free memory associated with this sector | |
1057 | * before overwriting unused sectors. | |
1058 | */ | |
86c49814 | 1059 | zram_slot_lock(zram, index); |
f40ac2ae | 1060 | zram_free_page(zram, index); |
db8ffbd4 MK |
1061 | |
1062 | if (flags) { | |
1063 | zram_set_flag(zram, index, flags); | |
4ebbe7f7 | 1064 | zram_set_element(zram, index, element); |
db8ffbd4 | 1065 | } else { |
4ebbe7f7 MK |
1066 | zram_set_handle(zram, index, handle); |
1067 | zram_set_obj_size(zram, index, comp_len); | |
1068 | } | |
86c49814 | 1069 | zram_slot_unlock(zram, index); |
306b0c95 | 1070 | |
8c921b2b | 1071 | /* Update stats */ |
90a7806e | 1072 | atomic64_inc(&zram->stats.pages_stored); |
ae85a807 | 1073 | return ret; |
1f7319c7 MK |
1074 | } |
1075 | ||
1076 | static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, | |
db8ffbd4 | 1077 | u32 index, int offset, struct bio *bio) |
1f7319c7 MK |
1078 | { |
1079 | int ret; | |
1080 | struct page *page = NULL; | |
1081 | void *src; | |
1082 | struct bio_vec vec; | |
1083 | ||
1084 | vec = *bvec; | |
1085 | if (is_partial_io(bvec)) { | |
1086 | void *dst; | |
1087 | /* | |
1088 | * This is a partial IO. We need to read the full page | |
1089 | * before to write the changes. | |
1090 | */ | |
1091 | page = alloc_page(GFP_NOIO|__GFP_HIGHMEM); | |
1092 | if (!page) | |
1093 | return -ENOMEM; | |
1094 | ||
8e654f8f | 1095 | ret = __zram_bvec_read(zram, page, index, bio, true); |
1f7319c7 MK |
1096 | if (ret) |
1097 | goto out; | |
1098 | ||
1099 | src = kmap_atomic(bvec->bv_page); | |
1100 | dst = kmap_atomic(page); | |
1101 | memcpy(dst + offset, src + bvec->bv_offset, bvec->bv_len); | |
1102 | kunmap_atomic(dst); | |
1103 | kunmap_atomic(src); | |
1104 | ||
1105 | vec.bv_page = page; | |
1106 | vec.bv_len = PAGE_SIZE; | |
1107 | vec.bv_offset = 0; | |
1108 | } | |
1109 | ||
db8ffbd4 | 1110 | ret = __zram_bvec_write(zram, &vec, index, bio); |
924bd88d | 1111 | out: |
397c6066 | 1112 | if (is_partial_io(bvec)) |
1f7319c7 | 1113 | __free_page(page); |
924bd88d | 1114 | return ret; |
8c921b2b JM |
1115 | } |
1116 | ||
f4659d8e JK |
1117 | /* |
1118 | * zram_bio_discard - handler on discard request | |
1119 | * @index: physical block index in PAGE_SIZE units | |
1120 | * @offset: byte offset within physical block | |
1121 | */ | |
1122 | static void zram_bio_discard(struct zram *zram, u32 index, | |
1123 | int offset, struct bio *bio) | |
1124 | { | |
1125 | size_t n = bio->bi_iter.bi_size; | |
1126 | ||
1127 | /* | |
1128 | * zram manages data in physical block size units. Because logical block | |
1129 | * size isn't identical with physical block size on some arch, we | |
1130 | * could get a discard request pointing to a specific offset within a | |
1131 | * certain physical block. Although we can handle this request by | |
1132 | * reading that physiclal block and decompressing and partially zeroing | |
1133 | * and re-compressing and then re-storing it, this isn't reasonable | |
1134 | * because our intent with a discard request is to save memory. So | |
1135 | * skipping this logical block is appropriate here. | |
1136 | */ | |
1137 | if (offset) { | |
38515c73 | 1138 | if (n <= (PAGE_SIZE - offset)) |
f4659d8e JK |
1139 | return; |
1140 | ||
38515c73 | 1141 | n -= (PAGE_SIZE - offset); |
f4659d8e JK |
1142 | index++; |
1143 | } | |
1144 | ||
1145 | while (n >= PAGE_SIZE) { | |
86c49814 | 1146 | zram_slot_lock(zram, index); |
f4659d8e | 1147 | zram_free_page(zram, index); |
86c49814 | 1148 | zram_slot_unlock(zram, index); |
015254da | 1149 | atomic64_inc(&zram->stats.notify_free); |
f4659d8e JK |
1150 | index++; |
1151 | n -= PAGE_SIZE; | |
1152 | } | |
1153 | } | |
1154 | ||
ae85a807 MK |
1155 | /* |
1156 | * Returns errno if it has some problem. Otherwise return 0 or 1. | |
1157 | * Returns 0 if IO request was done synchronously | |
1158 | * Returns 1 if IO request was successfully submitted. | |
1159 | */ | |
522698d7 | 1160 | static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index, |
db8ffbd4 | 1161 | int offset, bool is_write, struct bio *bio) |
9b3bb7ab | 1162 | { |
522698d7 | 1163 | unsigned long start_time = jiffies; |
c11f0c0b | 1164 | int rw_acct = is_write ? REQ_OP_WRITE : REQ_OP_READ; |
d62e26b3 | 1165 | struct request_queue *q = zram->disk->queue; |
9b3bb7ab | 1166 | int ret; |
9b3bb7ab | 1167 | |
d62e26b3 | 1168 | generic_start_io_acct(q, rw_acct, bvec->bv_len >> SECTOR_SHIFT, |
522698d7 | 1169 | &zram->disk->part0); |
46a51c80 | 1170 | |
c11f0c0b | 1171 | if (!is_write) { |
522698d7 | 1172 | atomic64_inc(&zram->stats.num_reads); |
8e654f8f | 1173 | ret = zram_bvec_read(zram, bvec, index, offset, bio); |
1f7319c7 | 1174 | flush_dcache_page(bvec->bv_page); |
522698d7 SS |
1175 | } else { |
1176 | atomic64_inc(&zram->stats.num_writes); | |
db8ffbd4 | 1177 | ret = zram_bvec_write(zram, bvec, index, offset, bio); |
1b672224 | 1178 | } |
9b3bb7ab | 1179 | |
d62e26b3 | 1180 | generic_end_io_acct(q, rw_acct, &zram->disk->part0, start_time); |
9b3bb7ab | 1181 | |
ae85a807 | 1182 | if (unlikely(ret < 0)) { |
c11f0c0b | 1183 | if (!is_write) |
522698d7 SS |
1184 | atomic64_inc(&zram->stats.failed_reads); |
1185 | else | |
1186 | atomic64_inc(&zram->stats.failed_writes); | |
1b672224 | 1187 | } |
9b3bb7ab | 1188 | |
1b672224 | 1189 | return ret; |
8c921b2b JM |
1190 | } |
1191 | ||
be257c61 | 1192 | static void __zram_make_request(struct zram *zram, struct bio *bio) |
8c921b2b | 1193 | { |
abf54548 | 1194 | int offset; |
8c921b2b | 1195 | u32 index; |
7988613b KO |
1196 | struct bio_vec bvec; |
1197 | struct bvec_iter iter; | |
8c921b2b | 1198 | |
4f024f37 KO |
1199 | index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT; |
1200 | offset = (bio->bi_iter.bi_sector & | |
1201 | (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; | |
8c921b2b | 1202 | |
31edeacd CH |
1203 | switch (bio_op(bio)) { |
1204 | case REQ_OP_DISCARD: | |
1205 | case REQ_OP_WRITE_ZEROES: | |
f4659d8e | 1206 | zram_bio_discard(zram, index, offset, bio); |
4246a0b6 | 1207 | bio_endio(bio); |
f4659d8e | 1208 | return; |
31edeacd CH |
1209 | default: |
1210 | break; | |
f4659d8e JK |
1211 | } |
1212 | ||
7988613b | 1213 | bio_for_each_segment(bvec, bio, iter) { |
e86942c7 MK |
1214 | struct bio_vec bv = bvec; |
1215 | unsigned int unwritten = bvec.bv_len; | |
924bd88d | 1216 | |
e86942c7 MK |
1217 | do { |
1218 | bv.bv_len = min_t(unsigned int, PAGE_SIZE - offset, | |
1219 | unwritten); | |
abf54548 | 1220 | if (zram_bvec_rw(zram, &bv, index, offset, |
db8ffbd4 | 1221 | op_is_write(bio_op(bio)), bio) < 0) |
924bd88d JM |
1222 | goto out; |
1223 | ||
e86942c7 MK |
1224 | bv.bv_offset += bv.bv_len; |
1225 | unwritten -= bv.bv_len; | |
924bd88d | 1226 | |
e86942c7 MK |
1227 | update_position(&index, &offset, &bv); |
1228 | } while (unwritten); | |
a1dd52af | 1229 | } |
306b0c95 | 1230 | |
4246a0b6 | 1231 | bio_endio(bio); |
7d7854b4 | 1232 | return; |
306b0c95 NG |
1233 | |
1234 | out: | |
306b0c95 | 1235 | bio_io_error(bio); |
306b0c95 NG |
1236 | } |
1237 | ||
306b0c95 | 1238 | /* |
f1e3cfff | 1239 | * Handler function for all zram I/O requests. |
306b0c95 | 1240 | */ |
dece1635 | 1241 | static blk_qc_t zram_make_request(struct request_queue *queue, struct bio *bio) |
306b0c95 | 1242 | { |
f1e3cfff | 1243 | struct zram *zram = queue->queuedata; |
306b0c95 | 1244 | |
54850e73 | 1245 | if (!valid_io_request(zram, bio->bi_iter.bi_sector, |
1246 | bio->bi_iter.bi_size)) { | |
da5cc7d3 | 1247 | atomic64_inc(&zram->stats.invalid_io); |
a09759ac | 1248 | goto error; |
6642a67c JM |
1249 | } |
1250 | ||
be257c61 | 1251 | __zram_make_request(zram, bio); |
dece1635 | 1252 | return BLK_QC_T_NONE; |
a09759ac | 1253 | |
0900beae JM |
1254 | error: |
1255 | bio_io_error(bio); | |
dece1635 | 1256 | return BLK_QC_T_NONE; |
306b0c95 NG |
1257 | } |
1258 | ||
2ccbec05 NG |
1259 | static void zram_slot_free_notify(struct block_device *bdev, |
1260 | unsigned long index) | |
107c161b | 1261 | { |
f1e3cfff | 1262 | struct zram *zram; |
107c161b | 1263 | |
f1e3cfff | 1264 | zram = bdev->bd_disk->private_data; |
a0c516cb | 1265 | |
86c49814 | 1266 | zram_slot_lock(zram, index); |
f614a9f4 | 1267 | zram_free_page(zram, index); |
86c49814 | 1268 | zram_slot_unlock(zram, index); |
f614a9f4 | 1269 | atomic64_inc(&zram->stats.notify_free); |
107c161b NG |
1270 | } |
1271 | ||
8c7f0102 | 1272 | static int zram_rw_page(struct block_device *bdev, sector_t sector, |
c11f0c0b | 1273 | struct page *page, bool is_write) |
8c7f0102 | 1274 | { |
ae85a807 | 1275 | int offset, ret; |
8c7f0102 | 1276 | u32 index; |
1277 | struct zram *zram; | |
1278 | struct bio_vec bv; | |
1279 | ||
98cc093c HY |
1280 | if (PageTransHuge(page)) |
1281 | return -ENOTSUPP; | |
8c7f0102 | 1282 | zram = bdev->bd_disk->private_data; |
08eee69f | 1283 | |
8c7f0102 | 1284 | if (!valid_io_request(zram, sector, PAGE_SIZE)) { |
1285 | atomic64_inc(&zram->stats.invalid_io); | |
ae85a807 | 1286 | ret = -EINVAL; |
a09759ac | 1287 | goto out; |
8c7f0102 | 1288 | } |
1289 | ||
1290 | index = sector >> SECTORS_PER_PAGE_SHIFT; | |
4ca82dab | 1291 | offset = (sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT; |
8c7f0102 | 1292 | |
1293 | bv.bv_page = page; | |
1294 | bv.bv_len = PAGE_SIZE; | |
1295 | bv.bv_offset = 0; | |
1296 | ||
db8ffbd4 | 1297 | ret = zram_bvec_rw(zram, &bv, index, offset, is_write, NULL); |
08eee69f | 1298 | out: |
8c7f0102 | 1299 | /* |
1300 | * If I/O fails, just return error(ie, non-zero) without | |
1301 | * calling page_endio. | |
1302 | * It causes resubmit the I/O with bio request by upper functions | |
1303 | * of rw_page(e.g., swap_readpage, __swap_writepage) and | |
1304 | * bio->bi_end_io does things to handle the error | |
1305 | * (e.g., SetPageError, set_page_dirty and extra works). | |
1306 | */ | |
ae85a807 MK |
1307 | if (unlikely(ret < 0)) |
1308 | return ret; | |
1309 | ||
1310 | switch (ret) { | |
1311 | case 0: | |
c11f0c0b | 1312 | page_endio(page, is_write, 0); |
ae85a807 MK |
1313 | break; |
1314 | case 1: | |
1315 | ret = 0; | |
1316 | break; | |
1317 | default: | |
1318 | WARN_ON(1); | |
1319 | } | |
1320 | return ret; | |
8c7f0102 | 1321 | } |
1322 | ||
522698d7 SS |
1323 | static void zram_reset_device(struct zram *zram) |
1324 | { | |
522698d7 SS |
1325 | struct zcomp *comp; |
1326 | u64 disksize; | |
306b0c95 | 1327 | |
522698d7 | 1328 | down_write(&zram->init_lock); |
9b3bb7ab | 1329 | |
522698d7 SS |
1330 | zram->limit_pages = 0; |
1331 | ||
1332 | if (!init_done(zram)) { | |
1333 | up_write(&zram->init_lock); | |
1334 | return; | |
1335 | } | |
1336 | ||
522698d7 SS |
1337 | comp = zram->comp; |
1338 | disksize = zram->disksize; | |
522698d7 | 1339 | zram->disksize = 0; |
522698d7 SS |
1340 | |
1341 | set_capacity(zram->disk, 0); | |
1342 | part_stat_set_all(&zram->disk->part0, 0); | |
1343 | ||
1344 | up_write(&zram->init_lock); | |
1345 | /* I/O operation under all of CPU are done so let's free */ | |
beb6602c | 1346 | zram_meta_free(zram, disksize); |
302128dc | 1347 | memset(&zram->stats, 0, sizeof(zram->stats)); |
522698d7 | 1348 | zcomp_destroy(comp); |
013bf95a | 1349 | reset_bdev(zram); |
522698d7 SS |
1350 | } |
1351 | ||
1352 | static ssize_t disksize_store(struct device *dev, | |
1353 | struct device_attribute *attr, const char *buf, size_t len) | |
2f6a3bed | 1354 | { |
522698d7 SS |
1355 | u64 disksize; |
1356 | struct zcomp *comp; | |
2f6a3bed | 1357 | struct zram *zram = dev_to_zram(dev); |
522698d7 | 1358 | int err; |
2f6a3bed | 1359 | |
522698d7 SS |
1360 | disksize = memparse(buf, NULL); |
1361 | if (!disksize) | |
1362 | return -EINVAL; | |
2f6a3bed | 1363 | |
beb6602c MK |
1364 | down_write(&zram->init_lock); |
1365 | if (init_done(zram)) { | |
1366 | pr_info("Cannot change disksize for initialized device\n"); | |
1367 | err = -EBUSY; | |
1368 | goto out_unlock; | |
1369 | } | |
1370 | ||
522698d7 | 1371 | disksize = PAGE_ALIGN(disksize); |
beb6602c MK |
1372 | if (!zram_meta_alloc(zram, disksize)) { |
1373 | err = -ENOMEM; | |
1374 | goto out_unlock; | |
1375 | } | |
522698d7 | 1376 | |
da9556a2 | 1377 | comp = zcomp_create(zram->compressor); |
522698d7 | 1378 | if (IS_ERR(comp)) { |
70864969 | 1379 | pr_err("Cannot initialise %s compressing backend\n", |
522698d7 SS |
1380 | zram->compressor); |
1381 | err = PTR_ERR(comp); | |
1382 | goto out_free_meta; | |
1383 | } | |
1384 | ||
522698d7 SS |
1385 | zram->comp = comp; |
1386 | zram->disksize = disksize; | |
1387 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); | |
b09ab054 | 1388 | zram_revalidate_disk(zram); |
e7ccfc4c | 1389 | up_write(&zram->init_lock); |
522698d7 SS |
1390 | |
1391 | return len; | |
1392 | ||
522698d7 | 1393 | out_free_meta: |
beb6602c MK |
1394 | zram_meta_free(zram, disksize); |
1395 | out_unlock: | |
1396 | up_write(&zram->init_lock); | |
522698d7 | 1397 | return err; |
2f6a3bed SS |
1398 | } |
1399 | ||
522698d7 SS |
1400 | static ssize_t reset_store(struct device *dev, |
1401 | struct device_attribute *attr, const char *buf, size_t len) | |
4f2109f6 | 1402 | { |
522698d7 SS |
1403 | int ret; |
1404 | unsigned short do_reset; | |
1405 | struct zram *zram; | |
1406 | struct block_device *bdev; | |
4f2109f6 | 1407 | |
f405c445 SS |
1408 | ret = kstrtou16(buf, 10, &do_reset); |
1409 | if (ret) | |
1410 | return ret; | |
1411 | ||
1412 | if (!do_reset) | |
1413 | return -EINVAL; | |
1414 | ||
522698d7 SS |
1415 | zram = dev_to_zram(dev); |
1416 | bdev = bdget_disk(zram->disk, 0); | |
522698d7 SS |
1417 | if (!bdev) |
1418 | return -ENOMEM; | |
4f2109f6 | 1419 | |
522698d7 | 1420 | mutex_lock(&bdev->bd_mutex); |
f405c445 SS |
1421 | /* Do not reset an active device or claimed device */ |
1422 | if (bdev->bd_openers || zram->claim) { | |
1423 | mutex_unlock(&bdev->bd_mutex); | |
1424 | bdput(bdev); | |
1425 | return -EBUSY; | |
522698d7 SS |
1426 | } |
1427 | ||
f405c445 SS |
1428 | /* From now on, anyone can't open /dev/zram[0-9] */ |
1429 | zram->claim = true; | |
1430 | mutex_unlock(&bdev->bd_mutex); | |
522698d7 | 1431 | |
f405c445 | 1432 | /* Make sure all the pending I/O are finished */ |
522698d7 SS |
1433 | fsync_bdev(bdev); |
1434 | zram_reset_device(zram); | |
b09ab054 | 1435 | zram_revalidate_disk(zram); |
522698d7 SS |
1436 | bdput(bdev); |
1437 | ||
f405c445 SS |
1438 | mutex_lock(&bdev->bd_mutex); |
1439 | zram->claim = false; | |
1440 | mutex_unlock(&bdev->bd_mutex); | |
1441 | ||
522698d7 | 1442 | return len; |
f405c445 SS |
1443 | } |
1444 | ||
1445 | static int zram_open(struct block_device *bdev, fmode_t mode) | |
1446 | { | |
1447 | int ret = 0; | |
1448 | struct zram *zram; | |
1449 | ||
1450 | WARN_ON(!mutex_is_locked(&bdev->bd_mutex)); | |
1451 | ||
1452 | zram = bdev->bd_disk->private_data; | |
1453 | /* zram was claimed to reset so open request fails */ | |
1454 | if (zram->claim) | |
1455 | ret = -EBUSY; | |
4f2109f6 SS |
1456 | |
1457 | return ret; | |
1458 | } | |
1459 | ||
522698d7 | 1460 | static const struct block_device_operations zram_devops = { |
f405c445 | 1461 | .open = zram_open, |
522698d7 SS |
1462 | .swap_slot_free_notify = zram_slot_free_notify, |
1463 | .rw_page = zram_rw_page, | |
1464 | .owner = THIS_MODULE | |
1465 | }; | |
1466 | ||
1467 | static DEVICE_ATTR_WO(compact); | |
1468 | static DEVICE_ATTR_RW(disksize); | |
1469 | static DEVICE_ATTR_RO(initstate); | |
1470 | static DEVICE_ATTR_WO(reset); | |
c87d1655 SS |
1471 | static DEVICE_ATTR_WO(mem_limit); |
1472 | static DEVICE_ATTR_WO(mem_used_max); | |
522698d7 SS |
1473 | static DEVICE_ATTR_RW(max_comp_streams); |
1474 | static DEVICE_ATTR_RW(comp_algorithm); | |
013bf95a MK |
1475 | #ifdef CONFIG_ZRAM_WRITEBACK |
1476 | static DEVICE_ATTR_RW(backing_dev); | |
1477 | #endif | |
a68eb3b6 | 1478 | |
9b3bb7ab SS |
1479 | static struct attribute *zram_disk_attrs[] = { |
1480 | &dev_attr_disksize.attr, | |
1481 | &dev_attr_initstate.attr, | |
1482 | &dev_attr_reset.attr, | |
99ebbd30 | 1483 | &dev_attr_compact.attr, |
9ada9da9 | 1484 | &dev_attr_mem_limit.attr, |
461a8eee | 1485 | &dev_attr_mem_used_max.attr, |
beca3ec7 | 1486 | &dev_attr_max_comp_streams.attr, |
e46b8a03 | 1487 | &dev_attr_comp_algorithm.attr, |
013bf95a MK |
1488 | #ifdef CONFIG_ZRAM_WRITEBACK |
1489 | &dev_attr_backing_dev.attr, | |
1490 | #endif | |
2f6a3bed | 1491 | &dev_attr_io_stat.attr, |
4f2109f6 | 1492 | &dev_attr_mm_stat.attr, |
623e47fc | 1493 | &dev_attr_debug_stat.attr, |
9b3bb7ab SS |
1494 | NULL, |
1495 | }; | |
1496 | ||
bc1bb362 | 1497 | static const struct attribute_group zram_disk_attr_group = { |
9b3bb7ab SS |
1498 | .attrs = zram_disk_attrs, |
1499 | }; | |
1500 | ||
92ff1528 SS |
1501 | /* |
1502 | * Allocate and initialize new zram device. the function returns | |
1503 | * '>= 0' device_id upon success, and negative value otherwise. | |
1504 | */ | |
1505 | static int zram_add(void) | |
306b0c95 | 1506 | { |
85508ec6 | 1507 | struct zram *zram; |
ee980160 | 1508 | struct request_queue *queue; |
92ff1528 | 1509 | int ret, device_id; |
85508ec6 SS |
1510 | |
1511 | zram = kzalloc(sizeof(struct zram), GFP_KERNEL); | |
1512 | if (!zram) | |
1513 | return -ENOMEM; | |
1514 | ||
92ff1528 | 1515 | ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL); |
85508ec6 SS |
1516 | if (ret < 0) |
1517 | goto out_free_dev; | |
92ff1528 | 1518 | device_id = ret; |
de1a21a0 | 1519 | |
0900beae | 1520 | init_rwsem(&zram->init_lock); |
306b0c95 | 1521 | |
ee980160 SS |
1522 | queue = blk_alloc_queue(GFP_KERNEL); |
1523 | if (!queue) { | |
306b0c95 NG |
1524 | pr_err("Error allocating disk queue for device %d\n", |
1525 | device_id); | |
85508ec6 SS |
1526 | ret = -ENOMEM; |
1527 | goto out_free_idr; | |
306b0c95 NG |
1528 | } |
1529 | ||
ee980160 | 1530 | blk_queue_make_request(queue, zram_make_request); |
306b0c95 | 1531 | |
85508ec6 | 1532 | /* gendisk structure */ |
f1e3cfff NG |
1533 | zram->disk = alloc_disk(1); |
1534 | if (!zram->disk) { | |
70864969 | 1535 | pr_err("Error allocating disk structure for device %d\n", |
306b0c95 | 1536 | device_id); |
201c7b72 | 1537 | ret = -ENOMEM; |
39a9b8ac | 1538 | goto out_free_queue; |
306b0c95 NG |
1539 | } |
1540 | ||
f1e3cfff NG |
1541 | zram->disk->major = zram_major; |
1542 | zram->disk->first_minor = device_id; | |
1543 | zram->disk->fops = &zram_devops; | |
ee980160 SS |
1544 | zram->disk->queue = queue; |
1545 | zram->disk->queue->queuedata = zram; | |
f1e3cfff NG |
1546 | zram->disk->private_data = zram; |
1547 | snprintf(zram->disk->disk_name, 16, "zram%d", device_id); | |
306b0c95 | 1548 | |
33863c21 | 1549 | /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */ |
f1e3cfff | 1550 | set_capacity(zram->disk, 0); |
b67d1ec1 SS |
1551 | /* zram devices sort of resembles non-rotational disks */ |
1552 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue); | |
b277da0a | 1553 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue); |
a1dd52af NG |
1554 | /* |
1555 | * To ensure that we always get PAGE_SIZE aligned | |
1556 | * and n*PAGE_SIZED sized I/O requests. | |
1557 | */ | |
f1e3cfff | 1558 | blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE); |
7b19b8d4 RJ |
1559 | blk_queue_logical_block_size(zram->disk->queue, |
1560 | ZRAM_LOGICAL_BLOCK_SIZE); | |
f1e3cfff NG |
1561 | blk_queue_io_min(zram->disk->queue, PAGE_SIZE); |
1562 | blk_queue_io_opt(zram->disk->queue, PAGE_SIZE); | |
f4659d8e | 1563 | zram->disk->queue->limits.discard_granularity = PAGE_SIZE; |
2bb4cd5c | 1564 | blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX); |
31edeacd CH |
1565 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue); |
1566 | ||
f4659d8e JK |
1567 | /* |
1568 | * zram_bio_discard() will clear all logical blocks if logical block | |
1569 | * size is identical with physical block size(PAGE_SIZE). But if it is | |
1570 | * different, we will skip discarding some parts of logical blocks in | |
1571 | * the part of the request range which isn't aligned to physical block | |
1572 | * size. So we can't ensure that all discarded logical blocks are | |
1573 | * zeroed. | |
1574 | */ | |
1575 | if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE) | |
31edeacd | 1576 | blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX); |
5d83d5a0 | 1577 | |
f1e3cfff | 1578 | add_disk(zram->disk); |
306b0c95 | 1579 | |
33863c21 NG |
1580 | ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj, |
1581 | &zram_disk_attr_group); | |
1582 | if (ret < 0) { | |
70864969 SS |
1583 | pr_err("Error creating sysfs group for device %d\n", |
1584 | device_id); | |
39a9b8ac | 1585 | goto out_free_disk; |
33863c21 | 1586 | } |
e46b8a03 | 1587 | strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); |
d12b63c9 SS |
1588 | |
1589 | pr_info("Added device: %s\n", zram->disk->disk_name); | |
92ff1528 | 1590 | return device_id; |
de1a21a0 | 1591 | |
39a9b8ac JL |
1592 | out_free_disk: |
1593 | del_gendisk(zram->disk); | |
1594 | put_disk(zram->disk); | |
1595 | out_free_queue: | |
ee980160 | 1596 | blk_cleanup_queue(queue); |
85508ec6 SS |
1597 | out_free_idr: |
1598 | idr_remove(&zram_index_idr, device_id); | |
1599 | out_free_dev: | |
1600 | kfree(zram); | |
de1a21a0 | 1601 | return ret; |
306b0c95 NG |
1602 | } |
1603 | ||
6566d1a3 | 1604 | static int zram_remove(struct zram *zram) |
306b0c95 | 1605 | { |
6566d1a3 SS |
1606 | struct block_device *bdev; |
1607 | ||
1608 | bdev = bdget_disk(zram->disk, 0); | |
1609 | if (!bdev) | |
1610 | return -ENOMEM; | |
1611 | ||
1612 | mutex_lock(&bdev->bd_mutex); | |
1613 | if (bdev->bd_openers || zram->claim) { | |
1614 | mutex_unlock(&bdev->bd_mutex); | |
1615 | bdput(bdev); | |
1616 | return -EBUSY; | |
1617 | } | |
1618 | ||
1619 | zram->claim = true; | |
1620 | mutex_unlock(&bdev->bd_mutex); | |
1621 | ||
85508ec6 SS |
1622 | /* |
1623 | * Remove sysfs first, so no one will perform a disksize | |
6566d1a3 SS |
1624 | * store while we destroy the devices. This also helps during |
1625 | * hot_remove -- zram_reset_device() is the last holder of | |
1626 | * ->init_lock, no later/concurrent disksize_store() or any | |
1627 | * other sysfs handlers are possible. | |
85508ec6 SS |
1628 | */ |
1629 | sysfs_remove_group(&disk_to_dev(zram->disk)->kobj, | |
1630 | &zram_disk_attr_group); | |
306b0c95 | 1631 | |
6566d1a3 SS |
1632 | /* Make sure all the pending I/O are finished */ |
1633 | fsync_bdev(bdev); | |
85508ec6 | 1634 | zram_reset_device(zram); |
6566d1a3 SS |
1635 | bdput(bdev); |
1636 | ||
1637 | pr_info("Removed device: %s\n", zram->disk->disk_name); | |
1638 | ||
85508ec6 SS |
1639 | blk_cleanup_queue(zram->disk->queue); |
1640 | del_gendisk(zram->disk); | |
1641 | put_disk(zram->disk); | |
1642 | kfree(zram); | |
6566d1a3 SS |
1643 | return 0; |
1644 | } | |
1645 | ||
1646 | /* zram-control sysfs attributes */ | |
27104a53 GKH |
1647 | |
1648 | /* | |
1649 | * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a | |
1650 | * sense that reading from this file does alter the state of your system -- it | |
1651 | * creates a new un-initialized zram device and returns back this device's | |
1652 | * device_id (or an error code if it fails to create a new device). | |
1653 | */ | |
6566d1a3 SS |
1654 | static ssize_t hot_add_show(struct class *class, |
1655 | struct class_attribute *attr, | |
1656 | char *buf) | |
1657 | { | |
1658 | int ret; | |
1659 | ||
1660 | mutex_lock(&zram_index_mutex); | |
1661 | ret = zram_add(); | |
1662 | mutex_unlock(&zram_index_mutex); | |
1663 | ||
1664 | if (ret < 0) | |
1665 | return ret; | |
1666 | return scnprintf(buf, PAGE_SIZE, "%d\n", ret); | |
1667 | } | |
f40609d1 | 1668 | static CLASS_ATTR_RO(hot_add); |
6566d1a3 SS |
1669 | |
1670 | static ssize_t hot_remove_store(struct class *class, | |
1671 | struct class_attribute *attr, | |
1672 | const char *buf, | |
1673 | size_t count) | |
1674 | { | |
1675 | struct zram *zram; | |
1676 | int ret, dev_id; | |
1677 | ||
1678 | /* dev_id is gendisk->first_minor, which is `int' */ | |
1679 | ret = kstrtoint(buf, 10, &dev_id); | |
1680 | if (ret) | |
1681 | return ret; | |
1682 | if (dev_id < 0) | |
1683 | return -EINVAL; | |
1684 | ||
1685 | mutex_lock(&zram_index_mutex); | |
1686 | ||
1687 | zram = idr_find(&zram_index_idr, dev_id); | |
17ec4cd9 | 1688 | if (zram) { |
6566d1a3 | 1689 | ret = zram_remove(zram); |
529e71e1 TI |
1690 | if (!ret) |
1691 | idr_remove(&zram_index_idr, dev_id); | |
17ec4cd9 | 1692 | } else { |
6566d1a3 | 1693 | ret = -ENODEV; |
17ec4cd9 | 1694 | } |
6566d1a3 SS |
1695 | |
1696 | mutex_unlock(&zram_index_mutex); | |
1697 | return ret ? ret : count; | |
85508ec6 | 1698 | } |
27104a53 | 1699 | static CLASS_ATTR_WO(hot_remove); |
a096cafc | 1700 | |
27104a53 GKH |
1701 | static struct attribute *zram_control_class_attrs[] = { |
1702 | &class_attr_hot_add.attr, | |
1703 | &class_attr_hot_remove.attr, | |
1704 | NULL, | |
6566d1a3 | 1705 | }; |
27104a53 | 1706 | ATTRIBUTE_GROUPS(zram_control_class); |
6566d1a3 SS |
1707 | |
1708 | static struct class zram_control_class = { | |
1709 | .name = "zram-control", | |
1710 | .owner = THIS_MODULE, | |
27104a53 | 1711 | .class_groups = zram_control_class_groups, |
6566d1a3 SS |
1712 | }; |
1713 | ||
85508ec6 SS |
1714 | static int zram_remove_cb(int id, void *ptr, void *data) |
1715 | { | |
1716 | zram_remove(ptr); | |
1717 | return 0; | |
1718 | } | |
a096cafc | 1719 | |
85508ec6 SS |
1720 | static void destroy_devices(void) |
1721 | { | |
6566d1a3 | 1722 | class_unregister(&zram_control_class); |
85508ec6 SS |
1723 | idr_for_each(&zram_index_idr, &zram_remove_cb, NULL); |
1724 | idr_destroy(&zram_index_idr); | |
a096cafc | 1725 | unregister_blkdev(zram_major, "zram"); |
1dd6c834 | 1726 | cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); |
306b0c95 NG |
1727 | } |
1728 | ||
f1e3cfff | 1729 | static int __init zram_init(void) |
306b0c95 | 1730 | { |
92ff1528 | 1731 | int ret; |
306b0c95 | 1732 | |
1dd6c834 AMG |
1733 | ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare", |
1734 | zcomp_cpu_up_prepare, zcomp_cpu_dead); | |
1735 | if (ret < 0) | |
1736 | return ret; | |
1737 | ||
6566d1a3 SS |
1738 | ret = class_register(&zram_control_class); |
1739 | if (ret) { | |
70864969 | 1740 | pr_err("Unable to register zram-control class\n"); |
1dd6c834 | 1741 | cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); |
6566d1a3 SS |
1742 | return ret; |
1743 | } | |
1744 | ||
f1e3cfff NG |
1745 | zram_major = register_blkdev(0, "zram"); |
1746 | if (zram_major <= 0) { | |
70864969 | 1747 | pr_err("Unable to get major number\n"); |
6566d1a3 | 1748 | class_unregister(&zram_control_class); |
1dd6c834 | 1749 | cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); |
a096cafc | 1750 | return -EBUSY; |
306b0c95 NG |
1751 | } |
1752 | ||
92ff1528 | 1753 | while (num_devices != 0) { |
6566d1a3 | 1754 | mutex_lock(&zram_index_mutex); |
92ff1528 | 1755 | ret = zram_add(); |
6566d1a3 | 1756 | mutex_unlock(&zram_index_mutex); |
92ff1528 | 1757 | if (ret < 0) |
a096cafc | 1758 | goto out_error; |
92ff1528 | 1759 | num_devices--; |
de1a21a0 NG |
1760 | } |
1761 | ||
306b0c95 | 1762 | return 0; |
de1a21a0 | 1763 | |
a096cafc | 1764 | out_error: |
85508ec6 | 1765 | destroy_devices(); |
306b0c95 NG |
1766 | return ret; |
1767 | } | |
1768 | ||
f1e3cfff | 1769 | static void __exit zram_exit(void) |
306b0c95 | 1770 | { |
85508ec6 | 1771 | destroy_devices(); |
306b0c95 NG |
1772 | } |
1773 | ||
f1e3cfff NG |
1774 | module_init(zram_init); |
1775 | module_exit(zram_exit); | |
306b0c95 | 1776 | |
9b3bb7ab | 1777 | module_param(num_devices, uint, 0); |
c3cdb40e | 1778 | MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices"); |
9b3bb7ab | 1779 | |
306b0c95 NG |
1780 | MODULE_LICENSE("Dual BSD/GPL"); |
1781 | MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>"); | |
f1e3cfff | 1782 | MODULE_DESCRIPTION("Compressed RAM Block Device"); |