zram_drv.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638
  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/highmem.h>
  24. #include <linux/slab.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/string.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/err.h>
  29. #include <linux/idr.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/cpuhotplug.h>
  33. #include <linux/part_stat.h>
  34. #include <linux/kernel_read_file.h>
  35. #include "zram_drv.h"
  36. static DEFINE_IDR(zram_index_idr);
  37. /* idr index must be protected */
  38. static DEFINE_MUTEX(zram_index_mutex);
  39. static int zram_major;
  40. static const char *default_compressor = CONFIG_ZRAM_DEF_COMP;
  41. /* Module params (documentation at end) */
  42. static unsigned int num_devices = 1;
  43. /*
  44. * Pages that compress to sizes equals or greater than this are stored
  45. * uncompressed in memory.
  46. */
  47. static size_t huge_class_size;
  48. static const struct block_device_operations zram_devops;
  49. static void zram_free_page(struct zram *zram, size_t index);
  50. static int zram_read_page(struct zram *zram, struct page *page, u32 index,
  51. struct bio *parent);
  52. static int zram_slot_trylock(struct zram *zram, u32 index)
  53. {
  54. return spin_trylock(&zram->table[index].lock);
  55. }
  56. static void zram_slot_lock(struct zram *zram, u32 index)
  57. {
  58. spin_lock(&zram->table[index].lock);
  59. }
  60. static void zram_slot_unlock(struct zram *zram, u32 index)
  61. {
  62. spin_unlock(&zram->table[index].lock);
  63. }
  64. static inline bool init_done(struct zram *zram)
  65. {
  66. return zram->disksize;
  67. }
  68. static inline struct zram *dev_to_zram(struct device *dev)
  69. {
  70. return (struct zram *)dev_to_disk(dev)->private_data;
  71. }
  72. static unsigned long zram_get_handle(struct zram *zram, u32 index)
  73. {
  74. return zram->table[index].handle;
  75. }
  76. static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
  77. {
  78. zram->table[index].handle = handle;
  79. }
  80. /* flag operations require table entry bit_spin_lock() being held */
  81. static bool zram_test_flag(struct zram *zram, u32 index,
  82. enum zram_pageflags flag)
  83. {
  84. return zram->table[index].flags & BIT(flag);
  85. }
  86. static void zram_set_flag(struct zram *zram, u32 index,
  87. enum zram_pageflags flag)
  88. {
  89. zram->table[index].flags |= BIT(flag);
  90. }
  91. static void zram_clear_flag(struct zram *zram, u32 index,
  92. enum zram_pageflags flag)
  93. {
  94. zram->table[index].flags &= ~BIT(flag);
  95. }
  96. static inline void zram_set_element(struct zram *zram, u32 index,
  97. unsigned long element)
  98. {
  99. zram->table[index].element = element;
  100. }
  101. static unsigned long zram_get_element(struct zram *zram, u32 index)
  102. {
  103. return zram->table[index].element;
  104. }
  105. static size_t zram_get_obj_size(struct zram *zram, u32 index)
  106. {
  107. return zram->table[index].flags & (BIT(ZRAM_FLAG_SHIFT) - 1);
  108. }
  109. static void zram_set_obj_size(struct zram *zram,
  110. u32 index, size_t size)
  111. {
  112. unsigned long flags = zram->table[index].flags >> ZRAM_FLAG_SHIFT;
  113. zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size;
  114. }
  115. static inline bool zram_allocated(struct zram *zram, u32 index)
  116. {
  117. return zram_get_obj_size(zram, index) ||
  118. zram_test_flag(zram, index, ZRAM_SAME) ||
  119. zram_test_flag(zram, index, ZRAM_WB);
  120. }
  121. #if PAGE_SIZE != 4096
  122. static inline bool is_partial_io(struct bio_vec *bvec)
  123. {
  124. return bvec->bv_len != PAGE_SIZE;
  125. }
  126. #define ZRAM_PARTIAL_IO 1
  127. #else
  128. static inline bool is_partial_io(struct bio_vec *bvec)
  129. {
  130. return false;
  131. }
  132. #endif
  133. static inline void zram_set_priority(struct zram *zram, u32 index, u32 prio)
  134. {
  135. prio &= ZRAM_COMP_PRIORITY_MASK;
  136. /*
  137. * Clear previous priority value first, in case if we recompress
  138. * further an already recompressed page
  139. */
  140. zram->table[index].flags &= ~(ZRAM_COMP_PRIORITY_MASK <<
  141. ZRAM_COMP_PRIORITY_BIT1);
  142. zram->table[index].flags |= (prio << ZRAM_COMP_PRIORITY_BIT1);
  143. }
  144. static inline u32 zram_get_priority(struct zram *zram, u32 index)
  145. {
  146. u32 prio = zram->table[index].flags >> ZRAM_COMP_PRIORITY_BIT1;
  147. return prio & ZRAM_COMP_PRIORITY_MASK;
  148. }
  149. static void zram_accessed(struct zram *zram, u32 index)
  150. {
  151. zram_clear_flag(zram, index, ZRAM_IDLE);
  152. #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
  153. zram->table[index].ac_time = ktime_get_boottime();
  154. #endif
  155. }
  156. static inline void update_used_max(struct zram *zram,
  157. const unsigned long pages)
  158. {
  159. unsigned long cur_max = atomic_long_read(&zram->stats.max_used_pages);
  160. do {
  161. if (cur_max >= pages)
  162. return;
  163. } while (!atomic_long_try_cmpxchg(&zram->stats.max_used_pages,
  164. &cur_max, pages));
  165. }
  166. static inline void zram_fill_page(void *ptr, unsigned long len,
  167. unsigned long value)
  168. {
  169. WARN_ON_ONCE(!IS_ALIGNED(len, sizeof(unsigned long)));
  170. memset_l(ptr, value, len / sizeof(unsigned long));
  171. }
  172. static bool page_same_filled(void *ptr, unsigned long *element)
  173. {
  174. unsigned long *page;
  175. unsigned long val;
  176. unsigned int pos, last_pos = PAGE_SIZE / sizeof(*page) - 1;
  177. page = (unsigned long *)ptr;
  178. val = page[0];
  179. if (val != page[last_pos])
  180. return false;
  181. for (pos = 1; pos < last_pos; pos++) {
  182. if (val != page[pos])
  183. return false;
  184. }
  185. *element = val;
  186. return true;
  187. }
  188. static ssize_t initstate_show(struct device *dev,
  189. struct device_attribute *attr, char *buf)
  190. {
  191. u32 val;
  192. struct zram *zram = dev_to_zram(dev);
  193. down_read(&zram->init_lock);
  194. val = init_done(zram);
  195. up_read(&zram->init_lock);
  196. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  197. }
  198. static ssize_t disksize_show(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct zram *zram = dev_to_zram(dev);
  202. return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
  203. }
  204. static ssize_t mem_limit_store(struct device *dev,
  205. struct device_attribute *attr, const char *buf, size_t len)
  206. {
  207. u64 limit;
  208. char *tmp;
  209. struct zram *zram = dev_to_zram(dev);
  210. limit = memparse(buf, &tmp);
  211. if (buf == tmp) /* no chars parsed, invalid input */
  212. return -EINVAL;
  213. down_write(&zram->init_lock);
  214. zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
  215. up_write(&zram->init_lock);
  216. return len;
  217. }
  218. static ssize_t mem_used_max_store(struct device *dev,
  219. struct device_attribute *attr, const char *buf, size_t len)
  220. {
  221. int err;
  222. unsigned long val;
  223. struct zram *zram = dev_to_zram(dev);
  224. err = kstrtoul(buf, 10, &val);
  225. if (err || val != 0)
  226. return -EINVAL;
  227. down_read(&zram->init_lock);
  228. if (init_done(zram)) {
  229. atomic_long_set(&zram->stats.max_used_pages,
  230. zs_get_total_pages(zram->mem_pool));
  231. }
  232. up_read(&zram->init_lock);
  233. return len;
  234. }
  235. /*
  236. * Mark all pages which are older than or equal to cutoff as IDLE.
  237. * Callers should hold the zram init lock in read mode
  238. */
  239. static void mark_idle(struct zram *zram, ktime_t cutoff)
  240. {
  241. int is_idle = 1;
  242. unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
  243. int index;
  244. for (index = 0; index < nr_pages; index++) {
  245. /*
  246. * Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
  247. * See the comment in writeback_store.
  248. *
  249. * Also do not mark ZRAM_SAME slots as ZRAM_IDLE, because no
  250. * post-processing (recompress, writeback) happens to the
  251. * ZRAM_SAME slot.
  252. *
  253. * And ZRAM_WB slots simply cannot be ZRAM_IDLE.
  254. */
  255. zram_slot_lock(zram, index);
  256. if (!zram_allocated(zram, index) ||
  257. zram_test_flag(zram, index, ZRAM_WB) ||
  258. zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
  259. zram_test_flag(zram, index, ZRAM_SAME)) {
  260. zram_slot_unlock(zram, index);
  261. continue;
  262. }
  263. #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
  264. is_idle = !cutoff ||
  265. ktime_after(cutoff, zram->table[index].ac_time);
  266. #endif
  267. if (is_idle)
  268. zram_set_flag(zram, index, ZRAM_IDLE);
  269. else
  270. zram_clear_flag(zram, index, ZRAM_IDLE);
  271. zram_slot_unlock(zram, index);
  272. }
  273. }
  274. static ssize_t idle_store(struct device *dev,
  275. struct device_attribute *attr, const char *buf, size_t len)
  276. {
  277. struct zram *zram = dev_to_zram(dev);
  278. ktime_t cutoff_time = 0;
  279. ssize_t rv = -EINVAL;
  280. if (!sysfs_streq(buf, "all")) {
  281. /*
  282. * If it did not parse as 'all' try to treat it as an integer
  283. * when we have memory tracking enabled.
  284. */
  285. u64 age_sec;
  286. if (IS_ENABLED(CONFIG_ZRAM_TRACK_ENTRY_ACTIME) && !kstrtoull(buf, 0, &age_sec))
  287. cutoff_time = ktime_sub(ktime_get_boottime(),
  288. ns_to_ktime(age_sec * NSEC_PER_SEC));
  289. else
  290. goto out;
  291. }
  292. down_read(&zram->init_lock);
  293. if (!init_done(zram))
  294. goto out_unlock;
  295. /*
  296. * A cutoff_time of 0 marks everything as idle, this is the
  297. * "all" behavior.
  298. */
  299. mark_idle(zram, cutoff_time);
  300. rv = len;
  301. out_unlock:
  302. up_read(&zram->init_lock);
  303. out:
  304. return rv;
  305. }
  306. #ifdef CONFIG_ZRAM_WRITEBACK
  307. static ssize_t writeback_limit_enable_store(struct device *dev,
  308. struct device_attribute *attr, const char *buf, size_t len)
  309. {
  310. struct zram *zram = dev_to_zram(dev);
  311. u64 val;
  312. ssize_t ret = -EINVAL;
  313. if (kstrtoull(buf, 10, &val))
  314. return ret;
  315. down_read(&zram->init_lock);
  316. spin_lock(&zram->wb_limit_lock);
  317. zram->wb_limit_enable = val;
  318. spin_unlock(&zram->wb_limit_lock);
  319. up_read(&zram->init_lock);
  320. ret = len;
  321. return ret;
  322. }
  323. static ssize_t writeback_limit_enable_show(struct device *dev,
  324. struct device_attribute *attr, char *buf)
  325. {
  326. bool val;
  327. struct zram *zram = dev_to_zram(dev);
  328. down_read(&zram->init_lock);
  329. spin_lock(&zram->wb_limit_lock);
  330. val = zram->wb_limit_enable;
  331. spin_unlock(&zram->wb_limit_lock);
  332. up_read(&zram->init_lock);
  333. return scnprintf(buf, PAGE_SIZE, "%d\n", val);
  334. }
  335. static ssize_t writeback_limit_store(struct device *dev,
  336. struct device_attribute *attr, const char *buf, size_t len)
  337. {
  338. struct zram *zram = dev_to_zram(dev);
  339. u64 val;
  340. ssize_t ret = -EINVAL;
  341. if (kstrtoull(buf, 10, &val))
  342. return ret;
  343. down_read(&zram->init_lock);
  344. spin_lock(&zram->wb_limit_lock);
  345. zram->bd_wb_limit = val;
  346. spin_unlock(&zram->wb_limit_lock);
  347. up_read(&zram->init_lock);
  348. ret = len;
  349. return ret;
  350. }
  351. static ssize_t writeback_limit_show(struct device *dev,
  352. struct device_attribute *attr, char *buf)
  353. {
  354. u64 val;
  355. struct zram *zram = dev_to_zram(dev);
  356. down_read(&zram->init_lock);
  357. spin_lock(&zram->wb_limit_lock);
  358. val = zram->bd_wb_limit;
  359. spin_unlock(&zram->wb_limit_lock);
  360. up_read(&zram->init_lock);
  361. return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
  362. }
  363. static void reset_bdev(struct zram *zram)
  364. {
  365. if (!zram->backing_dev)
  366. return;
  367. /* hope filp_close flush all of IO */
  368. filp_close(zram->backing_dev, NULL);
  369. zram->backing_dev = NULL;
  370. zram->bdev = NULL;
  371. zram->disk->fops = &zram_devops;
  372. kvfree(zram->bitmap);
  373. zram->bitmap = NULL;
  374. }
  375. static ssize_t backing_dev_show(struct device *dev,
  376. struct device_attribute *attr, char *buf)
  377. {
  378. struct file *file;
  379. struct zram *zram = dev_to_zram(dev);
  380. char *p;
  381. ssize_t ret;
  382. down_read(&zram->init_lock);
  383. file = zram->backing_dev;
  384. if (!file) {
  385. memcpy(buf, "none\n", 5);
  386. up_read(&zram->init_lock);
  387. return 5;
  388. }
  389. p = file_path(file, buf, PAGE_SIZE - 1);
  390. if (IS_ERR(p)) {
  391. ret = PTR_ERR(p);
  392. goto out;
  393. }
  394. ret = strlen(p);
  395. memmove(buf, p, ret);
  396. buf[ret++] = '\n';
  397. out:
  398. up_read(&zram->init_lock);
  399. return ret;
  400. }
  401. static ssize_t backing_dev_store(struct device *dev,
  402. struct device_attribute *attr, const char *buf, size_t len)
  403. {
  404. char *file_name;
  405. size_t sz;
  406. struct file *backing_dev = NULL;
  407. struct inode *inode;
  408. unsigned int bitmap_sz;
  409. unsigned long nr_pages, *bitmap = NULL;
  410. int err;
  411. struct zram *zram = dev_to_zram(dev);
  412. file_name = kmalloc(PATH_MAX, GFP_KERNEL);
  413. if (!file_name)
  414. return -ENOMEM;
  415. down_write(&zram->init_lock);
  416. if (init_done(zram)) {
  417. pr_info("Can't setup backing device for initialized device\n");
  418. err = -EBUSY;
  419. goto out;
  420. }
  421. strscpy(file_name, buf, PATH_MAX);
  422. /* ignore trailing newline */
  423. sz = strlen(file_name);
  424. if (sz > 0 && file_name[sz - 1] == '\n')
  425. file_name[sz - 1] = 0x00;
  426. backing_dev = filp_open(file_name, O_RDWR | O_LARGEFILE | O_EXCL, 0);
  427. if (IS_ERR(backing_dev)) {
  428. err = PTR_ERR(backing_dev);
  429. backing_dev = NULL;
  430. goto out;
  431. }
  432. inode = backing_dev->f_mapping->host;
  433. /* Support only block device in this moment */
  434. if (!S_ISBLK(inode->i_mode)) {
  435. err = -ENOTBLK;
  436. goto out;
  437. }
  438. nr_pages = i_size_read(inode) >> PAGE_SHIFT;
  439. /* Refuse to use zero sized device (also prevents self reference) */
  440. if (!nr_pages) {
  441. err = -EINVAL;
  442. goto out;
  443. }
  444. bitmap_sz = BITS_TO_LONGS(nr_pages) * sizeof(long);
  445. bitmap = kvzalloc(bitmap_sz, GFP_KERNEL);
  446. if (!bitmap) {
  447. err = -ENOMEM;
  448. goto out;
  449. }
  450. reset_bdev(zram);
  451. zram->bdev = I_BDEV(inode);
  452. zram->backing_dev = backing_dev;
  453. zram->bitmap = bitmap;
  454. zram->nr_pages = nr_pages;
  455. up_write(&zram->init_lock);
  456. pr_info("setup backing device %s\n", file_name);
  457. kfree(file_name);
  458. return len;
  459. out:
  460. kvfree(bitmap);
  461. if (backing_dev)
  462. filp_close(backing_dev, NULL);
  463. up_write(&zram->init_lock);
  464. kfree(file_name);
  465. return err;
  466. }
  467. static unsigned long alloc_block_bdev(struct zram *zram)
  468. {
  469. unsigned long blk_idx = 1;
  470. retry:
  471. /* skip 0 bit to confuse zram.handle = 0 */
  472. blk_idx = find_next_zero_bit(zram->bitmap, zram->nr_pages, blk_idx);
  473. if (blk_idx == zram->nr_pages)
  474. return 0;
  475. if (test_and_set_bit(blk_idx, zram->bitmap))
  476. goto retry;
  477. atomic64_inc(&zram->stats.bd_count);
  478. return blk_idx;
  479. }
  480. static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
  481. {
  482. int was_set;
  483. was_set = test_and_clear_bit(blk_idx, zram->bitmap);
  484. WARN_ON_ONCE(!was_set);
  485. atomic64_dec(&zram->stats.bd_count);
  486. }
  487. static void read_from_bdev_async(struct zram *zram, struct page *page,
  488. unsigned long entry, struct bio *parent)
  489. {
  490. struct bio *bio;
  491. bio = bio_alloc(zram->bdev, 1, parent->bi_opf, GFP_NOIO);
  492. bio->bi_iter.bi_sector = entry * (PAGE_SIZE >> 9);
  493. __bio_add_page(bio, page, PAGE_SIZE, 0);
  494. bio_chain(bio, parent);
  495. submit_bio(bio);
  496. }
  497. #define PAGE_WB_SIG "page_index="
  498. #define PAGE_WRITEBACK 0
  499. #define HUGE_WRITEBACK (1<<0)
  500. #define IDLE_WRITEBACK (1<<1)
  501. #define INCOMPRESSIBLE_WRITEBACK (1<<2)
  502. static ssize_t writeback_store(struct device *dev,
  503. struct device_attribute *attr, const char *buf, size_t len)
  504. {
  505. struct zram *zram = dev_to_zram(dev);
  506. unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
  507. unsigned long index = 0;
  508. struct bio bio;
  509. struct bio_vec bio_vec;
  510. struct page *page;
  511. ssize_t ret = len;
  512. int mode, err;
  513. unsigned long blk_idx = 0;
  514. if (sysfs_streq(buf, "idle"))
  515. mode = IDLE_WRITEBACK;
  516. else if (sysfs_streq(buf, "huge"))
  517. mode = HUGE_WRITEBACK;
  518. else if (sysfs_streq(buf, "huge_idle"))
  519. mode = IDLE_WRITEBACK | HUGE_WRITEBACK;
  520. else if (sysfs_streq(buf, "incompressible"))
  521. mode = INCOMPRESSIBLE_WRITEBACK;
  522. else {
  523. if (strncmp(buf, PAGE_WB_SIG, sizeof(PAGE_WB_SIG) - 1))
  524. return -EINVAL;
  525. if (kstrtol(buf + sizeof(PAGE_WB_SIG) - 1, 10, &index) ||
  526. index >= nr_pages)
  527. return -EINVAL;
  528. nr_pages = 1;
  529. mode = PAGE_WRITEBACK;
  530. }
  531. down_read(&zram->init_lock);
  532. if (!init_done(zram)) {
  533. ret = -EINVAL;
  534. goto release_init_lock;
  535. }
  536. /* Do not permit concurrent post-processing actions. */
  537. if (atomic_xchg(&zram->pp_in_progress, 1)) {
  538. up_read(&zram->init_lock);
  539. return -EAGAIN;
  540. }
  541. if (!zram->backing_dev) {
  542. ret = -ENODEV;
  543. goto release_init_lock;
  544. }
  545. page = alloc_page(GFP_KERNEL);
  546. if (!page) {
  547. ret = -ENOMEM;
  548. goto release_init_lock;
  549. }
  550. for (; nr_pages != 0; index++, nr_pages--) {
  551. spin_lock(&zram->wb_limit_lock);
  552. if (zram->wb_limit_enable && !zram->bd_wb_limit) {
  553. spin_unlock(&zram->wb_limit_lock);
  554. ret = -EIO;
  555. break;
  556. }
  557. spin_unlock(&zram->wb_limit_lock);
  558. if (!blk_idx) {
  559. blk_idx = alloc_block_bdev(zram);
  560. if (!blk_idx) {
  561. ret = -ENOSPC;
  562. break;
  563. }
  564. }
  565. zram_slot_lock(zram, index);
  566. if (!zram_allocated(zram, index))
  567. goto next;
  568. if (zram_test_flag(zram, index, ZRAM_WB) ||
  569. zram_test_flag(zram, index, ZRAM_SAME) ||
  570. zram_test_flag(zram, index, ZRAM_UNDER_WB))
  571. goto next;
  572. if (mode & IDLE_WRITEBACK &&
  573. !zram_test_flag(zram, index, ZRAM_IDLE))
  574. goto next;
  575. if (mode & HUGE_WRITEBACK &&
  576. !zram_test_flag(zram, index, ZRAM_HUGE))
  577. goto next;
  578. if (mode & INCOMPRESSIBLE_WRITEBACK &&
  579. !zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
  580. goto next;
  581. /*
  582. * Clearing ZRAM_UNDER_WB is duty of caller.
  583. * IOW, zram_free_page never clear it.
  584. */
  585. zram_set_flag(zram, index, ZRAM_UNDER_WB);
  586. /* Need for hugepage writeback racing */
  587. zram_set_flag(zram, index, ZRAM_IDLE);
  588. zram_slot_unlock(zram, index);
  589. if (zram_read_page(zram, page, index, NULL)) {
  590. zram_slot_lock(zram, index);
  591. zram_clear_flag(zram, index, ZRAM_UNDER_WB);
  592. zram_clear_flag(zram, index, ZRAM_IDLE);
  593. zram_slot_unlock(zram, index);
  594. continue;
  595. }
  596. bio_init(&bio, zram->bdev, &bio_vec, 1,
  597. REQ_OP_WRITE | REQ_SYNC);
  598. bio.bi_iter.bi_sector = blk_idx * (PAGE_SIZE >> 9);
  599. __bio_add_page(&bio, page, PAGE_SIZE, 0);
  600. /*
  601. * XXX: A single page IO would be inefficient for write
  602. * but it would be not bad as starter.
  603. */
  604. err = submit_bio_wait(&bio);
  605. if (err) {
  606. zram_slot_lock(zram, index);
  607. zram_clear_flag(zram, index, ZRAM_UNDER_WB);
  608. zram_clear_flag(zram, index, ZRAM_IDLE);
  609. zram_slot_unlock(zram, index);
  610. /*
  611. * BIO errors are not fatal, we continue and simply
  612. * attempt to writeback the remaining objects (pages).
  613. * At the same time we need to signal user-space that
  614. * some writes (at least one, but also could be all of
  615. * them) were not successful and we do so by returning
  616. * the most recent BIO error.
  617. */
  618. ret = err;
  619. continue;
  620. }
  621. atomic64_inc(&zram->stats.bd_writes);
  622. /*
  623. * We released zram_slot_lock so need to check if the slot was
  624. * changed. If there is freeing for the slot, we can catch it
  625. * easily by zram_allocated.
  626. * A subtle case is the slot is freed/reallocated/marked as
  627. * ZRAM_IDLE again. To close the race, idle_store doesn't
  628. * mark ZRAM_IDLE once it found the slot was ZRAM_UNDER_WB.
  629. * Thus, we could close the race by checking ZRAM_IDLE bit.
  630. */
  631. zram_slot_lock(zram, index);
  632. if (!zram_allocated(zram, index) ||
  633. !zram_test_flag(zram, index, ZRAM_IDLE)) {
  634. zram_clear_flag(zram, index, ZRAM_UNDER_WB);
  635. zram_clear_flag(zram, index, ZRAM_IDLE);
  636. goto next;
  637. }
  638. zram_free_page(zram, index);
  639. zram_clear_flag(zram, index, ZRAM_UNDER_WB);
  640. zram_set_flag(zram, index, ZRAM_WB);
  641. zram_set_element(zram, index, blk_idx);
  642. blk_idx = 0;
  643. atomic64_inc(&zram->stats.pages_stored);
  644. spin_lock(&zram->wb_limit_lock);
  645. if (zram->wb_limit_enable && zram->bd_wb_limit > 0)
  646. zram->bd_wb_limit -= 1UL << (PAGE_SHIFT - 12);
  647. spin_unlock(&zram->wb_limit_lock);
  648. next:
  649. zram_slot_unlock(zram, index);
  650. }
  651. if (blk_idx)
  652. free_block_bdev(zram, blk_idx);
  653. __free_page(page);
  654. release_init_lock:
  655. atomic_set(&zram->pp_in_progress, 0);
  656. up_read(&zram->init_lock);
  657. return ret;
  658. }
  659. struct zram_work {
  660. struct work_struct work;
  661. struct zram *zram;
  662. unsigned long entry;
  663. struct page *page;
  664. int error;
  665. };
  666. static void zram_sync_read(struct work_struct *work)
  667. {
  668. struct zram_work *zw = container_of(work, struct zram_work, work);
  669. struct bio_vec bv;
  670. struct bio bio;
  671. bio_init(&bio, zw->zram->bdev, &bv, 1, REQ_OP_READ);
  672. bio.bi_iter.bi_sector = zw->entry * (PAGE_SIZE >> 9);
  673. __bio_add_page(&bio, zw->page, PAGE_SIZE, 0);
  674. zw->error = submit_bio_wait(&bio);
  675. }
  676. /*
  677. * Block layer want one ->submit_bio to be active at a time, so if we use
  678. * chained IO with parent IO in same context, it's a deadlock. To avoid that,
  679. * use a worker thread context.
  680. */
  681. static int read_from_bdev_sync(struct zram *zram, struct page *page,
  682. unsigned long entry)
  683. {
  684. struct zram_work work;
  685. work.page = page;
  686. work.zram = zram;
  687. work.entry = entry;
  688. INIT_WORK_ONSTACK(&work.work, zram_sync_read);
  689. queue_work(system_unbound_wq, &work.work);
  690. flush_work(&work.work);
  691. destroy_work_on_stack(&work.work);
  692. return work.error;
  693. }
  694. static int read_from_bdev(struct zram *zram, struct page *page,
  695. unsigned long entry, struct bio *parent)
  696. {
  697. atomic64_inc(&zram->stats.bd_reads);
  698. if (!parent) {
  699. if (WARN_ON_ONCE(!IS_ENABLED(ZRAM_PARTIAL_IO)))
  700. return -EIO;
  701. return read_from_bdev_sync(zram, page, entry);
  702. }
  703. read_from_bdev_async(zram, page, entry, parent);
  704. return 0;
  705. }
  706. #else
  707. static inline void reset_bdev(struct zram *zram) {};
  708. static int read_from_bdev(struct zram *zram, struct page *page,
  709. unsigned long entry, struct bio *parent)
  710. {
  711. return -EIO;
  712. }
  713. static void free_block_bdev(struct zram *zram, unsigned long blk_idx) {};
  714. #endif
  715. #ifdef CONFIG_ZRAM_MEMORY_TRACKING
  716. static struct dentry *zram_debugfs_root;
  717. static void zram_debugfs_create(void)
  718. {
  719. zram_debugfs_root = debugfs_create_dir("zram", NULL);
  720. }
  721. static void zram_debugfs_destroy(void)
  722. {
  723. debugfs_remove_recursive(zram_debugfs_root);
  724. }
  725. static ssize_t read_block_state(struct file *file, char __user *buf,
  726. size_t count, loff_t *ppos)
  727. {
  728. char *kbuf;
  729. ssize_t index, written = 0;
  730. struct zram *zram = file->private_data;
  731. unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
  732. struct timespec64 ts;
  733. kbuf = kvmalloc(count, GFP_KERNEL);
  734. if (!kbuf)
  735. return -ENOMEM;
  736. down_read(&zram->init_lock);
  737. if (!init_done(zram)) {
  738. up_read(&zram->init_lock);
  739. kvfree(kbuf);
  740. return -EINVAL;
  741. }
  742. for (index = *ppos; index < nr_pages; index++) {
  743. int copied;
  744. zram_slot_lock(zram, index);
  745. if (!zram_allocated(zram, index))
  746. goto next;
  747. ts = ktime_to_timespec64(zram->table[index].ac_time);
  748. copied = snprintf(kbuf + written, count,
  749. "%12zd %12lld.%06lu %c%c%c%c%c%c\n",
  750. index, (s64)ts.tv_sec,
  751. ts.tv_nsec / NSEC_PER_USEC,
  752. zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
  753. zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
  754. zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.',
  755. zram_test_flag(zram, index, ZRAM_IDLE) ? 'i' : '.',
  756. zram_get_priority(zram, index) ? 'r' : '.',
  757. zram_test_flag(zram, index,
  758. ZRAM_INCOMPRESSIBLE) ? 'n' : '.');
  759. if (count <= copied) {
  760. zram_slot_unlock(zram, index);
  761. break;
  762. }
  763. written += copied;
  764. count -= copied;
  765. next:
  766. zram_slot_unlock(zram, index);
  767. *ppos += 1;
  768. }
  769. up_read(&zram->init_lock);
  770. if (copy_to_user(buf, kbuf, written))
  771. written = -EFAULT;
  772. kvfree(kbuf);
  773. return written;
  774. }
  775. static const struct file_operations proc_zram_block_state_op = {
  776. .open = simple_open,
  777. .read = read_block_state,
  778. .llseek = default_llseek,
  779. };
  780. static void zram_debugfs_register(struct zram *zram)
  781. {
  782. if (!zram_debugfs_root)
  783. return;
  784. zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
  785. zram_debugfs_root);
  786. debugfs_create_file("block_state", 0400, zram->debugfs_dir,
  787. zram, &proc_zram_block_state_op);
  788. }
  789. static void zram_debugfs_unregister(struct zram *zram)
  790. {
  791. debugfs_remove_recursive(zram->debugfs_dir);
  792. }
  793. #else
  794. static void zram_debugfs_create(void) {};
  795. static void zram_debugfs_destroy(void) {};
  796. static void zram_debugfs_register(struct zram *zram) {};
  797. static void zram_debugfs_unregister(struct zram *zram) {};
  798. #endif
  799. /*
  800. * We switched to per-cpu streams and this attr is not needed anymore.
  801. * However, we will keep it around for some time, because:
  802. * a) we may revert per-cpu streams in the future
  803. * b) it's visible to user space and we need to follow our 2 years
  804. * retirement rule; but we already have a number of 'soon to be
  805. * altered' attrs, so max_comp_streams need to wait for the next
  806. * layoff cycle.
  807. */
  808. static ssize_t max_comp_streams_show(struct device *dev,
  809. struct device_attribute *attr, char *buf)
  810. {
  811. return scnprintf(buf, PAGE_SIZE, "%d\n", num_online_cpus());
  812. }
  813. static ssize_t max_comp_streams_store(struct device *dev,
  814. struct device_attribute *attr, const char *buf, size_t len)
  815. {
  816. return len;
  817. }
  818. static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
  819. {
  820. /* Do not free statically defined compression algorithms */
  821. if (zram->comp_algs[prio] != default_compressor)
  822. kfree(zram->comp_algs[prio]);
  823. zram->comp_algs[prio] = alg;
  824. }
  825. static ssize_t __comp_algorithm_show(struct zram *zram, u32 prio, char *buf)
  826. {
  827. ssize_t sz;
  828. down_read(&zram->init_lock);
  829. sz = zcomp_available_show(zram->comp_algs[prio], buf);
  830. up_read(&zram->init_lock);
  831. return sz;
  832. }
  833. static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
  834. {
  835. char *compressor;
  836. size_t sz;
  837. sz = strlen(buf);
  838. if (sz >= CRYPTO_MAX_ALG_NAME)
  839. return -E2BIG;
  840. compressor = kstrdup(buf, GFP_KERNEL);
  841. if (!compressor)
  842. return -ENOMEM;
  843. /* ignore trailing newline */
  844. if (sz > 0 && compressor[sz - 1] == '\n')
  845. compressor[sz - 1] = 0x00;
  846. if (!zcomp_available_algorithm(compressor)) {
  847. kfree(compressor);
  848. return -EINVAL;
  849. }
  850. down_write(&zram->init_lock);
  851. if (init_done(zram)) {
  852. up_write(&zram->init_lock);
  853. kfree(compressor);
  854. pr_info("Can't change algorithm for initialized device\n");
  855. return -EBUSY;
  856. }
  857. comp_algorithm_set(zram, prio, compressor);
  858. up_write(&zram->init_lock);
  859. return 0;
  860. }
  861. static void comp_params_reset(struct zram *zram, u32 prio)
  862. {
  863. struct zcomp_params *params = &zram->params[prio];
  864. vfree(params->dict);
  865. params->level = ZCOMP_PARAM_NO_LEVEL;
  866. params->dict_sz = 0;
  867. params->dict = NULL;
  868. }
  869. static int comp_params_store(struct zram *zram, u32 prio, s32 level,
  870. const char *dict_path)
  871. {
  872. ssize_t sz = 0;
  873. comp_params_reset(zram, prio);
  874. if (dict_path) {
  875. sz = kernel_read_file_from_path(dict_path, 0,
  876. &zram->params[prio].dict,
  877. INT_MAX,
  878. NULL,
  879. READING_POLICY);
  880. if (sz < 0)
  881. return -EINVAL;
  882. }
  883. zram->params[prio].dict_sz = sz;
  884. zram->params[prio].level = level;
  885. return 0;
  886. }
  887. static ssize_t algorithm_params_store(struct device *dev,
  888. struct device_attribute *attr,
  889. const char *buf,
  890. size_t len)
  891. {
  892. s32 prio = ZRAM_PRIMARY_COMP, level = ZCOMP_PARAM_NO_LEVEL;
  893. char *args, *param, *val, *algo = NULL, *dict_path = NULL;
  894. struct zram *zram = dev_to_zram(dev);
  895. int ret;
  896. args = skip_spaces(buf);
  897. while (*args) {
  898. args = next_arg(args, &param, &val);
  899. if (!val || !*val)
  900. return -EINVAL;
  901. if (!strcmp(param, "priority")) {
  902. ret = kstrtoint(val, 10, &prio);
  903. if (ret)
  904. return ret;
  905. continue;
  906. }
  907. if (!strcmp(param, "level")) {
  908. ret = kstrtoint(val, 10, &level);
  909. if (ret)
  910. return ret;
  911. continue;
  912. }
  913. if (!strcmp(param, "algo")) {
  914. algo = val;
  915. continue;
  916. }
  917. if (!strcmp(param, "dict")) {
  918. dict_path = val;
  919. continue;
  920. }
  921. }
  922. /* Lookup priority by algorithm name */
  923. if (algo) {
  924. s32 p;
  925. prio = -EINVAL;
  926. for (p = ZRAM_PRIMARY_COMP; p < ZRAM_MAX_COMPS; p++) {
  927. if (!zram->comp_algs[p])
  928. continue;
  929. if (!strcmp(zram->comp_algs[p], algo)) {
  930. prio = p;
  931. break;
  932. }
  933. }
  934. }
  935. if (prio < ZRAM_PRIMARY_COMP || prio >= ZRAM_MAX_COMPS)
  936. return -EINVAL;
  937. ret = comp_params_store(zram, prio, level, dict_path);
  938. return ret ? ret : len;
  939. }
  940. static ssize_t comp_algorithm_show(struct device *dev,
  941. struct device_attribute *attr,
  942. char *buf)
  943. {
  944. struct zram *zram = dev_to_zram(dev);
  945. return __comp_algorithm_show(zram, ZRAM_PRIMARY_COMP, buf);
  946. }
  947. static ssize_t comp_algorithm_store(struct device *dev,
  948. struct device_attribute *attr,
  949. const char *buf,
  950. size_t len)
  951. {
  952. struct zram *zram = dev_to_zram(dev);
  953. int ret;
  954. ret = __comp_algorithm_store(zram, ZRAM_PRIMARY_COMP, buf);
  955. return ret ? ret : len;
  956. }
  957. #ifdef CONFIG_ZRAM_MULTI_COMP
  958. static ssize_t recomp_algorithm_show(struct device *dev,
  959. struct device_attribute *attr,
  960. char *buf)
  961. {
  962. struct zram *zram = dev_to_zram(dev);
  963. ssize_t sz = 0;
  964. u32 prio;
  965. for (prio = ZRAM_SECONDARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
  966. if (!zram->comp_algs[prio])
  967. continue;
  968. sz += scnprintf(buf + sz, PAGE_SIZE - sz - 2, "#%d: ", prio);
  969. sz += __comp_algorithm_show(zram, prio, buf + sz);
  970. }
  971. return sz;
  972. }
  973. static ssize_t recomp_algorithm_store(struct device *dev,
  974. struct device_attribute *attr,
  975. const char *buf,
  976. size_t len)
  977. {
  978. struct zram *zram = dev_to_zram(dev);
  979. int prio = ZRAM_SECONDARY_COMP;
  980. char *args, *param, *val;
  981. char *alg = NULL;
  982. int ret;
  983. args = skip_spaces(buf);
  984. while (*args) {
  985. args = next_arg(args, &param, &val);
  986. if (!val || !*val)
  987. return -EINVAL;
  988. if (!strcmp(param, "algo")) {
  989. alg = val;
  990. continue;
  991. }
  992. if (!strcmp(param, "priority")) {
  993. ret = kstrtoint(val, 10, &prio);
  994. if (ret)
  995. return ret;
  996. continue;
  997. }
  998. }
  999. if (!alg)
  1000. return -EINVAL;
  1001. if (prio < ZRAM_SECONDARY_COMP || prio >= ZRAM_MAX_COMPS)
  1002. return -EINVAL;
  1003. ret = __comp_algorithm_store(zram, prio, alg);
  1004. return ret ? ret : len;
  1005. }
  1006. #endif
  1007. static ssize_t compact_store(struct device *dev,
  1008. struct device_attribute *attr, const char *buf, size_t len)
  1009. {
  1010. struct zram *zram = dev_to_zram(dev);
  1011. down_read(&zram->init_lock);
  1012. if (!init_done(zram)) {
  1013. up_read(&zram->init_lock);
  1014. return -EINVAL;
  1015. }
  1016. zs_compact(zram->mem_pool);
  1017. up_read(&zram->init_lock);
  1018. return len;
  1019. }
  1020. static ssize_t io_stat_show(struct device *dev,
  1021. struct device_attribute *attr, char *buf)
  1022. {
  1023. struct zram *zram = dev_to_zram(dev);
  1024. ssize_t ret;
  1025. down_read(&zram->init_lock);
  1026. ret = scnprintf(buf, PAGE_SIZE,
  1027. "%8llu %8llu 0 %8llu\n",
  1028. (u64)atomic64_read(&zram->stats.failed_reads),
  1029. (u64)atomic64_read(&zram->stats.failed_writes),
  1030. (u64)atomic64_read(&zram->stats.notify_free));
  1031. up_read(&zram->init_lock);
  1032. return ret;
  1033. }
  1034. static ssize_t mm_stat_show(struct device *dev,
  1035. struct device_attribute *attr, char *buf)
  1036. {
  1037. struct zram *zram = dev_to_zram(dev);
  1038. struct zs_pool_stats pool_stats;
  1039. u64 orig_size, mem_used = 0;
  1040. long max_used;
  1041. ssize_t ret;
  1042. memset(&pool_stats, 0x00, sizeof(struct zs_pool_stats));
  1043. down_read(&zram->init_lock);
  1044. if (init_done(zram)) {
  1045. mem_used = zs_get_total_pages(zram->mem_pool);
  1046. zs_pool_stats(zram->mem_pool, &pool_stats);
  1047. }
  1048. orig_size = atomic64_read(&zram->stats.pages_stored);
  1049. max_used = atomic_long_read(&zram->stats.max_used_pages);
  1050. ret = scnprintf(buf, PAGE_SIZE,
  1051. "%8llu %8llu %8llu %8lu %8ld %8llu %8lu %8llu %8llu\n",
  1052. orig_size << PAGE_SHIFT,
  1053. (u64)atomic64_read(&zram->stats.compr_data_size),
  1054. mem_used << PAGE_SHIFT,
  1055. zram->limit_pages << PAGE_SHIFT,
  1056. max_used << PAGE_SHIFT,
  1057. (u64)atomic64_read(&zram->stats.same_pages),
  1058. atomic_long_read(&pool_stats.pages_compacted),
  1059. (u64)atomic64_read(&zram->stats.huge_pages),
  1060. (u64)atomic64_read(&zram->stats.huge_pages_since));
  1061. up_read(&zram->init_lock);
  1062. return ret;
  1063. }
  1064. #ifdef CONFIG_ZRAM_WRITEBACK
  1065. #define FOUR_K(x) ((x) * (1 << (PAGE_SHIFT - 12)))
  1066. static ssize_t bd_stat_show(struct device *dev,
  1067. struct device_attribute *attr, char *buf)
  1068. {
  1069. struct zram *zram = dev_to_zram(dev);
  1070. ssize_t ret;
  1071. down_read(&zram->init_lock);
  1072. ret = scnprintf(buf, PAGE_SIZE,
  1073. "%8llu %8llu %8llu\n",
  1074. FOUR_K((u64)atomic64_read(&zram->stats.bd_count)),
  1075. FOUR_K((u64)atomic64_read(&zram->stats.bd_reads)),
  1076. FOUR_K((u64)atomic64_read(&zram->stats.bd_writes)));
  1077. up_read(&zram->init_lock);
  1078. return ret;
  1079. }
  1080. #endif
  1081. static ssize_t debug_stat_show(struct device *dev,
  1082. struct device_attribute *attr, char *buf)
  1083. {
  1084. int version = 1;
  1085. struct zram *zram = dev_to_zram(dev);
  1086. ssize_t ret;
  1087. down_read(&zram->init_lock);
  1088. ret = scnprintf(buf, PAGE_SIZE,
  1089. "version: %d\n%8llu %8llu\n",
  1090. version,
  1091. (u64)atomic64_read(&zram->stats.writestall),
  1092. (u64)atomic64_read(&zram->stats.miss_free));
  1093. up_read(&zram->init_lock);
  1094. return ret;
  1095. }
  1096. static DEVICE_ATTR_RO(io_stat);
  1097. static DEVICE_ATTR_RO(mm_stat);
  1098. #ifdef CONFIG_ZRAM_WRITEBACK
  1099. static DEVICE_ATTR_RO(bd_stat);
  1100. #endif
  1101. static DEVICE_ATTR_RO(debug_stat);
  1102. static void zram_meta_free(struct zram *zram, u64 disksize)
  1103. {
  1104. size_t num_pages = disksize >> PAGE_SHIFT;
  1105. size_t index;
  1106. if (!zram->table)
  1107. return;
  1108. /* Free all pages that are still in this zram device */
  1109. for (index = 0; index < num_pages; index++)
  1110. zram_free_page(zram, index);
  1111. zs_destroy_pool(zram->mem_pool);
  1112. vfree(zram->table);
  1113. zram->table = NULL;
  1114. }
  1115. static bool zram_meta_alloc(struct zram *zram, u64 disksize)
  1116. {
  1117. size_t num_pages, index;
  1118. num_pages = disksize >> PAGE_SHIFT;
  1119. zram->table = vzalloc(array_size(num_pages, sizeof(*zram->table)));
  1120. if (!zram->table)
  1121. return false;
  1122. zram->mem_pool = zs_create_pool(zram->disk->disk_name);
  1123. if (!zram->mem_pool) {
  1124. vfree(zram->table);
  1125. zram->table = NULL;
  1126. return false;
  1127. }
  1128. if (!huge_class_size)
  1129. huge_class_size = zs_huge_class_size(zram->mem_pool);
  1130. for (index = 0; index < num_pages; index++)
  1131. spin_lock_init(&zram->table[index].lock);
  1132. return true;
  1133. }
  1134. /*
  1135. * To protect concurrent access to the same index entry,
  1136. * caller should hold this table index entry's bit_spinlock to
  1137. * indicate this index entry is accessing.
  1138. */
  1139. static void zram_free_page(struct zram *zram, size_t index)
  1140. {
  1141. unsigned long handle;
  1142. #ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
  1143. zram->table[index].ac_time = 0;
  1144. #endif
  1145. if (zram_test_flag(zram, index, ZRAM_IDLE))
  1146. zram_clear_flag(zram, index, ZRAM_IDLE);
  1147. if (zram_test_flag(zram, index, ZRAM_HUGE)) {
  1148. zram_clear_flag(zram, index, ZRAM_HUGE);
  1149. atomic64_dec(&zram->stats.huge_pages);
  1150. }
  1151. if (zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
  1152. zram_clear_flag(zram, index, ZRAM_INCOMPRESSIBLE);
  1153. zram_set_priority(zram, index, 0);
  1154. if (zram_test_flag(zram, index, ZRAM_WB)) {
  1155. zram_clear_flag(zram, index, ZRAM_WB);
  1156. free_block_bdev(zram, zram_get_element(zram, index));
  1157. goto out;
  1158. }
  1159. /*
  1160. * No memory is allocated for same element filled pages.
  1161. * Simply clear same page flag.
  1162. */
  1163. if (zram_test_flag(zram, index, ZRAM_SAME)) {
  1164. zram_clear_flag(zram, index, ZRAM_SAME);
  1165. atomic64_dec(&zram->stats.same_pages);
  1166. goto out;
  1167. }
  1168. handle = zram_get_handle(zram, index);
  1169. if (!handle)
  1170. return;
  1171. zs_free(zram->mem_pool, handle);
  1172. atomic64_sub(zram_get_obj_size(zram, index),
  1173. &zram->stats.compr_data_size);
  1174. out:
  1175. atomic64_dec(&zram->stats.pages_stored);
  1176. zram_set_handle(zram, index, 0);
  1177. zram_set_obj_size(zram, index, 0);
  1178. WARN_ON_ONCE(zram->table[index].flags &
  1179. ~(1UL << ZRAM_UNDER_WB));
  1180. }
  1181. /*
  1182. * Reads (decompresses if needed) a page from zspool (zsmalloc).
  1183. * Corresponding ZRAM slot should be locked.
  1184. */
  1185. static int zram_read_from_zspool(struct zram *zram, struct page *page,
  1186. u32 index)
  1187. {
  1188. struct zcomp_strm *zstrm;
  1189. unsigned long handle;
  1190. unsigned int size;
  1191. void *src, *dst;
  1192. u32 prio;
  1193. int ret;
  1194. handle = zram_get_handle(zram, index);
  1195. if (!handle || zram_test_flag(zram, index, ZRAM_SAME)) {
  1196. unsigned long value;
  1197. void *mem;
  1198. value = handle ? zram_get_element(zram, index) : 0;
  1199. mem = kmap_local_page(page);
  1200. zram_fill_page(mem, PAGE_SIZE, value);
  1201. kunmap_local(mem);
  1202. return 0;
  1203. }
  1204. size = zram_get_obj_size(zram, index);
  1205. if (size != PAGE_SIZE) {
  1206. prio = zram_get_priority(zram, index);
  1207. zstrm = zcomp_stream_get(zram->comps[prio]);
  1208. }
  1209. src = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
  1210. if (size == PAGE_SIZE) {
  1211. dst = kmap_local_page(page);
  1212. copy_page(dst, src);
  1213. kunmap_local(dst);
  1214. ret = 0;
  1215. } else {
  1216. dst = kmap_local_page(page);
  1217. ret = zcomp_decompress(zram->comps[prio], zstrm,
  1218. src, size, dst);
  1219. kunmap_local(dst);
  1220. zcomp_stream_put(zram->comps[prio]);
  1221. }
  1222. zs_unmap_object(zram->mem_pool, handle);
  1223. return ret;
  1224. }
  1225. static int zram_read_page(struct zram *zram, struct page *page, u32 index,
  1226. struct bio *parent)
  1227. {
  1228. int ret;
  1229. zram_slot_lock(zram, index);
  1230. if (!zram_test_flag(zram, index, ZRAM_WB)) {
  1231. /* Slot should be locked through out the function call */
  1232. ret = zram_read_from_zspool(zram, page, index);
  1233. zram_slot_unlock(zram, index);
  1234. } else {
  1235. /*
  1236. * The slot should be unlocked before reading from the backing
  1237. * device.
  1238. */
  1239. zram_slot_unlock(zram, index);
  1240. ret = read_from_bdev(zram, page, zram_get_element(zram, index),
  1241. parent);
  1242. }
  1243. /* Should NEVER happen. Return bio error if it does. */
  1244. if (WARN_ON(ret < 0))
  1245. pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
  1246. return ret;
  1247. }
  1248. /*
  1249. * Use a temporary buffer to decompress the page, as the decompressor
  1250. * always expects a full page for the output.
  1251. */
  1252. static int zram_bvec_read_partial(struct zram *zram, struct bio_vec *bvec,
  1253. u32 index, int offset)
  1254. {
  1255. struct page *page = alloc_page(GFP_NOIO);
  1256. int ret;
  1257. if (!page)
  1258. return -ENOMEM;
  1259. ret = zram_read_page(zram, page, index, NULL);
  1260. if (likely(!ret))
  1261. memcpy_to_bvec(bvec, page_address(page) + offset);
  1262. __free_page(page);
  1263. return ret;
  1264. }
  1265. static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  1266. u32 index, int offset, struct bio *bio)
  1267. {
  1268. if (is_partial_io(bvec))
  1269. return zram_bvec_read_partial(zram, bvec, index, offset);
  1270. return zram_read_page(zram, bvec->bv_page, index, bio);
  1271. }
  1272. static int zram_write_page(struct zram *zram, struct page *page, u32 index)
  1273. {
  1274. int ret = 0;
  1275. unsigned long alloced_pages;
  1276. unsigned long handle = -ENOMEM;
  1277. unsigned int comp_len = 0;
  1278. void *src, *dst, *mem;
  1279. struct zcomp_strm *zstrm;
  1280. unsigned long element = 0;
  1281. enum zram_pageflags flags = 0;
  1282. mem = kmap_local_page(page);
  1283. if (page_same_filled(mem, &element)) {
  1284. kunmap_local(mem);
  1285. /* Free memory associated with this sector now. */
  1286. flags = ZRAM_SAME;
  1287. atomic64_inc(&zram->stats.same_pages);
  1288. goto out;
  1289. }
  1290. kunmap_local(mem);
  1291. compress_again:
  1292. zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
  1293. src = kmap_local_page(page);
  1294. ret = zcomp_compress(zram->comps[ZRAM_PRIMARY_COMP], zstrm,
  1295. src, &comp_len);
  1296. kunmap_local(src);
  1297. if (unlikely(ret)) {
  1298. zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
  1299. pr_err("Compression failed! err=%d\n", ret);
  1300. zs_free(zram->mem_pool, handle);
  1301. return ret;
  1302. }
  1303. if (comp_len >= huge_class_size)
  1304. comp_len = PAGE_SIZE;
  1305. /*
  1306. * handle allocation has 2 paths:
  1307. * a) fast path is executed with preemption disabled (for
  1308. * per-cpu streams) and has __GFP_DIRECT_RECLAIM bit clear,
  1309. * since we can't sleep;
  1310. * b) slow path enables preemption and attempts to allocate
  1311. * the page with __GFP_DIRECT_RECLAIM bit set. we have to
  1312. * put per-cpu compression stream and, thus, to re-do
  1313. * the compression once handle is allocated.
  1314. *
  1315. * if we have a 'non-null' handle here then we are coming
  1316. * from the slow path and handle has already been allocated.
  1317. */
  1318. if (IS_ERR_VALUE(handle))
  1319. handle = zs_malloc(zram->mem_pool, comp_len,
  1320. __GFP_KSWAPD_RECLAIM |
  1321. __GFP_NOWARN |
  1322. __GFP_HIGHMEM |
  1323. __GFP_MOVABLE);
  1324. if (IS_ERR_VALUE(handle)) {
  1325. zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
  1326. atomic64_inc(&zram->stats.writestall);
  1327. handle = zs_malloc(zram->mem_pool, comp_len,
  1328. GFP_NOIO | __GFP_HIGHMEM |
  1329. __GFP_MOVABLE);
  1330. if (IS_ERR_VALUE(handle))
  1331. return PTR_ERR((void *)handle);
  1332. if (comp_len != PAGE_SIZE)
  1333. goto compress_again;
  1334. /*
  1335. * If the page is not compressible, you need to acquire the
  1336. * lock and execute the code below. The zcomp_stream_get()
  1337. * call is needed to disable the cpu hotplug and grab the
  1338. * zstrm buffer back. It is necessary that the dereferencing
  1339. * of the zstrm variable below occurs correctly.
  1340. */
  1341. zstrm = zcomp_stream_get(zram->comps[ZRAM_PRIMARY_COMP]);
  1342. }
  1343. alloced_pages = zs_get_total_pages(zram->mem_pool);
  1344. update_used_max(zram, alloced_pages);
  1345. if (zram->limit_pages && alloced_pages > zram->limit_pages) {
  1346. zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
  1347. zs_free(zram->mem_pool, handle);
  1348. return -ENOMEM;
  1349. }
  1350. dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
  1351. src = zstrm->buffer;
  1352. if (comp_len == PAGE_SIZE)
  1353. src = kmap_local_page(page);
  1354. memcpy(dst, src, comp_len);
  1355. if (comp_len == PAGE_SIZE)
  1356. kunmap_local(src);
  1357. zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]);
  1358. zs_unmap_object(zram->mem_pool, handle);
  1359. atomic64_add(comp_len, &zram->stats.compr_data_size);
  1360. out:
  1361. /*
  1362. * Free memory associated with this sector
  1363. * before overwriting unused sectors.
  1364. */
  1365. zram_slot_lock(zram, index);
  1366. zram_free_page(zram, index);
  1367. if (comp_len == PAGE_SIZE) {
  1368. zram_set_flag(zram, index, ZRAM_HUGE);
  1369. atomic64_inc(&zram->stats.huge_pages);
  1370. atomic64_inc(&zram->stats.huge_pages_since);
  1371. }
  1372. if (flags) {
  1373. zram_set_flag(zram, index, flags);
  1374. zram_set_element(zram, index, element);
  1375. } else {
  1376. zram_set_handle(zram, index, handle);
  1377. zram_set_obj_size(zram, index, comp_len);
  1378. }
  1379. zram_slot_unlock(zram, index);
  1380. /* Update stats */
  1381. atomic64_inc(&zram->stats.pages_stored);
  1382. return ret;
  1383. }
  1384. /*
  1385. * This is a partial IO. Read the full page before writing the changes.
  1386. */
  1387. static int zram_bvec_write_partial(struct zram *zram, struct bio_vec *bvec,
  1388. u32 index, int offset, struct bio *bio)
  1389. {
  1390. struct page *page = alloc_page(GFP_NOIO);
  1391. int ret;
  1392. if (!page)
  1393. return -ENOMEM;
  1394. ret = zram_read_page(zram, page, index, bio);
  1395. if (!ret) {
  1396. memcpy_from_bvec(page_address(page) + offset, bvec);
  1397. ret = zram_write_page(zram, page, index);
  1398. }
  1399. __free_page(page);
  1400. return ret;
  1401. }
  1402. static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec,
  1403. u32 index, int offset, struct bio *bio)
  1404. {
  1405. if (is_partial_io(bvec))
  1406. return zram_bvec_write_partial(zram, bvec, index, offset, bio);
  1407. return zram_write_page(zram, bvec->bv_page, index);
  1408. }
  1409. #ifdef CONFIG_ZRAM_MULTI_COMP
  1410. /*
  1411. * This function will decompress (unless it's ZRAM_HUGE) the page and then
  1412. * attempt to compress it using provided compression algorithm priority
  1413. * (which is potentially more effective).
  1414. *
  1415. * Corresponding ZRAM slot should be locked.
  1416. */
  1417. static int zram_recompress(struct zram *zram, u32 index, struct page *page,
  1418. u64 *num_recomp_pages, u32 threshold, u32 prio,
  1419. u32 prio_max)
  1420. {
  1421. struct zcomp_strm *zstrm = NULL;
  1422. unsigned long handle_old;
  1423. unsigned long handle_new;
  1424. unsigned int comp_len_old;
  1425. unsigned int comp_len_new;
  1426. unsigned int class_index_old;
  1427. unsigned int class_index_new;
  1428. u32 num_recomps = 0;
  1429. void *src, *dst;
  1430. int ret;
  1431. handle_old = zram_get_handle(zram, index);
  1432. if (!handle_old)
  1433. return -EINVAL;
  1434. comp_len_old = zram_get_obj_size(zram, index);
  1435. /*
  1436. * Do not recompress objects that are already "small enough".
  1437. */
  1438. if (comp_len_old < threshold)
  1439. return 0;
  1440. ret = zram_read_from_zspool(zram, page, index);
  1441. if (ret)
  1442. return ret;
  1443. /*
  1444. * We touched this entry so mark it as non-IDLE. This makes sure that
  1445. * we don't preserve IDLE flag and don't incorrectly pick this entry
  1446. * for different post-processing type (e.g. writeback).
  1447. */
  1448. zram_clear_flag(zram, index, ZRAM_IDLE);
  1449. class_index_old = zs_lookup_class_index(zram->mem_pool, comp_len_old);
  1450. /*
  1451. * Iterate the secondary comp algorithms list (in order of priority)
  1452. * and try to recompress the page.
  1453. */
  1454. for (; prio < prio_max; prio++) {
  1455. if (!zram->comps[prio])
  1456. continue;
  1457. /*
  1458. * Skip if the object is already re-compressed with a higher
  1459. * priority algorithm (or same algorithm).
  1460. */
  1461. if (prio <= zram_get_priority(zram, index))
  1462. continue;
  1463. num_recomps++;
  1464. zstrm = zcomp_stream_get(zram->comps[prio]);
  1465. src = kmap_local_page(page);
  1466. ret = zcomp_compress(zram->comps[prio], zstrm,
  1467. src, &comp_len_new);
  1468. kunmap_local(src);
  1469. if (ret) {
  1470. zcomp_stream_put(zram->comps[prio]);
  1471. return ret;
  1472. }
  1473. class_index_new = zs_lookup_class_index(zram->mem_pool,
  1474. comp_len_new);
  1475. /* Continue until we make progress */
  1476. if (class_index_new >= class_index_old ||
  1477. (threshold && comp_len_new >= threshold)) {
  1478. zcomp_stream_put(zram->comps[prio]);
  1479. continue;
  1480. }
  1481. /* Recompression was successful so break out */
  1482. break;
  1483. }
  1484. /*
  1485. * We did not try to recompress, e.g. when we have only one
  1486. * secondary algorithm and the page is already recompressed
  1487. * using that algorithm
  1488. */
  1489. if (!zstrm)
  1490. return 0;
  1491. /*
  1492. * Decrement the limit (if set) on pages we can recompress, even
  1493. * when current recompression was unsuccessful or did not compress
  1494. * the page below the threshold, because we still spent resources
  1495. * on it.
  1496. */
  1497. if (*num_recomp_pages)
  1498. *num_recomp_pages -= 1;
  1499. if (class_index_new >= class_index_old) {
  1500. /*
  1501. * Secondary algorithms failed to re-compress the page
  1502. * in a way that would save memory, mark the object as
  1503. * incompressible so that we will not try to compress
  1504. * it again.
  1505. *
  1506. * We need to make sure that all secondary algorithms have
  1507. * failed, so we test if the number of recompressions matches
  1508. * the number of active secondary algorithms.
  1509. */
  1510. if (num_recomps == zram->num_active_comps - 1)
  1511. zram_set_flag(zram, index, ZRAM_INCOMPRESSIBLE);
  1512. return 0;
  1513. }
  1514. /* Successful recompression but above threshold */
  1515. if (threshold && comp_len_new >= threshold)
  1516. return 0;
  1517. /*
  1518. * No direct reclaim (slow path) for handle allocation and no
  1519. * re-compression attempt (unlike in zram_write_bvec()) since
  1520. * we already have stored that object in zsmalloc. If we cannot
  1521. * alloc memory for recompressed object then we bail out and
  1522. * simply keep the old (existing) object in zsmalloc.
  1523. */
  1524. handle_new = zs_malloc(zram->mem_pool, comp_len_new,
  1525. __GFP_KSWAPD_RECLAIM |
  1526. __GFP_NOWARN |
  1527. __GFP_HIGHMEM |
  1528. __GFP_MOVABLE);
  1529. if (IS_ERR_VALUE(handle_new)) {
  1530. zcomp_stream_put(zram->comps[prio]);
  1531. return PTR_ERR((void *)handle_new);
  1532. }
  1533. dst = zs_map_object(zram->mem_pool, handle_new, ZS_MM_WO);
  1534. memcpy(dst, zstrm->buffer, comp_len_new);
  1535. zcomp_stream_put(zram->comps[prio]);
  1536. zs_unmap_object(zram->mem_pool, handle_new);
  1537. zram_free_page(zram, index);
  1538. zram_set_handle(zram, index, handle_new);
  1539. zram_set_obj_size(zram, index, comp_len_new);
  1540. zram_set_priority(zram, index, prio);
  1541. atomic64_add(comp_len_new, &zram->stats.compr_data_size);
  1542. atomic64_inc(&zram->stats.pages_stored);
  1543. return 0;
  1544. }
  1545. #define RECOMPRESS_IDLE (1 << 0)
  1546. #define RECOMPRESS_HUGE (1 << 1)
  1547. static ssize_t recompress_store(struct device *dev,
  1548. struct device_attribute *attr,
  1549. const char *buf, size_t len)
  1550. {
  1551. u32 prio = ZRAM_SECONDARY_COMP, prio_max = ZRAM_MAX_COMPS;
  1552. struct zram *zram = dev_to_zram(dev);
  1553. unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
  1554. char *args, *param, *val, *algo = NULL;
  1555. u64 num_recomp_pages = ULLONG_MAX;
  1556. u32 mode = 0, threshold = 0;
  1557. unsigned long index;
  1558. struct page *page;
  1559. ssize_t ret;
  1560. args = skip_spaces(buf);
  1561. while (*args) {
  1562. args = next_arg(args, &param, &val);
  1563. if (!val || !*val)
  1564. return -EINVAL;
  1565. if (!strcmp(param, "type")) {
  1566. if (!strcmp(val, "idle"))
  1567. mode = RECOMPRESS_IDLE;
  1568. if (!strcmp(val, "huge"))
  1569. mode = RECOMPRESS_HUGE;
  1570. if (!strcmp(val, "huge_idle"))
  1571. mode = RECOMPRESS_IDLE | RECOMPRESS_HUGE;
  1572. continue;
  1573. }
  1574. if (!strcmp(param, "max_pages")) {
  1575. /*
  1576. * Limit the number of entries (pages) we attempt to
  1577. * recompress.
  1578. */
  1579. ret = kstrtoull(val, 10, &num_recomp_pages);
  1580. if (ret)
  1581. return ret;
  1582. continue;
  1583. }
  1584. if (!strcmp(param, "threshold")) {
  1585. /*
  1586. * We will re-compress only idle objects equal or
  1587. * greater in size than watermark.
  1588. */
  1589. ret = kstrtouint(val, 10, &threshold);
  1590. if (ret)
  1591. return ret;
  1592. continue;
  1593. }
  1594. if (!strcmp(param, "algo")) {
  1595. algo = val;
  1596. continue;
  1597. }
  1598. if (!strcmp(param, "priority")) {
  1599. ret = kstrtouint(val, 10, &prio);
  1600. if (ret)
  1601. return ret;
  1602. if (prio == ZRAM_PRIMARY_COMP)
  1603. prio = ZRAM_SECONDARY_COMP;
  1604. prio_max = min(prio + 1, ZRAM_MAX_COMPS);
  1605. continue;
  1606. }
  1607. }
  1608. if (threshold >= huge_class_size)
  1609. return -EINVAL;
  1610. down_read(&zram->init_lock);
  1611. if (!init_done(zram)) {
  1612. ret = -EINVAL;
  1613. goto release_init_lock;
  1614. }
  1615. /* Do not permit concurrent post-processing actions. */
  1616. if (atomic_xchg(&zram->pp_in_progress, 1)) {
  1617. up_read(&zram->init_lock);
  1618. return -EAGAIN;
  1619. }
  1620. if (algo) {
  1621. bool found = false;
  1622. for (; prio < ZRAM_MAX_COMPS; prio++) {
  1623. if (!zram->comp_algs[prio])
  1624. continue;
  1625. if (!strcmp(zram->comp_algs[prio], algo)) {
  1626. prio_max = min(prio + 1, ZRAM_MAX_COMPS);
  1627. found = true;
  1628. break;
  1629. }
  1630. }
  1631. if (!found) {
  1632. ret = -EINVAL;
  1633. goto release_init_lock;
  1634. }
  1635. }
  1636. page = alloc_page(GFP_KERNEL);
  1637. if (!page) {
  1638. ret = -ENOMEM;
  1639. goto release_init_lock;
  1640. }
  1641. ret = len;
  1642. for (index = 0; index < nr_pages; index++) {
  1643. int err = 0;
  1644. if (!num_recomp_pages)
  1645. break;
  1646. zram_slot_lock(zram, index);
  1647. if (!zram_allocated(zram, index))
  1648. goto next;
  1649. if (mode & RECOMPRESS_IDLE &&
  1650. !zram_test_flag(zram, index, ZRAM_IDLE))
  1651. goto next;
  1652. if (mode & RECOMPRESS_HUGE &&
  1653. !zram_test_flag(zram, index, ZRAM_HUGE))
  1654. goto next;
  1655. if (zram_test_flag(zram, index, ZRAM_WB) ||
  1656. zram_test_flag(zram, index, ZRAM_UNDER_WB) ||
  1657. zram_test_flag(zram, index, ZRAM_SAME) ||
  1658. zram_test_flag(zram, index, ZRAM_INCOMPRESSIBLE))
  1659. goto next;
  1660. err = zram_recompress(zram, index, page, &num_recomp_pages,
  1661. threshold, prio, prio_max);
  1662. next:
  1663. zram_slot_unlock(zram, index);
  1664. if (err) {
  1665. ret = err;
  1666. break;
  1667. }
  1668. cond_resched();
  1669. }
  1670. __free_page(page);
  1671. release_init_lock:
  1672. atomic_set(&zram->pp_in_progress, 0);
  1673. up_read(&zram->init_lock);
  1674. return ret;
  1675. }
  1676. #endif
  1677. static void zram_bio_discard(struct zram *zram, struct bio *bio)
  1678. {
  1679. size_t n = bio->bi_iter.bi_size;
  1680. u32 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
  1681. u32 offset = (bio->bi_iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
  1682. SECTOR_SHIFT;
  1683. /*
  1684. * zram manages data in physical block size units. Because logical block
  1685. * size isn't identical with physical block size on some arch, we
  1686. * could get a discard request pointing to a specific offset within a
  1687. * certain physical block. Although we can handle this request by
  1688. * reading that physiclal block and decompressing and partially zeroing
  1689. * and re-compressing and then re-storing it, this isn't reasonable
  1690. * because our intent with a discard request is to save memory. So
  1691. * skipping this logical block is appropriate here.
  1692. */
  1693. if (offset) {
  1694. if (n <= (PAGE_SIZE - offset))
  1695. return;
  1696. n -= (PAGE_SIZE - offset);
  1697. index++;
  1698. }
  1699. while (n >= PAGE_SIZE) {
  1700. zram_slot_lock(zram, index);
  1701. zram_free_page(zram, index);
  1702. zram_slot_unlock(zram, index);
  1703. atomic64_inc(&zram->stats.notify_free);
  1704. index++;
  1705. n -= PAGE_SIZE;
  1706. }
  1707. bio_endio(bio);
  1708. }
  1709. static void zram_bio_read(struct zram *zram, struct bio *bio)
  1710. {
  1711. unsigned long start_time = bio_start_io_acct(bio);
  1712. struct bvec_iter iter = bio->bi_iter;
  1713. do {
  1714. u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
  1715. u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
  1716. SECTOR_SHIFT;
  1717. struct bio_vec bv = bio_iter_iovec(bio, iter);
  1718. bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
  1719. if (zram_bvec_read(zram, &bv, index, offset, bio) < 0) {
  1720. atomic64_inc(&zram->stats.failed_reads);
  1721. bio->bi_status = BLK_STS_IOERR;
  1722. break;
  1723. }
  1724. flush_dcache_page(bv.bv_page);
  1725. zram_slot_lock(zram, index);
  1726. zram_accessed(zram, index);
  1727. zram_slot_unlock(zram, index);
  1728. bio_advance_iter_single(bio, &iter, bv.bv_len);
  1729. } while (iter.bi_size);
  1730. bio_end_io_acct(bio, start_time);
  1731. bio_endio(bio);
  1732. }
  1733. static void zram_bio_write(struct zram *zram, struct bio *bio)
  1734. {
  1735. unsigned long start_time = bio_start_io_acct(bio);
  1736. struct bvec_iter iter = bio->bi_iter;
  1737. do {
  1738. u32 index = iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
  1739. u32 offset = (iter.bi_sector & (SECTORS_PER_PAGE - 1)) <<
  1740. SECTOR_SHIFT;
  1741. struct bio_vec bv = bio_iter_iovec(bio, iter);
  1742. bv.bv_len = min_t(u32, bv.bv_len, PAGE_SIZE - offset);
  1743. if (zram_bvec_write(zram, &bv, index, offset, bio) < 0) {
  1744. atomic64_inc(&zram->stats.failed_writes);
  1745. bio->bi_status = BLK_STS_IOERR;
  1746. break;
  1747. }
  1748. zram_slot_lock(zram, index);
  1749. zram_accessed(zram, index);
  1750. zram_slot_unlock(zram, index);
  1751. bio_advance_iter_single(bio, &iter, bv.bv_len);
  1752. } while (iter.bi_size);
  1753. bio_end_io_acct(bio, start_time);
  1754. bio_endio(bio);
  1755. }
  1756. /*
  1757. * Handler function for all zram I/O requests.
  1758. */
  1759. static void zram_submit_bio(struct bio *bio)
  1760. {
  1761. struct zram *zram = bio->bi_bdev->bd_disk->private_data;
  1762. switch (bio_op(bio)) {
  1763. case REQ_OP_READ:
  1764. zram_bio_read(zram, bio);
  1765. break;
  1766. case REQ_OP_WRITE:
  1767. zram_bio_write(zram, bio);
  1768. break;
  1769. case REQ_OP_DISCARD:
  1770. case REQ_OP_WRITE_ZEROES:
  1771. zram_bio_discard(zram, bio);
  1772. break;
  1773. default:
  1774. WARN_ON_ONCE(1);
  1775. bio_endio(bio);
  1776. }
  1777. }
  1778. static void zram_slot_free_notify(struct block_device *bdev,
  1779. unsigned long index)
  1780. {
  1781. struct zram *zram;
  1782. zram = bdev->bd_disk->private_data;
  1783. atomic64_inc(&zram->stats.notify_free);
  1784. if (!zram_slot_trylock(zram, index)) {
  1785. atomic64_inc(&zram->stats.miss_free);
  1786. return;
  1787. }
  1788. zram_free_page(zram, index);
  1789. zram_slot_unlock(zram, index);
  1790. }
  1791. static void zram_comp_params_reset(struct zram *zram)
  1792. {
  1793. u32 prio;
  1794. for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
  1795. comp_params_reset(zram, prio);
  1796. }
  1797. }
  1798. static void zram_destroy_comps(struct zram *zram)
  1799. {
  1800. u32 prio;
  1801. for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
  1802. struct zcomp *comp = zram->comps[prio];
  1803. zram->comps[prio] = NULL;
  1804. if (!comp)
  1805. continue;
  1806. zcomp_destroy(comp);
  1807. zram->num_active_comps--;
  1808. }
  1809. for (prio = ZRAM_PRIMARY_COMP; prio < ZRAM_MAX_COMPS; prio++) {
  1810. /* Do not free statically defined compression algorithms */
  1811. if (zram->comp_algs[prio] != default_compressor)
  1812. kfree(zram->comp_algs[prio]);
  1813. zram->comp_algs[prio] = NULL;
  1814. }
  1815. zram_comp_params_reset(zram);
  1816. }
  1817. static void zram_reset_device(struct zram *zram)
  1818. {
  1819. down_write(&zram->init_lock);
  1820. zram->limit_pages = 0;
  1821. set_capacity_and_notify(zram->disk, 0);
  1822. part_stat_set_all(zram->disk->part0, 0);
  1823. /* I/O operation under all of CPU are done so let's free */
  1824. zram_meta_free(zram, zram->disksize);
  1825. zram->disksize = 0;
  1826. zram_destroy_comps(zram);
  1827. memset(&zram->stats, 0, sizeof(zram->stats));
  1828. atomic_set(&zram->pp_in_progress, 0);
  1829. reset_bdev(zram);
  1830. comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
  1831. up_write(&zram->init_lock);
  1832. }
  1833. static ssize_t disksize_store(struct device *dev,
  1834. struct device_attribute *attr, const char *buf, size_t len)
  1835. {
  1836. u64 disksize;
  1837. struct zcomp *comp;
  1838. struct zram *zram = dev_to_zram(dev);
  1839. int err;
  1840. u32 prio;
  1841. disksize = memparse(buf, NULL);
  1842. if (!disksize)
  1843. return -EINVAL;
  1844. down_write(&zram->init_lock);
  1845. if (init_done(zram)) {
  1846. pr_info("Cannot change disksize for initialized device\n");
  1847. err = -EBUSY;
  1848. goto out_unlock;
  1849. }
  1850. disksize = PAGE_ALIGN(disksize);
  1851. if (!zram_meta_alloc(zram, disksize)) {
  1852. err = -ENOMEM;
  1853. goto out_unlock;
  1854. }
  1855. for (prio = 0; prio < ZRAM_MAX_COMPS; prio++) {
  1856. if (!zram->comp_algs[prio])
  1857. continue;
  1858. comp = zcomp_create(zram->comp_algs[prio],
  1859. &zram->params[prio]);
  1860. if (IS_ERR(comp)) {
  1861. pr_err("Cannot initialise %s compressing backend\n",
  1862. zram->comp_algs[prio]);
  1863. err = PTR_ERR(comp);
  1864. goto out_free_comps;
  1865. }
  1866. zram->comps[prio] = comp;
  1867. zram->num_active_comps++;
  1868. }
  1869. zram->disksize = disksize;
  1870. set_capacity_and_notify(zram->disk, zram->disksize >> SECTOR_SHIFT);
  1871. up_write(&zram->init_lock);
  1872. return len;
  1873. out_free_comps:
  1874. zram_destroy_comps(zram);
  1875. zram_meta_free(zram, disksize);
  1876. out_unlock:
  1877. up_write(&zram->init_lock);
  1878. return err;
  1879. }
  1880. static ssize_t reset_store(struct device *dev,
  1881. struct device_attribute *attr, const char *buf, size_t len)
  1882. {
  1883. int ret;
  1884. unsigned short do_reset;
  1885. struct zram *zram;
  1886. struct gendisk *disk;
  1887. ret = kstrtou16(buf, 10, &do_reset);
  1888. if (ret)
  1889. return ret;
  1890. if (!do_reset)
  1891. return -EINVAL;
  1892. zram = dev_to_zram(dev);
  1893. disk = zram->disk;
  1894. mutex_lock(&disk->open_mutex);
  1895. /* Do not reset an active device or claimed device */
  1896. if (disk_openers(disk) || zram->claim) {
  1897. mutex_unlock(&disk->open_mutex);
  1898. return -EBUSY;
  1899. }
  1900. /* From now on, anyone can't open /dev/zram[0-9] */
  1901. zram->claim = true;
  1902. mutex_unlock(&disk->open_mutex);
  1903. /* Make sure all the pending I/O are finished */
  1904. sync_blockdev(disk->part0);
  1905. zram_reset_device(zram);
  1906. mutex_lock(&disk->open_mutex);
  1907. zram->claim = false;
  1908. mutex_unlock(&disk->open_mutex);
  1909. return len;
  1910. }
  1911. static int zram_open(struct gendisk *disk, blk_mode_t mode)
  1912. {
  1913. struct zram *zram = disk->private_data;
  1914. WARN_ON(!mutex_is_locked(&disk->open_mutex));
  1915. /* zram was claimed to reset so open request fails */
  1916. if (zram->claim)
  1917. return -EBUSY;
  1918. return 0;
  1919. }
  1920. static const struct block_device_operations zram_devops = {
  1921. .open = zram_open,
  1922. .submit_bio = zram_submit_bio,
  1923. .swap_slot_free_notify = zram_slot_free_notify,
  1924. .owner = THIS_MODULE
  1925. };
  1926. static DEVICE_ATTR_WO(compact);
  1927. static DEVICE_ATTR_RW(disksize);
  1928. static DEVICE_ATTR_RO(initstate);
  1929. static DEVICE_ATTR_WO(reset);
  1930. static DEVICE_ATTR_WO(mem_limit);
  1931. static DEVICE_ATTR_WO(mem_used_max);
  1932. static DEVICE_ATTR_WO(idle);
  1933. static DEVICE_ATTR_RW(max_comp_streams);
  1934. static DEVICE_ATTR_RW(comp_algorithm);
  1935. #ifdef CONFIG_ZRAM_WRITEBACK
  1936. static DEVICE_ATTR_RW(backing_dev);
  1937. static DEVICE_ATTR_WO(writeback);
  1938. static DEVICE_ATTR_RW(writeback_limit);
  1939. static DEVICE_ATTR_RW(writeback_limit_enable);
  1940. #endif
  1941. #ifdef CONFIG_ZRAM_MULTI_COMP
  1942. static DEVICE_ATTR_RW(recomp_algorithm);
  1943. static DEVICE_ATTR_WO(recompress);
  1944. #endif
  1945. static DEVICE_ATTR_WO(algorithm_params);
  1946. static struct attribute *zram_disk_attrs[] = {
  1947. &dev_attr_disksize.attr,
  1948. &dev_attr_initstate.attr,
  1949. &dev_attr_reset.attr,
  1950. &dev_attr_compact.attr,
  1951. &dev_attr_mem_limit.attr,
  1952. &dev_attr_mem_used_max.attr,
  1953. &dev_attr_idle.attr,
  1954. &dev_attr_max_comp_streams.attr,
  1955. &dev_attr_comp_algorithm.attr,
  1956. #ifdef CONFIG_ZRAM_WRITEBACK
  1957. &dev_attr_backing_dev.attr,
  1958. &dev_attr_writeback.attr,
  1959. &dev_attr_writeback_limit.attr,
  1960. &dev_attr_writeback_limit_enable.attr,
  1961. #endif
  1962. &dev_attr_io_stat.attr,
  1963. &dev_attr_mm_stat.attr,
  1964. #ifdef CONFIG_ZRAM_WRITEBACK
  1965. &dev_attr_bd_stat.attr,
  1966. #endif
  1967. &dev_attr_debug_stat.attr,
  1968. #ifdef CONFIG_ZRAM_MULTI_COMP
  1969. &dev_attr_recomp_algorithm.attr,
  1970. &dev_attr_recompress.attr,
  1971. #endif
  1972. &dev_attr_algorithm_params.attr,
  1973. NULL,
  1974. };
  1975. ATTRIBUTE_GROUPS(zram_disk);
  1976. /*
  1977. * Allocate and initialize new zram device. the function returns
  1978. * '>= 0' device_id upon success, and negative value otherwise.
  1979. */
  1980. static int zram_add(void)
  1981. {
  1982. struct queue_limits lim = {
  1983. .logical_block_size = ZRAM_LOGICAL_BLOCK_SIZE,
  1984. /*
  1985. * To ensure that we always get PAGE_SIZE aligned and
  1986. * n*PAGE_SIZED sized I/O requests.
  1987. */
  1988. .physical_block_size = PAGE_SIZE,
  1989. .io_min = PAGE_SIZE,
  1990. .io_opt = PAGE_SIZE,
  1991. .max_hw_discard_sectors = UINT_MAX,
  1992. /*
  1993. * zram_bio_discard() will clear all logical blocks if logical
  1994. * block size is identical with physical block size(PAGE_SIZE).
  1995. * But if it is different, we will skip discarding some parts of
  1996. * logical blocks in the part of the request range which isn't
  1997. * aligned to physical block size. So we can't ensure that all
  1998. * discarded logical blocks are zeroed.
  1999. */
  2000. #if ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE
  2001. .max_write_zeroes_sectors = UINT_MAX,
  2002. #endif
  2003. .features = BLK_FEAT_STABLE_WRITES |
  2004. BLK_FEAT_SYNCHRONOUS,
  2005. };
  2006. struct zram *zram;
  2007. int ret, device_id;
  2008. zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
  2009. if (!zram)
  2010. return -ENOMEM;
  2011. ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
  2012. if (ret < 0)
  2013. goto out_free_dev;
  2014. device_id = ret;
  2015. init_rwsem(&zram->init_lock);
  2016. #ifdef CONFIG_ZRAM_WRITEBACK
  2017. spin_lock_init(&zram->wb_limit_lock);
  2018. #endif
  2019. /* gendisk structure */
  2020. zram->disk = blk_alloc_disk(&lim, NUMA_NO_NODE);
  2021. if (IS_ERR(zram->disk)) {
  2022. pr_err("Error allocating disk structure for device %d\n",
  2023. device_id);
  2024. ret = PTR_ERR(zram->disk);
  2025. goto out_free_idr;
  2026. }
  2027. zram->disk->major = zram_major;
  2028. zram->disk->first_minor = device_id;
  2029. zram->disk->minors = 1;
  2030. zram->disk->flags |= GENHD_FL_NO_PART;
  2031. zram->disk->fops = &zram_devops;
  2032. zram->disk->private_data = zram;
  2033. snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
  2034. atomic_set(&zram->pp_in_progress, 0);
  2035. zram_comp_params_reset(zram);
  2036. comp_algorithm_set(zram, ZRAM_PRIMARY_COMP, default_compressor);
  2037. /* Actual capacity set using sysfs (/sys/block/zram<id>/disksize */
  2038. set_capacity(zram->disk, 0);
  2039. ret = device_add_disk(NULL, zram->disk, zram_disk_groups);
  2040. if (ret)
  2041. goto out_cleanup_disk;
  2042. zram_debugfs_register(zram);
  2043. pr_info("Added device: %s\n", zram->disk->disk_name);
  2044. return device_id;
  2045. out_cleanup_disk:
  2046. put_disk(zram->disk);
  2047. out_free_idr:
  2048. idr_remove(&zram_index_idr, device_id);
  2049. out_free_dev:
  2050. kfree(zram);
  2051. return ret;
  2052. }
  2053. static int zram_remove(struct zram *zram)
  2054. {
  2055. bool claimed;
  2056. mutex_lock(&zram->disk->open_mutex);
  2057. if (disk_openers(zram->disk)) {
  2058. mutex_unlock(&zram->disk->open_mutex);
  2059. return -EBUSY;
  2060. }
  2061. claimed = zram->claim;
  2062. if (!claimed)
  2063. zram->claim = true;
  2064. mutex_unlock(&zram->disk->open_mutex);
  2065. zram_debugfs_unregister(zram);
  2066. if (claimed) {
  2067. /*
  2068. * If we were claimed by reset_store(), del_gendisk() will
  2069. * wait until reset_store() is done, so nothing need to do.
  2070. */
  2071. ;
  2072. } else {
  2073. /* Make sure all the pending I/O are finished */
  2074. sync_blockdev(zram->disk->part0);
  2075. zram_reset_device(zram);
  2076. }
  2077. pr_info("Removed device: %s\n", zram->disk->disk_name);
  2078. del_gendisk(zram->disk);
  2079. /* del_gendisk drains pending reset_store */
  2080. WARN_ON_ONCE(claimed && zram->claim);
  2081. /*
  2082. * disksize_store() may be called in between zram_reset_device()
  2083. * and del_gendisk(), so run the last reset to avoid leaking
  2084. * anything allocated with disksize_store()
  2085. */
  2086. zram_reset_device(zram);
  2087. put_disk(zram->disk);
  2088. kfree(zram);
  2089. return 0;
  2090. }
  2091. /* zram-control sysfs attributes */
  2092. /*
  2093. * NOTE: hot_add attribute is not the usual read-only sysfs attribute. In a
  2094. * sense that reading from this file does alter the state of your system -- it
  2095. * creates a new un-initialized zram device and returns back this device's
  2096. * device_id (or an error code if it fails to create a new device).
  2097. */
  2098. static ssize_t hot_add_show(const struct class *class,
  2099. const struct class_attribute *attr,
  2100. char *buf)
  2101. {
  2102. int ret;
  2103. mutex_lock(&zram_index_mutex);
  2104. ret = zram_add();
  2105. mutex_unlock(&zram_index_mutex);
  2106. if (ret < 0)
  2107. return ret;
  2108. return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
  2109. }
  2110. /* This attribute must be set to 0400, so CLASS_ATTR_RO() can not be used */
  2111. static struct class_attribute class_attr_hot_add =
  2112. __ATTR(hot_add, 0400, hot_add_show, NULL);
  2113. static ssize_t hot_remove_store(const struct class *class,
  2114. const struct class_attribute *attr,
  2115. const char *buf,
  2116. size_t count)
  2117. {
  2118. struct zram *zram;
  2119. int ret, dev_id;
  2120. /* dev_id is gendisk->first_minor, which is `int' */
  2121. ret = kstrtoint(buf, 10, &dev_id);
  2122. if (ret)
  2123. return ret;
  2124. if (dev_id < 0)
  2125. return -EINVAL;
  2126. mutex_lock(&zram_index_mutex);
  2127. zram = idr_find(&zram_index_idr, dev_id);
  2128. if (zram) {
  2129. ret = zram_remove(zram);
  2130. if (!ret)
  2131. idr_remove(&zram_index_idr, dev_id);
  2132. } else {
  2133. ret = -ENODEV;
  2134. }
  2135. mutex_unlock(&zram_index_mutex);
  2136. return ret ? ret : count;
  2137. }
  2138. static CLASS_ATTR_WO(hot_remove);
  2139. static struct attribute *zram_control_class_attrs[] = {
  2140. &class_attr_hot_add.attr,
  2141. &class_attr_hot_remove.attr,
  2142. NULL,
  2143. };
  2144. ATTRIBUTE_GROUPS(zram_control_class);
  2145. static struct class zram_control_class = {
  2146. .name = "zram-control",
  2147. .class_groups = zram_control_class_groups,
  2148. };
  2149. static int zram_remove_cb(int id, void *ptr, void *data)
  2150. {
  2151. WARN_ON_ONCE(zram_remove(ptr));
  2152. return 0;
  2153. }
  2154. static void destroy_devices(void)
  2155. {
  2156. class_unregister(&zram_control_class);
  2157. idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
  2158. zram_debugfs_destroy();
  2159. idr_destroy(&zram_index_idr);
  2160. unregister_blkdev(zram_major, "zram");
  2161. cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
  2162. }
  2163. static int __init zram_init(void)
  2164. {
  2165. struct zram_table_entry zram_te;
  2166. int ret;
  2167. BUILD_BUG_ON(__NR_ZRAM_PAGEFLAGS > sizeof(zram_te.flags) * 8);
  2168. ret = cpuhp_setup_state_multi(CPUHP_ZCOMP_PREPARE, "block/zram:prepare",
  2169. zcomp_cpu_up_prepare, zcomp_cpu_dead);
  2170. if (ret < 0)
  2171. return ret;
  2172. ret = class_register(&zram_control_class);
  2173. if (ret) {
  2174. pr_err("Unable to register zram-control class\n");
  2175. cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
  2176. return ret;
  2177. }
  2178. zram_debugfs_create();
  2179. zram_major = register_blkdev(0, "zram");
  2180. if (zram_major <= 0) {
  2181. pr_err("Unable to get major number\n");
  2182. class_unregister(&zram_control_class);
  2183. cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
  2184. return -EBUSY;
  2185. }
  2186. while (num_devices != 0) {
  2187. mutex_lock(&zram_index_mutex);
  2188. ret = zram_add();
  2189. mutex_unlock(&zram_index_mutex);
  2190. if (ret < 0)
  2191. goto out_error;
  2192. num_devices--;
  2193. }
  2194. return 0;
  2195. out_error:
  2196. destroy_devices();
  2197. return ret;
  2198. }
  2199. static void __exit zram_exit(void)
  2200. {
  2201. destroy_devices();
  2202. }
  2203. module_init(zram_init);
  2204. module_exit(zram_exit);
  2205. module_param(num_devices, uint, 0);
  2206. MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
  2207. MODULE_LICENSE("Dual BSD/GPL");
  2208. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
  2209. MODULE_DESCRIPTION("Compressed RAM Block Device");