zram_drv.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. #define ZRAM_ATTR_RO(name) \
  40. static ssize_t zram_attr_##name##_show(struct device *d, \
  41. struct device_attribute *attr, char *b) \
  42. { \
  43. struct zram *zram = dev_to_zram(d); \
  44. return scnprintf(b, PAGE_SIZE, "%llu\n", \
  45. (u64)atomic64_read(&zram->stats.name)); \
  46. } \
  47. static struct device_attribute dev_attr_##name = \
  48. __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL);
  49. static inline int init_done(struct zram *zram)
  50. {
  51. return zram->meta != NULL;
  52. }
  53. static inline struct zram *dev_to_zram(struct device *dev)
  54. {
  55. return (struct zram *)dev_to_disk(dev)->private_data;
  56. }
  57. static ssize_t disksize_show(struct device *dev,
  58. struct device_attribute *attr, char *buf)
  59. {
  60. struct zram *zram = dev_to_zram(dev);
  61. return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
  62. }
  63. static ssize_t initstate_show(struct device *dev,
  64. struct device_attribute *attr, char *buf)
  65. {
  66. u32 val;
  67. struct zram *zram = dev_to_zram(dev);
  68. down_read(&zram->init_lock);
  69. val = init_done(zram);
  70. up_read(&zram->init_lock);
  71. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  72. }
  73. static ssize_t orig_data_size_show(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct zram *zram = dev_to_zram(dev);
  77. return scnprintf(buf, PAGE_SIZE, "%llu\n",
  78. (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
  79. }
  80. static ssize_t mem_used_total_show(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. u64 val = 0;
  84. struct zram *zram = dev_to_zram(dev);
  85. struct zram_meta *meta = zram->meta;
  86. down_read(&zram->init_lock);
  87. if (init_done(zram))
  88. val = zs_get_total_size_bytes(meta->mem_pool);
  89. up_read(&zram->init_lock);
  90. return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
  91. }
  92. static ssize_t max_comp_streams_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. int val;
  96. struct zram *zram = dev_to_zram(dev);
  97. down_read(&zram->init_lock);
  98. val = zram->max_comp_streams;
  99. up_read(&zram->init_lock);
  100. return scnprintf(buf, PAGE_SIZE, "%d\n", val);
  101. }
  102. static ssize_t max_comp_streams_store(struct device *dev,
  103. struct device_attribute *attr, const char *buf, size_t len)
  104. {
  105. int num;
  106. struct zram *zram = dev_to_zram(dev);
  107. int ret;
  108. ret = kstrtoint(buf, 0, &num);
  109. if (ret < 0)
  110. return ret;
  111. if (num < 1)
  112. return -EINVAL;
  113. down_write(&zram->init_lock);
  114. if (init_done(zram)) {
  115. if (!zcomp_set_max_streams(zram->comp, num)) {
  116. pr_info("Cannot change max compression streams\n");
  117. ret = -EINVAL;
  118. goto out;
  119. }
  120. }
  121. zram->max_comp_streams = num;
  122. ret = len;
  123. out:
  124. up_write(&zram->init_lock);
  125. return ret;
  126. }
  127. static ssize_t comp_algorithm_show(struct device *dev,
  128. struct device_attribute *attr, char *buf)
  129. {
  130. size_t sz;
  131. struct zram *zram = dev_to_zram(dev);
  132. down_read(&zram->init_lock);
  133. sz = zcomp_available_show(zram->compressor, buf);
  134. up_read(&zram->init_lock);
  135. return sz;
  136. }
  137. static ssize_t comp_algorithm_store(struct device *dev,
  138. struct device_attribute *attr, const char *buf, size_t len)
  139. {
  140. struct zram *zram = dev_to_zram(dev);
  141. down_write(&zram->init_lock);
  142. if (init_done(zram)) {
  143. up_write(&zram->init_lock);
  144. pr_info("Can't change algorithm for initialized device\n");
  145. return -EBUSY;
  146. }
  147. strlcpy(zram->compressor, buf, sizeof(zram->compressor));
  148. up_write(&zram->init_lock);
  149. return len;
  150. }
  151. /* flag operations needs meta->tb_lock */
  152. static int zram_test_flag(struct zram_meta *meta, u32 index,
  153. enum zram_pageflags flag)
  154. {
  155. return meta->table[index].flags & BIT(flag);
  156. }
  157. static void zram_set_flag(struct zram_meta *meta, u32 index,
  158. enum zram_pageflags flag)
  159. {
  160. meta->table[index].flags |= BIT(flag);
  161. }
  162. static void zram_clear_flag(struct zram_meta *meta, u32 index,
  163. enum zram_pageflags flag)
  164. {
  165. meta->table[index].flags &= ~BIT(flag);
  166. }
  167. static inline int is_partial_io(struct bio_vec *bvec)
  168. {
  169. return bvec->bv_len != PAGE_SIZE;
  170. }
  171. /*
  172. * Check if request is within bounds and aligned on zram logical blocks.
  173. */
  174. static inline int valid_io_request(struct zram *zram, struct bio *bio)
  175. {
  176. u64 start, end, bound;
  177. /* unaligned request */
  178. if (unlikely(bio->bi_sector &
  179. (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
  180. return 0;
  181. if (unlikely(bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
  182. return 0;
  183. start = bio->bi_sector;
  184. end = start + (bio->bi_size >> SECTOR_SHIFT);
  185. bound = zram->disksize >> SECTOR_SHIFT;
  186. /* out of range range */
  187. if (unlikely(start >= bound || end > bound || start > end))
  188. return 0;
  189. /* I/O request is valid */
  190. return 1;
  191. }
  192. static void zram_meta_free(struct zram_meta *meta)
  193. {
  194. zs_destroy_pool(meta->mem_pool);
  195. vfree(meta->table);
  196. kfree(meta);
  197. }
  198. static struct zram_meta *zram_meta_alloc(u64 disksize)
  199. {
  200. size_t num_pages;
  201. struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
  202. if (!meta)
  203. goto out;
  204. num_pages = disksize >> PAGE_SHIFT;
  205. meta->table = vzalloc(num_pages * sizeof(*meta->table));
  206. if (!meta->table) {
  207. pr_err("Error allocating zram address table\n");
  208. goto free_meta;
  209. }
  210. meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
  211. if (!meta->mem_pool) {
  212. pr_err("Error creating memory pool\n");
  213. goto free_table;
  214. }
  215. rwlock_init(&meta->tb_lock);
  216. return meta;
  217. free_table:
  218. vfree(meta->table);
  219. free_meta:
  220. kfree(meta);
  221. meta = NULL;
  222. out:
  223. return meta;
  224. }
  225. static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
  226. {
  227. if (*offset + bvec->bv_len >= PAGE_SIZE)
  228. (*index)++;
  229. *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
  230. }
  231. static int page_zero_filled(void *ptr)
  232. {
  233. unsigned int pos;
  234. unsigned long *page;
  235. page = (unsigned long *)ptr;
  236. for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
  237. if (page[pos])
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. static void handle_zero_page(struct bio_vec *bvec)
  243. {
  244. struct page *page = bvec->bv_page;
  245. void *user_mem;
  246. user_mem = kmap_atomic(page);
  247. if (is_partial_io(bvec))
  248. memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
  249. else
  250. clear_page(user_mem);
  251. kunmap_atomic(user_mem);
  252. flush_dcache_page(page);
  253. }
  254. /* NOTE: caller should hold meta->tb_lock with write-side */
  255. static void zram_free_page(struct zram *zram, size_t index)
  256. {
  257. struct zram_meta *meta = zram->meta;
  258. unsigned long handle = meta->table[index].handle;
  259. if (unlikely(!handle)) {
  260. /*
  261. * No memory is allocated for zero filled pages.
  262. * Simply clear zero page flag.
  263. */
  264. if (zram_test_flag(meta, index, ZRAM_ZERO)) {
  265. zram_clear_flag(meta, index, ZRAM_ZERO);
  266. atomic64_dec(&zram->stats.zero_pages);
  267. }
  268. return;
  269. }
  270. zs_free(meta->mem_pool, handle);
  271. atomic64_sub(meta->table[index].size, &zram->stats.compr_data_size);
  272. atomic64_dec(&zram->stats.pages_stored);
  273. meta->table[index].handle = 0;
  274. meta->table[index].size = 0;
  275. }
  276. static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  277. {
  278. int ret = 0;
  279. unsigned char *cmem;
  280. struct zram_meta *meta = zram->meta;
  281. unsigned long handle;
  282. u16 size;
  283. read_lock(&meta->tb_lock);
  284. handle = meta->table[index].handle;
  285. size = meta->table[index].size;
  286. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  287. read_unlock(&meta->tb_lock);
  288. clear_page(mem);
  289. return 0;
  290. }
  291. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
  292. if (size == PAGE_SIZE)
  293. copy_page(mem, cmem);
  294. else
  295. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  296. zs_unmap_object(meta->mem_pool, handle);
  297. read_unlock(&meta->tb_lock);
  298. /* Should NEVER happen. Return bio error if it does. */
  299. if (unlikely(ret)) {
  300. pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
  301. atomic64_inc(&zram->stats.failed_reads);
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  307. u32 index, int offset, struct bio *bio)
  308. {
  309. int ret;
  310. struct page *page;
  311. unsigned char *user_mem, *uncmem = NULL;
  312. struct zram_meta *meta = zram->meta;
  313. page = bvec->bv_page;
  314. read_lock(&meta->tb_lock);
  315. if (unlikely(!meta->table[index].handle) ||
  316. zram_test_flag(meta, index, ZRAM_ZERO)) {
  317. read_unlock(&meta->tb_lock);
  318. handle_zero_page(bvec);
  319. return 0;
  320. }
  321. read_unlock(&meta->tb_lock);
  322. if (is_partial_io(bvec))
  323. /* Use a temporary buffer to decompress the page */
  324. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  325. user_mem = kmap_atomic(page);
  326. if (!is_partial_io(bvec))
  327. uncmem = user_mem;
  328. if (!uncmem) {
  329. pr_info("Unable to allocate temp memory\n");
  330. ret = -ENOMEM;
  331. goto out_cleanup;
  332. }
  333. ret = zram_decompress_page(zram, uncmem, index);
  334. /* Should NEVER happen. Return bio error if it does. */
  335. if (unlikely(ret))
  336. goto out_cleanup;
  337. if (is_partial_io(bvec))
  338. memcpy(user_mem + bvec->bv_offset, uncmem + offset,
  339. bvec->bv_len);
  340. flush_dcache_page(page);
  341. ret = 0;
  342. out_cleanup:
  343. kunmap_atomic(user_mem);
  344. if (is_partial_io(bvec))
  345. kfree(uncmem);
  346. return ret;
  347. }
  348. static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  349. int offset)
  350. {
  351. int ret = 0;
  352. size_t clen;
  353. unsigned long handle;
  354. struct page *page;
  355. unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
  356. struct zram_meta *meta = zram->meta;
  357. struct zcomp_strm *zstrm;
  358. bool locked = false;
  359. page = bvec->bv_page;
  360. if (is_partial_io(bvec)) {
  361. /*
  362. * This is a partial IO. We need to read the full page
  363. * before to write the changes.
  364. */
  365. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  366. if (!uncmem) {
  367. ret = -ENOMEM;
  368. goto out;
  369. }
  370. ret = zram_decompress_page(zram, uncmem, index);
  371. if (ret)
  372. goto out;
  373. }
  374. zstrm = zcomp_strm_find(zram->comp);
  375. locked = true;
  376. user_mem = kmap_atomic(page);
  377. if (is_partial_io(bvec)) {
  378. memcpy(uncmem + offset, user_mem + bvec->bv_offset,
  379. bvec->bv_len);
  380. kunmap_atomic(user_mem);
  381. user_mem = NULL;
  382. } else {
  383. uncmem = user_mem;
  384. }
  385. if (page_zero_filled(uncmem)) {
  386. if (user_mem)
  387. kunmap_atomic(user_mem);
  388. /* Free memory associated with this sector now. */
  389. write_lock(&zram->meta->tb_lock);
  390. zram_free_page(zram, index);
  391. zram_set_flag(meta, index, ZRAM_ZERO);
  392. write_unlock(&zram->meta->tb_lock);
  393. atomic64_inc(&zram->stats.zero_pages);
  394. ret = 0;
  395. goto out;
  396. }
  397. ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
  398. if (!is_partial_io(bvec)) {
  399. kunmap_atomic(user_mem);
  400. user_mem = NULL;
  401. uncmem = NULL;
  402. }
  403. if (unlikely(ret)) {
  404. pr_err("Compression failed! err=%d\n", ret);
  405. goto out;
  406. }
  407. src = zstrm->buffer;
  408. if (unlikely(clen > max_zpage_size)) {
  409. clen = PAGE_SIZE;
  410. if (is_partial_io(bvec))
  411. src = uncmem;
  412. }
  413. handle = zs_malloc(meta->mem_pool, clen);
  414. if (!handle) {
  415. pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
  416. index, clen);
  417. ret = -ENOMEM;
  418. goto out;
  419. }
  420. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
  421. if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
  422. src = kmap_atomic(page);
  423. copy_page(cmem, src);
  424. kunmap_atomic(src);
  425. } else {
  426. memcpy(cmem, src, clen);
  427. }
  428. zcomp_strm_release(zram->comp, zstrm);
  429. locked = false;
  430. zs_unmap_object(meta->mem_pool, handle);
  431. /*
  432. * Free memory associated with this sector
  433. * before overwriting unused sectors.
  434. */
  435. write_lock(&zram->meta->tb_lock);
  436. zram_free_page(zram, index);
  437. meta->table[index].handle = handle;
  438. meta->table[index].size = clen;
  439. write_unlock(&zram->meta->tb_lock);
  440. /* Update stats */
  441. atomic64_add(clen, &zram->stats.compr_data_size);
  442. atomic64_inc(&zram->stats.pages_stored);
  443. out:
  444. if (locked)
  445. zcomp_strm_release(zram->comp, zstrm);
  446. if (is_partial_io(bvec))
  447. kfree(uncmem);
  448. if (ret)
  449. atomic64_inc(&zram->stats.failed_writes);
  450. return ret;
  451. }
  452. static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
  453. int offset, struct bio *bio)
  454. {
  455. int ret;
  456. int rw = bio_data_dir(bio);
  457. if (rw == READ) {
  458. atomic64_inc(&zram->stats.num_reads);
  459. ret = zram_bvec_read(zram, bvec, index, offset, bio);
  460. } else {
  461. atomic64_inc(&zram->stats.num_writes);
  462. ret = zram_bvec_write(zram, bvec, index, offset);
  463. }
  464. return ret;
  465. }
  466. /*
  467. * zram_bio_discard - handler on discard request
  468. * @index: physical block index in PAGE_SIZE units
  469. * @offset: byte offset within physical block
  470. */
  471. static void zram_bio_discard(struct zram *zram, u32 index,
  472. int offset, struct bio *bio)
  473. {
  474. size_t n = bio->bi_size;
  475. /*
  476. * zram manages data in physical block size units. Because logical block
  477. * size isn't identical with physical block size on some arch, we
  478. * could get a discard request pointing to a specific offset within a
  479. * certain physical block. Although we can handle this request by
  480. * reading that physiclal block and decompressing and partially zeroing
  481. * and re-compressing and then re-storing it, this isn't reasonable
  482. * because our intent with a discard request is to save memory. So
  483. * skipping this logical block is appropriate here.
  484. */
  485. if (offset) {
  486. if (n < offset)
  487. return;
  488. n -= offset;
  489. index++;
  490. }
  491. while (n >= PAGE_SIZE) {
  492. /*
  493. * Discard request can be large so the lock hold times could be
  494. * lengthy. So take the lock once per page.
  495. */
  496. write_lock(&zram->meta->tb_lock);
  497. zram_free_page(zram, index);
  498. write_unlock(&zram->meta->tb_lock);
  499. index++;
  500. n -= PAGE_SIZE;
  501. }
  502. }
  503. static void zram_reset_device(struct zram *zram, bool reset_capacity)
  504. {
  505. size_t index;
  506. struct zram_meta *meta;
  507. down_write(&zram->init_lock);
  508. if (!init_done(zram)) {
  509. up_write(&zram->init_lock);
  510. return;
  511. }
  512. meta = zram->meta;
  513. /* Free all pages that are still in this zram device */
  514. for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
  515. unsigned long handle = meta->table[index].handle;
  516. if (!handle)
  517. continue;
  518. zs_free(meta->mem_pool, handle);
  519. }
  520. zcomp_destroy(zram->comp);
  521. zram->max_comp_streams = 1;
  522. zram_meta_free(zram->meta);
  523. zram->meta = NULL;
  524. /* Reset stats */
  525. memset(&zram->stats, 0, sizeof(zram->stats));
  526. zram->disksize = 0;
  527. if (reset_capacity)
  528. set_capacity(zram->disk, 0);
  529. up_write(&zram->init_lock);
  530. }
  531. static ssize_t disksize_store(struct device *dev,
  532. struct device_attribute *attr, const char *buf, size_t len)
  533. {
  534. u64 disksize;
  535. struct zcomp *comp;
  536. struct zram_meta *meta;
  537. struct zram *zram = dev_to_zram(dev);
  538. int err;
  539. disksize = memparse(buf, NULL);
  540. if (!disksize)
  541. return -EINVAL;
  542. disksize = PAGE_ALIGN(disksize);
  543. meta = zram_meta_alloc(disksize);
  544. if (!meta)
  545. return -ENOMEM;
  546. comp = zcomp_create(zram->compressor, zram->max_comp_streams);
  547. if (IS_ERR(comp)) {
  548. pr_info("Cannot initialise %s compressing backend\n",
  549. zram->compressor);
  550. err = PTR_ERR(comp);
  551. goto out_free_meta;
  552. }
  553. down_write(&zram->init_lock);
  554. if (init_done(zram)) {
  555. pr_info("Cannot change disksize for initialized device\n");
  556. err = -EBUSY;
  557. goto out_destroy_comp;
  558. }
  559. zram->meta = meta;
  560. zram->comp = comp;
  561. zram->disksize = disksize;
  562. set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
  563. up_write(&zram->init_lock);
  564. return len;
  565. out_destroy_comp:
  566. up_write(&zram->init_lock);
  567. zcomp_destroy(comp);
  568. out_free_meta:
  569. zram_meta_free(meta);
  570. return err;
  571. }
  572. static ssize_t reset_store(struct device *dev,
  573. struct device_attribute *attr, const char *buf, size_t len)
  574. {
  575. int ret;
  576. unsigned short do_reset;
  577. struct zram *zram;
  578. struct block_device *bdev;
  579. zram = dev_to_zram(dev);
  580. bdev = bdget_disk(zram->disk, 0);
  581. if (!bdev)
  582. return -ENOMEM;
  583. /* Do not reset an active device! */
  584. if (bdev->bd_holders) {
  585. ret = -EBUSY;
  586. goto out;
  587. }
  588. ret = kstrtou16(buf, 10, &do_reset);
  589. if (ret)
  590. goto out;
  591. if (!do_reset) {
  592. ret = -EINVAL;
  593. goto out;
  594. }
  595. /* Make sure all pending I/O is finished */
  596. fsync_bdev(bdev);
  597. bdput(bdev);
  598. zram_reset_device(zram, true);
  599. return len;
  600. out:
  601. bdput(bdev);
  602. return ret;
  603. }
  604. static void __zram_make_request(struct zram *zram, struct bio *bio)
  605. {
  606. int offset, i;
  607. u32 index;
  608. struct bio_vec *bvec;
  609. index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
  610. offset = (bio->bi_sector &
  611. (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
  612. if (unlikely(bio->bi_rw & REQ_DISCARD)) {
  613. zram_bio_discard(zram, index, offset, bio);
  614. bio_endio(bio, 0);
  615. return;
  616. }
  617. bio_for_each_segment(bvec, bio, i) {
  618. int max_transfer_size = PAGE_SIZE - offset;
  619. if (bvec->bv_len > max_transfer_size) {
  620. /*
  621. * zram_bvec_rw() can only make operation on a single
  622. * zram page. Split the bio vector.
  623. */
  624. struct bio_vec bv;
  625. bv.bv_page = bvec->bv_page;
  626. bv.bv_len = max_transfer_size;
  627. bv.bv_offset = bvec->bv_offset;
  628. if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0)
  629. goto out;
  630. bv.bv_len = bvec->bv_len - max_transfer_size;
  631. bv.bv_offset += max_transfer_size;
  632. if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0)
  633. goto out;
  634. } else
  635. if (zram_bvec_rw(zram, bvec, index, offset, bio) < 0)
  636. goto out;
  637. update_position(&index, &offset, bvec);
  638. }
  639. set_bit(BIO_UPTODATE, &bio->bi_flags);
  640. bio_endio(bio, 0);
  641. return;
  642. out:
  643. bio_io_error(bio);
  644. }
  645. /*
  646. * Handler function for all zram I/O requests.
  647. */
  648. static void zram_make_request(struct request_queue *queue, struct bio *bio)
  649. {
  650. struct zram *zram = queue->queuedata;
  651. down_read(&zram->init_lock);
  652. if (unlikely(!init_done(zram)))
  653. goto error;
  654. if (!valid_io_request(zram, bio)) {
  655. atomic64_inc(&zram->stats.invalid_io);
  656. goto error;
  657. }
  658. __zram_make_request(zram, bio);
  659. up_read(&zram->init_lock);
  660. return;
  661. error:
  662. up_read(&zram->init_lock);
  663. bio_io_error(bio);
  664. }
  665. static void zram_slot_free_notify(struct block_device *bdev,
  666. unsigned long index)
  667. {
  668. struct zram *zram;
  669. struct zram_meta *meta;
  670. zram = bdev->bd_disk->private_data;
  671. meta = zram->meta;
  672. write_lock(&meta->tb_lock);
  673. zram_free_page(zram, index);
  674. write_unlock(&meta->tb_lock);
  675. atomic64_inc(&zram->stats.notify_free);
  676. }
  677. static const struct block_device_operations zram_devops = {
  678. .swap_slot_free_notify = zram_slot_free_notify,
  679. .owner = THIS_MODULE
  680. };
  681. static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
  682. disksize_show, disksize_store);
  683. static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
  684. static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
  685. static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
  686. static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
  687. static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
  688. max_comp_streams_show, max_comp_streams_store);
  689. static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
  690. comp_algorithm_show, comp_algorithm_store);
  691. ZRAM_ATTR_RO(num_reads);
  692. ZRAM_ATTR_RO(num_writes);
  693. ZRAM_ATTR_RO(failed_reads);
  694. ZRAM_ATTR_RO(failed_writes);
  695. ZRAM_ATTR_RO(invalid_io);
  696. ZRAM_ATTR_RO(notify_free);
  697. ZRAM_ATTR_RO(zero_pages);
  698. ZRAM_ATTR_RO(compr_data_size);
  699. static struct attribute *zram_disk_attrs[] = {
  700. &dev_attr_disksize.attr,
  701. &dev_attr_initstate.attr,
  702. &dev_attr_reset.attr,
  703. &dev_attr_num_reads.attr,
  704. &dev_attr_num_writes.attr,
  705. &dev_attr_failed_reads.attr,
  706. &dev_attr_failed_writes.attr,
  707. &dev_attr_invalid_io.attr,
  708. &dev_attr_notify_free.attr,
  709. &dev_attr_zero_pages.attr,
  710. &dev_attr_orig_data_size.attr,
  711. &dev_attr_compr_data_size.attr,
  712. &dev_attr_mem_used_total.attr,
  713. &dev_attr_max_comp_streams.attr,
  714. &dev_attr_comp_algorithm.attr,
  715. NULL,
  716. };
  717. static struct attribute_group zram_disk_attr_group = {
  718. .attrs = zram_disk_attrs,
  719. };
  720. static int create_device(struct zram *zram, int device_id)
  721. {
  722. int ret = -ENOMEM;
  723. init_rwsem(&zram->init_lock);
  724. zram->queue = blk_alloc_queue(GFP_KERNEL);
  725. if (!zram->queue) {
  726. pr_err("Error allocating disk queue for device %d\n",
  727. device_id);
  728. goto out;
  729. }
  730. blk_queue_make_request(zram->queue, zram_make_request);
  731. zram->queue->queuedata = zram;
  732. /* gendisk structure */
  733. zram->disk = alloc_disk(1);
  734. if (!zram->disk) {
  735. pr_warn("Error allocating disk structure for device %d\n",
  736. device_id);
  737. goto out_free_queue;
  738. }
  739. zram->disk->major = zram_major;
  740. zram->disk->first_minor = device_id;
  741. zram->disk->fops = &zram_devops;
  742. zram->disk->queue = zram->queue;
  743. zram->disk->private_data = zram;
  744. snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
  745. /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
  746. set_capacity(zram->disk, 0);
  747. /* zram devices sort of resembles non-rotational disks */
  748. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
  749. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
  750. /*
  751. * To ensure that we always get PAGE_SIZE aligned
  752. * and n*PAGE_SIZED sized I/O requests.
  753. */
  754. blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
  755. blk_queue_logical_block_size(zram->disk->queue,
  756. ZRAM_LOGICAL_BLOCK_SIZE);
  757. blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
  758. blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
  759. zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
  760. zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
  761. /*
  762. * zram_bio_discard() will clear all logical blocks if logical block
  763. * size is identical with physical block size(PAGE_SIZE). But if it is
  764. * different, we will skip discarding some parts of logical blocks in
  765. * the part of the request range which isn't aligned to physical block
  766. * size. So we can't ensure that all discarded logical blocks are
  767. * zeroed.
  768. */
  769. if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
  770. zram->disk->queue->limits.discard_zeroes_data = 1;
  771. else
  772. zram->disk->queue->limits.discard_zeroes_data = 0;
  773. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
  774. add_disk(zram->disk);
  775. ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
  776. &zram_disk_attr_group);
  777. if (ret < 0) {
  778. pr_warn("Error creating sysfs group");
  779. goto out_free_disk;
  780. }
  781. strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
  782. zram->meta = NULL;
  783. zram->max_comp_streams = 1;
  784. return 0;
  785. out_free_disk:
  786. del_gendisk(zram->disk);
  787. put_disk(zram->disk);
  788. out_free_queue:
  789. blk_cleanup_queue(zram->queue);
  790. out:
  791. return ret;
  792. }
  793. static void destroy_device(struct zram *zram)
  794. {
  795. sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
  796. &zram_disk_attr_group);
  797. del_gendisk(zram->disk);
  798. put_disk(zram->disk);
  799. blk_cleanup_queue(zram->queue);
  800. }
  801. static int __init zram_init(void)
  802. {
  803. int ret, dev_id;
  804. if (num_devices > max_num_devices) {
  805. pr_warn("Invalid value for num_devices: %u\n",
  806. num_devices);
  807. ret = -EINVAL;
  808. goto out;
  809. }
  810. zram_major = register_blkdev(0, "zram");
  811. if (zram_major <= 0) {
  812. pr_warn("Unable to get major number\n");
  813. ret = -EBUSY;
  814. goto out;
  815. }
  816. /* Allocate the device array and initialize each one */
  817. zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
  818. if (!zram_devices) {
  819. ret = -ENOMEM;
  820. goto unregister;
  821. }
  822. for (dev_id = 0; dev_id < num_devices; dev_id++) {
  823. ret = create_device(&zram_devices[dev_id], dev_id);
  824. if (ret)
  825. goto free_devices;
  826. }
  827. pr_info("Created %u device(s) ...\n", num_devices);
  828. return 0;
  829. free_devices:
  830. while (dev_id)
  831. destroy_device(&zram_devices[--dev_id]);
  832. kfree(zram_devices);
  833. unregister:
  834. unregister_blkdev(zram_major, "zram");
  835. out:
  836. return ret;
  837. }
  838. static void __exit zram_exit(void)
  839. {
  840. int i;
  841. struct zram *zram;
  842. for (i = 0; i < num_devices; i++) {
  843. zram = &zram_devices[i];
  844. destroy_device(zram);
  845. /*
  846. * Shouldn't access zram->disk after destroy_device
  847. * because destroy_device already released zram->disk.
  848. */
  849. zram_reset_device(zram, false);
  850. }
  851. unregister_blkdev(zram_major, "zram");
  852. kfree(zram_devices);
  853. pr_debug("Cleanup done!\n");
  854. }
  855. module_init(zram_init);
  856. module_exit(zram_exit);
  857. module_param(num_devices, uint, 0);
  858. MODULE_PARM_DESC(num_devices, "Number of zram devices");
  859. MODULE_LICENSE("Dual BSD/GPL");
  860. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
  861. MODULE_DESCRIPTION("Compressed RAM Block Device");