zram_drv.c 30 KB

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