zram_drv.c 44 KB

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