1cd67e10aSMinchan Kim /*
2cd67e10aSMinchan Kim * Compressed RAM block device
3cd67e10aSMinchan Kim *
4cd67e10aSMinchan Kim * Copyright (C) 2008, 2009, 2010 Nitin Gupta
57bfb3de8SMinchan Kim * 2012, 2013 Minchan Kim
6cd67e10aSMinchan Kim *
7cd67e10aSMinchan Kim * This code is released using a dual license strategy: BSD/GPL
8cd67e10aSMinchan Kim * You can choose the licence that better fits your requirements.
9cd67e10aSMinchan Kim *
10cd67e10aSMinchan Kim * Released under the terms of 3-clause BSD License
11cd67e10aSMinchan Kim * Released under the terms of GNU General Public License Version 2.0
12cd67e10aSMinchan Kim *
13cd67e10aSMinchan Kim */
14cd67e10aSMinchan Kim
15cd67e10aSMinchan Kim #define KMSG_COMPONENT "zram"
16cd67e10aSMinchan Kim #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17cd67e10aSMinchan Kim
18cd67e10aSMinchan Kim #include <linux/module.h>
19cd67e10aSMinchan Kim #include <linux/kernel.h>
20cd67e10aSMinchan Kim #include <linux/bio.h>
21cd67e10aSMinchan Kim #include <linux/bitops.h>
22cd67e10aSMinchan Kim #include <linux/blkdev.h>
23cd67e10aSMinchan Kim #include <linux/buffer_head.h>
24cd67e10aSMinchan Kim #include <linux/device.h>
25cd67e10aSMinchan Kim #include <linux/highmem.h>
26cd67e10aSMinchan Kim #include <linux/slab.h>
27b09ab054SMinchan Kim #include <linux/backing-dev.h>
28cd67e10aSMinchan Kim #include <linux/string.h>
29cd67e10aSMinchan Kim #include <linux/vmalloc.h>
30fcfa8d95SSergey Senozhatsky #include <linux/err.h>
3185508ec6SSergey Senozhatsky #include <linux/idr.h>
326566d1a3SSergey Senozhatsky #include <linux/sysfs.h>
33c0265342SMinchan Kim #include <linux/debugfs.h>
341dd6c834SAnna-Maria Gleixner #include <linux/cpuhotplug.h>
35c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
36cd67e10aSMinchan Kim
37cd67e10aSMinchan Kim #include "zram_drv.h"
38cd67e10aSMinchan Kim
3985508ec6SSergey Senozhatsky static DEFINE_IDR(zram_index_idr);
406566d1a3SSergey Senozhatsky /* idr index must be protected */
416566d1a3SSergey Senozhatsky static DEFINE_MUTEX(zram_index_mutex);
426566d1a3SSergey Senozhatsky
43cd67e10aSMinchan Kim static int zram_major;
443d711a38SRui Salvaterra static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
45cd67e10aSMinchan Kim
46cd67e10aSMinchan Kim /* Module params (documentation at end) */
47cd67e10aSMinchan Kim static unsigned int num_devices = 1;
4860f5921aSSergey Senozhatsky /*
4960f5921aSSergey Senozhatsky * Pages that compress to sizes equals or greater than this are stored
5060f5921aSSergey Senozhatsky * uncompressed in memory.
5160f5921aSSergey Senozhatsky */
5260f5921aSSergey Senozhatsky static size_t huge_class_size;
53cd67e10aSMinchan Kim
54a8b456d0SChristoph Hellwig static const struct block_device_operations zram_devops;
55a8b456d0SChristoph Hellwig
561f7319c7SMinchan Kim static void zram_free_page(struct zram *zram, size_t index);
5779c744eeSChristoph Hellwig static int zram_read_page(struct zram *zram, struct page *page, u32 index,
584e3c87b9SChristoph Hellwig struct bio *parent);
591f7319c7SMinchan Kim
zram_slot_trylock(struct zram * zram,u32 index)603c9959e0SMinchan Kim static int zram_slot_trylock(struct zram *zram, u32 index)
613c9959e0SMinchan Kim {
627e529283SMinchan Kim return bit_spin_trylock(ZRAM_LOCK, &zram->table[index].flags);
633c9959e0SMinchan Kim }
643c9959e0SMinchan Kim
zram_slot_lock(struct zram * zram,u32 index)65c4d6c4ccSMinchan Kim static void zram_slot_lock(struct zram *zram, u32 index)
66c4d6c4ccSMinchan Kim {
677e529283SMinchan Kim bit_spin_lock(ZRAM_LOCK, &zram->table[index].flags);
68c4d6c4ccSMinchan Kim }
69c4d6c4ccSMinchan Kim
zram_slot_unlock(struct zram * zram,u32 index)70c4d6c4ccSMinchan Kim static void zram_slot_unlock(struct zram *zram, u32 index)
71c4d6c4ccSMinchan Kim {
727e529283SMinchan Kim bit_spin_unlock(ZRAM_LOCK, &zram->table[index].flags);
73c4d6c4ccSMinchan Kim }
74c4d6c4ccSMinchan Kim
init_done(struct zram * zram)7508eee69fSMinchan Kim static inline bool init_done(struct zram *zram)
76be2d1d56SSergey Senozhatsky {
7708eee69fSMinchan Kim return zram->disksize;
78be2d1d56SSergey Senozhatsky }
79be2d1d56SSergey Senozhatsky
dev_to_zram(struct device * dev)80cd67e10aSMinchan Kim static inline struct zram *dev_to_zram(struct device *dev)
81cd67e10aSMinchan Kim {
82cd67e10aSMinchan Kim return (struct zram *)dev_to_disk(dev)->private_data;
83cd67e10aSMinchan Kim }
84cd67e10aSMinchan Kim
zram_get_handle(struct zram * zram,u32 index)85643ae61dSMinchan Kim static unsigned long zram_get_handle(struct zram *zram, u32 index)
86643ae61dSMinchan Kim {
87643ae61dSMinchan Kim return zram->table[index].handle;
88643ae61dSMinchan Kim }
89643ae61dSMinchan Kim
zram_set_handle(struct zram * zram,u32 index,unsigned long handle)90643ae61dSMinchan Kim static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
91643ae61dSMinchan Kim {
92643ae61dSMinchan Kim zram->table[index].handle = handle;
93643ae61dSMinchan Kim }
94643ae61dSMinchan Kim
95b31177f2SSergey Senozhatsky /* flag operations require table entry bit_spin_lock() being held */
zram_test_flag(struct zram * zram,u32 index,enum zram_pageflags flag)96c0265342SMinchan Kim static bool zram_test_flag(struct zram *zram, u32 index,
97522698d7SSergey Senozhatsky enum zram_pageflags flag)
9899ebbd30SAndrew Morton {
997e529283SMinchan Kim return zram->table[index].flags & BIT(flag);
10099ebbd30SAndrew Morton }
10199ebbd30SAndrew Morton
zram_set_flag(struct zram * zram,u32 index,enum zram_pageflags flag)102beb6602cSMinchan Kim static void zram_set_flag(struct zram *zram, u32 index,
103522698d7SSergey Senozhatsky enum zram_pageflags flag)
104522698d7SSergey Senozhatsky {
1057e529283SMinchan Kim zram->table[index].flags |= BIT(flag);
10699ebbd30SAndrew Morton }
10799ebbd30SAndrew Morton
zram_clear_flag(struct zram * zram,u32 index,enum zram_pageflags flag)108beb6602cSMinchan Kim static void zram_clear_flag(struct zram *zram, u32 index,
109522698d7SSergey Senozhatsky enum zram_pageflags flag)
110cd67e10aSMinchan Kim {
1117e529283SMinchan Kim zram->table[index].flags &= ~BIT(flag);
112522698d7SSergey Senozhatsky }
113cd67e10aSMinchan Kim
zram_set_element(struct zram * zram,u32 index,unsigned long element)114beb6602cSMinchan Kim static inline void zram_set_element(struct zram *zram, u32 index,
1158e19d540Szhouxianrong unsigned long element)
1168e19d540Szhouxianrong {
117beb6602cSMinchan Kim zram->table[index].element = element;
1188e19d540Szhouxianrong }
1198e19d540Szhouxianrong
zram_get_element(struct zram * zram,u32 index)120643ae61dSMinchan Kim static unsigned long zram_get_element(struct zram *zram, u32 index)
1218e19d540Szhouxianrong {
122643ae61dSMinchan Kim return zram->table[index].element;
1238e19d540Szhouxianrong }
1248e19d540Szhouxianrong
zram_get_obj_size(struct zram * zram,u32 index)125beb6602cSMinchan Kim static size_t zram_get_obj_size(struct zram *zram, u32 index)
126522698d7SSergey Senozhatsky {
1277e529283SMinchan Kim return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
128522698d7SSergey Senozhatsky }
129522698d7SSergey Senozhatsky
zram_set_obj_size(struct zram * zram,u32 index,size_t size)130beb6602cSMinchan Kim static void zram_set_obj_size(struct zram *zram,
131522698d7SSergey Senozhatsky u32 index, size_t size)
132522698d7SSergey Senozhatsky {
1337e529283SMinchan Kim unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
134522698d7SSergey Senozhatsky
1357e529283SMinchan Kim zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
136522698d7SSergey Senozhatsky }
137522698d7SSergey Senozhatsky
zram_allocated(struct zram * zram,u32 index)138a939888eSMinchan Kim static inline bool zram_allocated(struct zram *zram, u32 index)
139a939888eSMinchan Kim {
140a939888eSMinchan Kim return zram_get_obj_size(zram, index) ||
141a939888eSMinchan Kim zram_test_flag(zram, index, ZRAM_SAME) ||
142a939888eSMinchan Kim zram_test_flag(zram, index, ZRAM_WB);
143a939888eSMinchan Kim }
144a939888eSMinchan Kim
1451f7319c7SMinchan Kim #if PAGE_SIZE != 4096
is_partial_io(struct bio_vec * bvec)1461c53e0d2SGeliang Tang static inline bool is_partial_io(struct bio_vec *bvec)
147522698d7SSergey Senozhatsky {
148522698d7SSergey Senozhatsky return bvec->bv_len != PAGE_SIZE;
149522698d7SSergey Senozhatsky }
150a70aae12SChristoph Hellwig #define ZRAM_PARTIAL_IO 1
1511f7319c7SMinchan Kim #else
is_partial_io(struct bio_vec * bvec)1521f7319c7SMinchan Kim static inline bool is_partial_io(struct bio_vec *bvec)
1531f7319c7SMinchan Kim {
1541f7319c7SMinchan Kim return false;
1551f7319c7SMinchan Kim }
1561f7319c7SMinchan Kim #endif
157522698d7SSergey Senozhatsky
zram_set_priority(struct zram * zram,u32 index,u32 prio)15884b33bf7SSergey Senozhatsky static inline void zram_set_priority(struct zram *zram, u32 index, u32 prio)
15984b33bf7SSergey Senozhatsky {
16084b33bf7SSergey Senozhatsky prio &= ZRAM_COMP_PRIORITY_MASK;
16184b33bf7SSergey Senozhatsky /*
16284b33bf7SSergey Senozhatsky * Clear previous priority value first, in case if we recompress
16384b33bf7SSergey Senozhatsky * further an already recompressed page
16484b33bf7SSergey Senozhatsky */
16584b33bf7SSergey Senozhatsky zram->table[index].flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
16684b33bf7SSergey Senozhatsky ZRAM_COMP_PRIORITY_BIT1);
16784b33bf7SSergey Senozhatsky zram->table[index].flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
16884b33bf7SSergey Senozhatsky }
16984b33bf7SSergey Senozhatsky
zram_get_priority(struct zram * zram,u32 index)17084b33bf7SSergey Senozhatsky static inline u32 zram_get_priority(struct zram *zram, u32 index)
17184b33bf7SSergey Senozhatsky {
17284b33bf7SSergey Senozhatsky u32 prio = zram->table[index].flags >> ZRAM_COMP_PRIORITY_BIT1;
17384b33bf7SSergey Senozhatsky
17484b33bf7SSergey Senozhatsky return prio & ZRAM_COMP_PRIORITY_MASK;
17584b33bf7SSergey Senozhatsky }
17684b33bf7SSergey Senozhatsky
zram_accessed(struct zram * zram,u32 index)177b7c3fd65SSergey Senozhatsky static void zram_accessed(struct zram *zram, u32 index)
178b7c3fd65SSergey Senozhatsky {
179b7c3fd65SSergey Senozhatsky zram_clear_flag(zram, index, ZRAM_IDLE);
180b7c3fd65SSergey Senozhatsky #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
181b7c3fd65SSergey Senozhatsky zram->table[index].ac_time = ktime_get_boottime();
182b7c3fd65SSergey Senozhatsky #endif
183b7c3fd65SSergey Senozhatsky }
184b7c3fd65SSergey Senozhatsky
update_used_max(struct zram * zram,const unsigned long pages)185522698d7SSergey Senozhatsky static inline void update_used_max(struct zram *zram,
186522698d7SSergey Senozhatsky const unsigned long pages)
187522698d7SSergey Senozhatsky {
18870ec04f3SUros Bizjak unsigned long cur_max = atomic_long_read(&zram->stats.max_used_pages);
189522698d7SSergey Senozhatsky
190522698d7SSergey Senozhatsky do {
19170ec04f3SUros Bizjak if (cur_max >= pages)
19270ec04f3SUros Bizjak return;
19370ec04f3SUros Bizjak } while (!atomic_long_try_cmpxchg(&zram->stats.max_used_pages,
19470ec04f3SUros Bizjak &cur_max, pages));
195522698d7SSergey Senozhatsky }
196522698d7SSergey Senozhatsky
zram_fill_page(void * ptr,unsigned long len,unsigned long value)19748ad1abeSMatthew Wilcox static inline void zram_fill_page(void *ptr, unsigned long len,
1988e19d540Szhouxianrong unsigned long value)
1998e19d540Szhouxianrong {
2008e19d540Szhouxianrong WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
20148ad1abeSMatthew Wilcox memset_l(ptr, value, len / sizeof(unsigned long));
2028e19d540Szhouxianrong }
2038e19d540Szhouxianrong
page_same_filled(void * ptr,unsigned long * element)2048e19d540Szhouxianrong static bool page_same_filled(void *ptr, unsigned long *element)
205522698d7SSergey Senozhatsky {
206522698d7SSergey Senozhatsky unsigned long *page;
207f0fe9984SSangwoo Park unsigned long val;
20890f82cbfSTaejoon Song unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
209522698d7SSergey Senozhatsky
210522698d7SSergey Senozhatsky page = (unsigned long *)ptr;
211f0fe9984SSangwoo Park val = page[0];
212522698d7SSergey Senozhatsky
21390f82cbfSTaejoon Song if (val != page[last_pos])
21490f82cbfSTaejoon Song return false;
21590f82cbfSTaejoon Song
21690f82cbfSTaejoon Song for (pos = 1; pos < last_pos; pos++) {
217f0fe9984SSangwoo Park if (val != page[pos])
2181c53e0d2SGeliang Tang return false;
219522698d7SSergey Senozhatsky }
220522698d7SSergey Senozhatsky
221f0fe9984SSangwoo Park *element = val;
2228e19d540Szhouxianrong
2231c53e0d2SGeliang Tang return true;
224522698d7SSergey Senozhatsky }
225522698d7SSergey Senozhatsky
initstate_show(struct device * dev,struct device_attribute * attr,char * buf)226cd67e10aSMinchan Kim static ssize_t initstate_show(struct device *dev,
227cd67e10aSMinchan Kim struct device_attribute *attr, char *buf)
228cd67e10aSMinchan Kim {
229a68eb3b6SSergey Senozhatsky u32 val;
230cd67e10aSMinchan Kim struct zram *zram = dev_to_zram(dev);
231cd67e10aSMinchan Kim
232a68eb3b6SSergey Senozhatsky down_read(&zram->init_lock);
233a68eb3b6SSergey Senozhatsky val = init_done(zram);
234a68eb3b6SSergey Senozhatsky up_read(&zram->init_lock);
235cd67e10aSMinchan Kim
23656b4e8cbSSergey Senozhatsky return scnprintf(buf, PAGE_SIZE, "%u\n", val);
237cd67e10aSMinchan Kim }
238cd67e10aSMinchan Kim
disksize_show(struct device * dev,struct device_attribute * attr,char * buf)239522698d7SSergey Senozhatsky static ssize_t disksize_show(struct device *dev,
240522698d7SSergey Senozhatsky struct device_attribute *attr, char *buf)
241522698d7SSergey Senozhatsky {
242522698d7SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
243522698d7SSergey Senozhatsky
244522698d7SSergey Senozhatsky return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
245522698d7SSergey Senozhatsky }
246522698d7SSergey Senozhatsky
mem_limit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2479ada9da9SMinchan Kim static ssize_t mem_limit_store(struct device *dev,
2489ada9da9SMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
2499ada9da9SMinchan Kim {
2509ada9da9SMinchan Kim u64 limit;
2519ada9da9SMinchan Kim char *tmp;
2529ada9da9SMinchan Kim struct zram *zram = dev_to_zram(dev);
2539ada9da9SMinchan Kim
2549ada9da9SMinchan Kim limit = memparse(buf, &tmp);
2559ada9da9SMinchan Kim if (buf == tmp) /* no chars parsed, invalid input */
2569ada9da9SMinchan Kim return -EINVAL;
2579ada9da9SMinchan Kim
2589ada9da9SMinchan Kim down_write(&zram->init_lock);
2599ada9da9SMinchan Kim zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
2609ada9da9SMinchan Kim up_write(&zram->init_lock);
2619ada9da9SMinchan Kim
2629ada9da9SMinchan Kim return len;
2639ada9da9SMinchan Kim }
2649ada9da9SMinchan Kim
mem_used_max_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)265461a8eeeSMinchan Kim static ssize_t mem_used_max_store(struct device *dev,
266461a8eeeSMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
267461a8eeeSMinchan Kim {
268461a8eeeSMinchan Kim int err;
269461a8eeeSMinchan Kim unsigned long val;
270461a8eeeSMinchan Kim struct zram *zram = dev_to_zram(dev);
271461a8eeeSMinchan Kim
272461a8eeeSMinchan Kim err = kstrtoul(buf, 10, &val);
273461a8eeeSMinchan Kim if (err || val != 0)
274461a8eeeSMinchan Kim return -EINVAL;
275461a8eeeSMinchan Kim
276461a8eeeSMinchan Kim down_read(&zram->init_lock);
2775a99e95bSWeijie Yang if (init_done(zram)) {
278461a8eeeSMinchan Kim atomic_long_set(&zram->stats.max_used_pages,
279beb6602cSMinchan Kim zs_get_total_pages(zram->mem_pool));
2805a99e95bSWeijie Yang }
281461a8eeeSMinchan Kim up_read(&zram->init_lock);
282461a8eeeSMinchan Kim
283461a8eeeSMinchan Kim return len;
284461a8eeeSMinchan Kim }
285461a8eeeSMinchan Kim
286755804d1SBrian Geffon /*
287755804d1SBrian Geffon * Mark all pages which are older than or equal to cutoff as IDLE.
288755804d1SBrian Geffon * Callers should hold the zram init lock in read mode
289755804d1SBrian Geffon */
mark_idle(struct zram * zram,ktime_t cutoff)290755804d1SBrian Geffon static void mark_idle(struct zram *zram, ktime_t cutoff)
291e82592c4SMinchan Kim {
292755804d1SBrian Geffon int is_idle = 1;
293e82592c4SMinchan Kim unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
294e82592c4SMinchan Kim int index;
295e82592c4SMinchan Kim
296e82592c4SMinchan Kim for (index = 0; index < nr_pages; index++) {
297a939888eSMinchan Kim /*
298a939888eSMinchan Kim * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
299a939888eSMinchan Kim * See the comment in writeback_store.
3009c251d13SSergey Senozhatsky *
3019c251d13SSergey Senozhatsky * Also do not mark ZRAM_SAME slots as ZRAM_IDLE, because no
3029c251d13SSergey Senozhatsky * post-processing (recompress, writeback) happens to the
3039c251d13SSergey Senozhatsky * ZRAM_SAME slot.
3049c251d13SSergey Senozhatsky *
3059c251d13SSergey Senozhatsky * And ZRAM_WB slots simply cannot be ZRAM_IDLE.
306a939888eSMinchan Kim */
307e82592c4SMinchan Kim zram_slot_lock(zram, index);
3089c251d13SSergey Senozhatsky if (!zram_allocated(zram, index) ||
3099c251d13SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_WB) ||
3109c251d13SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
3119c251d13SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_SAME)) {
3129c251d13SSergey Senozhatsky zram_slot_unlock(zram, index);
3139c251d13SSergey Senozhatsky continue;
3149c251d13SSergey Senozhatsky }
3159c251d13SSergey Senozhatsky
316b7c3fd65SSergey Senozhatsky #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
3179c251d13SSergey Senozhatsky is_idle = !cutoff ||
3189c251d13SSergey Senozhatsky ktime_after(cutoff, zram->table[index].ac_time);
319755804d1SBrian Geffon #endif
320755804d1SBrian Geffon if (is_idle)
321e82592c4SMinchan Kim zram_set_flag(zram, index, ZRAM_IDLE);
3227360a0e7SSergey Senozhatsky else
3237360a0e7SSergey Senozhatsky zram_clear_flag(zram, index, ZRAM_IDLE);
324e82592c4SMinchan Kim zram_slot_unlock(zram, index);
325e82592c4SMinchan Kim }
326755804d1SBrian Geffon }
327e82592c4SMinchan Kim
idle_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)328755804d1SBrian Geffon static ssize_t idle_store(struct device *dev,
329755804d1SBrian Geffon struct device_attribute *attr, const char *buf, size_t len)
330755804d1SBrian Geffon {
331755804d1SBrian Geffon struct zram *zram = dev_to_zram(dev);
332755804d1SBrian Geffon ktime_t cutoff_time = 0;
333755804d1SBrian Geffon ssize_t rv = -EINVAL;
334755804d1SBrian Geffon
335755804d1SBrian Geffon if (!sysfs_streq(buf, "all")) {
336755804d1SBrian Geffon /*
337f9bceb2fSSergey Senozhatsky * If it did not parse as 'all' try to treat it as an integer
338f9bceb2fSSergey Senozhatsky * when we have memory tracking enabled.
339755804d1SBrian Geffon */
340755804d1SBrian Geffon u64 age_sec;
341755804d1SBrian Geffon
342b7c3fd65SSergey Senozhatsky if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && !kstrtoull(buf, 0, &age_sec))
343755804d1SBrian Geffon cutoff_time = ktime_sub(ktime_get_boottime(),
344755804d1SBrian Geffon ns_to_ktime(age_sec * NSEC_PER_SEC));
345755804d1SBrian Geffon else
346755804d1SBrian Geffon goto out;
347755804d1SBrian Geffon }
348755804d1SBrian Geffon
349755804d1SBrian Geffon down_read(&zram->init_lock);
350755804d1SBrian Geffon if (!init_done(zram))
351755804d1SBrian Geffon goto out_unlock;
352755804d1SBrian Geffon
353f9bceb2fSSergey Senozhatsky /*
354f9bceb2fSSergey Senozhatsky * A cutoff_time of 0 marks everything as idle, this is the
355f9bceb2fSSergey Senozhatsky * "all" behavior.
356f9bceb2fSSergey Senozhatsky */
357755804d1SBrian Geffon mark_idle(zram, cutoff_time);
358755804d1SBrian Geffon rv = len;
359755804d1SBrian Geffon
360755804d1SBrian Geffon out_unlock:
361e82592c4SMinchan Kim up_read(&zram->init_lock);
362755804d1SBrian Geffon out:
363755804d1SBrian Geffon return rv;
364e82592c4SMinchan Kim }
365e82592c4SMinchan Kim
366013bf95aSMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
writeback_limit_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)3671d69a3f8SMinchan Kim static ssize_t writeback_limit_enable_store(struct device *dev,
3681d69a3f8SMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
3691d69a3f8SMinchan Kim {
3701d69a3f8SMinchan Kim struct zram *zram = dev_to_zram(dev);
3711d69a3f8SMinchan Kim u64 val;
3721d69a3f8SMinchan Kim ssize_t ret = -EINVAL;
3731d69a3f8SMinchan Kim
3741d69a3f8SMinchan Kim if (kstrtoull(buf, 10, &val))
3751d69a3f8SMinchan Kim return ret;
3761d69a3f8SMinchan Kim
3771d69a3f8SMinchan Kim down_read(&zram->init_lock);
3781d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
3791d69a3f8SMinchan Kim zram->wb_limit_enable = val;
3801d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
3811d69a3f8SMinchan Kim up_read(&zram->init_lock);
3821d69a3f8SMinchan Kim ret = len;
3831d69a3f8SMinchan Kim
3841d69a3f8SMinchan Kim return ret;
3851d69a3f8SMinchan Kim }
3861d69a3f8SMinchan Kim
writeback_limit_enable_show(struct device * dev,struct device_attribute * attr,char * buf)3871d69a3f8SMinchan Kim static ssize_t writeback_limit_enable_show(struct device *dev,
3881d69a3f8SMinchan Kim struct device_attribute *attr, char *buf)
3891d69a3f8SMinchan Kim {
3901d69a3f8SMinchan Kim bool val;
3911d69a3f8SMinchan Kim struct zram *zram = dev_to_zram(dev);
3921d69a3f8SMinchan Kim
3931d69a3f8SMinchan Kim down_read(&zram->init_lock);
3941d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
3951d69a3f8SMinchan Kim val = zram->wb_limit_enable;
3961d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
3971d69a3f8SMinchan Kim up_read(&zram->init_lock);
3981d69a3f8SMinchan Kim
3991d69a3f8SMinchan Kim return scnprintf(buf, PAGE_SIZE, "%d\n", val);
4001d69a3f8SMinchan Kim }
4011d69a3f8SMinchan Kim
writeback_limit_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)402bb416d18SMinchan Kim static ssize_t writeback_limit_store(struct device *dev,
403bb416d18SMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
404bb416d18SMinchan Kim {
405bb416d18SMinchan Kim struct zram *zram = dev_to_zram(dev);
406bb416d18SMinchan Kim u64 val;
407bb416d18SMinchan Kim ssize_t ret = -EINVAL;
408bb416d18SMinchan Kim
409bb416d18SMinchan Kim if (kstrtoull(buf, 10, &val))
410bb416d18SMinchan Kim return ret;
411bb416d18SMinchan Kim
412bb416d18SMinchan Kim down_read(&zram->init_lock);
4131d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
4141d69a3f8SMinchan Kim zram->bd_wb_limit = val;
4151d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
416bb416d18SMinchan Kim up_read(&zram->init_lock);
417bb416d18SMinchan Kim ret = len;
418bb416d18SMinchan Kim
419bb416d18SMinchan Kim return ret;
420bb416d18SMinchan Kim }
421bb416d18SMinchan Kim
writeback_limit_show(struct device * dev,struct device_attribute * attr,char * buf)422bb416d18SMinchan Kim static ssize_t writeback_limit_show(struct device *dev,
423bb416d18SMinchan Kim struct device_attribute *attr, char *buf)
424bb416d18SMinchan Kim {
425bb416d18SMinchan Kim u64 val;
426bb416d18SMinchan Kim struct zram *zram = dev_to_zram(dev);
427bb416d18SMinchan Kim
428bb416d18SMinchan Kim down_read(&zram->init_lock);
4291d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
4301d69a3f8SMinchan Kim val = zram->bd_wb_limit;
4311d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
432bb416d18SMinchan Kim up_read(&zram->init_lock);
433bb416d18SMinchan Kim
434bb416d18SMinchan Kim return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
435bb416d18SMinchan Kim }
436bb416d18SMinchan Kim
reset_bdev(struct zram * zram)437013bf95aSMinchan Kim static void reset_bdev(struct zram *zram)
438013bf95aSMinchan Kim {
439013bf95aSMinchan Kim struct block_device *bdev;
440013bf95aSMinchan Kim
4417e529283SMinchan Kim if (!zram->backing_dev)
442013bf95aSMinchan Kim return;
443013bf95aSMinchan Kim
444013bf95aSMinchan Kim bdev = zram->bdev;
4452736e8eeSChristoph Hellwig blkdev_put(bdev, zram);
446013bf95aSMinchan Kim /* hope filp_close flush all of IO */
447013bf95aSMinchan Kim filp_close(zram->backing_dev, NULL);
448013bf95aSMinchan Kim zram->backing_dev = NULL;
449013bf95aSMinchan Kim zram->bdev = NULL;
450a8b456d0SChristoph Hellwig zram->disk->fops = &zram_devops;
4511363d466SMinchan Kim kvfree(zram->bitmap);
4521363d466SMinchan Kim zram->bitmap = NULL;
453013bf95aSMinchan Kim }
454013bf95aSMinchan Kim
backing_dev_show(struct device * dev,struct device_attribute * attr,char * buf)455013bf95aSMinchan Kim static ssize_t backing_dev_show(struct device *dev,
456013bf95aSMinchan Kim struct device_attribute *attr, char *buf)
457013bf95aSMinchan Kim {
458f7daefe4SChenwandun struct file *file;
459013bf95aSMinchan Kim struct zram *zram = dev_to_zram(dev);
460013bf95aSMinchan Kim char *p;
461013bf95aSMinchan Kim ssize_t ret;
462013bf95aSMinchan Kim
463013bf95aSMinchan Kim down_read(&zram->init_lock);
464f7daefe4SChenwandun file = zram->backing_dev;
465f7daefe4SChenwandun if (!file) {
466013bf95aSMinchan Kim memcpy(buf, "none\n", 5);
467013bf95aSMinchan Kim up_read(&zram->init_lock);
468013bf95aSMinchan Kim return 5;
469013bf95aSMinchan Kim }
470013bf95aSMinchan Kim
471013bf95aSMinchan Kim p = file_path(file, buf, PAGE_SIZE - 1);
472013bf95aSMinchan Kim if (IS_ERR(p)) {
473013bf95aSMinchan Kim ret = PTR_ERR(p);
474013bf95aSMinchan Kim goto out;
475013bf95aSMinchan Kim }
476013bf95aSMinchan Kim
477013bf95aSMinchan Kim ret = strlen(p);
478013bf95aSMinchan Kim memmove(buf, p, ret);
479013bf95aSMinchan Kim buf[ret++] = '\n';
480013bf95aSMinchan Kim out:
481013bf95aSMinchan Kim up_read(&zram->init_lock);
482013bf95aSMinchan Kim return ret;
483013bf95aSMinchan Kim }
484013bf95aSMinchan Kim
backing_dev_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)485013bf95aSMinchan Kim static ssize_t backing_dev_store(struct device *dev,
486013bf95aSMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
487013bf95aSMinchan Kim {
488013bf95aSMinchan Kim char *file_name;
489c8bd134aSPeter Kalauskas size_t sz;
490013bf95aSMinchan Kim struct file *backing_dev = NULL;
491013bf95aSMinchan Kim struct inode *inode;
492013bf95aSMinchan Kim struct address_space *mapping;
493ee763e21SChristoph Hellwig unsigned int bitmap_sz;
4941363d466SMinchan Kim unsigned long nr_pages, *bitmap = NULL;
495013bf95aSMinchan Kim struct block_device *bdev = NULL;
496013bf95aSMinchan Kim int err;
497013bf95aSMinchan Kim struct zram *zram = dev_to_zram(dev);
498013bf95aSMinchan Kim
499013bf95aSMinchan Kim file_name = kmalloc(PATH_MAX, GFP_KERNEL);
500013bf95aSMinchan Kim if (!file_name)
501013bf95aSMinchan Kim return -ENOMEM;
502013bf95aSMinchan Kim
503013bf95aSMinchan Kim down_write(&zram->init_lock);
504013bf95aSMinchan Kim if (init_done(zram)) {
505013bf95aSMinchan Kim pr_info("Can't setup backing device for initialized device\n");
506013bf95aSMinchan Kim err = -EBUSY;
507013bf95aSMinchan Kim goto out;
508013bf95aSMinchan Kim }
509013bf95aSMinchan Kim
510e55e1b48SWolfram Sang strscpy(file_name, buf, PATH_MAX);
511c8bd134aSPeter Kalauskas /* ignore trailing newline */
512c8bd134aSPeter Kalauskas sz = strlen(file_name);
513c8bd134aSPeter Kalauskas if (sz > 0 && file_name[sz - 1] == '\n')
514c8bd134aSPeter Kalauskas file_name[sz - 1] = 0x00;
515013bf95aSMinchan Kim
516013bf95aSMinchan Kim backing_dev = filp_open(file_name, O_RDWR|O_LARGEFILE, 0);
517013bf95aSMinchan Kim if (IS_ERR(backing_dev)) {
518013bf95aSMinchan Kim err = PTR_ERR(backing_dev);
519013bf95aSMinchan Kim backing_dev = NULL;
520013bf95aSMinchan Kim goto out;
521013bf95aSMinchan Kim }
522013bf95aSMinchan Kim
523013bf95aSMinchan Kim mapping = backing_dev->f_mapping;
524013bf95aSMinchan Kim inode = mapping->host;
525013bf95aSMinchan Kim
526013bf95aSMinchan Kim /* Support only block device in this moment */
527013bf95aSMinchan Kim if (!S_ISBLK(inode->i_mode)) {
528013bf95aSMinchan Kim err = -ENOTBLK;
529013bf95aSMinchan Kim goto out;
530013bf95aSMinchan Kim }
531013bf95aSMinchan Kim
53205bdb996SChristoph Hellwig bdev = blkdev_get_by_dev(inode->i_rdev, BLK_OPEN_READ | BLK_OPEN_WRITE,
53305bdb996SChristoph Hellwig zram, NULL);
5340fc66c9dSChristoph Hellwig if (IS_ERR(bdev)) {
5350fc66c9dSChristoph Hellwig err = PTR_ERR(bdev);
5365547932dSMinchan Kim bdev = NULL;
537013bf95aSMinchan Kim goto out;
5385547932dSMinchan Kim }
539013bf95aSMinchan Kim
5401363d466SMinchan Kim nr_pages = i_size_read(inode) >> PAGE_SHIFT;
541aca0f94cSKairui Song /* Refuse to use zero sized device (also prevents self reference) */
542aca0f94cSKairui Song if (!nr_pages) {
543aca0f94cSKairui Song err = -EINVAL;
544aca0f94cSKairui Song goto out;
545aca0f94cSKairui Song }
546aca0f94cSKairui Song
5471363d466SMinchan Kim bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
5481363d466SMinchan Kim bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
5491363d466SMinchan Kim if (!bitmap) {
5501363d466SMinchan Kim err = -ENOMEM;
5511363d466SMinchan Kim goto out;
5521363d466SMinchan Kim }
5531363d466SMinchan Kim
554013bf95aSMinchan Kim reset_bdev(zram);
555013bf95aSMinchan Kim
556013bf95aSMinchan Kim zram->bdev = bdev;
557013bf95aSMinchan Kim zram->backing_dev = backing_dev;
5581363d466SMinchan Kim zram->bitmap = bitmap;
5591363d466SMinchan Kim zram->nr_pages = nr_pages;
560013bf95aSMinchan Kim up_write(&zram->init_lock);
561013bf95aSMinchan Kim
562013bf95aSMinchan Kim pr_info("setup backing device %s\n", file_name);
563013bf95aSMinchan Kim kfree(file_name);
564013bf95aSMinchan Kim
565013bf95aSMinchan Kim return len;
566013bf95aSMinchan Kim out:
5671363d466SMinchan Kim kvfree(bitmap);
5681363d466SMinchan Kim
569013bf95aSMinchan Kim if (bdev)
5702736e8eeSChristoph Hellwig blkdev_put(bdev, zram);
571013bf95aSMinchan Kim
572013bf95aSMinchan Kim if (backing_dev)
573013bf95aSMinchan Kim filp_close(backing_dev, NULL);
574013bf95aSMinchan Kim
575013bf95aSMinchan Kim up_write(&zram->init_lock);
576013bf95aSMinchan Kim
577013bf95aSMinchan Kim kfree(file_name);
578013bf95aSMinchan Kim
579013bf95aSMinchan Kim return err;
580013bf95aSMinchan Kim }
581013bf95aSMinchan Kim
alloc_block_bdev(struct zram * zram)5827e529283SMinchan Kim static unsigned long alloc_block_bdev(struct zram *zram)
5831363d466SMinchan Kim {
5843c9959e0SMinchan Kim unsigned long blk_idx = 1;
5853c9959e0SMinchan Kim retry:
5861363d466SMinchan Kim /* skip 0 bit to confuse zram.handle = 0 */
5873c9959e0SMinchan Kim blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
5883c9959e0SMinchan Kim if (blk_idx == zram->nr_pages)
5891363d466SMinchan Kim return 0;
5901363d466SMinchan Kim
5913c9959e0SMinchan Kim if (test_and_set_bit(blk_idx, zram->bitmap))
5923c9959e0SMinchan Kim goto retry;
5931363d466SMinchan Kim
59423eddf39SMinchan Kim atomic64_inc(&zram->stats.bd_count);
5953c9959e0SMinchan Kim return blk_idx;
5961363d466SMinchan Kim }
5971363d466SMinchan Kim
free_block_bdev(struct zram * zram,unsigned long blk_idx)5987e529283SMinchan Kim static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
5991363d466SMinchan Kim {
6001363d466SMinchan Kim int was_set;
6011363d466SMinchan Kim
6027e529283SMinchan Kim was_set = test_and_clear_bit(blk_idx, zram->bitmap);
6031363d466SMinchan Kim WARN_ON_ONCE(!was_set);
60423eddf39SMinchan Kim atomic64_dec(&zram->stats.bd_count);
6051363d466SMinchan Kim }
6061363d466SMinchan Kim
read_from_bdev_async(struct zram * zram,struct page * page,unsigned long entry,struct bio * parent)6070cd97a03SChristoph Hellwig static void read_from_bdev_async(struct zram *zram, struct page *page,
6088e654f8fSMinchan Kim unsigned long entry, struct bio *parent)
6098e654f8fSMinchan Kim {
6108e654f8fSMinchan Kim struct bio *bio;
6118e654f8fSMinchan Kim
6124e3c87b9SChristoph Hellwig bio = bio_alloc(zram->bdev, 1, parent->bi_opf, GFP_NOIO);
6138e654f8fSMinchan Kim bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
6140cd97a03SChristoph Hellwig __bio_add_page(bio, page, PAGE_SIZE, 0);
6158e654f8fSMinchan Kim bio_chain(bio, parent);
6168e654f8fSMinchan Kim submit_bio(bio);
6178e654f8fSMinchan Kim }
6188e654f8fSMinchan Kim
6190d835962SMinchan Kim #define PAGE_WB_SIG "page_index="
6200d835962SMinchan Kim
6210d835962SMinchan Kim #define PAGE_WRITEBACK 0
62230226b69SBrian Geffon #define HUGE_WRITEBACK (1<<0)
62330226b69SBrian Geffon #define IDLE_WRITEBACK (1<<1)
624b46f9ea3SSergey Senozhatsky #define INCOMPRESSIBLE_WRITEBACK (1<<2)
6250d835962SMinchan Kim
writeback_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)626a939888eSMinchan Kim static ssize_t writeback_store(struct device *dev,
627a939888eSMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
628a939888eSMinchan Kim {
629a939888eSMinchan Kim struct zram *zram = dev_to_zram(dev);
630a939888eSMinchan Kim unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
6310d835962SMinchan Kim unsigned long index = 0;
632a939888eSMinchan Kim struct bio bio;
633a939888eSMinchan Kim struct bio_vec bio_vec;
634a939888eSMinchan Kim struct page *page;
6353b82a051SColin Ian King ssize_t ret = len;
63657e0076eSMinchan Kim int mode, err;
637a939888eSMinchan Kim unsigned long blk_idx = 0;
638a939888eSMinchan Kim
6390bc9f5d1SMinchan Kim if (sysfs_streq(buf, "idle"))
640a939888eSMinchan Kim mode = IDLE_WRITEBACK;
6410bc9f5d1SMinchan Kim else if (sysfs_streq(buf, "huge"))
642a939888eSMinchan Kim mode = HUGE_WRITEBACK;
64330226b69SBrian Geffon else if (sysfs_streq(buf, "huge_idle"))
64430226b69SBrian Geffon mode = IDLE_WRITEBACK | HUGE_WRITEBACK;
645b46f9ea3SSergey Senozhatsky else if (sysfs_streq(buf, "incompressible"))
646b46f9ea3SSergey Senozhatsky mode = INCOMPRESSIBLE_WRITEBACK;
6470d835962SMinchan Kim else {
6480d835962SMinchan Kim if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1))
649a939888eSMinchan Kim return -EINVAL;
650a939888eSMinchan Kim
6512766f182SMinchan Kim if (kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index) ||
6522766f182SMinchan Kim index >= nr_pages)
6530d835962SMinchan Kim return -EINVAL;
6540d835962SMinchan Kim
6550d835962SMinchan Kim nr_pages = 1;
6560d835962SMinchan Kim mode = PAGE_WRITEBACK;
6570d835962SMinchan Kim }
6580d835962SMinchan Kim
659a939888eSMinchan Kim down_read(&zram->init_lock);
660a939888eSMinchan Kim if (!init_done(zram)) {
661a939888eSMinchan Kim ret = -EINVAL;
662a939888eSMinchan Kim goto release_init_lock;
663a939888eSMinchan Kim }
664a939888eSMinchan Kim
665a939888eSMinchan Kim if (!zram->backing_dev) {
666a939888eSMinchan Kim ret = -ENODEV;
667a939888eSMinchan Kim goto release_init_lock;
668a939888eSMinchan Kim }
669a939888eSMinchan Kim
670a939888eSMinchan Kim page = alloc_page(GFP_KERNEL);
671a939888eSMinchan Kim if (!page) {
672a939888eSMinchan Kim ret = -ENOMEM;
673a939888eSMinchan Kim goto release_init_lock;
674a939888eSMinchan Kim }
675a939888eSMinchan Kim
6762766f182SMinchan Kim for (; nr_pages != 0; index++, nr_pages--) {
6771d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
6781d69a3f8SMinchan Kim if (zram->wb_limit_enable && !zram->bd_wb_limit) {
6791d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
680bb416d18SMinchan Kim ret = -EIO;
681bb416d18SMinchan Kim break;
682bb416d18SMinchan Kim }
6831d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
684bb416d18SMinchan Kim
685a939888eSMinchan Kim if (!blk_idx) {
686a939888eSMinchan Kim blk_idx = alloc_block_bdev(zram);
687a939888eSMinchan Kim if (!blk_idx) {
688a939888eSMinchan Kim ret = -ENOSPC;
689a939888eSMinchan Kim break;
690a939888eSMinchan Kim }
691a939888eSMinchan Kim }
692a939888eSMinchan Kim
693a939888eSMinchan Kim zram_slot_lock(zram, index);
694a939888eSMinchan Kim if (!zram_allocated(zram, index))
695a939888eSMinchan Kim goto next;
696a939888eSMinchan Kim
697a939888eSMinchan Kim if (zram_test_flag(zram, index, ZRAM_WB) ||
698a939888eSMinchan Kim zram_test_flag(zram, index, ZRAM_SAME) ||
699a939888eSMinchan Kim zram_test_flag(zram, index, ZRAM_UNDER_WB))
700a939888eSMinchan Kim goto next;
701a939888eSMinchan Kim
70230226b69SBrian Geffon if (mode & IDLE_WRITEBACK &&
7031d69a3f8SMinchan Kim !zram_test_flag(zram, index, ZRAM_IDLE))
7041d69a3f8SMinchan Kim goto next;
70530226b69SBrian Geffon if (mode & HUGE_WRITEBACK &&
7061d69a3f8SMinchan Kim !zram_test_flag(zram, index, ZRAM_HUGE))
707a939888eSMinchan Kim goto next;
708b46f9ea3SSergey Senozhatsky if (mode & INCOMPRESSIBLE_WRITEBACK &&
709b46f9ea3SSergey Senozhatsky !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
710b46f9ea3SSergey Senozhatsky goto next;
711b46f9ea3SSergey Senozhatsky
712a939888eSMinchan Kim /*
713a939888eSMinchan Kim * Clearing ZRAM_UNDER_WB is duty of caller.
714a939888eSMinchan Kim * IOW, zram_free_page never clear it.
715a939888eSMinchan Kim */
716a939888eSMinchan Kim zram_set_flag(zram, index, ZRAM_UNDER_WB);
717a939888eSMinchan Kim /* Need for hugepage writeback racing */
718a939888eSMinchan Kim zram_set_flag(zram, index, ZRAM_IDLE);
719a939888eSMinchan Kim zram_slot_unlock(zram, index);
7204e3c87b9SChristoph Hellwig if (zram_read_page(zram, page, index, NULL)) {
721a939888eSMinchan Kim zram_slot_lock(zram, index);
722a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_UNDER_WB);
723a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_IDLE);
724a939888eSMinchan Kim zram_slot_unlock(zram, index);
725a939888eSMinchan Kim continue;
726a939888eSMinchan Kim }
727a939888eSMinchan Kim
72849add496SChristoph Hellwig bio_init(&bio, zram->bdev, &bio_vec, 1,
72949add496SChristoph Hellwig REQ_OP_WRITE | REQ_SYNC);
730a939888eSMinchan Kim bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
73134848c91SJohannes Thumshirn __bio_add_page(&bio, page, PAGE_SIZE, 0);
732a939888eSMinchan Kim
733a939888eSMinchan Kim /*
734a939888eSMinchan Kim * XXX: A single page IO would be inefficient for write
735a939888eSMinchan Kim * but it would be not bad as starter.
736a939888eSMinchan Kim */
73757e0076eSMinchan Kim err = submit_bio_wait(&bio);
73857e0076eSMinchan Kim if (err) {
739a939888eSMinchan Kim zram_slot_lock(zram, index);
740a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_UNDER_WB);
741a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_IDLE);
742a939888eSMinchan Kim zram_slot_unlock(zram, index);
74357e0076eSMinchan Kim /*
7449fda785dSSergey Senozhatsky * BIO errors are not fatal, we continue and simply
7459fda785dSSergey Senozhatsky * attempt to writeback the remaining objects (pages).
7469fda785dSSergey Senozhatsky * At the same time we need to signal user-space that
7479fda785dSSergey Senozhatsky * some writes (at least one, but also could be all of
7489fda785dSSergey Senozhatsky * them) were not successful and we do so by returning
7499fda785dSSergey Senozhatsky * the most recent BIO error.
75057e0076eSMinchan Kim */
75157e0076eSMinchan Kim ret = err;
752a939888eSMinchan Kim continue;
753a939888eSMinchan Kim }
754a939888eSMinchan Kim
75523eddf39SMinchan Kim atomic64_inc(&zram->stats.bd_writes);
756a939888eSMinchan Kim /*
757a939888eSMinchan Kim * We released zram_slot_lock so need to check if the slot was
758a939888eSMinchan Kim * changed. If there is freeing for the slot, we can catch it
759a939888eSMinchan Kim * easily by zram_allocated.
760a939888eSMinchan Kim * A subtle case is the slot is freed/reallocated/marked as
761a939888eSMinchan Kim * ZRAM_IDLE again. To close the race, idle_store doesn't
762a939888eSMinchan Kim * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
763a939888eSMinchan Kim * Thus, we could close the race by checking ZRAM_IDLE bit.
764a939888eSMinchan Kim */
765a939888eSMinchan Kim zram_slot_lock(zram, index);
766a939888eSMinchan Kim if (!zram_allocated(zram, index) ||
767a939888eSMinchan Kim !zram_test_flag(zram, index, ZRAM_IDLE)) {
768a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_UNDER_WB);
769a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_IDLE);
770a939888eSMinchan Kim goto next;
771a939888eSMinchan Kim }
772a939888eSMinchan Kim
773a939888eSMinchan Kim zram_free_page(zram, index);
774a939888eSMinchan Kim zram_clear_flag(zram, index, ZRAM_UNDER_WB);
775a939888eSMinchan Kim zram_set_flag(zram, index, ZRAM_WB);
776a939888eSMinchan Kim zram_set_element(zram, index, blk_idx);
777a939888eSMinchan Kim blk_idx = 0;
778a939888eSMinchan Kim atomic64_inc(&zram->stats.pages_stored);
7791d69a3f8SMinchan Kim spin_lock(&zram->wb_limit_lock);
7801d69a3f8SMinchan Kim if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
7811d69a3f8SMinchan Kim zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
7821d69a3f8SMinchan Kim spin_unlock(&zram->wb_limit_lock);
783a939888eSMinchan Kim next:
784a939888eSMinchan Kim zram_slot_unlock(zram, index);
785a939888eSMinchan Kim }
786a939888eSMinchan Kim
787a939888eSMinchan Kim if (blk_idx)
788a939888eSMinchan Kim free_block_bdev(zram, blk_idx);
789a939888eSMinchan Kim __free_page(page);
790a939888eSMinchan Kim release_init_lock:
791a939888eSMinchan Kim up_read(&zram->init_lock);
792a939888eSMinchan Kim
793a939888eSMinchan Kim return ret;
794a939888eSMinchan Kim }
795a939888eSMinchan Kim
7968e654f8fSMinchan Kim struct zram_work {
7978e654f8fSMinchan Kim struct work_struct work;
7988e654f8fSMinchan Kim struct zram *zram;
7998e654f8fSMinchan Kim unsigned long entry;
800fd45af53SChristoph Hellwig struct page *page;
8011e9460d1SChristoph Hellwig int error;
8028e654f8fSMinchan Kim };
8038e654f8fSMinchan Kim
zram_sync_read(struct work_struct * work)8048e654f8fSMinchan Kim static void zram_sync_read(struct work_struct *work)
8058e654f8fSMinchan Kim {
8068e654f8fSMinchan Kim struct zram_work *zw = container_of(work, struct zram_work, work);
8074e3c87b9SChristoph Hellwig struct bio_vec bv;
8084e3c87b9SChristoph Hellwig struct bio bio;
8098e654f8fSMinchan Kim
8104e3c87b9SChristoph Hellwig bio_init(&bio, zw->zram->bdev, &bv, 1, REQ_OP_READ);
8114e3c87b9SChristoph Hellwig bio.bi_iter.bi_sector = zw->entry * (PAGE_SIZE >> 9);
8124e3c87b9SChristoph Hellwig __bio_add_page(&bio, zw->page, PAGE_SIZE, 0);
8131e9460d1SChristoph Hellwig zw->error = submit_bio_wait(&bio);
8148e654f8fSMinchan Kim }
8158e654f8fSMinchan Kim
8168e654f8fSMinchan Kim /*
817c62b37d9SChristoph Hellwig * Block layer want one ->submit_bio to be active at a time, so if we use
818c62b37d9SChristoph Hellwig * chained IO with parent IO in same context, it's a deadlock. To avoid that,
819c62b37d9SChristoph Hellwig * use a worker thread context.
8208e654f8fSMinchan Kim */
read_from_bdev_sync(struct zram * zram,struct page * page,unsigned long entry)821fd45af53SChristoph Hellwig static int read_from_bdev_sync(struct zram *zram, struct page *page,
8224e3c87b9SChristoph Hellwig unsigned long entry)
8238e654f8fSMinchan Kim {
8248e654f8fSMinchan Kim struct zram_work work;
8258e654f8fSMinchan Kim
826fd45af53SChristoph Hellwig work.page = page;
8278e654f8fSMinchan Kim work.zram = zram;
8288e654f8fSMinchan Kim work.entry = entry;
8298e654f8fSMinchan Kim
8308e654f8fSMinchan Kim INIT_WORK_ONSTACK(&work.work, zram_sync_read);
8318e654f8fSMinchan Kim queue_work(system_unbound_wq, &work.work);
8328e654f8fSMinchan Kim flush_work(&work.work);
8338e654f8fSMinchan Kim destroy_work_on_stack(&work.work);
8348e654f8fSMinchan Kim
8351e9460d1SChristoph Hellwig return work.error;
8368e654f8fSMinchan Kim }
8378e654f8fSMinchan Kim
read_from_bdev(struct zram * zram,struct page * page,unsigned long entry,struct bio * parent)838fd45af53SChristoph Hellwig static int read_from_bdev(struct zram *zram, struct page *page,
8394e3c87b9SChristoph Hellwig unsigned long entry, struct bio *parent)
8408e654f8fSMinchan Kim {
84123eddf39SMinchan Kim atomic64_inc(&zram->stats.bd_reads);
8424e3c87b9SChristoph Hellwig if (!parent) {
843a70aae12SChristoph Hellwig if (WARN_ON_ONCE(!IS_ENABLED(ZRAM_PARTIAL_IO)))
844a70aae12SChristoph Hellwig return -EIO;
8454e3c87b9SChristoph Hellwig return read_from_bdev_sync(zram, page, entry);
846a70aae12SChristoph Hellwig }
8470cd97a03SChristoph Hellwig read_from_bdev_async(zram, page, entry, parent);
8481e9460d1SChristoph Hellwig return 0;
8498e654f8fSMinchan Kim }
850013bf95aSMinchan Kim #else
reset_bdev(struct zram * zram)851013bf95aSMinchan Kim static inline void reset_bdev(struct zram *zram) {};
read_from_bdev(struct zram * zram,struct page * page,unsigned long entry,struct bio * parent)852fd45af53SChristoph Hellwig static int read_from_bdev(struct zram *zram, struct page *page,
8534e3c87b9SChristoph Hellwig unsigned long entry, struct bio *parent)
8548e654f8fSMinchan Kim {
8558e654f8fSMinchan Kim return -EIO;
8568e654f8fSMinchan Kim }
8577e529283SMinchan Kim
free_block_bdev(struct zram * zram,unsigned long blk_idx)8587e529283SMinchan Kim static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
859013bf95aSMinchan Kim #endif
860013bf95aSMinchan Kim
861c0265342SMinchan Kim #ifdef CONFIG_ZRAM_MEMORY_TRACKING
862c0265342SMinchan Kim
863c0265342SMinchan Kim static struct dentry *zram_debugfs_root;
864c0265342SMinchan Kim
zram_debugfs_create(void)865c0265342SMinchan Kim static void zram_debugfs_create(void)
866c0265342SMinchan Kim {
867c0265342SMinchan Kim zram_debugfs_root = debugfs_create_dir("zram", NULL);
868c0265342SMinchan Kim }
869c0265342SMinchan Kim
zram_debugfs_destroy(void)870c0265342SMinchan Kim static void zram_debugfs_destroy(void)
871c0265342SMinchan Kim {
872c0265342SMinchan Kim debugfs_remove_recursive(zram_debugfs_root);
873c0265342SMinchan Kim }
874c0265342SMinchan Kim
read_block_state(struct file * file,char __user * buf,size_t count,loff_t * ppos)875c0265342SMinchan Kim static ssize_t read_block_state(struct file *file, char __user *buf,
876c0265342SMinchan Kim size_t count, loff_t *ppos)
877c0265342SMinchan Kim {
878c0265342SMinchan Kim char *kbuf;
879c0265342SMinchan Kim ssize_t index, written = 0;
880c0265342SMinchan Kim struct zram *zram = file->private_data;
881c0265342SMinchan Kim unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
882c0265342SMinchan Kim struct timespec64 ts;
883c0265342SMinchan Kim
884c0265342SMinchan Kim kbuf = kvmalloc(count, GFP_KERNEL);
885c0265342SMinchan Kim if (!kbuf)
886c0265342SMinchan Kim return -ENOMEM;
887c0265342SMinchan Kim
888c0265342SMinchan Kim down_read(&zram->init_lock);
889c0265342SMinchan Kim if (!init_done(zram)) {
890c0265342SMinchan Kim up_read(&zram->init_lock);
891c0265342SMinchan Kim kvfree(kbuf);
892c0265342SMinchan Kim return -EINVAL;
893c0265342SMinchan Kim }
894c0265342SMinchan Kim
895c0265342SMinchan Kim for (index = *ppos; index < nr_pages; index++) {
896c0265342SMinchan Kim int copied;
897c0265342SMinchan Kim
898c0265342SMinchan Kim zram_slot_lock(zram, index);
899c0265342SMinchan Kim if (!zram_allocated(zram, index))
900c0265342SMinchan Kim goto next;
901c0265342SMinchan Kim
902c0265342SMinchan Kim ts = ktime_to_timespec64(zram->table[index].ac_time);
903c0265342SMinchan Kim copied = snprintf(kbuf + written, count,
90477db7bb5SSergey Senozhatsky "%12zd %12lld.%06lu %c%c%c%c%c%c\n",
905c0265342SMinchan Kim index, (s64)ts.tv_sec,
906c0265342SMinchan Kim ts.tv_nsec / NSEC_PER_USEC,
907c0265342SMinchan Kim zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
908c0265342SMinchan Kim zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
909e82592c4SMinchan Kim zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
91060e9b39eSSergey Senozhatsky zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
91177db7bb5SSergey Senozhatsky zram_get_priority(zram, index) ? 'r' : '.',
91277db7bb5SSergey Senozhatsky zram_test_flag(zram, index,
91377db7bb5SSergey Senozhatsky ZRAM_INCOMPRESSIBLE) ? 'n' : '.');
914c0265342SMinchan Kim
915a88e03cfSDan Carpenter if (count <= copied) {
916c0265342SMinchan Kim zram_slot_unlock(zram, index);
917c0265342SMinchan Kim break;
918c0265342SMinchan Kim }
919c0265342SMinchan Kim written += copied;
920c0265342SMinchan Kim count -= copied;
921c0265342SMinchan Kim next:
922c0265342SMinchan Kim zram_slot_unlock(zram, index);
923c0265342SMinchan Kim *ppos += 1;
924c0265342SMinchan Kim }
925c0265342SMinchan Kim
926c0265342SMinchan Kim up_read(&zram->init_lock);
927c0265342SMinchan Kim if (copy_to_user(buf, kbuf, written))
928c0265342SMinchan Kim written = -EFAULT;
929c0265342SMinchan Kim kvfree(kbuf);
930c0265342SMinchan Kim
931c0265342SMinchan Kim return written;
932c0265342SMinchan Kim }
933c0265342SMinchan Kim
934c0265342SMinchan Kim static const struct file_operations proc_zram_block_state_op = {
935c0265342SMinchan Kim .open = simple_open,
936c0265342SMinchan Kim .read = read_block_state,
937c0265342SMinchan Kim .llseek = default_llseek,
938c0265342SMinchan Kim };
939c0265342SMinchan Kim
zram_debugfs_register(struct zram * zram)940c0265342SMinchan Kim static void zram_debugfs_register(struct zram *zram)
941c0265342SMinchan Kim {
942c0265342SMinchan Kim if (!zram_debugfs_root)
943c0265342SMinchan Kim return;
944c0265342SMinchan Kim
945c0265342SMinchan Kim zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
946c0265342SMinchan Kim zram_debugfs_root);
947c0265342SMinchan Kim debugfs_create_file("block_state", 0400, zram->debugfs_dir,
948c0265342SMinchan Kim zram, &proc_zram_block_state_op);
949c0265342SMinchan Kim }
950c0265342SMinchan Kim
zram_debugfs_unregister(struct zram * zram)951c0265342SMinchan Kim static void zram_debugfs_unregister(struct zram *zram)
952c0265342SMinchan Kim {
953c0265342SMinchan Kim debugfs_remove_recursive(zram->debugfs_dir);
954c0265342SMinchan Kim }
955c0265342SMinchan Kim #else
zram_debugfs_create(void)956c0265342SMinchan Kim static void zram_debugfs_create(void) {};
zram_debugfs_destroy(void)957c0265342SMinchan Kim static void zram_debugfs_destroy(void) {};
zram_debugfs_register(struct zram * zram)958c0265342SMinchan Kim static void zram_debugfs_register(struct zram *zram) {};
zram_debugfs_unregister(struct zram * zram)959c0265342SMinchan Kim static void zram_debugfs_unregister(struct zram *zram) {};
960c0265342SMinchan Kim #endif
961013bf95aSMinchan Kim
96243209ea2SSergey Senozhatsky /*
96343209ea2SSergey Senozhatsky * We switched to per-cpu streams and this attr is not needed anymore.
96443209ea2SSergey Senozhatsky * However, we will keep it around for some time, because:
96543209ea2SSergey Senozhatsky * a) we may revert per-cpu streams in the future
96643209ea2SSergey Senozhatsky * b) it's visible to user space and we need to follow our 2 years
96743209ea2SSergey Senozhatsky * retirement rule; but we already have a number of 'soon to be
96843209ea2SSergey Senozhatsky * altered' attrs, so max_comp_streams need to wait for the next
96943209ea2SSergey Senozhatsky * layoff cycle.
97043209ea2SSergey Senozhatsky */
max_comp_streams_show(struct device * dev,struct device_attribute * attr,char * buf)971522698d7SSergey Senozhatsky static ssize_t max_comp_streams_show(struct device *dev,
972522698d7SSergey Senozhatsky struct device_attribute *attr, char *buf)
973522698d7SSergey Senozhatsky {
97443209ea2SSergey Senozhatsky return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
975522698d7SSergey Senozhatsky }
976522698d7SSergey Senozhatsky
max_comp_streams_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)977beca3ec7SSergey Senozhatsky static ssize_t max_comp_streams_store(struct device *dev,
978beca3ec7SSergey Senozhatsky struct device_attribute *attr, const char *buf, size_t len)
979beca3ec7SSergey Senozhatsky {
98043209ea2SSergey Senozhatsky return len;
981beca3ec7SSergey Senozhatsky }
982beca3ec7SSergey Senozhatsky
comp_algorithm_set(struct zram * zram,u32 prio,const char * alg)983001d9273SSergey Senozhatsky static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
984e46b8a03SSergey Senozhatsky {
985001d9273SSergey Senozhatsky /* Do not free statically defined compression algorithms */
986001d9273SSergey Senozhatsky if (zram->comp_algs[prio] != default_compressor)
987001d9273SSergey Senozhatsky kfree(zram->comp_algs[prio]);
988001d9273SSergey Senozhatsky
989001d9273SSergey Senozhatsky zram->comp_algs[prio] = alg;
990001d9273SSergey Senozhatsky }
991001d9273SSergey Senozhatsky
__comp_algorithm_show(struct zram * zram,u32 prio,char * buf)992001d9273SSergey Senozhatsky static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *buf)
993001d9273SSergey Senozhatsky {
994001d9273SSergey Senozhatsky ssize_t sz;
995e46b8a03SSergey Senozhatsky
996e46b8a03SSergey Senozhatsky down_read(&zram->init_lock);
997001d9273SSergey Senozhatsky sz = zcomp_available_show(zram->comp_algs[prio], buf);
998e46b8a03SSergey Senozhatsky up_read(&zram->init_lock);
999e46b8a03SSergey Senozhatsky
1000e46b8a03SSergey Senozhatsky return sz;
1001e46b8a03SSergey Senozhatsky }
1002e46b8a03SSergey Senozhatsky
__comp_algorithm_store(struct zram * zram,u32 prio,const char * buf)1003001d9273SSergey Senozhatsky static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
10047ac07a26SSergey Senozhatsky {
10057ac07a26SSergey Senozhatsky char *compressor;
10064bbacd51SSergey Senozhatsky size_t sz;
10074bbacd51SSergey Senozhatsky
10087ac07a26SSergey Senozhatsky sz = strlen(buf);
10097ac07a26SSergey Senozhatsky if (sz >= CRYPTO_MAX_ALG_NAME)
10107ac07a26SSergey Senozhatsky return -E2BIG;
10117ac07a26SSergey Senozhatsky
10127ac07a26SSergey Senozhatsky compressor = kstrdup(buf, GFP_KERNEL);
10137ac07a26SSergey Senozhatsky if (!compressor)
10147ac07a26SSergey Senozhatsky return -ENOMEM;
10157ac07a26SSergey Senozhatsky
1016415403beSSergey Senozhatsky /* ignore trailing newline */
1017415403beSSergey Senozhatsky if (sz > 0 && compressor[sz - 1] == '\n')
1018415403beSSergey Senozhatsky compressor[sz - 1] = 0x00;
1019415403beSSergey Senozhatsky
10207ac07a26SSergey Senozhatsky if (!zcomp_available_algorithm(compressor)) {
10217ac07a26SSergey Senozhatsky kfree(compressor);
10221d5b43bfSLuis Henriques return -EINVAL;
10237ac07a26SSergey Senozhatsky }
10241d5b43bfSLuis Henriques
1025e46b8a03SSergey Senozhatsky down_write(&zram->init_lock);
1026e46b8a03SSergey Senozhatsky if (init_done(zram)) {
1027e46b8a03SSergey Senozhatsky up_write(&zram->init_lock);
10287ac07a26SSergey Senozhatsky kfree(compressor);
1029e46b8a03SSergey Senozhatsky pr_info("Can't change algorithm for initialized device\n");
1030e46b8a03SSergey Senozhatsky return -EBUSY;
1031e46b8a03SSergey Senozhatsky }
10324bbacd51SSergey Senozhatsky
1033001d9273SSergey Senozhatsky comp_algorithm_set(zram, prio, compressor);
1034e46b8a03SSergey Senozhatsky up_write(&zram->init_lock);
1035001d9273SSergey Senozhatsky return 0;
1036e46b8a03SSergey Senozhatsky }
1037e46b8a03SSergey Senozhatsky
comp_algorithm_show(struct device * dev,struct device_attribute * attr,char * buf)1038001d9273SSergey Senozhatsky static ssize_t comp_algorithm_show(struct device *dev,
1039001d9273SSergey Senozhatsky struct device_attribute *attr,
1040001d9273SSergey Senozhatsky char *buf)
1041001d9273SSergey Senozhatsky {
1042001d9273SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1043001d9273SSergey Senozhatsky
1044001d9273SSergey Senozhatsky return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf);
1045001d9273SSergey Senozhatsky }
1046001d9273SSergey Senozhatsky
comp_algorithm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1047001d9273SSergey Senozhatsky static ssize_t comp_algorithm_store(struct device *dev,
1048001d9273SSergey Senozhatsky struct device_attribute *attr,
1049001d9273SSergey Senozhatsky const char *buf,
1050001d9273SSergey Senozhatsky size_t len)
1051001d9273SSergey Senozhatsky {
1052001d9273SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1053001d9273SSergey Senozhatsky int ret;
1054001d9273SSergey Senozhatsky
1055001d9273SSergey Senozhatsky ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf);
1056001d9273SSergey Senozhatsky return ret ? ret : len;
1057001d9273SSergey Senozhatsky }
1058001d9273SSergey Senozhatsky
1059001d9273SSergey Senozhatsky #ifdef CONFIG_ZRAM_MULTI_COMP
recomp_algorithm_show(struct device * dev,struct device_attribute * attr,char * buf)1060001d9273SSergey Senozhatsky static ssize_t recomp_algorithm_show(struct device *dev,
1061001d9273SSergey Senozhatsky struct device_attribute *attr,
1062001d9273SSergey Senozhatsky char *buf)
1063001d9273SSergey Senozhatsky {
1064001d9273SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1065001d9273SSergey Senozhatsky ssize_t sz = 0;
1066001d9273SSergey Senozhatsky u32 prio;
1067001d9273SSergey Senozhatsky
1068001d9273SSergey Senozhatsky for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
1069001d9273SSergey Senozhatsky if (!zram->comp_algs[prio])
1070001d9273SSergey Senozhatsky continue;
1071001d9273SSergey Senozhatsky
1072001d9273SSergey Senozhatsky sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, "#%d: ", prio);
1073001d9273SSergey Senozhatsky sz += __comp_algorithm_show(zram, prio, buf + sz);
1074001d9273SSergey Senozhatsky }
1075001d9273SSergey Senozhatsky
1076001d9273SSergey Senozhatsky return sz;
1077001d9273SSergey Senozhatsky }
1078001d9273SSergey Senozhatsky
recomp_algorithm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1079001d9273SSergey Senozhatsky static ssize_t recomp_algorithm_store(struct device *dev,
1080001d9273SSergey Senozhatsky struct device_attribute *attr,
1081001d9273SSergey Senozhatsky const char *buf,
1082001d9273SSergey Senozhatsky size_t len)
1083001d9273SSergey Senozhatsky {
1084001d9273SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1085001d9273SSergey Senozhatsky int prio = ZRAM_SECONDARY_COMP;
1086001d9273SSergey Senozhatsky char *args, *param, *val;
1087001d9273SSergey Senozhatsky char *alg = NULL;
1088001d9273SSergey Senozhatsky int ret;
1089001d9273SSergey Senozhatsky
1090001d9273SSergey Senozhatsky args = skip_spaces(buf);
1091001d9273SSergey Senozhatsky while (*args) {
1092001d9273SSergey Senozhatsky args = next_arg(args, ¶m, &val);
1093001d9273SSergey Senozhatsky
1094df32de14SSergey Senozhatsky if (!val || !*val)
1095001d9273SSergey Senozhatsky return -EINVAL;
1096001d9273SSergey Senozhatsky
1097001d9273SSergey Senozhatsky if (!strcmp(param, "algo")) {
1098001d9273SSergey Senozhatsky alg = val;
1099001d9273SSergey Senozhatsky continue;
1100001d9273SSergey Senozhatsky }
1101001d9273SSergey Senozhatsky
1102001d9273SSergey Senozhatsky if (!strcmp(param, "priority")) {
1103001d9273SSergey Senozhatsky ret = kstrtoint(val, 10, &prio);
1104001d9273SSergey Senozhatsky if (ret)
1105001d9273SSergey Senozhatsky return ret;
1106001d9273SSergey Senozhatsky continue;
1107001d9273SSergey Senozhatsky }
1108001d9273SSergey Senozhatsky }
1109001d9273SSergey Senozhatsky
1110001d9273SSergey Senozhatsky if (!alg)
1111001d9273SSergey Senozhatsky return -EINVAL;
1112001d9273SSergey Senozhatsky
1113001d9273SSergey Senozhatsky if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS)
1114001d9273SSergey Senozhatsky return -EINVAL;
1115001d9273SSergey Senozhatsky
1116001d9273SSergey Senozhatsky ret = __comp_algorithm_store(zram, prio, alg);
1117001d9273SSergey Senozhatsky return ret ? ret : len;
1118001d9273SSergey Senozhatsky }
1119001d9273SSergey Senozhatsky #endif
1120001d9273SSergey Senozhatsky
compact_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)1121522698d7SSergey Senozhatsky static ssize_t compact_store(struct device *dev,
1122522698d7SSergey Senozhatsky struct device_attribute *attr, const char *buf, size_t len)
1123cd67e10aSMinchan Kim {
1124522698d7SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1125522698d7SSergey Senozhatsky
1126522698d7SSergey Senozhatsky down_read(&zram->init_lock);
1127522698d7SSergey Senozhatsky if (!init_done(zram)) {
1128522698d7SSergey Senozhatsky up_read(&zram->init_lock);
1129522698d7SSergey Senozhatsky return -EINVAL;
1130cd67e10aSMinchan Kim }
1131cd67e10aSMinchan Kim
1132beb6602cSMinchan Kim zs_compact(zram->mem_pool);
1133522698d7SSergey Senozhatsky up_read(&zram->init_lock);
1134522698d7SSergey Senozhatsky
1135522698d7SSergey Senozhatsky return len;
1136cd67e10aSMinchan Kim }
1137cd67e10aSMinchan Kim
io_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1138522698d7SSergey Senozhatsky static ssize_t io_stat_show(struct device *dev,
1139522698d7SSergey Senozhatsky struct device_attribute *attr, char *buf)
1140cd67e10aSMinchan Kim {
1141522698d7SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1142522698d7SSergey Senozhatsky ssize_t ret;
1143522698d7SSergey Senozhatsky
1144522698d7SSergey Senozhatsky down_read(&zram->init_lock);
1145522698d7SSergey Senozhatsky ret = scnprintf(buf, PAGE_SIZE,
11469fe95babSChristoph Hellwig "%8llu %8llu 0 %8llu\n",
1147522698d7SSergey Senozhatsky (u64)atomic64_read(&zram->stats.failed_reads),
1148522698d7SSergey Senozhatsky (u64)atomic64_read(&zram->stats.failed_writes),
1149522698d7SSergey Senozhatsky (u64)atomic64_read(&zram->stats.notify_free));
1150522698d7SSergey Senozhatsky up_read(&zram->init_lock);
1151522698d7SSergey Senozhatsky
1152522698d7SSergey Senozhatsky return ret;
1153d2d5e762SWeijie Yang }
1154d2d5e762SWeijie Yang
mm_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1155522698d7SSergey Senozhatsky static ssize_t mm_stat_show(struct device *dev,
1156522698d7SSergey Senozhatsky struct device_attribute *attr, char *buf)
1157d2d5e762SWeijie Yang {
1158522698d7SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
11597d3f3938SSergey Senozhatsky struct zs_pool_stats pool_stats;
1160522698d7SSergey Senozhatsky u64 orig_size, mem_used = 0;
1161522698d7SSergey Senozhatsky long max_used;
1162522698d7SSergey Senozhatsky ssize_t ret;
1163522698d7SSergey Senozhatsky
11647d3f3938SSergey Senozhatsky memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
11657d3f3938SSergey Senozhatsky
1166522698d7SSergey Senozhatsky down_read(&zram->init_lock);
11677d3f3938SSergey Senozhatsky if (init_done(zram)) {
1168beb6602cSMinchan Kim mem_used = zs_get_total_pages(zram->mem_pool);
1169beb6602cSMinchan Kim zs_pool_stats(zram->mem_pool, &pool_stats);
11707d3f3938SSergey Senozhatsky }
1171522698d7SSergey Senozhatsky
1172522698d7SSergey Senozhatsky orig_size = atomic64_read(&zram->stats.pages_stored);
1173522698d7SSergey Senozhatsky max_used = atomic_long_read(&zram->stats.max_used_pages);
1174522698d7SSergey Senozhatsky
1175522698d7SSergey Senozhatsky ret = scnprintf(buf, PAGE_SIZE,
1176194e28daSMinchan Kim "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
1177522698d7SSergey Senozhatsky orig_size << PAGE_SHIFT,
1178522698d7SSergey Senozhatsky (u64)atomic64_read(&zram->stats.compr_data_size),
1179522698d7SSergey Senozhatsky mem_used << PAGE_SHIFT,
1180522698d7SSergey Senozhatsky zram->limit_pages << PAGE_SHIFT,
1181522698d7SSergey Senozhatsky max_used << PAGE_SHIFT,
11828e19d540Szhouxianrong (u64)atomic64_read(&zram->stats.same_pages),
118323959281SRokudo Yan atomic_long_read(&pool_stats.pages_compacted),
1184194e28daSMinchan Kim (u64)atomic64_read(&zram->stats.huge_pages),
1185194e28daSMinchan Kim (u64)atomic64_read(&zram->stats.huge_pages_since));
1186522698d7SSergey Senozhatsky up_read(&zram->init_lock);
1187522698d7SSergey Senozhatsky
1188522698d7SSergey Senozhatsky return ret;
1189d2d5e762SWeijie Yang }
1190d2d5e762SWeijie Yang
119123eddf39SMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
1192bb416d18SMinchan Kim #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
bd_stat_show(struct device * dev,struct device_attribute * attr,char * buf)119323eddf39SMinchan Kim static ssize_t bd_stat_show(struct device *dev,
119423eddf39SMinchan Kim struct device_attribute *attr, char *buf)
119523eddf39SMinchan Kim {
119623eddf39SMinchan Kim struct zram *zram = dev_to_zram(dev);
119723eddf39SMinchan Kim ssize_t ret;
119823eddf39SMinchan Kim
119923eddf39SMinchan Kim down_read(&zram->init_lock);
120023eddf39SMinchan Kim ret = scnprintf(buf, PAGE_SIZE,
120123eddf39SMinchan Kim "%8llu %8llu %8llu\n",
1202bb416d18SMinchan Kim FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
1203bb416d18SMinchan Kim FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
1204bb416d18SMinchan Kim FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
120523eddf39SMinchan Kim up_read(&zram->init_lock);
120623eddf39SMinchan Kim
120723eddf39SMinchan Kim return ret;
120823eddf39SMinchan Kim }
120923eddf39SMinchan Kim #endif
121023eddf39SMinchan Kim
debug_stat_show(struct device * dev,struct device_attribute * attr,char * buf)1211623e47fcSSergey Senozhatsky static ssize_t debug_stat_show(struct device *dev,
1212623e47fcSSergey Senozhatsky struct device_attribute *attr, char *buf)
1213623e47fcSSergey Senozhatsky {
121437887783SJiri Slaby int version = 1;
1215623e47fcSSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
1216623e47fcSSergey Senozhatsky ssize_t ret;
1217623e47fcSSergey Senozhatsky
1218623e47fcSSergey Senozhatsky down_read(&zram->init_lock);
1219623e47fcSSergey Senozhatsky ret = scnprintf(buf, PAGE_SIZE,
122037887783SJiri Slaby "version: %d\n%8llu %8llu\n",
1221623e47fcSSergey Senozhatsky version,
122237887783SJiri Slaby (u64)atomic64_read(&zram->stats.writestall),
12233c9959e0SMinchan Kim (u64)atomic64_read(&zram->stats.miss_free));
1224623e47fcSSergey Senozhatsky up_read(&zram->init_lock);
1225623e47fcSSergey Senozhatsky
1226623e47fcSSergey Senozhatsky return ret;
1227623e47fcSSergey Senozhatsky }
1228623e47fcSSergey Senozhatsky
1229522698d7SSergey Senozhatsky static DEVICE_ATTR_RO(io_stat);
1230522698d7SSergey Senozhatsky static DEVICE_ATTR_RO(mm_stat);
123123eddf39SMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
123223eddf39SMinchan Kim static DEVICE_ATTR_RO(bd_stat);
123323eddf39SMinchan Kim #endif
1234623e47fcSSergey Senozhatsky static DEVICE_ATTR_RO(debug_stat);
1235d2d5e762SWeijie Yang
zram_meta_free(struct zram * zram,u64 disksize)1236beb6602cSMinchan Kim static void zram_meta_free(struct zram *zram, u64 disksize)
1237cd67e10aSMinchan Kim {
12381fec1172SGanesh Mahendran size_t num_pages = disksize >> PAGE_SHIFT;
12391fec1172SGanesh Mahendran size_t index;
12401fec1172SGanesh Mahendran
12410b5b0b65SKairui Song if (!zram->table)
12420b5b0b65SKairui Song return;
12430b5b0b65SKairui Song
12441fec1172SGanesh Mahendran /* Free all pages that are still in this zram device */
1245302128dcSMinchan Kim for (index = 0; index < num_pages; index++)
1246302128dcSMinchan Kim zram_free_page(zram, index);
12471fec1172SGanesh Mahendran
1248beb6602cSMinchan Kim zs_destroy_pool(zram->mem_pool);
1249beb6602cSMinchan Kim vfree(zram->table);
12500b5b0b65SKairui Song zram->table = NULL;
1251cd67e10aSMinchan Kim }
1252cd67e10aSMinchan Kim
zram_meta_alloc(struct zram * zram,u64 disksize)1253beb6602cSMinchan Kim static bool zram_meta_alloc(struct zram *zram, u64 disksize)
1254cd67e10aSMinchan Kim {
1255cd67e10aSMinchan Kim size_t num_pages;
1256cd67e10aSMinchan Kim
1257cd67e10aSMinchan Kim num_pages = disksize >> PAGE_SHIFT;
1258fad953ceSKees Cook zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
1259beb6602cSMinchan Kim if (!zram->table)
1260beb6602cSMinchan Kim return false;
1261beb6602cSMinchan Kim
1262beb6602cSMinchan Kim zram->mem_pool = zs_create_pool(zram->disk->disk_name);
1263beb6602cSMinchan Kim if (!zram->mem_pool) {
1264beb6602cSMinchan Kim vfree(zram->table);
1265571d3f60SKairui Song zram->table = NULL;
1266beb6602cSMinchan Kim return false;
1267cd67e10aSMinchan Kim }
1268cd67e10aSMinchan Kim
126960f5921aSSergey Senozhatsky if (!huge_class_size)
127060f5921aSSergey Senozhatsky huge_class_size = zs_huge_class_size(zram->mem_pool);
1271beb6602cSMinchan Kim return true;
1272cd67e10aSMinchan Kim }
1273cd67e10aSMinchan Kim
1274d2d5e762SWeijie Yang /*
1275d2d5e762SWeijie Yang * To protect concurrent access to the same index entry,
1276d2d5e762SWeijie Yang * caller should hold this table index entry's bit_spinlock to
1277d2d5e762SWeijie Yang * indicate this index entry is accessing.
1278d2d5e762SWeijie Yang */
zram_free_page(struct zram * zram,size_t index)1279cd67e10aSMinchan Kim static void zram_free_page(struct zram *zram, size_t index)
1280cd67e10aSMinchan Kim {
1281db8ffbd4SMinchan Kim unsigned long handle;
1282db8ffbd4SMinchan Kim
1283b7c3fd65SSergey Senozhatsky #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
12847e529283SMinchan Kim zram->table[index].ac_time = 0;
12857e529283SMinchan Kim #endif
1286e82592c4SMinchan Kim if (zram_test_flag(zram, index, ZRAM_IDLE))
1287e82592c4SMinchan Kim zram_clear_flag(zram, index, ZRAM_IDLE);
1288e82592c4SMinchan Kim
128989e85bceSMinchan Kim if (zram_test_flag(zram, index, ZRAM_HUGE)) {
129089e85bceSMinchan Kim zram_clear_flag(zram, index, ZRAM_HUGE);
129189e85bceSMinchan Kim atomic64_dec(&zram->stats.huge_pages);
129289e85bceSMinchan Kim }
129389e85bceSMinchan Kim
129484b33bf7SSergey Senozhatsky if (zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
129584b33bf7SSergey Senozhatsky zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE);
129684b33bf7SSergey Senozhatsky
129784b33bf7SSergey Senozhatsky zram_set_priority(zram, index, 0);
129884b33bf7SSergey Senozhatsky
12997e529283SMinchan Kim if (zram_test_flag(zram, index, ZRAM_WB)) {
13007e529283SMinchan Kim zram_clear_flag(zram, index, ZRAM_WB);
13017e529283SMinchan Kim free_block_bdev(zram, zram_get_element(zram, index));
13027e529283SMinchan Kim goto out;
1303db8ffbd4SMinchan Kim }
1304cd67e10aSMinchan Kim
1305cd67e10aSMinchan Kim /*
13068e19d540Szhouxianrong * No memory is allocated for same element filled pages.
13078e19d540Szhouxianrong * Simply clear same page flag.
1308cd67e10aSMinchan Kim */
1309beb6602cSMinchan Kim if (zram_test_flag(zram, index, ZRAM_SAME)) {
1310beb6602cSMinchan Kim zram_clear_flag(zram, index, ZRAM_SAME);
13118e19d540Szhouxianrong atomic64_dec(&zram->stats.same_pages);
13127e529283SMinchan Kim goto out;
1313cd67e10aSMinchan Kim }
1314cd67e10aSMinchan Kim
1315db8ffbd4SMinchan Kim handle = zram_get_handle(zram, index);
13168e19d540Szhouxianrong if (!handle)
13178e19d540Szhouxianrong return;
13188e19d540Szhouxianrong
1319beb6602cSMinchan Kim zs_free(zram->mem_pool, handle);
1320cd67e10aSMinchan Kim
1321beb6602cSMinchan Kim atomic64_sub(zram_get_obj_size(zram, index),
1322d2d5e762SWeijie Yang &zram->stats.compr_data_size);
13237e529283SMinchan Kim out:
132490a7806eSSergey Senozhatsky atomic64_dec(&zram->stats.pages_stored);
1325643ae61dSMinchan Kim zram_set_handle(zram, index, 0);
1326beb6602cSMinchan Kim zram_set_obj_size(zram, index, 0);
1327a939888eSMinchan Kim WARN_ON_ONCE(zram->table[index].flags &
1328a939888eSMinchan Kim ~(1UL << ZRAM_LOCK | 1UL << ZRAM_UNDER_WB));
1329cd67e10aSMinchan Kim }
1330cd67e10aSMinchan Kim
13315561347aSSergey Senozhatsky /*
13325561347aSSergey Senozhatsky * Reads (decompresses if needed) a page from zspool (zsmalloc).
13335561347aSSergey Senozhatsky * Corresponding ZRAM slot should be locked.
13345561347aSSergey Senozhatsky */
zram_read_from_zspool(struct zram * zram,struct page * page,u32 index)13355561347aSSergey Senozhatsky static int zram_read_from_zspool(struct zram *zram, struct page *page,
13365561347aSSergey Senozhatsky u32 index)
1337cd67e10aSMinchan Kim {
13380669d2b2SPeter Zijlstra struct zcomp_strm *zstrm;
133992967471SMinchan Kim unsigned long handle;
1340ebaf9ab5SSergey Senozhatsky unsigned int size;
13411f7319c7SMinchan Kim void *src, *dst;
134284b33bf7SSergey Senozhatsky u32 prio;
13430669d2b2SPeter Zijlstra int ret;
13441f7319c7SMinchan Kim
1345643ae61dSMinchan Kim handle = zram_get_handle(zram, index);
1346ae94264eSMinchan Kim if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
1347ae94264eSMinchan Kim unsigned long value;
1348ae94264eSMinchan Kim void *mem;
1349ae94264eSMinchan Kim
1350ae94264eSMinchan Kim value = handle ? zram_get_element(zram, index) : 0;
1351ae94264eSMinchan Kim mem = kmap_atomic(page);
1352ae94264eSMinchan Kim zram_fill_page(mem, PAGE_SIZE, value);
1353ae94264eSMinchan Kim kunmap_atomic(mem);
1354ae94264eSMinchan Kim return 0;
1355ae94264eSMinchan Kim }
1356ae94264eSMinchan Kim
1357beb6602cSMinchan Kim size = zram_get_obj_size(zram, index);
1358cd67e10aSMinchan Kim
135984b33bf7SSergey Senozhatsky if (size != PAGE_SIZE) {
136084b33bf7SSergey Senozhatsky prio = zram_get_priority(zram, index);
136184b33bf7SSergey Senozhatsky zstrm = zcomp_stream_get(zram->comps[prio]);
136284b33bf7SSergey Senozhatsky }
13630669d2b2SPeter Zijlstra
1364beb6602cSMinchan Kim src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
1365ebaf9ab5SSergey Senozhatsky if (size == PAGE_SIZE) {
13661f7319c7SMinchan Kim dst = kmap_atomic(page);
13671f7319c7SMinchan Kim memcpy(dst, src, PAGE_SIZE);
13681f7319c7SMinchan Kim kunmap_atomic(dst);
13691f7319c7SMinchan Kim ret = 0;
1370ebaf9ab5SSergey Senozhatsky } else {
13711f7319c7SMinchan Kim dst = kmap_atomic(page);
13721f7319c7SMinchan Kim ret = zcomp_decompress(zstrm, src, size, dst);
13731f7319c7SMinchan Kim kunmap_atomic(dst);
137484b33bf7SSergey Senozhatsky zcomp_stream_put(zram->comps[prio]);
1375ebaf9ab5SSergey Senozhatsky }
1376beb6602cSMinchan Kim zs_unmap_object(zram->mem_pool, handle);
13775561347aSSergey Senozhatsky return ret;
13785561347aSSergey Senozhatsky }
13795561347aSSergey Senozhatsky
zram_read_page(struct zram * zram,struct page * page,u32 index,struct bio * parent)1380ffb0a9e6SChristoph Hellwig static int zram_read_page(struct zram *zram, struct page *page, u32 index,
13814e3c87b9SChristoph Hellwig struct bio *parent)
13825561347aSSergey Senozhatsky {
13835561347aSSergey Senozhatsky int ret;
13845561347aSSergey Senozhatsky
13855561347aSSergey Senozhatsky zram_slot_lock(zram, index);
13865561347aSSergey Senozhatsky if (!zram_test_flag(zram, index, ZRAM_WB)) {
13875561347aSSergey Senozhatsky /* Slot should be locked through out the function call */
13885561347aSSergey Senozhatsky ret = zram_read_from_zspool(zram, page, index);
13895561347aSSergey Senozhatsky zram_slot_unlock(zram, index);
13905561347aSSergey Senozhatsky } else {
1391fd45af53SChristoph Hellwig /*
1392fd45af53SChristoph Hellwig * The slot should be unlocked before reading from the backing
1393fd45af53SChristoph Hellwig * device.
1394fd45af53SChristoph Hellwig */
139586c49814SMinchan Kim zram_slot_unlock(zram, index);
1396cd67e10aSMinchan Kim
1397fd45af53SChristoph Hellwig ret = read_from_bdev(zram, page, zram_get_element(zram, index),
13984e3c87b9SChristoph Hellwig parent);
13995561347aSSergey Senozhatsky }
14005561347aSSergey Senozhatsky
1401cd67e10aSMinchan Kim /* Should NEVER happen. Return bio error if it does. */
14025561347aSSergey Senozhatsky if (WARN_ON(ret < 0))
1403cd67e10aSMinchan Kim pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
1404cd67e10aSMinchan Kim
14051f7319c7SMinchan Kim return ret;
1406cd67e10aSMinchan Kim }
1407cd67e10aSMinchan Kim
1408889ae916SChristoph Hellwig /*
1409889ae916SChristoph Hellwig * Use a temporary buffer to decompress the page, as the decompressor
1410889ae916SChristoph Hellwig * always expects a full page for the output.
1411889ae916SChristoph Hellwig */
zram_bvec_read_partial(struct zram * zram,struct bio_vec * bvec,u32 index,int offset)1412889ae916SChristoph Hellwig static int zram_bvec_read_partial(struct zram *zram, struct bio_vec *bvec,
14134e3c87b9SChristoph Hellwig u32 index, int offset)
1414cd67e10aSMinchan Kim {
1415889ae916SChristoph Hellwig struct page *page = alloc_page(GFP_NOIO);
1416cd67e10aSMinchan Kim int ret;
14171f7319c7SMinchan Kim
14181f7319c7SMinchan Kim if (!page)
14191f7319c7SMinchan Kim return -ENOMEM;
14204e3c87b9SChristoph Hellwig ret = zram_read_page(zram, page, index, NULL);
1421889ae916SChristoph Hellwig if (likely(!ret))
1422889ae916SChristoph Hellwig memcpy_to_bvec(bvec, page_address(page) + offset);
14231f7319c7SMinchan Kim __free_page(page);
1424cd67e10aSMinchan Kim return ret;
1425cd67e10aSMinchan Kim }
1426cd67e10aSMinchan Kim
zram_bvec_read(struct zram * zram,struct bio_vec * bvec,u32 index,int offset,struct bio * bio)1427cd67e10aSMinchan Kim static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
1428cd67e10aSMinchan Kim u32 index, int offset, struct bio *bio)
1429cd67e10aSMinchan Kim {
1430f575a5adSChristoph Hellwig if (is_partial_io(bvec))
14314e3c87b9SChristoph Hellwig return zram_bvec_read_partial(zram, bvec, index, offset);
14324e3c87b9SChristoph Hellwig return zram_read_page(zram, bvec->bv_page, index, bio);
1433cd67e10aSMinchan Kim }
1434cd67e10aSMinchan Kim
zram_write_page(struct zram * zram,struct page * page,u32 index)14356aa4b839SChristoph Hellwig static int zram_write_page(struct zram *zram, struct page *page, u32 index)
1436cd67e10aSMinchan Kim {
1437ae85a807SMinchan Kim int ret = 0;
1438461a8eeeSMinchan Kim unsigned long alloced_pages;
143937887783SJiri Slaby unsigned long handle = -ENOMEM;
144097ec7c8bSMinchan Kim unsigned int comp_len = 0;
144197ec7c8bSMinchan Kim void *src, *dst, *mem;
144297ec7c8bSMinchan Kim struct zcomp_strm *zstrm;
144397ec7c8bSMinchan Kim unsigned long element = 0;
144497ec7c8bSMinchan Kim enum zram_pageflags flags = 0;
144597ec7c8bSMinchan Kim
144697ec7c8bSMinchan Kim mem = kmap_atomic(page);
144797ec7c8bSMinchan Kim if (page_same_filled(mem, &element)) {
144897ec7c8bSMinchan Kim kunmap_atomic(mem);
144997ec7c8bSMinchan Kim /* Free memory associated with this sector now. */
145097ec7c8bSMinchan Kim flags = ZRAM_SAME;
145197ec7c8bSMinchan Kim atomic64_inc(&zram->stats.same_pages);
145297ec7c8bSMinchan Kim goto out;
145397ec7c8bSMinchan Kim }
145497ec7c8bSMinchan Kim kunmap_atomic(mem);
1455cd67e10aSMinchan Kim
145637887783SJiri Slaby compress_again:
14577ac07a26SSergey Senozhatsky zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
14581f7319c7SMinchan Kim src = kmap_atomic(page);
145997ec7c8bSMinchan Kim ret = zcomp_compress(zstrm, src, &comp_len);
14601f7319c7SMinchan Kim kunmap_atomic(src);
1461cd67e10aSMinchan Kim
1462b7ca232eSSergey Senozhatsky if (unlikely(ret)) {
14637ac07a26SSergey Senozhatsky zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1464cd67e10aSMinchan Kim pr_err("Compression failed! err=%d\n", ret);
146537887783SJiri Slaby zs_free(zram->mem_pool, handle);
14661f7319c7SMinchan Kim return ret;
1467cd67e10aSMinchan Kim }
1468da9556a2SSergey Senozhatsky
1469a939888eSMinchan Kim if (comp_len >= huge_class_size)
147089e85bceSMinchan Kim comp_len = PAGE_SIZE;
147137887783SJiri Slaby /*
147237887783SJiri Slaby * handle allocation has 2 paths:
147337887783SJiri Slaby * a) fast path is executed with preemption disabled (for
147437887783SJiri Slaby * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
147537887783SJiri Slaby * since we can't sleep;
147637887783SJiri Slaby * b) slow path enables preemption and attempts to allocate
147737887783SJiri Slaby * the page with __GFP_DIRECT_RECLAIM bit set. we have to
147837887783SJiri Slaby * put per-cpu compression stream and, thus, to re-do
147937887783SJiri Slaby * the compression once handle is allocated.
148037887783SJiri Slaby *
148137887783SJiri Slaby * if we have a 'non-null' handle here then we are coming
148237887783SJiri Slaby * from the slow path and handle has already been allocated.
148337887783SJiri Slaby */
1484f24ee92cSSergey Senozhatsky if (IS_ERR_VALUE(handle))
1485beb6602cSMinchan Kim handle = zs_malloc(zram->mem_pool, comp_len,
1486da9556a2SSergey Senozhatsky __GFP_KSWAPD_RECLAIM |
1487da9556a2SSergey Senozhatsky __GFP_NOWARN |
14889bc482d3SMinchan Kim __GFP_HIGHMEM |
14899bc482d3SMinchan Kim __GFP_MOVABLE);
1490f24ee92cSSergey Senozhatsky if (IS_ERR_VALUE(handle)) {
14917ac07a26SSergey Senozhatsky zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
149237887783SJiri Slaby atomic64_inc(&zram->stats.writestall);
149337887783SJiri Slaby handle = zs_malloc(zram->mem_pool, comp_len,
149437887783SJiri Slaby GFP_NOIO | __GFP_HIGHMEM |
149537887783SJiri Slaby __GFP_MOVABLE);
1496f24ee92cSSergey Senozhatsky if (IS_ERR_VALUE(handle))
1497c7e6f17bSHui Zhu return PTR_ERR((void *)handle);
1498641608f3SAlexey Romanov
1499641608f3SAlexey Romanov if (comp_len != PAGE_SIZE)
1500641608f3SAlexey Romanov goto compress_again;
1501641608f3SAlexey Romanov /*
1502f9bceb2fSSergey Senozhatsky * If the page is not compressible, you need to acquire the
1503f9bceb2fSSergey Senozhatsky * lock and execute the code below. The zcomp_stream_get()
1504f9bceb2fSSergey Senozhatsky * call is needed to disable the cpu hotplug and grab the
1505f9bceb2fSSergey Senozhatsky * zstrm buffer back. It is necessary that the dereferencing
1506f9bceb2fSSergey Senozhatsky * of the zstrm variable below occurs correctly.
1507641608f3SAlexey Romanov */
15087ac07a26SSergey Senozhatsky zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
1509cd67e10aSMinchan Kim }
15109ada9da9SMinchan Kim
1511beb6602cSMinchan Kim alloced_pages = zs_get_total_pages(zram->mem_pool);
151212372755SSergey SENOZHATSKY update_used_max(zram, alloced_pages);
151312372755SSergey SENOZHATSKY
1514461a8eeeSMinchan Kim if (zram->limit_pages && alloced_pages > zram->limit_pages) {
15157ac07a26SSergey Senozhatsky zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1516beb6602cSMinchan Kim zs_free(zram->mem_pool, handle);
15171f7319c7SMinchan Kim return -ENOMEM;
15189ada9da9SMinchan Kim }
15199ada9da9SMinchan Kim
1520beb6602cSMinchan Kim dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
15211f7319c7SMinchan Kim
15221f7319c7SMinchan Kim src = zstrm->buffer;
15231f7319c7SMinchan Kim if (comp_len == PAGE_SIZE)
1524cd67e10aSMinchan Kim src = kmap_atomic(page);
15251f7319c7SMinchan Kim memcpy(dst, src, comp_len);
15261f7319c7SMinchan Kim if (comp_len == PAGE_SIZE)
1527cd67e10aSMinchan Kim kunmap_atomic(src);
1528cd67e10aSMinchan Kim
15297ac07a26SSergey Senozhatsky zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
1530beb6602cSMinchan Kim zs_unmap_object(zram->mem_pool, handle);
15314ebbe7f7SMinchan Kim atomic64_add(comp_len, &zram->stats.compr_data_size);
15324ebbe7f7SMinchan Kim out:
1533cd67e10aSMinchan Kim /*
1534cd67e10aSMinchan Kim * Free memory associated with this sector
1535cd67e10aSMinchan Kim * before overwriting unused sectors.
1536cd67e10aSMinchan Kim */
153786c49814SMinchan Kim zram_slot_lock(zram, index);
1538cd67e10aSMinchan Kim zram_free_page(zram, index);
1539db8ffbd4SMinchan Kim
154089e85bceSMinchan Kim if (comp_len == PAGE_SIZE) {
154189e85bceSMinchan Kim zram_set_flag(zram, index, ZRAM_HUGE);
154289e85bceSMinchan Kim atomic64_inc(&zram->stats.huge_pages);
1543194e28daSMinchan Kim atomic64_inc(&zram->stats.huge_pages_since);
154489e85bceSMinchan Kim }
154589e85bceSMinchan Kim
1546db8ffbd4SMinchan Kim if (flags) {
1547db8ffbd4SMinchan Kim zram_set_flag(zram, index, flags);
15484ebbe7f7SMinchan Kim zram_set_element(zram, index, element);
15494ebbe7f7SMinchan Kim } else {
1550643ae61dSMinchan Kim zram_set_handle(zram, index, handle);
1551beb6602cSMinchan Kim zram_set_obj_size(zram, index, comp_len);
15524ebbe7f7SMinchan Kim }
155386c49814SMinchan Kim zram_slot_unlock(zram, index);
1554cd67e10aSMinchan Kim
1555cd67e10aSMinchan Kim /* Update stats */
155690a7806eSSergey Senozhatsky atomic64_inc(&zram->stats.pages_stored);
1557ae85a807SMinchan Kim return ret;
15581f7319c7SMinchan Kim }
15591f7319c7SMinchan Kim
1560a0b81ae7SChristoph Hellwig /*
1561a0b81ae7SChristoph Hellwig * This is a partial IO. Read the full page before writing the changes.
1562a0b81ae7SChristoph Hellwig */
zram_bvec_write_partial(struct zram * zram,struct bio_vec * bvec,u32 index,int offset,struct bio * bio)1563a0b81ae7SChristoph Hellwig static int zram_bvec_write_partial(struct zram *zram, struct bio_vec *bvec,
1564db8ffbd4SMinchan Kim u32 index, int offset, struct bio *bio)
15651f7319c7SMinchan Kim {
1566a0b81ae7SChristoph Hellwig struct page *page = alloc_page(GFP_NOIO);
15671f7319c7SMinchan Kim int ret;
15681f7319c7SMinchan Kim
15691f7319c7SMinchan Kim if (!page)
15701f7319c7SMinchan Kim return -ENOMEM;
15711f7319c7SMinchan Kim
15724e3c87b9SChristoph Hellwig ret = zram_read_page(zram, page, index, bio);
1573a0b81ae7SChristoph Hellwig if (!ret) {
1574f575a5adSChristoph Hellwig memcpy_from_bvec(page_address(page) + offset, bvec);
15756aa4b839SChristoph Hellwig ret = zram_write_page(zram, page, index);
15761f7319c7SMinchan Kim }
15771f7319c7SMinchan Kim __free_page(page);
1578cd67e10aSMinchan Kim return ret;
1579cd67e10aSMinchan Kim }
1580cd67e10aSMinchan Kim
zram_bvec_write(struct zram * zram,struct bio_vec * bvec,u32 index,int offset,struct bio * bio)1581a0b81ae7SChristoph Hellwig static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
1582a0b81ae7SChristoph Hellwig u32 index, int offset, struct bio *bio)
1583a0b81ae7SChristoph Hellwig {
1584a0b81ae7SChristoph Hellwig if (is_partial_io(bvec))
1585a0b81ae7SChristoph Hellwig return zram_bvec_write_partial(zram, bvec, index, offset, bio);
1586a0b81ae7SChristoph Hellwig return zram_write_page(zram, bvec->bv_page, index);
1587a0b81ae7SChristoph Hellwig }
1588a0b81ae7SChristoph Hellwig
158984b33bf7SSergey Senozhatsky #ifdef CONFIG_ZRAM_MULTI_COMP
159084b33bf7SSergey Senozhatsky /*
159184b33bf7SSergey Senozhatsky * This function will decompress (unless it's ZRAM_HUGE) the page and then
159284b33bf7SSergey Senozhatsky * attempt to compress it using provided compression algorithm priority
159384b33bf7SSergey Senozhatsky * (which is potentially more effective).
159484b33bf7SSergey Senozhatsky *
159584b33bf7SSergey Senozhatsky * Corresponding ZRAM slot should be locked.
159684b33bf7SSergey Senozhatsky */
zram_recompress(struct zram * zram,u32 index,struct page * page,u32 threshold,u32 prio,u32 prio_max)159784b33bf7SSergey Senozhatsky static int zram_recompress(struct zram *zram, u32 index, struct page *page,
159884b33bf7SSergey Senozhatsky u32 threshold, u32 prio, u32 prio_max)
159984b33bf7SSergey Senozhatsky {
160084b33bf7SSergey Senozhatsky struct zcomp_strm *zstrm = NULL;
160184b33bf7SSergey Senozhatsky unsigned long handle_old;
160284b33bf7SSergey Senozhatsky unsigned long handle_new;
160384b33bf7SSergey Senozhatsky unsigned int comp_len_old;
160484b33bf7SSergey Senozhatsky unsigned int comp_len_new;
16057c2af309SAlexey Romanov unsigned int class_index_old;
16067c2af309SAlexey Romanov unsigned int class_index_new;
1607a55cf964SSergey Senozhatsky u32 num_recomps = 0;
160884b33bf7SSergey Senozhatsky void *src, *dst;
160984b33bf7SSergey Senozhatsky int ret;
161084b33bf7SSergey Senozhatsky
161184b33bf7SSergey Senozhatsky handle_old = zram_get_handle(zram, index);
161284b33bf7SSergey Senozhatsky if (!handle_old)
161384b33bf7SSergey Senozhatsky return -EINVAL;
161484b33bf7SSergey Senozhatsky
161584b33bf7SSergey Senozhatsky comp_len_old = zram_get_obj_size(zram, index);
161684b33bf7SSergey Senozhatsky /*
161784b33bf7SSergey Senozhatsky * Do not recompress objects that are already "small enough".
161884b33bf7SSergey Senozhatsky */
161984b33bf7SSergey Senozhatsky if (comp_len_old < threshold)
162084b33bf7SSergey Senozhatsky return 0;
162184b33bf7SSergey Senozhatsky
162284b33bf7SSergey Senozhatsky ret = zram_read_from_zspool(zram, page, index);
162384b33bf7SSergey Senozhatsky if (ret)
162484b33bf7SSergey Senozhatsky return ret;
162584b33bf7SSergey Senozhatsky
16269a8b989dSSergey Senozhatsky /*
16279a8b989dSSergey Senozhatsky * We touched this entry so mark it as non-IDLE. This makes sure that
16289a8b989dSSergey Senozhatsky * we don't preserve IDLE flag and don't incorrectly pick this entry
16299a8b989dSSergey Senozhatsky * for different post-processing type (e.g. writeback).
16309a8b989dSSergey Senozhatsky */
16319a8b989dSSergey Senozhatsky zram_clear_flag(zram, index, ZRAM_IDLE);
16329a8b989dSSergey Senozhatsky
16337c2af309SAlexey Romanov class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old);
163484b33bf7SSergey Senozhatsky /*
163584b33bf7SSergey Senozhatsky * Iterate the secondary comp algorithms list (in order of priority)
163684b33bf7SSergey Senozhatsky * and try to recompress the page.
163784b33bf7SSergey Senozhatsky */
163884b33bf7SSergey Senozhatsky for (; prio < prio_max; prio++) {
163984b33bf7SSergey Senozhatsky if (!zram->comps[prio])
164084b33bf7SSergey Senozhatsky continue;
164184b33bf7SSergey Senozhatsky
164284b33bf7SSergey Senozhatsky /*
164384b33bf7SSergey Senozhatsky * Skip if the object is already re-compressed with a higher
164484b33bf7SSergey Senozhatsky * priority algorithm (or same algorithm).
164584b33bf7SSergey Senozhatsky */
164684b33bf7SSergey Senozhatsky if (prio <= zram_get_priority(zram, index))
164784b33bf7SSergey Senozhatsky continue;
164884b33bf7SSergey Senozhatsky
1649a55cf964SSergey Senozhatsky num_recomps++;
165084b33bf7SSergey Senozhatsky zstrm = zcomp_stream_get(zram->comps[prio]);
165184b33bf7SSergey Senozhatsky src = kmap_atomic(page);
165284b33bf7SSergey Senozhatsky ret = zcomp_compress(zstrm, src, &comp_len_new);
165384b33bf7SSergey Senozhatsky kunmap_atomic(src);
165484b33bf7SSergey Senozhatsky
165584b33bf7SSergey Senozhatsky if (ret) {
165684b33bf7SSergey Senozhatsky zcomp_stream_put(zram->comps[prio]);
165784b33bf7SSergey Senozhatsky return ret;
165884b33bf7SSergey Senozhatsky }
165984b33bf7SSergey Senozhatsky
16607c2af309SAlexey Romanov class_index_new = zs_lookup_class_index(zram->mem_pool,
16617c2af309SAlexey Romanov comp_len_new);
16627c2af309SAlexey Romanov
166384b33bf7SSergey Senozhatsky /* Continue until we make progress */
16644942cf6aSSergey Senozhatsky if (class_index_new >= class_index_old ||
166584b33bf7SSergey Senozhatsky (threshold && comp_len_new >= threshold)) {
166684b33bf7SSergey Senozhatsky zcomp_stream_put(zram->comps[prio]);
166784b33bf7SSergey Senozhatsky continue;
166884b33bf7SSergey Senozhatsky }
166984b33bf7SSergey Senozhatsky
167084b33bf7SSergey Senozhatsky /* Recompression was successful so break out */
167184b33bf7SSergey Senozhatsky break;
167284b33bf7SSergey Senozhatsky }
167384b33bf7SSergey Senozhatsky
167484b33bf7SSergey Senozhatsky /*
167584b33bf7SSergey Senozhatsky * We did not try to recompress, e.g. when we have only one
167684b33bf7SSergey Senozhatsky * secondary algorithm and the page is already recompressed
167784b33bf7SSergey Senozhatsky * using that algorithm
167884b33bf7SSergey Senozhatsky */
167984b33bf7SSergey Senozhatsky if (!zstrm)
168084b33bf7SSergey Senozhatsky return 0;
168184b33bf7SSergey Senozhatsky
16824942cf6aSSergey Senozhatsky if (class_index_new >= class_index_old) {
1683a55cf964SSergey Senozhatsky /*
1684a55cf964SSergey Senozhatsky * Secondary algorithms failed to re-compress the page
1685a55cf964SSergey Senozhatsky * in a way that would save memory, mark the object as
1686a55cf964SSergey Senozhatsky * incompressible so that we will not try to compress
1687a55cf964SSergey Senozhatsky * it again.
1688a55cf964SSergey Senozhatsky *
1689a55cf964SSergey Senozhatsky * We need to make sure that all secondary algorithms have
1690a55cf964SSergey Senozhatsky * failed, so we test if the number of recompressions matches
1691a55cf964SSergey Senozhatsky * the number of active secondary algorithms.
1692a55cf964SSergey Senozhatsky */
1693a55cf964SSergey Senozhatsky if (num_recomps == zram->num_active_comps - 1)
169484b33bf7SSergey Senozhatsky zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE);
169584b33bf7SSergey Senozhatsky return 0;
169684b33bf7SSergey Senozhatsky }
169784b33bf7SSergey Senozhatsky
169884b33bf7SSergey Senozhatsky /* Successful recompression but above threshold */
169984b33bf7SSergey Senozhatsky if (threshold && comp_len_new >= threshold)
170084b33bf7SSergey Senozhatsky return 0;
170184b33bf7SSergey Senozhatsky
170284b33bf7SSergey Senozhatsky /*
170384b33bf7SSergey Senozhatsky * No direct reclaim (slow path) for handle allocation and no
17046aa4b839SChristoph Hellwig * re-compression attempt (unlike in zram_write_bvec()) since
170584b33bf7SSergey Senozhatsky * we already have stored that object in zsmalloc. If we cannot
170684b33bf7SSergey Senozhatsky * alloc memory for recompressed object then we bail out and
170784b33bf7SSergey Senozhatsky * simply keep the old (existing) object in zsmalloc.
170884b33bf7SSergey Senozhatsky */
170984b33bf7SSergey Senozhatsky handle_new = zs_malloc(zram->mem_pool, comp_len_new,
171084b33bf7SSergey Senozhatsky __GFP_KSWAPD_RECLAIM |
171184b33bf7SSergey Senozhatsky __GFP_NOWARN |
171284b33bf7SSergey Senozhatsky __GFP_HIGHMEM |
171384b33bf7SSergey Senozhatsky __GFP_MOVABLE);
171484b33bf7SSergey Senozhatsky if (IS_ERR_VALUE(handle_new)) {
171584b33bf7SSergey Senozhatsky zcomp_stream_put(zram->comps[prio]);
171684b33bf7SSergey Senozhatsky return PTR_ERR((void *)handle_new);
171784b33bf7SSergey Senozhatsky }
171884b33bf7SSergey Senozhatsky
171984b33bf7SSergey Senozhatsky dst = zs_map_object(zram->mem_pool, handle_new, ZS_MM_WO);
172084b33bf7SSergey Senozhatsky memcpy(dst, zstrm->buffer, comp_len_new);
172184b33bf7SSergey Senozhatsky zcomp_stream_put(zram->comps[prio]);
172284b33bf7SSergey Senozhatsky
172384b33bf7SSergey Senozhatsky zs_unmap_object(zram->mem_pool, handle_new);
172484b33bf7SSergey Senozhatsky
172584b33bf7SSergey Senozhatsky zram_free_page(zram, index);
172684b33bf7SSergey Senozhatsky zram_set_handle(zram, index, handle_new);
172784b33bf7SSergey Senozhatsky zram_set_obj_size(zram, index, comp_len_new);
172884b33bf7SSergey Senozhatsky zram_set_priority(zram, index, prio);
172984b33bf7SSergey Senozhatsky
173084b33bf7SSergey Senozhatsky atomic64_add(comp_len_new, &zram->stats.compr_data_size);
173184b33bf7SSergey Senozhatsky atomic64_inc(&zram->stats.pages_stored);
173284b33bf7SSergey Senozhatsky
173384b33bf7SSergey Senozhatsky return 0;
173484b33bf7SSergey Senozhatsky }
173584b33bf7SSergey Senozhatsky
173684b33bf7SSergey Senozhatsky #define RECOMPRESS_IDLE (1 << 0)
173784b33bf7SSergey Senozhatsky #define RECOMPRESS_HUGE (1 << 1)
173884b33bf7SSergey Senozhatsky
recompress_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)173984b33bf7SSergey Senozhatsky static ssize_t recompress_store(struct device *dev,
174084b33bf7SSergey Senozhatsky struct device_attribute *attr,
174184b33bf7SSergey Senozhatsky const char *buf, size_t len)
174284b33bf7SSergey Senozhatsky {
1743a55cf964SSergey Senozhatsky u32 prio = ZRAM_SECONDARY_COMP, prio_max = ZRAM_MAX_COMPS;
174484b33bf7SSergey Senozhatsky struct zram *zram = dev_to_zram(dev);
174584b33bf7SSergey Senozhatsky unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
1746a55cf964SSergey Senozhatsky char *args, *param, *val, *algo = NULL;
1747a55cf964SSergey Senozhatsky u32 mode = 0, threshold = 0;
174884b33bf7SSergey Senozhatsky unsigned long index;
174984b33bf7SSergey Senozhatsky struct page *page;
175084b33bf7SSergey Senozhatsky ssize_t ret;
175184b33bf7SSergey Senozhatsky
175284b33bf7SSergey Senozhatsky args = skip_spaces(buf);
175384b33bf7SSergey Senozhatsky while (*args) {
175484b33bf7SSergey Senozhatsky args = next_arg(args, ¶m, &val);
175584b33bf7SSergey Senozhatsky
1756df32de14SSergey Senozhatsky if (!val || !*val)
175784b33bf7SSergey Senozhatsky return -EINVAL;
175884b33bf7SSergey Senozhatsky
175984b33bf7SSergey Senozhatsky if (!strcmp(param, "type")) {
176084b33bf7SSergey Senozhatsky if (!strcmp(val, "idle"))
176184b33bf7SSergey Senozhatsky mode = RECOMPRESS_IDLE;
176284b33bf7SSergey Senozhatsky if (!strcmp(val, "huge"))
176384b33bf7SSergey Senozhatsky mode = RECOMPRESS_HUGE;
176484b33bf7SSergey Senozhatsky if (!strcmp(val, "huge_idle"))
176584b33bf7SSergey Senozhatsky mode = RECOMPRESS_IDLE | RECOMPRESS_HUGE;
176684b33bf7SSergey Senozhatsky continue;
176784b33bf7SSergey Senozhatsky }
176884b33bf7SSergey Senozhatsky
176984b33bf7SSergey Senozhatsky if (!strcmp(param, "threshold")) {
177084b33bf7SSergey Senozhatsky /*
177184b33bf7SSergey Senozhatsky * We will re-compress only idle objects equal or
177284b33bf7SSergey Senozhatsky * greater in size than watermark.
177384b33bf7SSergey Senozhatsky */
177484b33bf7SSergey Senozhatsky ret = kstrtouint(val, 10, &threshold);
177584b33bf7SSergey Senozhatsky if (ret)
177684b33bf7SSergey Senozhatsky return ret;
177784b33bf7SSergey Senozhatsky continue;
177884b33bf7SSergey Senozhatsky }
1779a55cf964SSergey Senozhatsky
1780a55cf964SSergey Senozhatsky if (!strcmp(param, "algo")) {
1781a55cf964SSergey Senozhatsky algo = val;
1782a55cf964SSergey Senozhatsky continue;
1783a55cf964SSergey Senozhatsky }
178484b33bf7SSergey Senozhatsky }
178584b33bf7SSergey Senozhatsky
1786cb0551adSSergey Senozhatsky if (threshold >= huge_class_size)
178784b33bf7SSergey Senozhatsky return -EINVAL;
178884b33bf7SSergey Senozhatsky
178984b33bf7SSergey Senozhatsky down_read(&zram->init_lock);
179084b33bf7SSergey Senozhatsky if (!init_done(zram)) {
179184b33bf7SSergey Senozhatsky ret = -EINVAL;
179284b33bf7SSergey Senozhatsky goto release_init_lock;
179384b33bf7SSergey Senozhatsky }
179484b33bf7SSergey Senozhatsky
1795a55cf964SSergey Senozhatsky if (algo) {
1796a55cf964SSergey Senozhatsky bool found = false;
1797a55cf964SSergey Senozhatsky
1798a55cf964SSergey Senozhatsky for (; prio < ZRAM_MAX_COMPS; prio++) {
1799a55cf964SSergey Senozhatsky if (!zram->comp_algs[prio])
1800a55cf964SSergey Senozhatsky continue;
1801a55cf964SSergey Senozhatsky
1802a55cf964SSergey Senozhatsky if (!strcmp(zram->comp_algs[prio], algo)) {
1803a55cf964SSergey Senozhatsky prio_max = min(prio + 1, ZRAM_MAX_COMPS);
1804a55cf964SSergey Senozhatsky found = true;
1805a55cf964SSergey Senozhatsky break;
1806a55cf964SSergey Senozhatsky }
1807a55cf964SSergey Senozhatsky }
1808a55cf964SSergey Senozhatsky
1809a55cf964SSergey Senozhatsky if (!found) {
1810a55cf964SSergey Senozhatsky ret = -EINVAL;
1811a55cf964SSergey Senozhatsky goto release_init_lock;
1812a55cf964SSergey Senozhatsky }
1813a55cf964SSergey Senozhatsky }
1814a55cf964SSergey Senozhatsky
181584b33bf7SSergey Senozhatsky page = alloc_page(GFP_KERNEL);
181684b33bf7SSergey Senozhatsky if (!page) {
181784b33bf7SSergey Senozhatsky ret = -ENOMEM;
181884b33bf7SSergey Senozhatsky goto release_init_lock;
181984b33bf7SSergey Senozhatsky }
182084b33bf7SSergey Senozhatsky
182184b33bf7SSergey Senozhatsky ret = len;
182284b33bf7SSergey Senozhatsky for (index = 0; index < nr_pages; index++) {
182384b33bf7SSergey Senozhatsky int err = 0;
182484b33bf7SSergey Senozhatsky
182584b33bf7SSergey Senozhatsky zram_slot_lock(zram, index);
182684b33bf7SSergey Senozhatsky
182784b33bf7SSergey Senozhatsky if (!zram_allocated(zram, index))
182884b33bf7SSergey Senozhatsky goto next;
182984b33bf7SSergey Senozhatsky
183084b33bf7SSergey Senozhatsky if (mode & RECOMPRESS_IDLE &&
183184b33bf7SSergey Senozhatsky !zram_test_flag(zram, index, ZRAM_IDLE))
183284b33bf7SSergey Senozhatsky goto next;
183384b33bf7SSergey Senozhatsky
183484b33bf7SSergey Senozhatsky if (mode & RECOMPRESS_HUGE &&
183584b33bf7SSergey Senozhatsky !zram_test_flag(zram, index, ZRAM_HUGE))
183684b33bf7SSergey Senozhatsky goto next;
183784b33bf7SSergey Senozhatsky
183884b33bf7SSergey Senozhatsky if (zram_test_flag(zram, index, ZRAM_WB) ||
183984b33bf7SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
184084b33bf7SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_SAME) ||
184184b33bf7SSergey Senozhatsky zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
184284b33bf7SSergey Senozhatsky goto next;
184384b33bf7SSergey Senozhatsky
184484b33bf7SSergey Senozhatsky err = zram_recompress(zram, index, page, threshold,
1845a55cf964SSergey Senozhatsky prio, prio_max);
184684b33bf7SSergey Senozhatsky next:
184784b33bf7SSergey Senozhatsky zram_slot_unlock(zram, index);
184884b33bf7SSergey Senozhatsky if (err) {
184984b33bf7SSergey Senozhatsky ret = err;
185084b33bf7SSergey Senozhatsky break;
185184b33bf7SSergey Senozhatsky }
185284b33bf7SSergey Senozhatsky
185384b33bf7SSergey Senozhatsky cond_resched();
185484b33bf7SSergey Senozhatsky }
185584b33bf7SSergey Senozhatsky
185684b33bf7SSergey Senozhatsky __free_page(page);
185784b33bf7SSergey Senozhatsky
185884b33bf7SSergey Senozhatsky release_init_lock:
185984b33bf7SSergey Senozhatsky up_read(&zram->init_lock);
186084b33bf7SSergey Senozhatsky return ret;
186184b33bf7SSergey Senozhatsky }
186284b33bf7SSergey Senozhatsky #endif
186384b33bf7SSergey Senozhatsky
zram_bio_discard(struct zram * zram,struct bio * bio)18640120dd6eSChristoph Hellwig static void zram_bio_discard(struct zram *zram, struct bio *bio)
1865f4659d8eSJoonsoo Kim {
1866f4659d8eSJoonsoo Kim size_t n = bio->bi_iter.bi_size;
18670120dd6eSChristoph Hellwig u32 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
18680120dd6eSChristoph Hellwig u32 offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
18690120dd6eSChristoph Hellwig SECTOR_SHIFT;
1870f4659d8eSJoonsoo Kim
1871f4659d8eSJoonsoo Kim /*
1872f4659d8eSJoonsoo Kim * zram manages data in physical block size units. Because logical block
1873f4659d8eSJoonsoo Kim * size isn't identical with physical block size on some arch, we
1874f4659d8eSJoonsoo Kim * could get a discard request pointing to a specific offset within a
1875f4659d8eSJoonsoo Kim * certain physical block. Although we can handle this request by
1876f4659d8eSJoonsoo Kim * reading that physiclal block and decompressing and partially zeroing
1877f4659d8eSJoonsoo Kim * and re-compressing and then re-storing it, this isn't reasonable
1878f4659d8eSJoonsoo Kim * because our intent with a discard request is to save memory. So
1879f4659d8eSJoonsoo Kim * skipping this logical block is appropriate here.
1880f4659d8eSJoonsoo Kim */
1881f4659d8eSJoonsoo Kim if (offset) {
188238515c73SWeijie Yang if (n <= (PAGE_SIZE - offset))
1883f4659d8eSJoonsoo Kim return;
1884f4659d8eSJoonsoo Kim
188538515c73SWeijie Yang n -= (PAGE_SIZE - offset);
1886f4659d8eSJoonsoo Kim index++;
1887f4659d8eSJoonsoo Kim }
1888f4659d8eSJoonsoo Kim
1889f4659d8eSJoonsoo Kim while (n >= PAGE_SIZE) {
189086c49814SMinchan Kim zram_slot_lock(zram, index);
1891f4659d8eSJoonsoo Kim zram_free_page(zram, index);
189286c49814SMinchan Kim zram_slot_unlock(zram, index);
1893015254daSSergey Senozhatsky atomic64_inc(&zram->stats.notify_free);
1894f4659d8eSJoonsoo Kim index++;
1895f4659d8eSJoonsoo Kim n -= PAGE_SIZE;
1896f4659d8eSJoonsoo Kim }
18970120dd6eSChristoph Hellwig
18980120dd6eSChristoph Hellwig bio_endio(bio);
1899f4659d8eSJoonsoo Kim }
1900f4659d8eSJoonsoo Kim
zram_bio_read(struct zram * zram,struct bio * bio)190182ca875dSChristoph Hellwig static void zram_bio_read(struct zram *zram, struct bio *bio)
1902522698d7SSergey Senozhatsky {
190395848dcbSChristoph Hellwig unsigned long start_time = bio_start_io_acct(bio);
190495848dcbSChristoph Hellwig struct bvec_iter iter = bio->bi_iter;
1905522698d7SSergey Senozhatsky
190695848dcbSChristoph Hellwig do {
1907af8b04c6SChristoph Hellwig u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
1908af8b04c6SChristoph Hellwig u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
1909af8b04c6SChristoph Hellwig SECTOR_SHIFT;
191095848dcbSChristoph Hellwig struct bio_vec bv = bio_iter_iovec(bio, iter);
191195848dcbSChristoph Hellwig
191295848dcbSChristoph Hellwig bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
1913522698d7SSergey Senozhatsky
191482ca875dSChristoph Hellwig if (zram_bvec_read(zram, &bv, index, offset, bio) < 0) {
191582ca875dSChristoph Hellwig atomic64_inc(&zram->stats.failed_reads);
1916d7614e44SChristoph Hellwig bio->bi_status = BLK_STS_IOERR;
1917d7614e44SChristoph Hellwig break;
1918d7614e44SChristoph Hellwig }
191982ca875dSChristoph Hellwig flush_dcache_page(bv.bv_page);
192082ca875dSChristoph Hellwig
192182ca875dSChristoph Hellwig zram_slot_lock(zram, index);
192282ca875dSChristoph Hellwig zram_accessed(zram, index);
192382ca875dSChristoph Hellwig zram_slot_unlock(zram, index);
192495848dcbSChristoph Hellwig
192595848dcbSChristoph Hellwig bio_advance_iter_single(bio, &iter, bv.bv_len);
192695848dcbSChristoph Hellwig } while (iter.bi_size);
192795848dcbSChristoph Hellwig
192882ca875dSChristoph Hellwig bio_end_io_acct(bio, start_time);
192982ca875dSChristoph Hellwig bio_endio(bio);
193082ca875dSChristoph Hellwig }
193182ca875dSChristoph Hellwig
zram_bio_write(struct zram * zram,struct bio * bio)193282ca875dSChristoph Hellwig static void zram_bio_write(struct zram *zram, struct bio *bio)
193382ca875dSChristoph Hellwig {
193495848dcbSChristoph Hellwig unsigned long start_time = bio_start_io_acct(bio);
193595848dcbSChristoph Hellwig struct bvec_iter iter = bio->bi_iter;
193682ca875dSChristoph Hellwig
193795848dcbSChristoph Hellwig do {
193882ca875dSChristoph Hellwig u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
193982ca875dSChristoph Hellwig u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
194082ca875dSChristoph Hellwig SECTOR_SHIFT;
194195848dcbSChristoph Hellwig struct bio_vec bv = bio_iter_iovec(bio, iter);
194295848dcbSChristoph Hellwig
194395848dcbSChristoph Hellwig bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
194482ca875dSChristoph Hellwig
194582ca875dSChristoph Hellwig if (zram_bvec_write(zram, &bv, index, offset, bio) < 0) {
194682ca875dSChristoph Hellwig atomic64_inc(&zram->stats.failed_writes);
194782ca875dSChristoph Hellwig bio->bi_status = BLK_STS_IOERR;
194882ca875dSChristoph Hellwig break;
1949522698d7SSergey Senozhatsky }
1950522698d7SSergey Senozhatsky
1951522698d7SSergey Senozhatsky zram_slot_lock(zram, index);
1952522698d7SSergey Senozhatsky zram_accessed(zram, index);
1953522698d7SSergey Senozhatsky zram_slot_unlock(zram, index);
195495848dcbSChristoph Hellwig
195595848dcbSChristoph Hellwig bio_advance_iter_single(bio, &iter, bv.bv_len);
195695848dcbSChristoph Hellwig } while (iter.bi_size);
195795848dcbSChristoph Hellwig
1958d7614e44SChristoph Hellwig bio_end_io_acct(bio, start_time);
19594246a0b6SChristoph Hellwig bio_endio(bio);
1960522698d7SSergey Senozhatsky }
1961522698d7SSergey Senozhatsky
1962522698d7SSergey Senozhatsky /*
1963522698d7SSergey Senozhatsky * Handler function for all zram I/O requests.
1964522698d7SSergey Senozhatsky */
zram_submit_bio(struct bio * bio)19653e08773cSChristoph Hellwig static void zram_submit_bio(struct bio *bio)
1966522698d7SSergey Senozhatsky {
1967309dca30SChristoph Hellwig struct zram *zram = bio->bi_bdev->bd_disk->private_data;
1968522698d7SSergey Senozhatsky
1969d6eea009SChristoph Hellwig switch (bio_op(bio)) {
1970d6eea009SChristoph Hellwig case REQ_OP_READ:
197182ca875dSChristoph Hellwig zram_bio_read(zram, bio);
197282ca875dSChristoph Hellwig break;
1973d6eea009SChristoph Hellwig case REQ_OP_WRITE:
197482ca875dSChristoph Hellwig zram_bio_write(zram, bio);
1975d6eea009SChristoph Hellwig break;
1976d6eea009SChristoph Hellwig case REQ_OP_DISCARD:
1977d6eea009SChristoph Hellwig case REQ_OP_WRITE_ZEROES:
1978d6eea009SChristoph Hellwig zram_bio_discard(zram, bio);
1979d6eea009SChristoph Hellwig break;
1980d6eea009SChristoph Hellwig default:
1981d6eea009SChristoph Hellwig WARN_ON_ONCE(1);
1982d6eea009SChristoph Hellwig bio_endio(bio);
1983522698d7SSergey Senozhatsky }
1984522698d7SSergey Senozhatsky }
1985522698d7SSergey Senozhatsky
zram_slot_free_notify(struct block_device * bdev,unsigned long index)1986522698d7SSergey Senozhatsky static void zram_slot_free_notify(struct block_device *bdev,
1987522698d7SSergey Senozhatsky unsigned long index)
1988522698d7SSergey Senozhatsky {
1989522698d7SSergey Senozhatsky struct zram *zram;
1990522698d7SSergey Senozhatsky
1991522698d7SSergey Senozhatsky zram = bdev->bd_disk->private_data;
1992522698d7SSergey Senozhatsky
19933c9959e0SMinchan Kim atomic64_inc(&zram->stats.notify_free);
19943c9959e0SMinchan Kim if (!zram_slot_trylock(zram, index)) {
19953c9959e0SMinchan Kim atomic64_inc(&zram->stats.miss_free);
19963c9959e0SMinchan Kim return;
19973c9959e0SMinchan Kim }
19983c9959e0SMinchan Kim
1999522698d7SSergey Senozhatsky zram_free_page(zram, index);
200086c49814SMinchan Kim zram_slot_unlock(zram, index);
2001522698d7SSergey Senozhatsky }
2002522698d7SSergey Senozhatsky
zram_destroy_comps(struct zram * zram)20037ac07a26SSergey Senozhatsky static void zram_destroy_comps(struct zram *zram)
20047ac07a26SSergey Senozhatsky {
20057ac07a26SSergey Senozhatsky u32 prio;
20067ac07a26SSergey Senozhatsky
20077ac07a26SSergey Senozhatsky for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
20087ac07a26SSergey Senozhatsky struct zcomp *comp = zram->comps[prio];
20097ac07a26SSergey Senozhatsky
20107ac07a26SSergey Senozhatsky zram->comps[prio] = NULL;
20117ac07a26SSergey Senozhatsky if (!comp)
20127ac07a26SSergey Senozhatsky continue;
20137ac07a26SSergey Senozhatsky zcomp_destroy(comp);
2014a55cf964SSergey Senozhatsky zram->num_active_comps--;
20157ac07a26SSergey Senozhatsky }
20166272936fSSergey Senozhatsky
20176e20720bSAndrey Skvortsov for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
20186e20720bSAndrey Skvortsov /* Do not free statically defined compression algorithms */
20196e20720bSAndrey Skvortsov if (zram->comp_algs[prio] != default_compressor)
20206272936fSSergey Senozhatsky kfree(zram->comp_algs[prio]);
20216272936fSSergey Senozhatsky zram->comp_algs[prio] = NULL;
20226272936fSSergey Senozhatsky }
20237ac07a26SSergey Senozhatsky }
20247ac07a26SSergey Senozhatsky
zram_reset_device(struct zram * zram)2025ba6b17d6SSergey Senozhatsky static void zram_reset_device(struct zram *zram)
2026cd67e10aSMinchan Kim {
2027cd67e10aSMinchan Kim down_write(&zram->init_lock);
20289ada9da9SMinchan Kim
20299ada9da9SMinchan Kim zram->limit_pages = 0;
20309ada9da9SMinchan Kim
20316e017a39SChristoph Hellwig set_capacity_and_notify(zram->disk, 0);
20328446fe92SChristoph Hellwig part_stat_set_all(zram->disk->part0, 0);
2033a096cafcSSergey Senozhatsky
203408eee69fSMinchan Kim /* I/O operation under all of CPU are done so let's free */
20356d2453c3SSergey Senozhatsky zram_meta_free(zram, zram->disksize);
20366d2453c3SSergey Senozhatsky zram->disksize = 0;
20377ac07a26SSergey Senozhatsky zram_destroy_comps(zram);
2038302128dcSMinchan Kim memset(&zram->stats, 0, sizeof(zram->stats));
2039013bf95aSMinchan Kim reset_bdev(zram);
20406f163779SMing Lei
20417ac07a26SSergey Senozhatsky comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
20426f163779SMing Lei up_write(&zram->init_lock);
2043cd67e10aSMinchan Kim }
2044cd67e10aSMinchan Kim
disksize_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2045cd67e10aSMinchan Kim static ssize_t disksize_store(struct device *dev,
2046cd67e10aSMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
2047cd67e10aSMinchan Kim {
2048cd67e10aSMinchan Kim u64 disksize;
2049d61f98c7SSergey Senozhatsky struct zcomp *comp;
2050cd67e10aSMinchan Kim struct zram *zram = dev_to_zram(dev);
2051fcfa8d95SSergey Senozhatsky int err;
20527ac07a26SSergey Senozhatsky u32 prio;
2053cd67e10aSMinchan Kim
2054cd67e10aSMinchan Kim disksize = memparse(buf, NULL);
2055cd67e10aSMinchan Kim if (!disksize)
2056cd67e10aSMinchan Kim return -EINVAL;
2057cd67e10aSMinchan Kim
2058beb6602cSMinchan Kim down_write(&zram->init_lock);
2059beb6602cSMinchan Kim if (init_done(zram)) {
2060beb6602cSMinchan Kim pr_info("Cannot change disksize for initialized device\n");
2061beb6602cSMinchan Kim err = -EBUSY;
2062beb6602cSMinchan Kim goto out_unlock;
2063beb6602cSMinchan Kim }
2064beb6602cSMinchan Kim
2065cd67e10aSMinchan Kim disksize = PAGE_ALIGN(disksize);
2066beb6602cSMinchan Kim if (!zram_meta_alloc(zram, disksize)) {
2067beb6602cSMinchan Kim err = -ENOMEM;
2068beb6602cSMinchan Kim goto out_unlock;
2069beb6602cSMinchan Kim }
2070b67d1ec1SSergey Senozhatsky
20717ac07a26SSergey Senozhatsky for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
20727ac07a26SSergey Senozhatsky if (!zram->comp_algs[prio])
20737ac07a26SSergey Senozhatsky continue;
20747ac07a26SSergey Senozhatsky
20757ac07a26SSergey Senozhatsky comp = zcomp_create(zram->comp_algs[prio]);
2076fcfa8d95SSergey Senozhatsky if (IS_ERR(comp)) {
207770864969SSergey Senozhatsky pr_err("Cannot initialise %s compressing backend\n",
20787ac07a26SSergey Senozhatsky zram->comp_algs[prio]);
2079fcfa8d95SSergey Senozhatsky err = PTR_ERR(comp);
20807ac07a26SSergey Senozhatsky goto out_free_comps;
2081d61f98c7SSergey Senozhatsky }
2082d61f98c7SSergey Senozhatsky
20837ac07a26SSergey Senozhatsky zram->comps[prio] = comp;
2084a55cf964SSergey Senozhatsky zram->num_active_comps++;
20857ac07a26SSergey Senozhatsky }
2086cd67e10aSMinchan Kim zram->disksize = disksize;
20876e017a39SChristoph Hellwig set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
2088e7ccfc4cSMinchan Kim up_write(&zram->init_lock);
2089b4c5c609SMinchan Kim
2090cd67e10aSMinchan Kim return len;
2091b7ca232eSSergey Senozhatsky
20927ac07a26SSergey Senozhatsky out_free_comps:
20937ac07a26SSergey Senozhatsky zram_destroy_comps(zram);
2094beb6602cSMinchan Kim zram_meta_free(zram, disksize);
2095beb6602cSMinchan Kim out_unlock:
2096beb6602cSMinchan Kim up_write(&zram->init_lock);
2097b7ca232eSSergey Senozhatsky return err;
2098cd67e10aSMinchan Kim }
2099cd67e10aSMinchan Kim
reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)2100cd67e10aSMinchan Kim static ssize_t reset_store(struct device *dev,
2101cd67e10aSMinchan Kim struct device_attribute *attr, const char *buf, size_t len)
2102cd67e10aSMinchan Kim {
2103cd67e10aSMinchan Kim int ret;
2104cd67e10aSMinchan Kim unsigned short do_reset;
2105cd67e10aSMinchan Kim struct zram *zram;
2106d666e20eSChristoph Hellwig struct gendisk *disk;
2107cd67e10aSMinchan Kim
2108f405c445SSergey Senozhatsky ret = kstrtou16(buf, 10, &do_reset);
2109f405c445SSergey Senozhatsky if (ret)
2110f405c445SSergey Senozhatsky return ret;
2111f405c445SSergey Senozhatsky
2112f405c445SSergey Senozhatsky if (!do_reset)
2113f405c445SSergey Senozhatsky return -EINVAL;
2114f405c445SSergey Senozhatsky
2115cd67e10aSMinchan Kim zram = dev_to_zram(dev);
2116d666e20eSChristoph Hellwig disk = zram->disk;
2117cd67e10aSMinchan Kim
2118d666e20eSChristoph Hellwig mutex_lock(&disk->open_mutex);
2119f405c445SSergey Senozhatsky /* Do not reset an active device or claimed device */
2120dbdc1be3SChristoph Hellwig if (disk_openers(disk) || zram->claim) {
2121d666e20eSChristoph Hellwig mutex_unlock(&disk->open_mutex);
2122f405c445SSergey Senozhatsky return -EBUSY;
2123cd67e10aSMinchan Kim }
2124cd67e10aSMinchan Kim
2125f405c445SSergey Senozhatsky /* From now on, anyone can't open /dev/zram[0-9] */
2126f405c445SSergey Senozhatsky zram->claim = true;
2127d666e20eSChristoph Hellwig mutex_unlock(&disk->open_mutex);
2128cd67e10aSMinchan Kim
2129f405c445SSergey Senozhatsky /* Make sure all the pending I/O are finished */
2130d666e20eSChristoph Hellwig sync_blockdev(disk->part0);
2131ba6b17d6SSergey Senozhatsky zram_reset_device(zram);
2132cd67e10aSMinchan Kim
2133d666e20eSChristoph Hellwig mutex_lock(&disk->open_mutex);
2134f405c445SSergey Senozhatsky zram->claim = false;
2135d666e20eSChristoph Hellwig mutex_unlock(&disk->open_mutex);
2136f405c445SSergey Senozhatsky
2137f405c445SSergey Senozhatsky return len;
2138f405c445SSergey Senozhatsky }
2139f405c445SSergey Senozhatsky
zram_open(struct gendisk * disk,blk_mode_t mode)214005bdb996SChristoph Hellwig static int zram_open(struct gendisk *disk, blk_mode_t mode)
2141f405c445SSergey Senozhatsky {
2142d32e2bf8SChristoph Hellwig struct zram *zram = disk->private_data;
2143f405c445SSergey Senozhatsky
2144d32e2bf8SChristoph Hellwig WARN_ON(!mutex_is_locked(&disk->open_mutex));
2145f405c445SSergey Senozhatsky
2146f405c445SSergey Senozhatsky /* zram was claimed to reset so open request fails */
2147f405c445SSergey Senozhatsky if (zram->claim)
2148d32e2bf8SChristoph Hellwig return -EBUSY;
2149d32e2bf8SChristoph Hellwig return 0;
2150cd67e10aSMinchan Kim }
2151cd67e10aSMinchan Kim
2152cd67e10aSMinchan Kim static const struct block_device_operations zram_devops = {
2153f405c445SSergey Senozhatsky .open = zram_open,
2154c62b37d9SChristoph Hellwig .submit_bio = zram_submit_bio,
2155cd67e10aSMinchan Kim .swap_slot_free_notify = zram_slot_free_notify,
2156cd67e10aSMinchan Kim .owner = THIS_MODULE
2157cd67e10aSMinchan Kim };
2158cd67e10aSMinchan Kim
215999ebbd30SAndrew Morton static DEVICE_ATTR_WO(compact);
2160083914eaSGanesh Mahendran static DEVICE_ATTR_RW(disksize);
2161083914eaSGanesh Mahendran static DEVICE_ATTR_RO(initstate);
2162083914eaSGanesh Mahendran static DEVICE_ATTR_WO(reset);
2163c87d1655SSergey Senozhatsky static DEVICE_ATTR_WO(mem_limit);
2164c87d1655SSergey Senozhatsky static DEVICE_ATTR_WO(mem_used_max);
2165e82592c4SMinchan Kim static DEVICE_ATTR_WO(idle);
2166083914eaSGanesh Mahendran static DEVICE_ATTR_RW(max_comp_streams);
2167083914eaSGanesh Mahendran static DEVICE_ATTR_RW(comp_algorithm);
2168013bf95aSMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
2169013bf95aSMinchan Kim static DEVICE_ATTR_RW(backing_dev);
2170a939888eSMinchan Kim static DEVICE_ATTR_WO(writeback);
2171bb416d18SMinchan Kim static DEVICE_ATTR_RW(writeback_limit);
21721d69a3f8SMinchan Kim static DEVICE_ATTR_RW(writeback_limit_enable);
2173013bf95aSMinchan Kim #endif
2174001d9273SSergey Senozhatsky #ifdef CONFIG_ZRAM_MULTI_COMP
2175001d9273SSergey Senozhatsky static DEVICE_ATTR_RW(recomp_algorithm);
217684b33bf7SSergey Senozhatsky static DEVICE_ATTR_WO(recompress);
2177001d9273SSergey Senozhatsky #endif
2178cd67e10aSMinchan Kim
2179cd67e10aSMinchan Kim static struct attribute *zram_disk_attrs[] = {
2180cd67e10aSMinchan Kim &dev_attr_disksize.attr,
2181cd67e10aSMinchan Kim &dev_attr_initstate.attr,
2182cd67e10aSMinchan Kim &dev_attr_reset.attr,
218399ebbd30SAndrew Morton &dev_attr_compact.attr,
21849ada9da9SMinchan Kim &dev_attr_mem_limit.attr,
2185461a8eeeSMinchan Kim &dev_attr_mem_used_max.attr,
2186e82592c4SMinchan Kim &dev_attr_idle.attr,
2187beca3ec7SSergey Senozhatsky &dev_attr_max_comp_streams.attr,
2188e46b8a03SSergey Senozhatsky &dev_attr_comp_algorithm.attr,
2189013bf95aSMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
2190013bf95aSMinchan Kim &dev_attr_backing_dev.attr,
2191a939888eSMinchan Kim &dev_attr_writeback.attr,
2192bb416d18SMinchan Kim &dev_attr_writeback_limit.attr,
21931d69a3f8SMinchan Kim &dev_attr_writeback_limit_enable.attr,
2194013bf95aSMinchan Kim #endif
21952f6a3bedSSergey Senozhatsky &dev_attr_io_stat.attr,
21964f2109f6SSergey Senozhatsky &dev_attr_mm_stat.attr,
219723eddf39SMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
219823eddf39SMinchan Kim &dev_attr_bd_stat.attr,
219923eddf39SMinchan Kim #endif
2200623e47fcSSergey Senozhatsky &dev_attr_debug_stat.attr,
2201001d9273SSergey Senozhatsky #ifdef CONFIG_ZRAM_MULTI_COMP
2202001d9273SSergey Senozhatsky &dev_attr_recomp_algorithm.attr,
220384b33bf7SSergey Senozhatsky &dev_attr_recompress.attr,
2204001d9273SSergey Senozhatsky #endif
2205cd67e10aSMinchan Kim NULL,
2206cd67e10aSMinchan Kim };
2207cd67e10aSMinchan Kim
22087f0d2672SLuis Chamberlain ATTRIBUTE_GROUPS(zram_disk);
220998af4d4dSHannes Reinecke
221092ff1528SSergey Senozhatsky /*
221192ff1528SSergey Senozhatsky * Allocate and initialize new zram device. the function returns
221292ff1528SSergey Senozhatsky * '>= 0' device_id upon success, and negative value otherwise.
221392ff1528SSergey Senozhatsky */
zram_add(void)221492ff1528SSergey Senozhatsky static int zram_add(void)
2215cd67e10aSMinchan Kim {
221685508ec6SSergey Senozhatsky struct zram *zram;
221792ff1528SSergey Senozhatsky int ret, device_id;
221885508ec6SSergey Senozhatsky
221985508ec6SSergey Senozhatsky zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
222085508ec6SSergey Senozhatsky if (!zram)
222185508ec6SSergey Senozhatsky return -ENOMEM;
222285508ec6SSergey Senozhatsky
222392ff1528SSergey Senozhatsky ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
222485508ec6SSergey Senozhatsky if (ret < 0)
222585508ec6SSergey Senozhatsky goto out_free_dev;
222692ff1528SSergey Senozhatsky device_id = ret;
2227cd67e10aSMinchan Kim
2228cd67e10aSMinchan Kim init_rwsem(&zram->init_lock);
22291d69a3f8SMinchan Kim #ifdef CONFIG_ZRAM_WRITEBACK
22301d69a3f8SMinchan Kim spin_lock_init(&zram->wb_limit_lock);
22311d69a3f8SMinchan Kim #endif
22327681750bSChristoph Hellwig
22337681750bSChristoph Hellwig /* gendisk structure */
22347681750bSChristoph Hellwig zram->disk = blk_alloc_disk(NUMA_NO_NODE);
22357681750bSChristoph Hellwig if (!zram->disk) {
22367681750bSChristoph Hellwig pr_err("Error allocating disk structure for device %d\n",
2237cd67e10aSMinchan Kim device_id);
223885508ec6SSergey Senozhatsky ret = -ENOMEM;
223985508ec6SSergey Senozhatsky goto out_free_idr;
2240cd67e10aSMinchan Kim }
2241cd67e10aSMinchan Kim
2242cd67e10aSMinchan Kim zram->disk->major = zram_major;
2243cd67e10aSMinchan Kim zram->disk->first_minor = device_id;
22447681750bSChristoph Hellwig zram->disk->minors = 1;
22451ebe2e5fSChristoph Hellwig zram->disk->flags |= GENHD_FL_NO_PART;
2246cd67e10aSMinchan Kim zram->disk->fops = &zram_devops;
2247cd67e10aSMinchan Kim zram->disk->private_data = zram;
2248cd67e10aSMinchan Kim snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
2249cd67e10aSMinchan Kim
2250*c7ee791eSLiu Shixin comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
2251*c7ee791eSLiu Shixin
2252071acb30SJeongHyeon Lee /* Actual capacity set using sysfs (/sys/block/zram<id>/disksize */
2253cd67e10aSMinchan Kim set_capacity(zram->disk, 0);
2254b67d1ec1SSergey Senozhatsky /* zram devices sort of resembles non-rotational disks */
22558b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NONROT, zram->disk->queue);
22563222d8c2SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_SYNCHRONOUS, zram->disk->queue);
2257e447a015SMinchan Kim
2258cd67e10aSMinchan Kim /*
2259cd67e10aSMinchan Kim * To ensure that we always get PAGE_SIZE aligned
2260cd67e10aSMinchan Kim * and n*PAGE_SIZED sized I/O requests.
2261cd67e10aSMinchan Kim */
2262cd67e10aSMinchan Kim blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
2263cd67e10aSMinchan Kim blk_queue_logical_block_size(zram->disk->queue,
2264cd67e10aSMinchan Kim ZRAM_LOGICAL_BLOCK_SIZE);
2265cd67e10aSMinchan Kim blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
2266cd67e10aSMinchan Kim blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
2267f4659d8eSJoonsoo Kim zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
22682bb4cd5cSJens Axboe blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
226931edeacdSChristoph Hellwig
2270f4659d8eSJoonsoo Kim /*
2271f4659d8eSJoonsoo Kim * zram_bio_discard() will clear all logical blocks if logical block
2272f4659d8eSJoonsoo Kim * size is identical with physical block size(PAGE_SIZE). But if it is
2273f4659d8eSJoonsoo Kim * different, we will skip discarding some parts of logical blocks in
2274f4659d8eSJoonsoo Kim * the part of the request range which isn't aligned to physical block
2275f4659d8eSJoonsoo Kim * size. So we can't ensure that all discarded logical blocks are
2276f4659d8eSJoonsoo Kim * zeroed.
2277f4659d8eSJoonsoo Kim */
2278f4659d8eSJoonsoo Kim if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
227931edeacdSChristoph Hellwig blk_queue_max_write_zeroes_sectors(zram->disk->queue, UINT_MAX);
2280cd67e10aSMinchan Kim
228137887783SJiri Slaby blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, zram->disk->queue);
22827f0d2672SLuis Chamberlain ret = device_add_disk(NULL, zram->disk, zram_disk_groups);
22835e2e1cc4SLuis Chamberlain if (ret)
22845e2e1cc4SLuis Chamberlain goto out_cleanup_disk;
2285cd67e10aSMinchan Kim
2286c0265342SMinchan Kim zram_debugfs_register(zram);
2287d12b63c9SSergey Senozhatsky pr_info("Added device: %s\n", zram->disk->disk_name);
228892ff1528SSergey Senozhatsky return device_id;
2289cd67e10aSMinchan Kim
22905e2e1cc4SLuis Chamberlain out_cleanup_disk:
22918b9ab626SChristoph Hellwig put_disk(zram->disk);
229285508ec6SSergey Senozhatsky out_free_idr:
229385508ec6SSergey Senozhatsky idr_remove(&zram_index_idr, device_id);
229485508ec6SSergey Senozhatsky out_free_dev:
229585508ec6SSergey Senozhatsky kfree(zram);
2296cd67e10aSMinchan Kim return ret;
2297cd67e10aSMinchan Kim }
2298cd67e10aSMinchan Kim
zram_remove(struct zram * zram)22996566d1a3SSergey Senozhatsky static int zram_remove(struct zram *zram)
2300cd67e10aSMinchan Kim {
23018c54499aSMing Lei bool claimed;
23026566d1a3SSergey Senozhatsky
23037a86d6dcSChristoph Hellwig mutex_lock(&zram->disk->open_mutex);
2304dbdc1be3SChristoph Hellwig if (disk_openers(zram->disk)) {
23057a86d6dcSChristoph Hellwig mutex_unlock(&zram->disk->open_mutex);
23066566d1a3SSergey Senozhatsky return -EBUSY;
23076566d1a3SSergey Senozhatsky }
23086566d1a3SSergey Senozhatsky
23098c54499aSMing Lei claimed = zram->claim;
23108c54499aSMing Lei if (!claimed)
23116566d1a3SSergey Senozhatsky zram->claim = true;
23127a86d6dcSChristoph Hellwig mutex_unlock(&zram->disk->open_mutex);
23136566d1a3SSergey Senozhatsky
2314c0265342SMinchan Kim zram_debugfs_unregister(zram);
2315cd67e10aSMinchan Kim
23168c54499aSMing Lei if (claimed) {
23178c54499aSMing Lei /*
23188c54499aSMing Lei * If we were claimed by reset_store(), del_gendisk() will
23198c54499aSMing Lei * wait until reset_store() is done, so nothing need to do.
23208c54499aSMing Lei */
23218c54499aSMing Lei ;
23228c54499aSMing Lei } else {
23236566d1a3SSergey Senozhatsky /* Make sure all the pending I/O are finished */
23247a86d6dcSChristoph Hellwig sync_blockdev(zram->disk->part0);
2325a096cafcSSergey Senozhatsky zram_reset_device(zram);
23268c54499aSMing Lei }
23276566d1a3SSergey Senozhatsky
23286566d1a3SSergey Senozhatsky pr_info("Removed device: %s\n", zram->disk->disk_name);
23296566d1a3SSergey Senozhatsky
2330cd67e10aSMinchan Kim del_gendisk(zram->disk);
23318c54499aSMing Lei
23328c54499aSMing Lei /* del_gendisk drains pending reset_store */
23338c54499aSMing Lei WARN_ON_ONCE(claimed && zram->claim);
23348c54499aSMing Lei
23355a4b6536SMing Lei /*
23365a4b6536SMing Lei * disksize_store() may be called in between zram_reset_device()
23375a4b6536SMing Lei * and del_gendisk(), so run the last reset to avoid leaking
23385a4b6536SMing Lei * anything allocated with disksize_store()
23395a4b6536SMing Lei */
23405a4b6536SMing Lei zram_reset_device(zram);
23415a4b6536SMing Lei
23428b9ab626SChristoph Hellwig put_disk(zram->disk);
234385508ec6SSergey Senozhatsky kfree(zram);
23446566d1a3SSergey Senozhatsky return 0;
2345cd67e10aSMinchan Kim }
2346cd67e10aSMinchan Kim
23476566d1a3SSergey Senozhatsky /* zram-control sysfs attributes */
234827104a53SGreg Kroah-Hartman
234927104a53SGreg Kroah-Hartman /*
235027104a53SGreg Kroah-Hartman * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
235127104a53SGreg Kroah-Hartman * sense that reading from this file does alter the state of your system -- it
235227104a53SGreg Kroah-Hartman * creates a new un-initialized zram device and returns back this device's
235327104a53SGreg Kroah-Hartman * device_id (or an error code if it fails to create a new device).
235427104a53SGreg Kroah-Hartman */
hot_add_show(const struct class * class,const struct class_attribute * attr,char * buf)235575a2d422SGreg Kroah-Hartman static ssize_t hot_add_show(const struct class *class,
235675a2d422SGreg Kroah-Hartman const struct class_attribute *attr,
23576566d1a3SSergey Senozhatsky char *buf)
23586566d1a3SSergey Senozhatsky {
23596566d1a3SSergey Senozhatsky int ret;
23606566d1a3SSergey Senozhatsky
23616566d1a3SSergey Senozhatsky mutex_lock(&zram_index_mutex);
23626566d1a3SSergey Senozhatsky ret = zram_add();
23636566d1a3SSergey Senozhatsky mutex_unlock(&zram_index_mutex);
23646566d1a3SSergey Senozhatsky
23656566d1a3SSergey Senozhatsky if (ret < 0)
23666566d1a3SSergey Senozhatsky return ret;
23676566d1a3SSergey Senozhatsky return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
23686566d1a3SSergey Senozhatsky }
2369ca9d081bSGreg Kroah-Hartman /* This attribute must be set to 0400, so CLASS_ATTR_RO() can not be used */
2370ca9d081bSGreg Kroah-Hartman static struct class_attribute class_attr_hot_add =
2371ca9d081bSGreg Kroah-Hartman __ATTR(hot_add, 0400, hot_add_show, NULL);
23726566d1a3SSergey Senozhatsky
hot_remove_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t count)237375a2d422SGreg Kroah-Hartman static ssize_t hot_remove_store(const struct class *class,
237475a2d422SGreg Kroah-Hartman const struct class_attribute *attr,
23756566d1a3SSergey Senozhatsky const char *buf,
23766566d1a3SSergey Senozhatsky size_t count)
23776566d1a3SSergey Senozhatsky {
23786566d1a3SSergey Senozhatsky struct zram *zram;
23796566d1a3SSergey Senozhatsky int ret, dev_id;
23806566d1a3SSergey Senozhatsky
23816566d1a3SSergey Senozhatsky /* dev_id is gendisk->first_minor, which is `int' */
23826566d1a3SSergey Senozhatsky ret = kstrtoint(buf, 10, &dev_id);
23836566d1a3SSergey Senozhatsky if (ret)
23846566d1a3SSergey Senozhatsky return ret;
23856566d1a3SSergey Senozhatsky if (dev_id < 0)
23866566d1a3SSergey Senozhatsky return -EINVAL;
23876566d1a3SSergey Senozhatsky
23886566d1a3SSergey Senozhatsky mutex_lock(&zram_index_mutex);
23896566d1a3SSergey Senozhatsky
23906566d1a3SSergey Senozhatsky zram = idr_find(&zram_index_idr, dev_id);
239117ec4cd9SJerome Marchand if (zram) {
23926566d1a3SSergey Senozhatsky ret = zram_remove(zram);
2393529e71e1STakashi Iwai if (!ret)
239417ec4cd9SJerome Marchand idr_remove(&zram_index_idr, dev_id);
239517ec4cd9SJerome Marchand } else {
23966566d1a3SSergey Senozhatsky ret = -ENODEV;
239717ec4cd9SJerome Marchand }
23986566d1a3SSergey Senozhatsky
23996566d1a3SSergey Senozhatsky mutex_unlock(&zram_index_mutex);
24006566d1a3SSergey Senozhatsky return ret ? ret : count;
24016566d1a3SSergey Senozhatsky }
240227104a53SGreg Kroah-Hartman static CLASS_ATTR_WO(hot_remove);
24036566d1a3SSergey Senozhatsky
240427104a53SGreg Kroah-Hartman static struct attribute *zram_control_class_attrs[] = {
240527104a53SGreg Kroah-Hartman &class_attr_hot_add.attr,
240627104a53SGreg Kroah-Hartman &class_attr_hot_remove.attr,
240727104a53SGreg Kroah-Hartman NULL,
24086566d1a3SSergey Senozhatsky };
240927104a53SGreg Kroah-Hartman ATTRIBUTE_GROUPS(zram_control_class);
24106566d1a3SSergey Senozhatsky
24116566d1a3SSergey Senozhatsky static struct class zram_control_class = {
24126566d1a3SSergey Senozhatsky .name = "zram-control",
241327104a53SGreg Kroah-Hartman .class_groups = zram_control_class_groups,
24146566d1a3SSergey Senozhatsky };
24156566d1a3SSergey Senozhatsky
zram_remove_cb(int id,void * ptr,void * data)241685508ec6SSergey Senozhatsky static int zram_remove_cb(int id, void *ptr, void *data)
241785508ec6SSergey Senozhatsky {
24188c54499aSMing Lei WARN_ON_ONCE(zram_remove(ptr));
241985508ec6SSergey Senozhatsky return 0;
242085508ec6SSergey Senozhatsky }
242185508ec6SSergey Senozhatsky
destroy_devices(void)242285508ec6SSergey Senozhatsky static void destroy_devices(void)
242385508ec6SSergey Senozhatsky {
24246566d1a3SSergey Senozhatsky class_unregister(&zram_control_class);
242585508ec6SSergey Senozhatsky idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
2426c0265342SMinchan Kim zram_debugfs_destroy();
242785508ec6SSergey Senozhatsky idr_destroy(&zram_index_idr);
2428a096cafcSSergey Senozhatsky unregister_blkdev(zram_major, "zram");
24291dd6c834SAnna-Maria Gleixner cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2430a096cafcSSergey Senozhatsky }
2431a096cafcSSergey Senozhatsky
zram_init(void)2432cd67e10aSMinchan Kim static int __init zram_init(void)
2433cd67e10aSMinchan Kim {
243492ff1528SSergey Senozhatsky int ret;
2435cd67e10aSMinchan Kim
2436f635725cSSergey Senozhatsky BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > BITS_PER_LONG);
2437f635725cSSergey Senozhatsky
24381dd6c834SAnna-Maria Gleixner ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
24391dd6c834SAnna-Maria Gleixner zcomp_cpu_up_prepare, zcomp_cpu_dead);
24401dd6c834SAnna-Maria Gleixner if (ret < 0)
24411dd6c834SAnna-Maria Gleixner return ret;
24421dd6c834SAnna-Maria Gleixner
24436566d1a3SSergey Senozhatsky ret = class_register(&zram_control_class);
24446566d1a3SSergey Senozhatsky if (ret) {
244570864969SSergey Senozhatsky pr_err("Unable to register zram-control class\n");
24461dd6c834SAnna-Maria Gleixner cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
24476566d1a3SSergey Senozhatsky return ret;
24486566d1a3SSergey Senozhatsky }
24496566d1a3SSergey Senozhatsky
2450c0265342SMinchan Kim zram_debugfs_create();
2451cd67e10aSMinchan Kim zram_major = register_blkdev(0, "zram");
2452cd67e10aSMinchan Kim if (zram_major <= 0) {
245370864969SSergey Senozhatsky pr_err("Unable to get major number\n");
24546566d1a3SSergey Senozhatsky class_unregister(&zram_control_class);
24551dd6c834SAnna-Maria Gleixner cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
2456a096cafcSSergey Senozhatsky return -EBUSY;
2457cd67e10aSMinchan Kim }
2458cd67e10aSMinchan Kim
245992ff1528SSergey Senozhatsky while (num_devices != 0) {
24606566d1a3SSergey Senozhatsky mutex_lock(&zram_index_mutex);
246192ff1528SSergey Senozhatsky ret = zram_add();
24626566d1a3SSergey Senozhatsky mutex_unlock(&zram_index_mutex);
246392ff1528SSergey Senozhatsky if (ret < 0)
2464a096cafcSSergey Senozhatsky goto out_error;
246592ff1528SSergey Senozhatsky num_devices--;
2466cd67e10aSMinchan Kim }
2467cd67e10aSMinchan Kim
2468cd67e10aSMinchan Kim return 0;
2469cd67e10aSMinchan Kim
2470a096cafcSSergey Senozhatsky out_error:
247185508ec6SSergey Senozhatsky destroy_devices();
2472cd67e10aSMinchan Kim return ret;
2473cd67e10aSMinchan Kim }
2474cd67e10aSMinchan Kim
zram_exit(void)2475cd67e10aSMinchan Kim static void __exit zram_exit(void)
2476cd67e10aSMinchan Kim {
247785508ec6SSergey Senozhatsky destroy_devices();
2478cd67e10aSMinchan Kim }
2479cd67e10aSMinchan Kim
2480cd67e10aSMinchan Kim module_init(zram_init);
2481cd67e10aSMinchan Kim module_exit(zram_exit);
2482cd67e10aSMinchan Kim
2483cd67e10aSMinchan Kim module_param(num_devices, uint, 0);
2484c3cdb40eSSergey Senozhatsky MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
2485cd67e10aSMinchan Kim
2486cd67e10aSMinchan Kim MODULE_LICENSE("Dual BSD/GPL");
2487cd67e10aSMinchan Kim MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
2488cd67e10aSMinchan Kim MODULE_DESCRIPTION("Compressed RAM Block Device");
2489