zram_drv.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. * Compressed RAM block device
  3. *
  4. * Copyright (C) 2008, 2009, 2010 Nitin Gupta
  5. * 2012, 2013 Minchan Kim
  6. *
  7. * This code is released using a dual license strategy: BSD/GPL
  8. * You can choose the licence that better fits your requirements.
  9. *
  10. * Released under the terms of 3-clause BSD License
  11. * Released under the terms of GNU General Public License Version 2.0
  12. *
  13. */
  14. #define KMSG_COMPONENT "zram"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #ifdef CONFIG_ZRAM_DEBUG
  17. #define DEBUG
  18. #endif
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bio.h>
  22. #include <linux/bitops.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/device.h>
  26. #include <linux/genhd.h>
  27. #include <linux/highmem.h>
  28. #include <linux/slab.h>
  29. #include <linux/lzo.h>
  30. #include <linux/string.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/err.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #ifdef CONFIG_ZSM
  36. #include <linux/rbtree.h>
  37. #include <linux/time.h>
  38. #endif
  39. #include "zram_drv.h"
  40. /* Globals */
  41. static int zram_major;
  42. static struct zram *zram_devices;
  43. static const char *default_compressor = "lzo";
  44. /* Module params (documentation at end) */
  45. static unsigned int num_devices = 1;
  46. #ifdef CONFIG_ZSM
  47. #define SIZE_MASK (BIT(ZRAM_FLAG_SHIFT) - 1)
  48. #define TABLE_GET_SIZE(X) (X & SIZE_MASK)
  49. static struct rb_root root_zram_tree = RB_ROOT;
  50. static struct rb_root root_zram_tree_4k = RB_ROOT;
  51. spinlock_t zram_node_mutex;
  52. spinlock_t zram_node4k_mutex;
  53. static int zram_test_flag(struct zram_meta *meta, u32 index,
  54. enum zram_pageflags flag);
  55. static void zram_set_flag(struct zram_meta *meta, u32 index,
  56. enum zram_pageflags flag);
  57. static void zram_clear_flag(struct zram_meta *meta, u32 index,
  58. enum zram_pageflags flag);
  59. static int zsm_test_flag(struct zram_meta *meta, struct zram_table_entry *node,
  60. enum zram_pageflags flag)
  61. {
  62. return node->flags & BIT(flag);
  63. }
  64. static void zsm_set_flag(struct zram_meta *meta, struct zram_table_entry *node,
  65. enum zram_pageflags flag)
  66. {
  67. node->flags |= BIT(flag);
  68. }
  69. static int zsm_test_flag_index(struct zram_meta *meta, u32 index,
  70. enum zram_pageflags flag)
  71. {
  72. return meta->table[index].flags & BIT(flag);
  73. }
  74. static void zsm_set_flag_index(struct zram_meta *meta, u32 index,
  75. enum zram_pageflags flag)
  76. {
  77. meta->table[index].flags |= BIT(flag);
  78. }
  79. static void zsm_clear_flag_index(struct zram_meta *meta, u32 index,
  80. enum zram_pageflags flag)
  81. {
  82. meta->table[index].flags &= ~BIT(flag);
  83. }
  84. static struct zram_table_entry *search_node_in_zram_list(struct zram *zram, struct zram_meta *meta,
  85. struct zram_table_entry *input_node, struct zram_table_entry *found_node, unsigned char *match_content,
  86. u32 clen)
  87. {
  88. struct list_head *list_node = NULL;
  89. struct zram_table_entry *current_node = NULL;
  90. unsigned char *cmem;
  91. int one_node_in_list = 0;
  92. int compare_count = 0;
  93. int ret;
  94. list_node = found_node->head.next;
  95. if (list_node == &(found_node->head))
  96. one_node_in_list = 1;
  97. while ((list_node != &(found_node->head)) || one_node_in_list) {
  98. one_node_in_list = 0;
  99. current_node = list_entry(list_node, struct zram_table_entry, head);
  100. if ((clen != TABLE_GET_SIZE(current_node->value)) || !zsm_test_flag(meta, current_node,
  101. ZRAM_FIRST_NODE)) {
  102. list_node = list_node->next;
  103. } else {
  104. if (zsm_test_flag(meta, current_node, ZRAM_ZSM_DONE_NODE) && (current_node->handle != 0)) {
  105. cmem = zs_map_object(meta->mem_pool, current_node->handle, ZS_MM_RO);
  106. ret = memcmp(cmem, match_content, TABLE_GET_SIZE(input_node->value));
  107. compare_count++;
  108. if (ret == 0) {
  109. zs_unmap_object(meta->mem_pool, current_node->handle);
  110. return current_node;
  111. }
  112. list_node = list_node->next;
  113. zs_unmap_object(meta->mem_pool, current_node->handle);
  114. } else {
  115. pr_warn("[ZSM] current node is not ready %x and handle is %lx\n",
  116. current_node->copy_index, current_node->handle);
  117. list_node = list_node->next;
  118. }
  119. }
  120. }
  121. return NULL;
  122. }
  123. static struct zram_table_entry *search_node_in_zram_tree(struct zram_table_entry *input_node,
  124. struct rb_node **parent_node, struct rb_node ***new_node, unsigned char *match_content,
  125. struct rb_root *local_root_zram_tree)
  126. {
  127. struct rb_node **new = &(local_root_zram_tree->rb_node);
  128. struct zram_table_entry *current_node = NULL;
  129. struct rb_node *parent = NULL;
  130. current_node = rb_entry(*new, struct zram_table_entry, node);
  131. if (input_node == NULL) {
  132. pr_err("[zram][search_node_in_zram_tree] input_node is NULL\n");
  133. return NULL;
  134. }
  135. if (current_node == NULL) {
  136. *new_node = new;
  137. *parent_node = NULL;
  138. return NULL;
  139. }
  140. while (*new) {
  141. current_node = rb_entry(*new, struct zram_table_entry, node);
  142. parent = *new;
  143. if (input_node->checksum > current_node->checksum) {
  144. new = &parent->rb_right;
  145. } else if (input_node->checksum < current_node->checksum) {
  146. new = &parent->rb_left;
  147. } else {
  148. if (TABLE_GET_SIZE(input_node->value) > TABLE_GET_SIZE(current_node->value))
  149. new = &parent->rb_right;
  150. else if (TABLE_GET_SIZE(input_node->value) < TABLE_GET_SIZE(current_node->value))
  151. new = &parent->rb_left;
  152. else
  153. return current_node;
  154. }
  155. }
  156. *parent_node = parent;
  157. *new_node = new;
  158. return NULL;
  159. }
  160. static u32 insert_node_to_zram_tree(struct zram *zram, struct zram_meta *meta, u32 index, unsigned char *match_content,
  161. struct rb_root *local_root_zram_tree, u32 clen)
  162. {
  163. struct zram_table_entry *current_node = NULL;
  164. struct zram_table_entry *node_in_list = NULL;
  165. struct rb_node *parent = NULL;
  166. struct rb_node **new = NULL;
  167. struct zram_table_entry *input_node;
  168. input_node = &(meta->table[index]);
  169. zsm_set_flag_index(meta, index, ZRAM_ZSM_NODE);
  170. current_node = search_node_in_zram_tree(input_node, &parent, &new, match_content, local_root_zram_tree);
  171. /* found node in zram_tree */
  172. if (NULL != current_node) {
  173. if (!zsm_test_flag(meta, current_node, ZRAM_RB_NODE)) {
  174. pr_err("[ZRAM]ERROR !!found wrong rb node 0x%p\n", (void *)current_node);
  175. BUG_ON(1);
  176. }
  177. /* check if there is any other node in this position. */
  178. node_in_list = search_node_in_zram_list(zram, meta, input_node, current_node, match_content, clen);
  179. /* found the same node in list */
  180. if (NULL != node_in_list) {
  181. /* insert node after the found node */
  182. if (!zsm_test_flag(meta, current_node, ZRAM_FIRST_NODE)) {
  183. pr_err("[ZRAM]ERROR !!found wrong first node 0x%p\n", (void *)node_in_list);
  184. BUG_ON(1);
  185. }
  186. input_node->next_index = node_in_list->next_index;
  187. node_in_list->next_index = index;
  188. input_node->copy_index = node_in_list->copy_index;
  189. /* found the same node and add ref count */
  190. node_in_list->copy_count++;
  191. if (unlikely(TABLE_GET_SIZE(input_node->value) > max_zpage_size))
  192. atomic64_add((u64)TABLE_GET_SIZE(input_node->value), &zram->stats.zsm_saved4k);
  193. else
  194. atomic64_add((u64)TABLE_GET_SIZE(input_node->value), &zram->stats.zsm_saved);
  195. input_node->handle = node_in_list->handle;
  196. list_add(&input_node->head, &node_in_list->head);
  197. zsm_set_flag_index(meta, index, ZRAM_ZSM_DONE_NODE);
  198. return 1;
  199. }
  200. /* can't found node in list */
  201. zsm_set_flag_index(meta, index, ZRAM_FIRST_NODE);
  202. list_add(&input_node->head, &current_node->head);
  203. } else {
  204. /* insert node into rb tree */
  205. zsm_set_flag_index(meta, index, ZRAM_FIRST_NODE);
  206. zsm_set_flag_index(meta, index, ZRAM_RB_NODE);
  207. rb_link_node(&(meta->table[index].node), parent, new);
  208. rb_insert_color(&(meta->table[index].node), local_root_zram_tree);
  209. }
  210. return 0;
  211. }
  212. static int remove_node_from_zram_list(struct zram *zram, struct zram_meta *meta, u32 index)
  213. {
  214. u32 next_index = 0xffffffff;
  215. u32 pre_index = 0xffffffff;
  216. u32 current_index = 0xffffffff;
  217. u32 copy_index = 0xffffffff;
  218. u32 i = 0;
  219. next_index = meta->table[index].next_index;
  220. list_del(&(meta->table[index].head));
  221. /* check if there is the same content in list */
  222. if (index != next_index) {/* found the same page content */
  223. if (zsm_test_flag_index(meta, index, ZRAM_FIRST_NODE)) {/* delete the fist node of content */
  224. if (meta->table[index].copy_count <= 0) {
  225. pr_err("[ZRAM]ERROR !!count < 0\n ");
  226. BUG_ON(1);
  227. return 1;
  228. }
  229. current_index = meta->table[next_index].next_index;
  230. meta->table[next_index].copy_index = next_index;
  231. pre_index = next_index;
  232. while (current_index != index) {
  233. i++;
  234. if (i >= 4096 && (i%1000 == 0)) {
  235. if (i > meta->table[index].copy_count) {
  236. BUG_ON(1);
  237. break;
  238. }
  239. }
  240. meta->table[current_index].copy_index = next_index;
  241. pre_index = current_index;
  242. current_index = meta->table[current_index].next_index;
  243. }
  244. meta->table[pre_index].next_index = meta->table[index].next_index;
  245. meta->table[next_index].copy_count = meta->table[index].copy_count - 1;
  246. zsm_clear_flag_index(meta, index, ZRAM_FIRST_NODE);
  247. zsm_set_flag_index(meta, next_index, ZRAM_FIRST_NODE);
  248. } else {
  249. current_index = meta->table[index].copy_index;
  250. pre_index = current_index;
  251. current_index = meta->table[current_index].next_index;
  252. while (index != current_index) {
  253. i++;
  254. if (i >= 4096 && (i%1000 == 0)) {
  255. u32 tmp_index = 0;
  256. tmp_index = meta->table[current_index].copy_index;
  257. if (i > meta->table[tmp_index].copy_count) {
  258. BUG_ON(1);
  259. break;
  260. }
  261. }
  262. pre_index = current_index;
  263. current_index = meta->table[current_index].next_index;
  264. }
  265. meta->table[pre_index].next_index = meta->table[index].next_index;
  266. copy_index = meta->table[index].copy_index;
  267. meta->table[copy_index].copy_count = meta->table[copy_index].copy_count - 1;
  268. }
  269. if (unlikely(TABLE_GET_SIZE(meta->table[index].value) > max_zpage_size))
  270. atomic64_sub((u64)TABLE_GET_SIZE(meta->table[index].value), &zram->stats.zsm_saved4k);
  271. else
  272. atomic64_sub((u64)TABLE_GET_SIZE(meta->table[index].value), &zram->stats.zsm_saved);
  273. return 1;
  274. }
  275. /* can't found the same page content */
  276. if (zsm_test_flag_index(meta, index, ZRAM_FIRST_NODE)) {
  277. zsm_clear_flag_index(meta, index, ZRAM_FIRST_NODE);
  278. } else {
  279. pr_err("[ZRAM]ERROR!index != next_index, flag != ZRAM_FIRST_NODE index %x\n ", index);
  280. BUG_ON(1);
  281. }
  282. if (meta->table[index].copy_count != 0) {
  283. pr_err("[ZRAM]ERROR !!index != next_index and count != 0 index %x\n ", index);
  284. BUG_ON(1);
  285. }
  286. return 0;
  287. }
  288. static int remove_node_from_zram_tree(struct zram *zram, struct zram_meta *meta, u32 index,
  289. struct rb_root *local_root_zram_tree)
  290. {
  291. int ret;
  292. if (zsm_test_flag_index(meta, index, ZRAM_ZSM_NODE)) {
  293. zsm_clear_flag_index(meta, index, ZRAM_ZSM_NODE);
  294. } else {
  295. pr_err("[ZSM] index %x is not belongs to zsm node\n", index);
  296. BUG_ON(1);
  297. }
  298. if (zsm_test_flag_index(meta, index, ZRAM_ZSM_DONE_NODE))
  299. zsm_clear_flag_index(meta, index, ZRAM_ZSM_DONE_NODE);
  300. else
  301. pr_err("[ZSM] index node %x is not set and will be removed\n", index);
  302. /* if it is rb node, choose other node from list and replace original node. */
  303. if (zsm_test_flag_index(meta, index, ZRAM_RB_NODE)) {
  304. zsm_clear_flag_index(meta, index, ZRAM_RB_NODE);
  305. /* found next node in list */
  306. if (&(meta->table[index].head) != meta->table[index].head.next) {
  307. struct zram_table_entry *next_table;
  308. next_table = list_entry(meta->table[index].head.next, struct zram_table_entry, head);
  309. rb_replace_node(&(meta->table[index].node), &(next_table->node), local_root_zram_tree);
  310. zsm_set_flag(meta, next_table, ZRAM_RB_NODE);
  311. ret = remove_node_from_zram_list(zram, meta, index);
  312. return ret;
  313. }
  314. /* if no other node can be found in list just remove node from rb tree and free handle */
  315. if (zsm_test_flag_index(meta, index, ZRAM_FIRST_NODE)) {
  316. zsm_clear_flag_index(meta, index, ZRAM_FIRST_NODE);
  317. } else {
  318. pr_err("[ZRAM]ERROR !!ZRAM_RB_NODR's flag != ZRAM_FIRST_NODE index %x\n ",
  319. index);
  320. BUG_ON(1);
  321. }
  322. rb_erase(&(meta->table[index].node), local_root_zram_tree);
  323. return 0;
  324. }
  325. ret = remove_node_from_zram_list(zram, meta, index);
  326. return ret;
  327. }
  328. #endif
  329. #define ZRAM_ATTR_RO(name) \
  330. static ssize_t zram_attr_##name##_show(struct device *d, \
  331. struct device_attribute *attr, char *b) \
  332. { \
  333. struct zram *zram = dev_to_zram(d); \
  334. return scnprintf(b, PAGE_SIZE, "%llu\n", \
  335. (u64)atomic64_read(&zram->stats.name)); \
  336. } \
  337. static struct device_attribute dev_attr_##name = \
  338. __ATTR(name, S_IRUGO, zram_attr_##name##_show, NULL)
  339. static inline int init_done(struct zram *zram)
  340. {
  341. return zram->meta != NULL;
  342. }
  343. static inline struct zram *dev_to_zram(struct device *dev)
  344. {
  345. return (struct zram *)dev_to_disk(dev)->private_data;
  346. }
  347. static ssize_t disksize_show(struct device *dev,
  348. struct device_attribute *attr, char *buf)
  349. {
  350. struct zram *zram = dev_to_zram(dev);
  351. return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
  352. }
  353. static ssize_t initstate_show(struct device *dev,
  354. struct device_attribute *attr, char *buf)
  355. {
  356. u32 val;
  357. struct zram *zram = dev_to_zram(dev);
  358. down_read(&zram->init_lock);
  359. val = init_done(zram);
  360. up_read(&zram->init_lock);
  361. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  362. }
  363. static ssize_t orig_data_size_show(struct device *dev,
  364. struct device_attribute *attr, char *buf)
  365. {
  366. struct zram *zram = dev_to_zram(dev);
  367. return scnprintf(buf, PAGE_SIZE, "%llu\n",
  368. (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
  369. }
  370. static ssize_t mem_used_total_show(struct device *dev,
  371. struct device_attribute *attr, char *buf)
  372. {
  373. u64 val = 0;
  374. struct zram *zram = dev_to_zram(dev);
  375. down_read(&zram->init_lock);
  376. if (init_done(zram)) {
  377. struct zram_meta *meta = zram->meta;
  378. val = zs_get_total_pages(meta->mem_pool);
  379. }
  380. up_read(&zram->init_lock);
  381. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  382. }
  383. static ssize_t max_comp_streams_show(struct device *dev,
  384. struct device_attribute *attr, char *buf)
  385. {
  386. int val;
  387. struct zram *zram = dev_to_zram(dev);
  388. down_read(&zram->init_lock);
  389. val = zram->max_comp_streams;
  390. up_read(&zram->init_lock);
  391. return scnprintf(buf, PAGE_SIZE, "%d\n", val);
  392. }
  393. static ssize_t mem_limit_show(struct device *dev,
  394. struct device_attribute *attr, char *buf)
  395. {
  396. u64 val;
  397. struct zram *zram = dev_to_zram(dev);
  398. down_read(&zram->init_lock);
  399. val = zram->limit_pages;
  400. up_read(&zram->init_lock);
  401. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  402. }
  403. static ssize_t mem_limit_store(struct device *dev,
  404. struct device_attribute *attr, const char *buf, size_t len)
  405. {
  406. u64 limit;
  407. char *tmp;
  408. struct zram *zram = dev_to_zram(dev);
  409. limit = memparse(buf, &tmp);
  410. if (buf == tmp) /* no chars parsed, invalid input */
  411. return -EINVAL;
  412. down_write(&zram->init_lock);
  413. zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
  414. up_write(&zram->init_lock);
  415. return len;
  416. }
  417. static ssize_t mem_used_max_show(struct device *dev,
  418. struct device_attribute *attr, char *buf)
  419. {
  420. u64 val = 0;
  421. struct zram *zram = dev_to_zram(dev);
  422. down_read(&zram->init_lock);
  423. if (init_done(zram))
  424. val = atomic_long_read(&zram->stats.max_used_pages);
  425. up_read(&zram->init_lock);
  426. return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
  427. }
  428. static ssize_t mem_used_max_store(struct device *dev,
  429. struct device_attribute *attr, const char *buf, size_t len)
  430. {
  431. int err;
  432. unsigned long val;
  433. struct zram *zram = dev_to_zram(dev);
  434. err = kstrtoul(buf, 10, &val);
  435. if (err || val != 0)
  436. return -EINVAL;
  437. down_read(&zram->init_lock);
  438. if (init_done(zram)) {
  439. struct zram_meta *meta = zram->meta;
  440. atomic_long_set(&zram->stats.max_used_pages,
  441. zs_get_total_pages(meta->mem_pool));
  442. }
  443. up_read(&zram->init_lock);
  444. return len;
  445. }
  446. static ssize_t max_comp_streams_store(struct device *dev,
  447. struct device_attribute *attr, const char *buf, size_t len)
  448. {
  449. int num;
  450. struct zram *zram = dev_to_zram(dev);
  451. int ret;
  452. ret = kstrtoint(buf, 0, &num);
  453. if (ret < 0)
  454. return ret;
  455. if (num < 1)
  456. return -EINVAL;
  457. down_write(&zram->init_lock);
  458. if (init_done(zram)) {
  459. if (!zcomp_set_max_streams(zram->comp, num)) {
  460. pr_info("Cannot change max compression streams\n");
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. }
  465. zram->max_comp_streams = num;
  466. ret = len;
  467. out:
  468. up_write(&zram->init_lock);
  469. return ret;
  470. }
  471. static ssize_t comp_algorithm_show(struct device *dev,
  472. struct device_attribute *attr, char *buf)
  473. {
  474. size_t sz;
  475. struct zram *zram = dev_to_zram(dev);
  476. down_read(&zram->init_lock);
  477. sz = zcomp_available_show(zram->compressor, buf);
  478. up_read(&zram->init_lock);
  479. return sz;
  480. }
  481. static ssize_t comp_algorithm_store(struct device *dev,
  482. struct device_attribute *attr, const char *buf, size_t len)
  483. {
  484. struct zram *zram = dev_to_zram(dev);
  485. down_write(&zram->init_lock);
  486. if (init_done(zram)) {
  487. up_write(&zram->init_lock);
  488. pr_info("Can't change algorithm for initialized device\n");
  489. return -EBUSY;
  490. }
  491. strlcpy(zram->compressor, buf, sizeof(zram->compressor));
  492. up_write(&zram->init_lock);
  493. return len;
  494. }
  495. /* flag operations needs meta->tb_lock */
  496. static int zram_test_flag(struct zram_meta *meta, u32 index,
  497. enum zram_pageflags flag)
  498. {
  499. return meta->table[index].value & BIT(flag);
  500. }
  501. static void zram_set_flag(struct zram_meta *meta, u32 index,
  502. enum zram_pageflags flag)
  503. {
  504. meta->table[index].value |= BIT(flag);
  505. }
  506. static void zram_clear_flag(struct zram_meta *meta, u32 index,
  507. enum zram_pageflags flag)
  508. {
  509. meta->table[index].value &= ~BIT(flag);
  510. }
  511. static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
  512. {
  513. return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
  514. }
  515. static void zram_set_obj_size(struct zram_meta *meta,
  516. u32 index, size_t size)
  517. {
  518. unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
  519. meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
  520. }
  521. static inline int is_partial_io(struct bio_vec *bvec)
  522. {
  523. return bvec->bv_len != PAGE_SIZE;
  524. }
  525. /*
  526. * Check if request is within bounds and aligned on zram logical blocks.
  527. */
  528. static inline int valid_io_request(struct zram *zram, struct bio *bio)
  529. {
  530. u64 start, end, bound;
  531. /* unaligned request */
  532. if (unlikely(bio->bi_iter.bi_sector &
  533. (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
  534. return 0;
  535. if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
  536. return 0;
  537. start = bio->bi_iter.bi_sector;
  538. end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
  539. bound = zram->disksize >> SECTOR_SHIFT;
  540. /* out of range range */
  541. if (unlikely(start >= bound || end > bound || start > end))
  542. return 0;
  543. /* I/O request is valid */
  544. return 1;
  545. }
  546. static void zram_meta_free(struct zram_meta *meta)
  547. {
  548. zs_destroy_pool(meta->mem_pool);
  549. vfree(meta->table);
  550. kfree(meta);
  551. }
  552. static struct zram_meta *zram_meta_alloc(u64 disksize)
  553. {
  554. size_t num_pages;
  555. struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
  556. if (!meta)
  557. goto out;
  558. num_pages = disksize >> PAGE_SHIFT;
  559. meta->table = vzalloc(num_pages * sizeof(*meta->table));
  560. if (!meta->table) {
  561. pr_err("Error allocating zram address table\n");
  562. goto free_meta;
  563. }
  564. meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
  565. if (!meta->mem_pool) {
  566. pr_err("Error creating memory pool\n");
  567. goto free_table;
  568. }
  569. return meta;
  570. free_table:
  571. vfree(meta->table);
  572. free_meta:
  573. kfree(meta);
  574. meta = NULL;
  575. out:
  576. return meta;
  577. }
  578. static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
  579. {
  580. if (*offset + bvec->bv_len >= PAGE_SIZE)
  581. (*index)++;
  582. *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
  583. }
  584. static int page_zero_filled(void *ptr)
  585. {
  586. unsigned int pos;
  587. unsigned long *page;
  588. page = (unsigned long *)ptr;
  589. for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
  590. if (page[pos])
  591. return 0;
  592. }
  593. return 1;
  594. }
  595. static void handle_zero_page(struct bio_vec *bvec)
  596. {
  597. struct page *page = bvec->bv_page;
  598. void *user_mem;
  599. user_mem = kmap_atomic(page);
  600. if (is_partial_io(bvec))
  601. memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
  602. else
  603. clear_page(user_mem);
  604. kunmap_atomic(user_mem);
  605. flush_dcache_page(page);
  606. }
  607. /*
  608. * To protect concurrent access to the same index entry,
  609. * caller should hold this table index entry's bit_spinlock to
  610. * indicate this index entry is accessing.
  611. */
  612. static void zram_free_page(struct zram *zram, size_t index)
  613. {
  614. struct zram_meta *meta = zram->meta;
  615. unsigned long handle = meta->table[index].handle;
  616. #ifdef CONFIG_ZSM
  617. int ret = 0;
  618. #endif
  619. if (unlikely(!handle)) {
  620. /*
  621. * No memory is allocated for zero filled pages.
  622. * Simply clear zero page flag.
  623. */
  624. if (zram_test_flag(meta, index, ZRAM_ZERO)) {
  625. zram_clear_flag(meta, index, ZRAM_ZERO);
  626. atomic64_dec(&zram->stats.zero_pages);
  627. }
  628. return;
  629. }
  630. #ifdef CONFIG_ZSM
  631. if (!zram_test_flag(meta, index, ZRAM_ZERO) && zsm_test_flag_index(meta, index, ZRAM_ZSM_NODE)) {
  632. if (TABLE_GET_SIZE(meta->table[index].value) == PAGE_SIZE) {
  633. spin_lock(&zram_node4k_mutex);
  634. ret = remove_node_from_zram_tree(zram, meta, index, &root_zram_tree_4k);
  635. spin_unlock(&zram_node4k_mutex);
  636. } else {
  637. spin_lock(&zram_node_mutex);
  638. ret = remove_node_from_zram_tree(zram, meta, index, &root_zram_tree);
  639. spin_unlock(&zram_node_mutex);
  640. }
  641. } else if (!zsm_test_flag_index(meta, index, ZRAM_ZSM_NODE))
  642. pr_err("[ZSM]ERROR! try to free noexist ZSM node index %x\n", index);
  643. if (ret == 0) {
  644. zs_free(meta->mem_pool, handle);
  645. atomic64_sub(zram_get_obj_size(meta, index), &zram->stats.compr_data_size);
  646. atomic64_dec(&zram->stats.pages_stored);
  647. }
  648. #else
  649. zs_free(meta->mem_pool, handle);
  650. atomic64_sub(zram_get_obj_size(meta, index), &zram->stats.compr_data_size);
  651. atomic64_dec(&zram->stats.pages_stored);
  652. #endif
  653. meta->table[index].handle = 0;
  654. zram_set_obj_size(meta, index, 0);
  655. }
  656. static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
  657. {
  658. int ret = 0;
  659. unsigned char *cmem;
  660. struct zram_meta *meta = zram->meta;
  661. unsigned long handle;
  662. size_t size;
  663. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  664. handle = meta->table[index].handle;
  665. size = zram_get_obj_size(meta, index);
  666. if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
  667. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  668. clear_page(mem);
  669. return 0;
  670. }
  671. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
  672. if (size == PAGE_SIZE)
  673. copy_page(mem, cmem);
  674. else
  675. ret = zcomp_decompress(zram->comp, cmem, size, mem);
  676. zs_unmap_object(meta->mem_pool, handle);
  677. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  678. /* Should NEVER happen. Return bio error if it does. */
  679. if (unlikely(ret)) {
  680. pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
  681. return ret;
  682. }
  683. return 0;
  684. }
  685. static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
  686. u32 index, int offset, struct bio *bio)
  687. {
  688. int ret;
  689. struct page *page;
  690. unsigned char *user_mem, *uncmem = NULL;
  691. struct zram_meta *meta = zram->meta;
  692. page = bvec->bv_page;
  693. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  694. if (unlikely(!meta->table[index].handle) ||
  695. zram_test_flag(meta, index, ZRAM_ZERO)) {
  696. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  697. handle_zero_page(bvec);
  698. return 0;
  699. }
  700. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  701. if (is_partial_io(bvec))
  702. /* Use a temporary buffer to decompress the page */
  703. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  704. user_mem = kmap_atomic(page);
  705. if (!is_partial_io(bvec))
  706. uncmem = user_mem;
  707. if (!uncmem) {
  708. pr_info("Unable to allocate temp memory\n");
  709. ret = -ENOMEM;
  710. goto out_cleanup;
  711. }
  712. ret = zram_decompress_page(zram, uncmem, index);
  713. /* Should NEVER happen. Return bio error if it does. */
  714. if (unlikely(ret))
  715. goto out_cleanup;
  716. if (is_partial_io(bvec))
  717. memcpy(user_mem + bvec->bv_offset, uncmem + offset,
  718. bvec->bv_len);
  719. flush_dcache_page(page);
  720. ret = 0;
  721. out_cleanup:
  722. kunmap_atomic(user_mem);
  723. if (is_partial_io(bvec))
  724. kfree(uncmem);
  725. return ret;
  726. }
  727. static inline void update_used_max(struct zram *zram,
  728. const unsigned long pages)
  729. {
  730. int old_max, cur_max;
  731. old_max = atomic_long_read(&zram->stats.max_used_pages);
  732. do {
  733. cur_max = old_max;
  734. if (pages > cur_max)
  735. old_max = atomic_long_cmpxchg(
  736. &zram->stats.max_used_pages, cur_max, pages);
  737. } while (old_max != cur_max);
  738. }
  739. static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
  740. int offset)
  741. {
  742. int ret = 0;
  743. #ifdef CONFIG_ZSM
  744. int checksum = 0;
  745. #endif
  746. size_t clen;
  747. unsigned long handle;
  748. struct page *page;
  749. unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
  750. struct zram_meta *meta = zram->meta;
  751. struct zcomp_strm *zstrm;
  752. bool locked = false;
  753. unsigned long alloced_pages;
  754. page = bvec->bv_page;
  755. if (is_partial_io(bvec)) {
  756. /*
  757. * This is a partial IO. We need to read the full page
  758. * before to write the changes.
  759. */
  760. uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
  761. if (!uncmem) {
  762. ret = -ENOMEM;
  763. goto out;
  764. }
  765. ret = zram_decompress_page(zram, uncmem, index);
  766. if (ret)
  767. goto out;
  768. #ifdef CONFIG_ZSM
  769. if (!zram_test_flag(meta, index, ZRAM_ZERO)) {
  770. if (zram_get_obj_size(meta, index) == PAGE_SIZE) {
  771. spin_lock(&zram_node4k_mutex);
  772. ret = remove_node_from_zram_tree(zram, meta, index, &root_zram_tree_4k);
  773. spin_unlock(&zram_node4k_mutex);
  774. } else {
  775. spin_lock(&zram_node_mutex);
  776. ret = remove_node_from_zram_tree(zram, meta, index, &root_zram_tree);
  777. spin_unlock(&zram_node_mutex);
  778. }
  779. }
  780. #endif
  781. }
  782. zstrm = zcomp_strm_find(zram->comp);
  783. locked = true;
  784. user_mem = kmap_atomic(page);
  785. if (is_partial_io(bvec)) {
  786. memcpy(uncmem + offset, user_mem + bvec->bv_offset,
  787. bvec->bv_len);
  788. kunmap_atomic(user_mem);
  789. user_mem = NULL;
  790. } else {
  791. uncmem = user_mem;
  792. }
  793. if (page_zero_filled(uncmem)) {
  794. if (user_mem)
  795. kunmap_atomic(user_mem);
  796. /* Free memory associated with this sector now. */
  797. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  798. zram_free_page(zram, index);
  799. zram_set_flag(meta, index, ZRAM_ZERO);
  800. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  801. atomic64_inc(&zram->stats.zero_pages);
  802. ret = 0;
  803. goto out;
  804. }
  805. #ifdef CONFIG_ZSM
  806. ret = zcomp_compress_zram(zram->comp, zstrm, uncmem, &clen, &checksum);
  807. #else
  808. ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
  809. #endif
  810. if (!is_partial_io(bvec)) {
  811. kunmap_atomic(user_mem);
  812. user_mem = NULL;
  813. uncmem = NULL;
  814. }
  815. if (unlikely(ret)) {
  816. pr_err("Compression failed! err=%d\n", ret);
  817. goto out;
  818. }
  819. src = zstrm->buffer;
  820. if (unlikely(clen > max_zpage_size)) {
  821. clen = PAGE_SIZE;
  822. if (is_partial_io(bvec))
  823. src = uncmem;
  824. #ifdef CONFIG_ZSM
  825. {
  826. int search_ret = 0;
  827. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  828. zram_free_page(zram, index);
  829. meta->table[index].checksum = checksum;
  830. zram_set_obj_size(meta, index, clen);
  831. meta->table[index].next_index = index;
  832. meta->table[index].copy_index = index;
  833. meta->table[index].copy_count = 0;
  834. INIT_LIST_HEAD(&(meta->table[index].head));
  835. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  836. if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
  837. src = kmap_atomic(page);
  838. spin_lock(&zram_node4k_mutex);
  839. search_ret = insert_node_to_zram_tree(zram, meta, index, src, &root_zram_tree_4k, clen);
  840. spin_unlock(&zram_node4k_mutex);
  841. kunmap_atomic(src);
  842. } else {
  843. spin_lock(&zram_node4k_mutex);
  844. search_ret = insert_node_to_zram_tree(zram, meta, index, src, &root_zram_tree_4k, clen);
  845. spin_unlock(&zram_node4k_mutex);
  846. }
  847. if (search_ret) {
  848. ret = 0;
  849. goto out;
  850. }
  851. }
  852. #endif
  853. }
  854. #ifdef CONFIG_ZSM
  855. if (unlikely(clen <= max_zpage_size)) {
  856. int search_ret = 0;
  857. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  858. zram_free_page(zram, index);
  859. meta->table[index].checksum = checksum;
  860. zram_set_obj_size(meta, index, clen);
  861. meta->table[index].next_index = index;
  862. meta->table[index].copy_index = index;
  863. meta->table[index].copy_count = 0;
  864. INIT_LIST_HEAD(&(meta->table[index].head));
  865. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  866. spin_lock(&zram_node_mutex);
  867. search_ret = insert_node_to_zram_tree(zram, meta, index, src, &root_zram_tree, clen);
  868. spin_unlock(&zram_node_mutex);
  869. if (search_ret) {
  870. ret = 0;
  871. goto out;
  872. }
  873. }
  874. #endif
  875. handle = zs_malloc(meta->mem_pool, clen);
  876. if (!handle) {
  877. pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
  878. index, clen);
  879. ret = -ENOMEM;
  880. goto out;
  881. }
  882. alloced_pages = zs_get_total_pages(meta->mem_pool);
  883. if (zram->limit_pages && alloced_pages > zram->limit_pages) {
  884. zs_free(meta->mem_pool, handle);
  885. ret = -ENOMEM;
  886. goto out;
  887. }
  888. update_used_max(zram, alloced_pages);
  889. cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
  890. if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
  891. src = kmap_atomic(page);
  892. copy_page(cmem, src);
  893. kunmap_atomic(src);
  894. } else {
  895. memcpy(cmem, src, clen);
  896. }
  897. zcomp_strm_release(zram->comp, zstrm);
  898. locked = false;
  899. zs_unmap_object(meta->mem_pool, handle);
  900. /*
  901. * Free memory associated with this sector
  902. * before overwriting unused sectors.
  903. */
  904. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  905. zram_free_page(zram, index);
  906. meta->table[index].handle = handle;
  907. zram_set_obj_size(meta, index, clen);
  908. #ifdef CONFIG_ZSM
  909. zsm_set_flag_index(meta, index, ZRAM_ZSM_DONE_NODE);
  910. #endif
  911. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  912. /* Update stats */
  913. atomic64_add(clen, &zram->stats.compr_data_size);
  914. atomic64_inc(&zram->stats.pages_stored);
  915. out:
  916. if (locked)
  917. zcomp_strm_release(zram->comp, zstrm);
  918. if (is_partial_io(bvec))
  919. kfree(uncmem);
  920. return ret;
  921. }
  922. static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
  923. int offset, struct bio *bio)
  924. {
  925. int ret;
  926. int rw = bio_data_dir(bio);
  927. if (rw == READ) {
  928. atomic64_inc(&zram->stats.num_reads);
  929. ret = zram_bvec_read(zram, bvec, index, offset, bio);
  930. } else {
  931. atomic64_inc(&zram->stats.num_writes);
  932. ret = zram_bvec_write(zram, bvec, index, offset);
  933. }
  934. if (unlikely(ret)) {
  935. if (rw == READ)
  936. atomic64_inc(&zram->stats.failed_reads);
  937. else
  938. atomic64_inc(&zram->stats.failed_writes);
  939. }
  940. return ret;
  941. }
  942. /*
  943. * zram_bio_discard - handler on discard request
  944. * @index: physical block index in PAGE_SIZE units
  945. * @offset: byte offset within physical block
  946. */
  947. static void zram_bio_discard(struct zram *zram, u32 index,
  948. int offset, struct bio *bio)
  949. {
  950. size_t n = bio->bi_iter.bi_size;
  951. struct zram_meta *meta = zram->meta;
  952. /*
  953. * zram manages data in physical block size units. Because logical block
  954. * size isn't identical with physical block size on some arch, we
  955. * could get a discard request pointing to a specific offset within a
  956. * certain physical block. Although we can handle this request by
  957. * reading that physiclal block and decompressing and partially zeroing
  958. * and re-compressing and then re-storing it, this isn't reasonable
  959. * because our intent with a discard request is to save memory. So
  960. * skipping this logical block is appropriate here.
  961. */
  962. if (offset) {
  963. if (n <= (PAGE_SIZE - offset))
  964. return;
  965. n -= (PAGE_SIZE - offset);
  966. index++;
  967. }
  968. while (n >= PAGE_SIZE) {
  969. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  970. zram_free_page(zram, index);
  971. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  972. atomic64_inc(&zram->stats.notify_free);
  973. index++;
  974. n -= PAGE_SIZE;
  975. }
  976. }
  977. static void zram_reset_device(struct zram *zram, bool reset_capacity)
  978. {
  979. size_t index;
  980. struct zram_meta *meta;
  981. down_write(&zram->init_lock);
  982. zram->limit_pages = 0;
  983. if (!init_done(zram)) {
  984. up_write(&zram->init_lock);
  985. return;
  986. }
  987. meta = zram->meta;
  988. /* Free all pages that are still in this zram device */
  989. for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
  990. unsigned long handle = meta->table[index].handle;
  991. if (!handle)
  992. continue;
  993. zs_free(meta->mem_pool, handle);
  994. }
  995. zcomp_destroy(zram->comp);
  996. zram->max_comp_streams = 1;
  997. zram_meta_free(zram->meta);
  998. zram->meta = NULL;
  999. /* Reset stats */
  1000. memset(&zram->stats, 0, sizeof(zram->stats));
  1001. zram->disksize = 0;
  1002. if (reset_capacity)
  1003. set_capacity(zram->disk, 0);
  1004. up_write(&zram->init_lock);
  1005. /*
  1006. * Revalidate disk out of the init_lock to avoid lockdep splat.
  1007. * It's okay because disk's capacity is protected by init_lock
  1008. * so that revalidate_disk always sees up-to-date capacity.
  1009. */
  1010. if (reset_capacity)
  1011. revalidate_disk(zram->disk);
  1012. }
  1013. static ssize_t disksize_store(struct device *dev,
  1014. struct device_attribute *attr, const char *buf, size_t len)
  1015. {
  1016. u64 disksize;
  1017. struct zcomp *comp;
  1018. struct zram_meta *meta;
  1019. struct zram *zram = dev_to_zram(dev);
  1020. int err;
  1021. disksize = memparse(buf, NULL);
  1022. if (!disksize) {
  1023. /* Give it a default disksize */
  1024. disksize = default_disksize_perc_ram * ((totalram_pages << PAGE_SHIFT) / 100);
  1025. /* Promote the default disksize if totalram_pages is smaller */
  1026. if (totalram_pages < SUPPOSED_TOTALRAM)
  1027. disksize += (disksize >> 1);
  1028. }
  1029. disksize = PAGE_ALIGN(disksize);
  1030. meta = zram_meta_alloc(disksize);
  1031. if (!meta)
  1032. return -ENOMEM;
  1033. comp = zcomp_create(zram->compressor, zram->max_comp_streams);
  1034. if (IS_ERR(comp)) {
  1035. pr_info("Cannot initialise %s compressing backend\n",
  1036. zram->compressor);
  1037. err = PTR_ERR(comp);
  1038. goto out_free_meta;
  1039. }
  1040. down_write(&zram->init_lock);
  1041. if (init_done(zram)) {
  1042. pr_info("Cannot change disksize for initialized device\n");
  1043. err = -EBUSY;
  1044. goto out_destroy_comp;
  1045. }
  1046. zram->meta = meta;
  1047. zram->comp = comp;
  1048. zram->disksize = disksize;
  1049. set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
  1050. up_write(&zram->init_lock);
  1051. /*
  1052. * Revalidate disk out of the init_lock to avoid lockdep splat.
  1053. * It's okay because disk's capacity is protected by init_lock
  1054. * so that revalidate_disk always sees up-to-date capacity.
  1055. */
  1056. revalidate_disk(zram->disk);
  1057. return len;
  1058. out_destroy_comp:
  1059. up_write(&zram->init_lock);
  1060. zcomp_destroy(comp);
  1061. out_free_meta:
  1062. zram_meta_free(meta);
  1063. return err;
  1064. }
  1065. static ssize_t reset_store(struct device *dev,
  1066. struct device_attribute *attr, const char *buf, size_t len)
  1067. {
  1068. int ret;
  1069. unsigned short do_reset;
  1070. struct zram *zram;
  1071. struct block_device *bdev;
  1072. zram = dev_to_zram(dev);
  1073. bdev = bdget_disk(zram->disk, 0);
  1074. if (!bdev)
  1075. return -ENOMEM;
  1076. /* Do not reset an active device! */
  1077. if (bdev->bd_holders) {
  1078. ret = -EBUSY;
  1079. goto out;
  1080. }
  1081. ret = kstrtou16(buf, 10, &do_reset);
  1082. if (ret)
  1083. goto out;
  1084. if (!do_reset) {
  1085. ret = -EINVAL;
  1086. goto out;
  1087. }
  1088. /* Make sure all pending I/O is finished */
  1089. fsync_bdev(bdev);
  1090. bdput(bdev);
  1091. zram_reset_device(zram, true);
  1092. return len;
  1093. out:
  1094. bdput(bdev);
  1095. return ret;
  1096. }
  1097. static void __zram_make_request(struct zram *zram, struct bio *bio)
  1098. {
  1099. int offset;
  1100. u32 index;
  1101. struct bio_vec bvec;
  1102. struct bvec_iter iter;
  1103. index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
  1104. offset = (bio->bi_iter.bi_sector &
  1105. (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
  1106. if (unlikely(bio->bi_rw & REQ_DISCARD)) {
  1107. zram_bio_discard(zram, index, offset, bio);
  1108. bio_endio(bio, 0);
  1109. return;
  1110. }
  1111. bio_for_each_segment(bvec, bio, iter) {
  1112. int max_transfer_size = PAGE_SIZE - offset;
  1113. if (bvec.bv_len > max_transfer_size) {
  1114. /*
  1115. * zram_bvec_rw() can only make operation on a single
  1116. * zram page. Split the bio vector.
  1117. */
  1118. struct bio_vec bv;
  1119. bv.bv_page = bvec.bv_page;
  1120. bv.bv_len = max_transfer_size;
  1121. bv.bv_offset = bvec.bv_offset;
  1122. if (zram_bvec_rw(zram, &bv, index, offset, bio) < 0)
  1123. goto out;
  1124. bv.bv_len = bvec.bv_len - max_transfer_size;
  1125. bv.bv_offset += max_transfer_size;
  1126. if (zram_bvec_rw(zram, &bv, index + 1, 0, bio) < 0)
  1127. goto out;
  1128. } else
  1129. if (zram_bvec_rw(zram, &bvec, index, offset, bio) < 0)
  1130. goto out;
  1131. update_position(&index, &offset, &bvec);
  1132. }
  1133. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1134. bio_endio(bio, 0);
  1135. return;
  1136. out:
  1137. bio_io_error(bio);
  1138. }
  1139. /*
  1140. * Handler function for all zram I/O requests.
  1141. */
  1142. static void zram_make_request(struct request_queue *queue, struct bio *bio)
  1143. {
  1144. struct zram *zram = queue->queuedata;
  1145. down_read(&zram->init_lock);
  1146. if (unlikely(!init_done(zram)))
  1147. goto error;
  1148. if (!valid_io_request(zram, bio)) {
  1149. atomic64_inc(&zram->stats.invalid_io);
  1150. goto error;
  1151. }
  1152. __zram_make_request(zram, bio);
  1153. up_read(&zram->init_lock);
  1154. return;
  1155. error:
  1156. up_read(&zram->init_lock);
  1157. bio_io_error(bio);
  1158. }
  1159. static void zram_slot_free_notify(struct block_device *bdev,
  1160. unsigned long index)
  1161. {
  1162. struct zram *zram;
  1163. struct zram_meta *meta;
  1164. zram = bdev->bd_disk->private_data;
  1165. meta = zram->meta;
  1166. bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
  1167. zram_free_page(zram, index);
  1168. bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
  1169. atomic64_inc(&zram->stats.notify_free);
  1170. }
  1171. static const struct block_device_operations zram_devops = {
  1172. .swap_slot_free_notify = zram_slot_free_notify,
  1173. .owner = THIS_MODULE
  1174. };
  1175. static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
  1176. disksize_show, disksize_store);
  1177. static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
  1178. static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
  1179. static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
  1180. static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
  1181. static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
  1182. mem_limit_store);
  1183. static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
  1184. mem_used_max_store);
  1185. static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
  1186. max_comp_streams_show, max_comp_streams_store);
  1187. static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
  1188. comp_algorithm_show, comp_algorithm_store);
  1189. ZRAM_ATTR_RO(num_reads);
  1190. ZRAM_ATTR_RO(num_writes);
  1191. ZRAM_ATTR_RO(failed_reads);
  1192. ZRAM_ATTR_RO(failed_writes);
  1193. ZRAM_ATTR_RO(invalid_io);
  1194. ZRAM_ATTR_RO(notify_free);
  1195. ZRAM_ATTR_RO(zero_pages);
  1196. ZRAM_ATTR_RO(compr_data_size);
  1197. static struct attribute *zram_disk_attrs[] = {
  1198. &dev_attr_disksize.attr,
  1199. &dev_attr_initstate.attr,
  1200. &dev_attr_reset.attr,
  1201. &dev_attr_num_reads.attr,
  1202. &dev_attr_num_writes.attr,
  1203. &dev_attr_failed_reads.attr,
  1204. &dev_attr_failed_writes.attr,
  1205. &dev_attr_invalid_io.attr,
  1206. &dev_attr_notify_free.attr,
  1207. &dev_attr_zero_pages.attr,
  1208. &dev_attr_orig_data_size.attr,
  1209. &dev_attr_compr_data_size.attr,
  1210. &dev_attr_mem_used_total.attr,
  1211. &dev_attr_mem_limit.attr,
  1212. &dev_attr_mem_used_max.attr,
  1213. &dev_attr_max_comp_streams.attr,
  1214. &dev_attr_comp_algorithm.attr,
  1215. NULL,
  1216. };
  1217. static struct attribute_group zram_disk_attr_group = {
  1218. .attrs = zram_disk_attrs,
  1219. };
  1220. static int create_device(struct zram *zram, int device_id)
  1221. {
  1222. int ret = -ENOMEM;
  1223. init_rwsem(&zram->init_lock);
  1224. #ifdef CONFIG_ZSM
  1225. spin_lock_init(&zram_node_mutex);
  1226. spin_lock_init(&zram_node4k_mutex);
  1227. #endif
  1228. zram->queue = blk_alloc_queue(GFP_KERNEL);
  1229. if (!zram->queue) {
  1230. pr_err("Error allocating disk queue for device %d\n",
  1231. device_id);
  1232. goto out;
  1233. }
  1234. blk_queue_make_request(zram->queue, zram_make_request);
  1235. zram->queue->queuedata = zram;
  1236. /* gendisk structure */
  1237. zram->disk = alloc_disk(1);
  1238. if (!zram->disk) {
  1239. pr_warn("Error allocating disk structure for device %d\n",
  1240. device_id);
  1241. goto out_free_queue;
  1242. }
  1243. zram->disk->major = zram_major;
  1244. zram->disk->first_minor = device_id;
  1245. zram->disk->fops = &zram_devops;
  1246. zram->disk->queue = zram->queue;
  1247. zram->disk->private_data = zram;
  1248. snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
  1249. /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
  1250. set_capacity(zram->disk, 0);
  1251. /* zram devices sort of resembles non-rotational disks */
  1252. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
  1253. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
  1254. /*
  1255. * To ensure that we always get PAGE_SIZE aligned
  1256. * and n*PAGE_SIZED sized I/O requests.
  1257. */
  1258. blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
  1259. blk_queue_logical_block_size(zram->disk->queue,
  1260. ZRAM_LOGICAL_BLOCK_SIZE);
  1261. blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
  1262. blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
  1263. zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
  1264. zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
  1265. /*
  1266. * zram_bio_discard() will clear all logical blocks if logical block
  1267. * size is identical with physical block size(PAGE_SIZE). But if it is
  1268. * different, we will skip discarding some parts of logical blocks in
  1269. * the part of the request range which isn't aligned to physical block
  1270. * size. So we can't ensure that all discarded logical blocks are
  1271. * zeroed.
  1272. */
  1273. if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
  1274. zram->disk->queue->limits.discard_zeroes_data = 1;
  1275. else
  1276. zram->disk->queue->limits.discard_zeroes_data = 0;
  1277. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
  1278. add_disk(zram->disk);
  1279. ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
  1280. &zram_disk_attr_group);
  1281. if (ret < 0) {
  1282. pr_warn("Error creating sysfs group");
  1283. goto out_free_disk;
  1284. }
  1285. strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
  1286. zram->meta = NULL;
  1287. zram->max_comp_streams = 1;
  1288. return 0;
  1289. out_free_disk:
  1290. del_gendisk(zram->disk);
  1291. put_disk(zram->disk);
  1292. out_free_queue:
  1293. blk_cleanup_queue(zram->queue);
  1294. out:
  1295. return ret;
  1296. }
  1297. static void destroy_device(struct zram *zram)
  1298. {
  1299. sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
  1300. &zram_disk_attr_group);
  1301. del_gendisk(zram->disk);
  1302. put_disk(zram->disk);
  1303. blk_cleanup_queue(zram->queue);
  1304. }
  1305. unsigned long zram_mlog(void)
  1306. {
  1307. #define P2K(x) (((unsigned long)x) << (PAGE_SHIFT - 10))
  1308. if (num_devices == 1 && init_done(zram_devices))
  1309. return P2K(zs_get_total_pages(zram_devices->meta->mem_pool));
  1310. #undef P2K
  1311. return 0;
  1312. }
  1313. #ifdef CONFIG_PROC_FS
  1314. static int zraminfo_proc_show(struct seq_file *m, void *v)
  1315. {
  1316. if (num_devices == 1 && init_done(zram_devices)) {
  1317. #define P2K(x) (((unsigned long)x) << (PAGE_SHIFT - 10))
  1318. #define B2K(x) (((unsigned long)x) >> (10))
  1319. seq_printf(m,
  1320. "DiskSize: %8lu kB\n"
  1321. "OrigSize: %8lu kB\n"
  1322. "ComprSize: %8lu kB\n"
  1323. "MemUsed: %8lu kB\n"
  1324. "ZeroPage: %8lu kB\n"
  1325. "NotifyFree: %8lu kB\n"
  1326. "FailReads: %8lu kB\n"
  1327. "FailWrites: %8lu kB\n"
  1328. "NumReads: %8lu kB\n"
  1329. "NumWrites: %8lu kB\n"
  1330. "InvalidIO: %8lu kB\n"
  1331. #ifdef CONFIG_ZSM
  1332. "ZSM saved: %8lu kB\n"
  1333. "ZSM4k saved: %8lu kB\n"
  1334. #endif
  1335. "MaxUsedPages: %8lu kB\n"
  1336. ,
  1337. B2K(zram_devices->disksize),
  1338. P2K(atomic64_read(&zram_devices->stats.pages_stored)),
  1339. B2K(atomic64_read(&zram_devices->stats.compr_data_size)),
  1340. P2K(zs_get_total_pages(zram_devices->meta->mem_pool)),
  1341. P2K(atomic64_read(&zram_devices->stats.zero_pages)),
  1342. P2K(atomic64_read(&zram_devices->stats.notify_free)),
  1343. P2K(atomic64_read(&zram_devices->stats.failed_reads)),
  1344. P2K(atomic64_read(&zram_devices->stats.failed_writes)),
  1345. P2K(atomic64_read(&zram_devices->stats.num_reads)),
  1346. P2K(atomic64_read(&zram_devices->stats.num_writes)),
  1347. P2K(atomic64_read(&zram_devices->stats.invalid_io)),
  1348. #ifdef CONFIG_ZSM
  1349. B2K(atomic64_read(&zram_devices->stats.zsm_saved)),
  1350. B2K(atomic64_read(&zram_devices->stats.zsm_saved4k)),
  1351. #endif
  1352. P2K(atomic_long_read(&zram_devices->stats.max_used_pages))
  1353. );
  1354. #undef P2K
  1355. #undef B2K
  1356. seq_printf(m, "Algorithm: [%s]\n", zram_devices->compressor);
  1357. }
  1358. return 0;
  1359. }
  1360. static int zraminfo_proc_open(struct inode *inode, struct file *file)
  1361. {
  1362. return single_open(file, zraminfo_proc_show, NULL);
  1363. }
  1364. static const struct file_operations zraminfo_proc_fops = {
  1365. .open = zraminfo_proc_open,
  1366. .read = seq_read,
  1367. .llseek = seq_lseek,
  1368. .release = single_release,
  1369. };
  1370. #endif
  1371. static int __init zram_init(void)
  1372. {
  1373. int ret, dev_id;
  1374. if (num_devices > max_num_devices) {
  1375. pr_warn("Invalid value for num_devices: %u\n",
  1376. num_devices);
  1377. ret = -EINVAL;
  1378. goto out;
  1379. }
  1380. zram_major = register_blkdev(0, "zram");
  1381. if (zram_major <= 0) {
  1382. pr_warn("Unable to get major number\n");
  1383. ret = -EBUSY;
  1384. goto out;
  1385. }
  1386. /* Allocate the device array and initialize each one */
  1387. zram_devices = kcalloc(num_devices, sizeof(struct zram), GFP_KERNEL);
  1388. if (!zram_devices) {
  1389. ret = -ENOMEM;
  1390. goto unregister;
  1391. }
  1392. for (dev_id = 0; dev_id < num_devices; dev_id++) {
  1393. ret = create_device(&zram_devices[dev_id], dev_id);
  1394. if (ret)
  1395. goto free_devices;
  1396. }
  1397. #ifdef CONFIG_PROC_FS
  1398. proc_create("zraminfo", 0, NULL, &zraminfo_proc_fops);
  1399. #endif
  1400. pr_info("Created %u device(s) ...\n", num_devices);
  1401. return 0;
  1402. free_devices:
  1403. while (dev_id)
  1404. destroy_device(&zram_devices[--dev_id]);
  1405. kfree(zram_devices);
  1406. unregister:
  1407. unregister_blkdev(zram_major, "zram");
  1408. out:
  1409. return ret;
  1410. }
  1411. static void __exit zram_exit(void)
  1412. {
  1413. int i;
  1414. struct zram *zram;
  1415. for (i = 0; i < num_devices; i++) {
  1416. zram = &zram_devices[i];
  1417. destroy_device(zram);
  1418. /*
  1419. * Shouldn't access zram->disk after destroy_device
  1420. * because destroy_device already released zram->disk.
  1421. */
  1422. zram_reset_device(zram, false);
  1423. }
  1424. unregister_blkdev(zram_major, "zram");
  1425. kfree(zram_devices);
  1426. pr_debug("Cleanup done!\n");
  1427. }
  1428. module_init(zram_init);
  1429. module_exit(zram_exit);
  1430. module_param(num_devices, uint, 0);
  1431. MODULE_PARM_DESC(num_devices, "Number of zram devices");
  1432. MODULE_LICENSE("Dual BSD/GPL");
  1433. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
  1434. MODULE_DESCRIPTION("Compressed RAM Block Device");