zram_drv.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Compressed RAM block device
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Nitin Gupta
  5. * 2012, 2013 Minchan Kim
  6. *
  7. * This code is released using a dual license strategy: BSD/GPL
  8. * You can choose the licence that better fits your requirements.
  9. *
  10. * Released under the terms of 3-clause BSD License
  11. * Released under the terms of GNU General Public License Version 2.0
  12. *
  13. */
  14. #define KMSG_COMPONENT "zram"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #ifdef CONFIG_ZRAM_DEBUG
  17. #define DEBUG
  18. #endif
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bio.h>
  22. #include <linux/bitops.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/device.h>
  26. #include <linux/genhd.h>
  27. #include <linux/highmem.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/err.h>
  32. #include "zram_drv.h"
  33. /* Globals */
  34. static int zram_major;
  35. static struct zram *zram_devices;
  36. static const char *default_compressor = "lzo";
  37. /* Module params (documentation at end) */
  38. static unsigned int num_devices = 1;
  39. static inline void deprecated_attr_warn(const char *name)
  40. {
  41. pr_warn_once("%d (%s) Attribute %s (and others) will be removed. %s\n",
  42. task_pid_nr(current),
  43. current->comm,
  44. name,
  45. "See zram documentation.");
  46. }
  47. #define ZRAM_ATTR_RO(name) \
  48. static ssize_t zram_attr_##name##_show(struct device *d, \
  49. struct device_attribute *attr, char *b) \
  50. { \
  51. struct zram *zram = dev_to_zram(d); \
  52. \
  53. deprecated_attr_warn(__stringify(name)); \
  54. return scnprintf(b, PAGE_SIZE, "%llu\n", \
  55. (u64)atomic64_read(&zram->stats.name)); \
  56. } \
  57. static struct device_attribute dev_attr_##name = \
  58. __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL);
  59. static inline bool init_done(struct zram *zram)
  60. {
  61. return zram->disksize;
  62. }
  63. static inline struct zram *dev_to_zram(struct device *dev)
  64. {
  65. return (struct zram *)dev_to_disk(dev)->private_data;
  66. }
  67. static ssize_t compact_store(struct device *dev,
  68. struct device_attribute *attr, const char *buf, size_t len)
  69. {
  70. unsigned long nr_migrated;
  71. struct zram *zram = dev_to_zram(dev);
  72. struct zram_meta *meta;
  73. down_read(&zram->init_lock);
  74. if (!init_done(zram)) {
  75. up_read(&zram->init_lock);
  76. return -EINVAL;
  77. }
  78. meta = zram->meta;
  79. nr_migrated = zs_compact(meta->mem_pool);
  80. atomic64_add(nr_migrated, &zram->stats.num_migrated);
  81. up_read(&zram->init_lock);
  82. return len;
  83. }
  84. static ssize_t disksize_show(struct device *dev,
  85. struct device_attribute *attr, char *buf)
  86. {
  87. struct zram *zram = dev_to_zram(dev);
  88. return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
  89. }
  90. static ssize_t initstate_show(struct device *dev,
  91. struct device_attribute *attr, char *buf)
  92. {
  93. u32 val;
  94. struct zram *zram = dev_to_zram(dev);
  95. down_read(&zram->init_lock);
  96. val = init_done(zram);
  97. up_read(&zram->init_lock);
  98. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  99. }
  100. static ssize_t orig_data_size_show(struct device *dev,
  101. struct device_attribute *attr, char *buf)
  102. {
  103. struct zram *zram = dev_to_zram(dev);
  104. deprecated_attr_warn("orig_data_size");
  105. return scnprintf(buf, PAGE_SIZE, "%llu\n",
  106. (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
  107. }
  108. static ssize_t mem_used_total_show(struct device *dev,
  109. struct device_attribute *attr, char *buf)
  110. {
  111. u64 val = 0;
  112. struct zram *zram = dev_to_zram(dev);
  113. deprecated_attr_warn("mem_used_total");
  114. down_read(&zram->init_lock);
  115. if (init_done(zram)) {
  116. struct zram_meta *meta = zram->meta;
  117. val = zs_get_total_pages(meta->mem_pool);
  118. }
  119. up_read(&zram->init_lock);
  120. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  121. }
  122. static ssize_t max_comp_streams_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. int val;
  126. struct zram *zram = dev_to_zram(dev);
  127. down_read(&zram->init_lock);
  128. val = zram->max_comp_streams;
  129. up_read(&zram->init_lock);
  130. return scnprintf(buf, PAGE_SIZE, "%d\n", val);
  131. }
  132. static ssize_t mem_limit_show(struct device *dev,
  133. struct device_attribute *attr, char *buf)
  134. {
  135. u64 val;
  136. struct zram *zram = dev_to_zram(dev);
  137. deprecated_attr_warn("mem_limit");
  138. down_read(&zram->init_lock);
  139. val = zram->limit_pages;
  140. up_read(&zram->init_lock);
  141. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  142. }
  143. static ssize_t mem_limit_store(struct device *dev,
  144. struct device_attribute *attr, const char *buf, size_t len)
  145. {
  146. u64 limit;
  147. char *tmp;
  148. struct zram *zram = dev_to_zram(dev);
  149. limit = memparse(buf, &tmp);
  150. if (buf == tmp) /* no chars parsed, invalid input */
  151. return -EINVAL;
  152. down_write(&zram->init_lock);
  153. zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
  154. up_write(&zram->init_lock);
  155. return len;
  156. }
  157. static ssize_t mem_used_max_show(struct device *dev,
  158. struct device_attribute *attr, char *buf)
  159. {
  160. u64 val = 0;
  161. struct zram *zram = dev_to_zram(dev);
  162. deprecated_attr_warn("mem_used_max");
  163. down_read(&zram->init_lock);
  164. if (init_done(zram))
  165. val = atomic_long_read(&zram->stats.max_used_pages);
  166. up_read(&zram->init_lock);
  167. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  168. }
  169. static ssize_t mem_used_max_store(struct device *dev,
  170. struct device_attribute *attr, const char *buf, size_t len)
  171. {
  172. int err;
  173. unsigned long val;
  174. struct zram *zram = dev_to_zram(dev);
  175. err = kstrtoul(buf, 10, &val);
  176. if (err || val != 0)
  177. return -EINVAL;
  178. down_read(&zram->init_lock);
  179. if (init_done(zram)) {
  180. struct zram_meta *meta = zram->meta;
  181. atomic_long_set(&zram->stats.max_used_pages,
  182. zs_get_total_pages(meta->mem_pool));
  183. }
  184. up_read(&zram->init_lock);
  185. return len;
  186. }
  187. static ssize_t max_comp_streams_store(struct device *dev,
  188. struct device_attribute *attr, const char *buf, size_t len)
  189. {
  190. int num;
  191. struct zram *zram = dev_to_zram(dev);
  192. int ret;
  193. ret = kstrtoint(buf, 0, &num);
  194. if (ret < 0)
  195. return ret;
  196. if (num < 1)
  197. return -EINVAL;
  198. down_write(&zram->init_lock);
  199. if (init_done(zram)) {
  200. if (!zcomp_set_max_streams(zram->comp, num)) {
  201. pr_info("Cannot change max compression streams\n");
  202. ret = -EINVAL;
  203. goto out;
  204. }
  205. }
  206. zram->max_comp_streams = num;
  207. ret = len;
  208. out:
  209. up_write(&zram->init_lock);
  210. return ret;
  211. }
  212. static ssize_t comp_algorithm_show(struct device *dev,
  213. struct device_attribute *attr, char *buf)
  214. {
  215. size_t sz;
  216. struct zram *zram = dev_to_zram(dev);
  217. down_read(&zram->init_lock);
  218. sz = zcomp_available_show(zram->compressor, buf);
  219. up_read(&zram->init_lock);
  220. return sz;
  221. }
  222. static ssize_t comp_algorithm_store(struct device *dev,
  223. struct device_attribute *attr, const char *buf, size_t len)
  224. {
  225. struct zram *zram = dev_to_zram(dev);
  226. down_write(&zram->init_lock);
  227. if (init_done(zram)) {
  228. up_write(&zram->init_lock);
  229. pr_info("Can't change algorithm for initialized device\n");
  230. return -EBUSY;
  231. }
  232. strlcpy(zram->compressor, buf, sizeof(zram->compressor));
  233. up_write(&zram->init_lock);
  234. return len;
  235. }
  236. /* flag operations needs meta->tb_lock */
  237. static int zram_test_flag(struct zram_meta *meta, u32 index,
  238. enum zram_pageflags flag)
  239. {
  240. return meta->table[index].value & BIT(flag);
  241. }
  242. static void zram_set_flag(struct zram_meta *meta, u32 index,
  243. enum zram_pageflags flag)
  244. {
  245. meta->table[index].value |= BIT(flag);
  246. }
  247. static void zram_clear_flag(struct zram_meta *meta, u32 index,
  248. enum zram_pageflags flag)
  249. {
  250. meta->table[index].value &= ~BIT(flag);
  251. }
  252. static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
  253. {
  254. return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
  255. }
  256. static void zram_set_obj_size(struct zram_meta *meta,
  257. u32 index, size_t size)
  258. {
  259. unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
  260. meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
  261. }
  262. static inline int is_partial_io(struct bio_vec *bvec)
  263. {
  264. return bvec->bv_len != PAGE_SIZE;
  265. }
  266. /*
  267. * Check if request is within bounds and aligned on zram logical blocks.
  268. */
  269. static inline int valid_io_request(struct zram *zram,
  270. sector_t start, unsigned int size)
  271. {
  272. u64 end, bound;
  273. /* unaligned request */
  274. if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
  275. return 0;
  276. if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
  277. return 0;
  278. end = start + (size >> SECTOR_SHIFT);
  279. bound = zram->disksize >> SECTOR_SHIFT;
  280. /* out of range range */
  281. if (unlikely(start >= bound || end > bound || start > end))
  282. return 0;
  283. /* I/O request is valid */
  284. return 1;
  285. }
  286. static void zram_meta_free(struct zram_meta *meta, u64 disksize)
  287. {
  288. size_t num_pages = disksize >> PAGE_SHIFT;
  289. size_t index;
  290. /* Free all pages that are still in this zram device */
  291. for (index = 0; index < num_pages; index++) {
  292. unsigned long handle = meta->table[index].handle;
  293. if (!handle)
  294. continue;
  295. zs_free(meta->mem_pool, handle);
  296. }
  297. zs_destroy_pool(meta->mem_pool);
  298. vfree(meta->table);
  299. kfree(meta);
  300. }
  301. static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
  302. {
  303. size_t num_pages;
  304. char pool_name[8];
  305. struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
  306. if (!meta)
  307. return NULL;
  308. num_pages = disksize >> PAGE_SHIFT;
  309. meta->table = vzalloc(num_pages * sizeof(*meta->table));
  310. if (!meta->table) {
  311. pr_err("Error allocating zram address table\n");
  312. goto out_error;
  313. }
  314. snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);
  315. meta->mem_pool = zs_create_pool(pool_name, GFP_NOIO | __GFP_HIGHMEM);
  316. if (!meta->mem_pool) {
  317. pr_err("Error creating memory pool\n");
  318. goto out_error;
  319. }
  320. return meta;
  321. out_error:
  322. vfree(meta->table);
  323. kfree(meta);
  324. return NULL;
  325. }
  326. static inline bool zram_meta_get(struct zram *zram)
  327. {
  328. if (atomic_inc_not_zero(&zram->refcount))
  329. return true;
  330. return false;
  331. }
  332. static inline void zram_meta_put(struct zram *zram)
  333. {
  334. atomic_dec(&zram->refcount);
  335. }
  336. static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
  337. {
  338. if (*offset + bvec->bv_len >= PAGE_SIZE)
  339. (*index)++;
  340. *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
  341. }
  342. static int page_zero_filled(void *ptr)
  343. {
  344. unsigned int pos;
  345. unsigned long *page;
  346. page = (unsigned long *)ptr;
  347. for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
  348. if (page[pos])
  349. return 0;
  350. }
  351. return 1;
  352. }
  353. static void handle_zero_page(struct bio_vec *bvec)
  354. {
  355. struct page *page = bvec->bv_page;
  356. void *user_mem;
  357. user_mem = kmap_atomic(page);
  358. if (is_partial_io(bvec))
  359. memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
  360. else
  361. clear_page(user_mem);
  362. kunmap_atomic(user_mem);
  363. flush_dcache_page(page);
  364. }
  365. /*
  366. * To protect concurrent access to the same index entry,
  367. * caller should hold this table index entry's bit_spinlock to
  368. * indicate this index entry is accessing.
  369. */
  370. static void zram_free_page(struct zram *zram, size_t index)
  371. {
  372. struct zram_meta *meta = zram->meta;
  373. unsigned long handle = meta->table[index].handle;
  374. if (unlikely(!handle)) {
  375. /*
  376. * No memory is allocated for zero filled pages.
  377. * Simply clear zero page flag.
  378. */
  379. if (zram_test_flag(meta, index, ZRAM_ZERO)) {
  380. zram_clear_flag(meta, index, ZRAM_ZERO);
  381. atomic64_dec(&zram->stats.zero_pages);
  382. }
  383. return;
  384. }
  385. zs_free(meta->mem_pool, handle);
  386. atomic64_sub(zram_get_obj_size(meta, index),
  387. &zram->stats.compr_data_size);
  388. atomic64_dec(&zram->stats.pages_stored);
  389. meta->table[index].handle = 0;
  390. zram_set_obj_size(meta, index, 0);
  391. }
  392. static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  393. {
  394. int ret = 0;
  395. unsigned char *cmem;
  396. struct zram_meta *meta = zram->meta;
  397. unsigned long handle;
  398. size_t size;
  399. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  400. handle = meta->table[index].handle;
  401. size = zram_get_obj_size(meta, index);
  402. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  403. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  404. memset(mem, 0, PAGE_SIZE);
  405. return 0;
  406. }
  407. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
  408. if (size == PAGE_SIZE)
  409. memcpy(mem, cmem, PAGE_SIZE);
  410. else
  411. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  412. zs_unmap_object(meta->mem_pool, handle);
  413. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  414. /* Should NEVER happen. Return bio error if it does. */
  415. if (unlikely(ret)) {
  416. pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
  417. return ret;
  418. }
  419. return 0;
  420. }
  421. static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  422. u32 index, int offset)
  423. {
  424. int ret;
  425. struct page *page;
  426. unsigned char *user_mem, *uncmem = NULL;
  427. struct zram_meta *meta = zram->meta;
  428. page = bvec->bv_page;
  429. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  430. if (unlikely(!meta->table[index].handle) ||
  431. zram_test_flag(meta, index, ZRAM_ZERO)) {
  432. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  433. handle_zero_page(bvec);
  434. return 0;
  435. }
  436. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  437. if (is_partial_io(bvec))
  438. /* Use a temporary buffer to decompress the page */
  439. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  440. user_mem = kmap_atomic(page);
  441. if (!is_partial_io(bvec))
  442. uncmem = user_mem;
  443. if (!uncmem) {
  444. pr_info("Unable to allocate temp memory\n");
  445. ret = -ENOMEM;
  446. goto out_cleanup;
  447. }
  448. ret = zram_decompress_page(zram, uncmem, index);
  449. /* Should NEVER happen. Return bio error if it does. */
  450. if (unlikely(ret))
  451. goto out_cleanup;
  452. if (is_partial_io(bvec))
  453. memcpy(user_mem + bvec->bv_offset, uncmem + offset,
  454. bvec->bv_len);
  455. flush_dcache_page(page);
  456. ret = 0;
  457. out_cleanup:
  458. kunmap_atomic(user_mem);
  459. if (is_partial_io(bvec))
  460. kfree(uncmem);
  461. return ret;
  462. }
  463. static inline void update_used_max(struct zram *zram,
  464. const unsigned long pages)
  465. {
  466. unsigned long old_max, cur_max;
  467. old_max = atomic_long_read(&zram->stats.max_used_pages);
  468. do {
  469. cur_max = old_max;
  470. if (pages > cur_max)
  471. old_max = atomic_long_cmpxchg(
  472. &zram->stats.max_used_pages, cur_max, pages);
  473. } while (old_max != cur_max);
  474. }
  475. static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  476. int offset)
  477. {
  478. int ret = 0;
  479. size_t clen;
  480. unsigned long handle;
  481. struct page *page;
  482. unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
  483. struct zram_meta *meta = zram->meta;
  484. struct zcomp_strm *zstrm;
  485. bool locked = false;
  486. unsigned long alloced_pages;
  487. page = bvec->bv_page;
  488. if (is_partial_io(bvec)) {
  489. /*
  490. * This is a partial IO. We need to read the full page
  491. * before to write the changes.
  492. */
  493. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  494. if (!uncmem) {
  495. ret = -ENOMEM;
  496. goto out;
  497. }
  498. ret = zram_decompress_page(zram, uncmem, index);
  499. if (ret)
  500. goto out;
  501. }
  502. zstrm = zcomp_strm_find(zram->comp);
  503. locked = true;
  504. user_mem = kmap_atomic(page);
  505. if (is_partial_io(bvec)) {
  506. memcpy(uncmem + offset, user_mem + bvec->bv_offset,
  507. bvec->bv_len);
  508. kunmap_atomic(user_mem);
  509. user_mem = NULL;
  510. } else {
  511. uncmem = user_mem;
  512. }
  513. if (page_zero_filled(uncmem)) {
  514. if (user_mem)
  515. kunmap_atomic(user_mem);
  516. /* Free memory associated with this sector now. */
  517. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  518. zram_free_page(zram, index);
  519. zram_set_flag(meta, index, ZRAM_ZERO);
  520. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  521. atomic64_inc(&zram->stats.zero_pages);
  522. ret = 0;
  523. goto out;
  524. }
  525. ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
  526. if (!is_partial_io(bvec)) {
  527. kunmap_atomic(user_mem);
  528. user_mem = NULL;
  529. uncmem = NULL;
  530. }
  531. if (unlikely(ret)) {
  532. pr_err("Compression failed! err=%d\n", ret);
  533. goto out;
  534. }
  535. src = zstrm->buffer;
  536. if (unlikely(clen > max_zpage_size)) {
  537. clen = PAGE_SIZE;
  538. if (is_partial_io(bvec))
  539. src = uncmem;
  540. }
  541. handle = zs_malloc(meta->mem_pool, clen);
  542. if (!handle) {
  543. pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
  544. index, clen);
  545. ret = -ENOMEM;
  546. goto out;
  547. }
  548. alloced_pages = zs_get_total_pages(meta->mem_pool);
  549. if (zram->limit_pages && alloced_pages > zram->limit_pages) {
  550. zs_free(meta->mem_pool, handle);
  551. ret = -ENOMEM;
  552. goto out;
  553. }
  554. update_used_max(zram, alloced_pages);
  555. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
  556. if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
  557. src = kmap_atomic(page);
  558. memcpy(cmem, src, PAGE_SIZE);
  559. kunmap_atomic(src);
  560. } else {
  561. memcpy(cmem, src, clen);
  562. }
  563. zcomp_strm_release(zram->comp, zstrm);
  564. locked = false;
  565. zs_unmap_object(meta->mem_pool, handle);
  566. /*
  567. * Free memory associated with this sector
  568. * before overwriting unused sectors.
  569. */
  570. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  571. zram_free_page(zram, index);
  572. meta->table[index].handle = handle;
  573. zram_set_obj_size(meta, index, clen);
  574. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  575. /* Update stats */
  576. atomic64_add(clen, &zram->stats.compr_data_size);
  577. atomic64_inc(&zram->stats.pages_stored);
  578. out:
  579. if (locked)
  580. zcomp_strm_release(zram->comp, zstrm);
  581. if (is_partial_io(bvec))
  582. kfree(uncmem);
  583. return ret;
  584. }
  585. static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
  586. int offset, int rw)
  587. {
  588. int ret;
  589. if (rw == READ) {
  590. atomic64_inc(&zram->stats.num_reads);
  591. ret = zram_bvec_read(zram, bvec, index, offset);
  592. } else {
  593. atomic64_inc(&zram->stats.num_writes);
  594. ret = zram_bvec_write(zram, bvec, index, offset);
  595. }
  596. if (unlikely(ret)) {
  597. if (rw == READ)
  598. atomic64_inc(&zram->stats.failed_reads);
  599. else
  600. atomic64_inc(&zram->stats.failed_writes);
  601. }
  602. return ret;
  603. }
  604. /*
  605. * zram_bio_discard - handler on discard request
  606. * @index: physical block index in PAGE_SIZE units
  607. * @offset: byte offset within physical block
  608. */
  609. static void zram_bio_discard(struct zram *zram, u32 index,
  610. int offset, struct bio *bio)
  611. {
  612. size_t n = bio->bi_size;
  613. struct zram_meta *meta = zram->meta;
  614. /*
  615. * zram manages data in physical block size units. Because logical block
  616. * size isn't identical with physical block size on some arch, we
  617. * could get a discard request pointing to a specific offset within a
  618. * certain physical block. Although we can handle this request by
  619. * reading that physiclal block and decompressing and partially zeroing
  620. * and re-compressing and then re-storing it, this isn't reasonable
  621. * because our intent with a discard request is to save memory. So
  622. * skipping this logical block is appropriate here.
  623. */
  624. if (offset) {
  625. if (n <= (PAGE_SIZE - offset))
  626. return;
  627. n -= (PAGE_SIZE - offset);
  628. index++;
  629. }
  630. while (n >= PAGE_SIZE) {
  631. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  632. zram_free_page(zram, index);
  633. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  634. atomic64_inc(&zram->stats.notify_free);
  635. index++;
  636. n -= PAGE_SIZE;
  637. }
  638. }
  639. static void zram_reset_device(struct zram *zram)
  640. {
  641. struct zram_meta *meta;
  642. struct zcomp *comp;
  643. u64 disksize;
  644. down_write(&zram->init_lock);
  645. zram->limit_pages = 0;
  646. if (!init_done(zram)) {
  647. up_write(&zram->init_lock);
  648. return;
  649. }
  650. meta = zram->meta;
  651. comp = zram->comp;
  652. disksize = zram->disksize;
  653. /*
  654. * Refcount will go down to 0 eventually and r/w handler
  655. * cannot handle further I/O so it will bail out by
  656. * check zram_meta_get.
  657. */
  658. zram_meta_put(zram);
  659. /*
  660. * We want to free zram_meta in process context to avoid
  661. * deadlock between reclaim path and any other locks.
  662. */
  663. wait_event(zram->io_done, atomic_read(&zram->refcount) == 0);
  664. /* Reset stats */
  665. memset(&zram->stats, 0, sizeof(zram->stats));
  666. zram->disksize = 0;
  667. zram->max_comp_streams = 1;
  668. set_capacity(zram->disk, 0);
  669. part_stat_set_all(&zram->disk->part0, 0);
  670. up_write(&zram->init_lock);
  671. /* I/O operation under all of CPU are done so let's free */
  672. zram_meta_free(meta, disksize);
  673. zcomp_destroy(comp);
  674. }
  675. static ssize_t disksize_store(struct device *dev,
  676. struct device_attribute *attr, const char *buf, size_t len)
  677. {
  678. u64 disksize;
  679. struct zcomp *comp;
  680. struct zram_meta *meta;
  681. struct zram *zram = dev_to_zram(dev);
  682. int err;
  683. disksize = memparse(buf, NULL);
  684. if (!disksize)
  685. return -EINVAL;
  686. disksize = PAGE_ALIGN(disksize);
  687. meta = zram_meta_alloc(zram->disk->first_minor, disksize);
  688. if (!meta)
  689. return -ENOMEM;
  690. comp = zcomp_create(zram->compressor, zram->max_comp_streams);
  691. if (IS_ERR(comp)) {
  692. pr_info("Cannot initialise %s compressing backend\n",
  693. zram->compressor);
  694. err = PTR_ERR(comp);
  695. goto out_free_meta;
  696. }
  697. down_write(&zram->init_lock);
  698. if (init_done(zram)) {
  699. pr_info("Cannot change disksize for initialized device\n");
  700. err = -EBUSY;
  701. goto out_destroy_comp;
  702. }
  703. init_waitqueue_head(&zram->io_done);
  704. atomic_set(&zram->refcount, 1);
  705. zram->meta = meta;
  706. zram->comp = comp;
  707. zram->disksize = disksize;
  708. set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
  709. up_write(&zram->init_lock);
  710. /*
  711. * Revalidate disk out of the init_lock to avoid lockdep splat.
  712. * It's okay because disk's capacity is protected by init_lock
  713. * so that revalidate_disk always sees up-to-date capacity.
  714. */
  715. revalidate_disk(zram->disk);
  716. return len;
  717. out_destroy_comp:
  718. up_write(&zram->init_lock);
  719. zcomp_destroy(comp);
  720. out_free_meta:
  721. zram_meta_free(meta, disksize);
  722. return err;
  723. }
  724. static ssize_t reset_store(struct device *dev,
  725. struct device_attribute *attr, const char *buf, size_t len)
  726. {
  727. int ret;
  728. unsigned short do_reset;
  729. struct zram *zram;
  730. struct block_device *bdev;
  731. zram = dev_to_zram(dev);
  732. bdev = bdget_disk(zram->disk, 0);
  733. if (!bdev)
  734. return -ENOMEM;
  735. mutex_lock(&bdev->bd_mutex);
  736. /* Do not reset an active device! */
  737. if (bdev->bd_openers) {
  738. ret = -EBUSY;
  739. goto out;
  740. }
  741. ret = kstrtou16(buf, 10, &do_reset);
  742. if (ret)
  743. goto out;
  744. if (!do_reset) {
  745. ret = -EINVAL;
  746. goto out;
  747. }
  748. /* Make sure all pending I/O is finished */
  749. fsync_bdev(bdev);
  750. zram_reset_device(zram);
  751. mutex_unlock(&bdev->bd_mutex);
  752. revalidate_disk(zram->disk);
  753. bdput(bdev);
  754. return len;
  755. out:
  756. mutex_unlock(&bdev->bd_mutex);
  757. bdput(bdev);
  758. return ret;
  759. }
  760. static void __zram_make_request(struct zram *zram, struct bio *bio)
  761. {
  762. int i, offset, rw;
  763. u32 index;
  764. struct bio_vec *bvec;
  765. index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
  766. offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
  767. if (unlikely(bio->bi_rw & REQ_DISCARD)) {
  768. zram_bio_discard(zram, index, offset, bio);
  769. bio_endio(bio, 0);
  770. return;
  771. }
  772. rw = bio_data_dir(bio);
  773. bio_for_each_segment(bvec, bio, i) {
  774. int max_transfer_size = PAGE_SIZE - offset;
  775. if (bvec->bv_len > max_transfer_size) {
  776. /*
  777. * zram_bvec_rw() can only make operation on a single
  778. * zram page. Split the bio vector.
  779. */
  780. struct bio_vec bv;
  781. bv.bv_page = bvec->bv_page;
  782. bv.bv_len = max_transfer_size;
  783. bv.bv_offset = bvec->bv_offset;
  784. if (zram_bvec_rw(zram, &bv, index, offset, rw) < 0)
  785. goto out;
  786. bv.bv_len = bvec->bv_len - max_transfer_size;
  787. bv.bv_offset += max_transfer_size;
  788. if (zram_bvec_rw(zram, &bv, index + 1, 0, rw) < 0)
  789. goto out;
  790. } else
  791. if (zram_bvec_rw(zram, bvec, index, offset, rw) < 0)
  792. goto out;
  793. update_position(&index, &offset, bvec);
  794. }
  795. set_bit(BIO_UPTODATE, &bio->bi_flags);
  796. bio_endio(bio, 0);
  797. return;
  798. out:
  799. bio_io_error(bio);
  800. }
  801. /*
  802. * Handler function for all zram I/O requests.
  803. */
  804. static void zram_make_request(struct request_queue *queue, struct bio *bio)
  805. {
  806. struct zram *zram = queue->queuedata;
  807. if (unlikely(!zram_meta_get(zram)))
  808. goto error;
  809. if (!valid_io_request(zram, bio->bi_sector,
  810. bio->bi_size)) {
  811. atomic64_inc(&zram->stats.invalid_io);
  812. goto put_zram;
  813. }
  814. __zram_make_request(zram, bio);
  815. zram_meta_put(zram);
  816. return;
  817. put_zram:
  818. zram_meta_put(zram);
  819. error:
  820. bio_io_error(bio);
  821. }
  822. static void zram_slot_free_notify(struct block_device *bdev,
  823. unsigned long index)
  824. {
  825. struct zram *zram;
  826. struct zram_meta *meta;
  827. zram = bdev->bd_disk->private_data;
  828. meta = zram->meta;
  829. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  830. zram_free_page(zram, index);
  831. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  832. atomic64_inc(&zram->stats.notify_free);
  833. }
  834. static const struct block_device_operations zram_devops = {
  835. .swap_slot_free_notify = zram_slot_free_notify,
  836. .owner = THIS_MODULE
  837. };
  838. static DEVICE_ATTR(compact, S_IWUSR, NULL, compact_store);
  839. static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
  840. disksize_show, disksize_store);
  841. static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
  842. static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
  843. static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
  844. static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
  845. static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
  846. mem_limit_store);
  847. static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
  848. mem_used_max_store);
  849. static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
  850. max_comp_streams_show, max_comp_streams_store);
  851. static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
  852. comp_algorithm_show, comp_algorithm_store);
  853. static ssize_t io_stat_show(struct device *dev,
  854. struct device_attribute *attr, char *buf)
  855. {
  856. struct zram *zram = dev_to_zram(dev);
  857. ssize_t ret;
  858. down_read(&zram->init_lock);
  859. ret = scnprintf(buf, PAGE_SIZE,
  860. "%8llu %8llu %8llu %8llu\n",
  861. (u64)atomic64_read(&zram->stats.failed_reads),
  862. (u64)atomic64_read(&zram->stats.failed_writes),
  863. (u64)atomic64_read(&zram->stats.invalid_io),
  864. (u64)atomic64_read(&zram->stats.notify_free));
  865. up_read(&zram->init_lock);
  866. return ret;
  867. }
  868. static ssize_t mm_stat_show(struct device *dev,
  869. struct device_attribute *attr, char *buf)
  870. {
  871. struct zram *zram = dev_to_zram(dev);
  872. u64 orig_size, mem_used = 0;
  873. long max_used;
  874. ssize_t ret;
  875. down_read(&zram->init_lock);
  876. if (init_done(zram))
  877. mem_used = zs_get_total_pages(zram->meta->mem_pool);
  878. orig_size = atomic64_read(&zram->stats.pages_stored);
  879. max_used = atomic_long_read(&zram->stats.max_used_pages);
  880. ret = scnprintf(buf, PAGE_SIZE,
  881. "%8llu %8llu %8llu %8lu %8ld %8llu %8llu\n",
  882. orig_size << PAGE_SHIFT,
  883. (u64)atomic64_read(&zram->stats.compr_data_size),
  884. mem_used << PAGE_SHIFT,
  885. zram->limit_pages << PAGE_SHIFT,
  886. max_used << PAGE_SHIFT,
  887. (u64)atomic64_read(&zram->stats.zero_pages),
  888. (u64)atomic64_read(&zram->stats.num_migrated));
  889. up_read(&zram->init_lock);
  890. return ret;
  891. }
  892. static DEVICE_ATTR(io_stat, S_IRUGO, io_stat_show, NULL);
  893. static DEVICE_ATTR(mm_stat, S_IRUGO, mm_stat_show, NULL);
  894. ZRAM_ATTR_RO(num_reads);
  895. ZRAM_ATTR_RO(num_writes);
  896. ZRAM_ATTR_RO(failed_reads);
  897. ZRAM_ATTR_RO(failed_writes);
  898. ZRAM_ATTR_RO(invalid_io);
  899. ZRAM_ATTR_RO(notify_free);
  900. ZRAM_ATTR_RO(zero_pages);
  901. ZRAM_ATTR_RO(compr_data_size);
  902. static struct attribute *zram_disk_attrs[] = {
  903. &dev_attr_disksize.attr,
  904. &dev_attr_initstate.attr,
  905. &dev_attr_reset.attr,
  906. &dev_attr_num_reads.attr,
  907. &dev_attr_num_writes.attr,
  908. &dev_attr_failed_reads.attr,
  909. &dev_attr_failed_writes.attr,
  910. &dev_attr_compact.attr,
  911. &dev_attr_invalid_io.attr,
  912. &dev_attr_notify_free.attr,
  913. &dev_attr_zero_pages.attr,
  914. &dev_attr_orig_data_size.attr,
  915. &dev_attr_compr_data_size.attr,
  916. &dev_attr_mem_used_total.attr,
  917. &dev_attr_mem_limit.attr,
  918. &dev_attr_mem_used_max.attr,
  919. &dev_attr_max_comp_streams.attr,
  920. &dev_attr_comp_algorithm.attr,
  921. &dev_attr_io_stat.attr,
  922. &dev_attr_mm_stat.attr,
  923. NULL,
  924. };
  925. static struct attribute_group zram_disk_attr_group = {
  926. .attrs = zram_disk_attrs,
  927. };
  928. static int create_device(struct zram *zram, int device_id)
  929. {
  930. struct request_queue *queue;
  931. int ret = -ENOMEM;
  932. init_rwsem(&zram->init_lock);
  933. queue = blk_alloc_queue(GFP_KERNEL);
  934. if (!queue) {
  935. pr_err("Error allocating disk queue for device %d\n",
  936. device_id);
  937. goto out;
  938. }
  939. blk_queue_make_request(queue, zram_make_request);
  940. /* gendisk structure */
  941. zram->disk = alloc_disk(1);
  942. if (!zram->disk) {
  943. pr_warn("Error allocating disk structure for device %d\n",
  944. device_id);
  945. ret = -ENOMEM;
  946. goto out_free_queue;
  947. }
  948. zram->disk->major = zram_major;
  949. zram->disk->first_minor = device_id;
  950. zram->disk->fops = &zram_devops;
  951. zram->disk->queue = queue;
  952. zram->disk->queue->queuedata = zram;
  953. zram->disk->private_data = zram;
  954. snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
  955. /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
  956. set_capacity(zram->disk, 0);
  957. /* zram devices sort of resembles non-rotational disks */
  958. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
  959. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
  960. /*
  961. * To ensure that we always get PAGE_SIZE aligned
  962. * and n*PAGE_SIZED sized I/O requests.
  963. */
  964. blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
  965. blk_queue_logical_block_size(zram->disk->queue,
  966. ZRAM_LOGICAL_BLOCK_SIZE);
  967. blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
  968. blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
  969. zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
  970. zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
  971. /*
  972. * zram_bio_discard() will clear all logical blocks if logical block
  973. * size is identical with physical block size(PAGE_SIZE). But if it is
  974. * different, we will skip discarding some parts of logical blocks in
  975. * the part of the request range which isn't aligned to physical block
  976. * size. So we can't ensure that all discarded logical blocks are
  977. * zeroed.
  978. */
  979. if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
  980. zram->disk->queue->limits.discard_zeroes_data = 1;
  981. else
  982. zram->disk->queue->limits.discard_zeroes_data = 0;
  983. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
  984. add_disk(zram->disk);
  985. ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
  986. &zram_disk_attr_group);
  987. if (ret < 0) {
  988. pr_warn("Error creating sysfs group");
  989. goto out_free_disk;
  990. }
  991. strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
  992. zram->meta = NULL;
  993. zram->max_comp_streams = 1;
  994. return 0;
  995. out_free_disk:
  996. del_gendisk(zram->disk);
  997. put_disk(zram->disk);
  998. out_free_queue:
  999. blk_cleanup_queue(queue);
  1000. out:
  1001. return ret;
  1002. }
  1003. static void destroy_devices(unsigned int nr)
  1004. {
  1005. struct zram *zram;
  1006. unsigned int i;
  1007. for (i = 0; i < nr; i++) {
  1008. zram = &zram_devices[i];
  1009. /*
  1010. * Remove sysfs first, so no one will perform a disksize
  1011. * store while we destroy the devices
  1012. */
  1013. sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
  1014. &zram_disk_attr_group);
  1015. zram_reset_device(zram);
  1016. blk_cleanup_queue(zram->disk->queue);
  1017. del_gendisk(zram->disk);
  1018. put_disk(zram->disk);
  1019. }
  1020. kfree(zram_devices);
  1021. unregister_blkdev(zram_major, "zram");
  1022. pr_info("Destroyed %u device(s)\n", nr);
  1023. }
  1024. static int __init zram_init(void)
  1025. {
  1026. int ret, dev_id;
  1027. if (num_devices > max_num_devices) {
  1028. pr_warn("Invalid value for num_devices: %u\n",
  1029. num_devices);
  1030. return -EINVAL;
  1031. }
  1032. zram_major = register_blkdev(0, "zram");
  1033. if (zram_major <= 0) {
  1034. pr_warn("Unable to get major number\n");
  1035. return -EBUSY;
  1036. }
  1037. /* Allocate the device array and initialize each one */
  1038. zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
  1039. if (!zram_devices) {
  1040. unregister_blkdev(zram_major, "zram");
  1041. return -ENOMEM;
  1042. }
  1043. for (dev_id = 0; dev_id < num_devices; dev_id++) {
  1044. ret = create_device(&zram_devices[dev_id], dev_id);
  1045. if (ret)
  1046. goto out_error;
  1047. }
  1048. pr_info("Created %u device(s)\n", num_devices);
  1049. return 0;
  1050. out_error:
  1051. destroy_devices(dev_id);
  1052. return ret;
  1053. }
  1054. static void __exit zram_exit(void)
  1055. {
  1056. destroy_devices(num_devices);
  1057. }
  1058. module_init(zram_init);
  1059. module_exit(zram_exit);
  1060. module_param(num_devices, uint, 0);
  1061. MODULE_PARM_DESC(num_devices, "Number of zram devices");
  1062. MODULE_LICENSE("Dual BSD/GPL");
  1063. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
  1064. MODULE_DESCRIPTION("Compressed RAM Block Device");