dm-bufio.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. /*
  2. * Copyright (C) 2009-2011 Red Hat, Inc.
  3. *
  4. * Author: Mikulas Patocka <mpatocka@redhat.com>
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/dm-bufio.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/dm-io.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/shrinker.h>
  16. #include <linux/module.h>
  17. #include <linux/rbtree.h>
  18. #include <linux/stacktrace.h>
  19. #define DM_MSG_PREFIX "bufio"
  20. /*
  21. * Memory management policy:
  22. * Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory
  23. * or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower).
  24. * Always allocate at least DM_BUFIO_MIN_BUFFERS buffers.
  25. * Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT
  26. * dirty buffers.
  27. */
  28. #define DM_BUFIO_MIN_BUFFERS 8
  29. #define DM_BUFIO_MEMORY_PERCENT 2
  30. #define DM_BUFIO_VMALLOC_PERCENT 25
  31. #define DM_BUFIO_WRITEBACK_PERCENT 75
  32. /*
  33. * Check buffer ages in this interval (seconds)
  34. */
  35. #define DM_BUFIO_WORK_TIMER_SECS 30
  36. /*
  37. * Free buffers when they are older than this (seconds)
  38. */
  39. #define DM_BUFIO_DEFAULT_AGE_SECS 300
  40. /*
  41. * The nr of bytes of cached data to keep around.
  42. */
  43. #define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024)
  44. /*
  45. * Align buffer writes to this boundary.
  46. * Tests show that SSDs have the highest IOPS when using 4k writes.
  47. */
  48. #define DM_BUFIO_WRITE_ALIGN 4096
  49. /*
  50. * dm_buffer->list_mode
  51. */
  52. #define LIST_CLEAN 0
  53. #define LIST_DIRTY 1
  54. #define LIST_SIZE 2
  55. /*
  56. * Linking of buffers:
  57. * All buffers are linked to cache_hash with their hash_list field.
  58. *
  59. * Clean buffers that are not being written (B_WRITING not set)
  60. * are linked to lru[LIST_CLEAN] with their lru_list field.
  61. *
  62. * Dirty and clean buffers that are being written are linked to
  63. * lru[LIST_DIRTY] with their lru_list field. When the write
  64. * finishes, the buffer cannot be relinked immediately (because we
  65. * are in an interrupt context and relinking requires process
  66. * context), so some clean-not-writing buffers can be held on
  67. * dirty_lru too. They are later added to lru in the process
  68. * context.
  69. */
  70. struct dm_bufio_client {
  71. struct mutex lock;
  72. struct list_head lru[LIST_SIZE];
  73. unsigned long n_buffers[LIST_SIZE];
  74. struct block_device *bdev;
  75. unsigned block_size;
  76. s8 sectors_per_block_bits;
  77. void (*alloc_callback)(struct dm_buffer *);
  78. void (*write_callback)(struct dm_buffer *);
  79. struct kmem_cache *slab_buffer;
  80. struct kmem_cache *slab_cache;
  81. struct dm_io_client *dm_io;
  82. struct list_head reserved_buffers;
  83. unsigned need_reserved_buffers;
  84. unsigned minimum_buffers;
  85. struct rb_root buffer_tree;
  86. wait_queue_head_t free_buffer_wait;
  87. sector_t start;
  88. int async_write_error;
  89. struct list_head client_list;
  90. struct shrinker shrinker;
  91. };
  92. /*
  93. * Buffer state bits.
  94. */
  95. #define B_READING 0
  96. #define B_WRITING 1
  97. #define B_DIRTY 2
  98. /*
  99. * Describes how the block was allocated:
  100. * kmem_cache_alloc(), __get_free_pages() or vmalloc().
  101. * See the comment at alloc_buffer_data.
  102. */
  103. enum data_mode {
  104. DATA_MODE_SLAB = 0,
  105. DATA_MODE_GET_FREE_PAGES = 1,
  106. DATA_MODE_VMALLOC = 2,
  107. DATA_MODE_LIMIT = 3
  108. };
  109. struct dm_buffer {
  110. struct rb_node node;
  111. struct list_head lru_list;
  112. sector_t block;
  113. void *data;
  114. unsigned char data_mode; /* DATA_MODE_* */
  115. unsigned char list_mode; /* LIST_* */
  116. blk_status_t read_error;
  117. blk_status_t write_error;
  118. unsigned hold_count;
  119. unsigned long state;
  120. unsigned long last_accessed;
  121. unsigned dirty_start;
  122. unsigned dirty_end;
  123. unsigned write_start;
  124. unsigned write_end;
  125. struct dm_bufio_client *c;
  126. struct list_head write_list;
  127. void (*end_io)(struct dm_buffer *, blk_status_t);
  128. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  129. #define MAX_STACK 10
  130. struct stack_trace stack_trace;
  131. unsigned long stack_entries[MAX_STACK];
  132. #endif
  133. };
  134. /*----------------------------------------------------------------*/
  135. #define dm_bufio_in_request() (!!current->bio_list)
  136. static void dm_bufio_lock(struct dm_bufio_client *c)
  137. {
  138. mutex_lock_nested(&c->lock, dm_bufio_in_request());
  139. }
  140. static int dm_bufio_trylock(struct dm_bufio_client *c)
  141. {
  142. return mutex_trylock(&c->lock);
  143. }
  144. static void dm_bufio_unlock(struct dm_bufio_client *c)
  145. {
  146. mutex_unlock(&c->lock);
  147. }
  148. /*----------------------------------------------------------------*/
  149. /*
  150. * Default cache size: available memory divided by the ratio.
  151. */
  152. static unsigned long dm_bufio_default_cache_size;
  153. /*
  154. * Total cache size set by the user.
  155. */
  156. static unsigned long dm_bufio_cache_size;
  157. /*
  158. * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change
  159. * at any time. If it disagrees, the user has changed cache size.
  160. */
  161. static unsigned long dm_bufio_cache_size_latch;
  162. static DEFINE_SPINLOCK(param_spinlock);
  163. /*
  164. * Buffers are freed after this timeout
  165. */
  166. static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS;
  167. static unsigned long dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES;
  168. static unsigned long dm_bufio_peak_allocated;
  169. static unsigned long dm_bufio_allocated_kmem_cache;
  170. static unsigned long dm_bufio_allocated_get_free_pages;
  171. static unsigned long dm_bufio_allocated_vmalloc;
  172. static unsigned long dm_bufio_current_allocated;
  173. /*----------------------------------------------------------------*/
  174. /*
  175. * Per-client cache: dm_bufio_cache_size / dm_bufio_client_count
  176. */
  177. static unsigned long dm_bufio_cache_size_per_client;
  178. /*
  179. * The current number of clients.
  180. */
  181. static int dm_bufio_client_count;
  182. /*
  183. * The list of all clients.
  184. */
  185. static LIST_HEAD(dm_bufio_all_clients);
  186. /*
  187. * This mutex protects dm_bufio_cache_size_latch,
  188. * dm_bufio_cache_size_per_client and dm_bufio_client_count
  189. */
  190. static DEFINE_MUTEX(dm_bufio_clients_lock);
  191. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  192. static void buffer_record_stack(struct dm_buffer *b)
  193. {
  194. b->stack_trace.nr_entries = 0;
  195. b->stack_trace.max_entries = MAX_STACK;
  196. b->stack_trace.entries = b->stack_entries;
  197. b->stack_trace.skip = 2;
  198. save_stack_trace(&b->stack_trace);
  199. }
  200. #endif
  201. /*----------------------------------------------------------------
  202. * A red/black tree acts as an index for all the buffers.
  203. *--------------------------------------------------------------*/
  204. static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block)
  205. {
  206. struct rb_node *n = c->buffer_tree.rb_node;
  207. struct dm_buffer *b;
  208. while (n) {
  209. b = container_of(n, struct dm_buffer, node);
  210. if (b->block == block)
  211. return b;
  212. n = (b->block < block) ? n->rb_left : n->rb_right;
  213. }
  214. return NULL;
  215. }
  216. static void __insert(struct dm_bufio_client *c, struct dm_buffer *b)
  217. {
  218. struct rb_node **new = &c->buffer_tree.rb_node, *parent = NULL;
  219. struct dm_buffer *found;
  220. while (*new) {
  221. found = container_of(*new, struct dm_buffer, node);
  222. if (found->block == b->block) {
  223. BUG_ON(found != b);
  224. return;
  225. }
  226. parent = *new;
  227. new = (found->block < b->block) ?
  228. &((*new)->rb_left) : &((*new)->rb_right);
  229. }
  230. rb_link_node(&b->node, parent, new);
  231. rb_insert_color(&b->node, &c->buffer_tree);
  232. }
  233. static void __remove(struct dm_bufio_client *c, struct dm_buffer *b)
  234. {
  235. rb_erase(&b->node, &c->buffer_tree);
  236. }
  237. /*----------------------------------------------------------------*/
  238. static void adjust_total_allocated(unsigned char data_mode, long diff)
  239. {
  240. static unsigned long * const class_ptr[DATA_MODE_LIMIT] = {
  241. &dm_bufio_allocated_kmem_cache,
  242. &dm_bufio_allocated_get_free_pages,
  243. &dm_bufio_allocated_vmalloc,
  244. };
  245. spin_lock(&param_spinlock);
  246. *class_ptr[data_mode] += diff;
  247. dm_bufio_current_allocated += diff;
  248. if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
  249. dm_bufio_peak_allocated = dm_bufio_current_allocated;
  250. spin_unlock(&param_spinlock);
  251. }
  252. /*
  253. * Change the number of clients and recalculate per-client limit.
  254. */
  255. static void __cache_size_refresh(void)
  256. {
  257. BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock));
  258. BUG_ON(dm_bufio_client_count < 0);
  259. dm_bufio_cache_size_latch = READ_ONCE(dm_bufio_cache_size);
  260. /*
  261. * Use default if set to 0 and report the actual cache size used.
  262. */
  263. if (!dm_bufio_cache_size_latch) {
  264. (void)cmpxchg(&dm_bufio_cache_size, 0,
  265. dm_bufio_default_cache_size);
  266. dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
  267. }
  268. dm_bufio_cache_size_per_client = dm_bufio_cache_size_latch /
  269. (dm_bufio_client_count ? : 1);
  270. }
  271. /*
  272. * Allocating buffer data.
  273. *
  274. * Small buffers are allocated with kmem_cache, to use space optimally.
  275. *
  276. * For large buffers, we choose between get_free_pages and vmalloc.
  277. * Each has advantages and disadvantages.
  278. *
  279. * __get_free_pages can randomly fail if the memory is fragmented.
  280. * __vmalloc won't randomly fail, but vmalloc space is limited (it may be
  281. * as low as 128M) so using it for caching is not appropriate.
  282. *
  283. * If the allocation may fail we use __get_free_pages. Memory fragmentation
  284. * won't have a fatal effect here, but it just causes flushes of some other
  285. * buffers and more I/O will be performed. Don't use __get_free_pages if it
  286. * always fails (i.e. order >= MAX_ORDER).
  287. *
  288. * If the allocation shouldn't fail we use __vmalloc. This is only for the
  289. * initial reserve allocation, so there's no risk of wasting all vmalloc
  290. * space.
  291. */
  292. static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
  293. unsigned char *data_mode)
  294. {
  295. if (unlikely(c->slab_cache != NULL)) {
  296. *data_mode = DATA_MODE_SLAB;
  297. return kmem_cache_alloc(c->slab_cache, gfp_mask);
  298. }
  299. if (c->block_size <= KMALLOC_MAX_SIZE &&
  300. gfp_mask & __GFP_NORETRY) {
  301. *data_mode = DATA_MODE_GET_FREE_PAGES;
  302. return (void *)__get_free_pages(gfp_mask,
  303. c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT));
  304. }
  305. *data_mode = DATA_MODE_VMALLOC;
  306. /*
  307. * __vmalloc allocates the data pages and auxiliary structures with
  308. * gfp_flags that were specified, but pagetables are always allocated
  309. * with GFP_KERNEL, no matter what was specified as gfp_mask.
  310. *
  311. * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that
  312. * all allocations done by this process (including pagetables) are done
  313. * as if GFP_NOIO was specified.
  314. */
  315. if (gfp_mask & __GFP_NORETRY) {
  316. unsigned noio_flag = memalloc_noio_save();
  317. void *ptr = __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL);
  318. memalloc_noio_restore(noio_flag);
  319. return ptr;
  320. }
  321. return __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL);
  322. }
  323. /*
  324. * Free buffer's data.
  325. */
  326. static void free_buffer_data(struct dm_bufio_client *c,
  327. void *data, unsigned char data_mode)
  328. {
  329. switch (data_mode) {
  330. case DATA_MODE_SLAB:
  331. kmem_cache_free(c->slab_cache, data);
  332. break;
  333. case DATA_MODE_GET_FREE_PAGES:
  334. free_pages((unsigned long)data,
  335. c->sectors_per_block_bits - (PAGE_SHIFT - SECTOR_SHIFT));
  336. break;
  337. case DATA_MODE_VMALLOC:
  338. vfree(data);
  339. break;
  340. default:
  341. DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d",
  342. data_mode);
  343. BUG();
  344. }
  345. }
  346. /*
  347. * Allocate buffer and its data.
  348. */
  349. static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask)
  350. {
  351. struct dm_buffer *b = kmem_cache_alloc(c->slab_buffer, gfp_mask);
  352. if (!b)
  353. return NULL;
  354. b->c = c;
  355. b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode);
  356. if (!b->data) {
  357. kmem_cache_free(c->slab_buffer, b);
  358. return NULL;
  359. }
  360. adjust_total_allocated(b->data_mode, (long)c->block_size);
  361. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  362. memset(&b->stack_trace, 0, sizeof(b->stack_trace));
  363. #endif
  364. return b;
  365. }
  366. /*
  367. * Free buffer and its data.
  368. */
  369. static void free_buffer(struct dm_buffer *b)
  370. {
  371. struct dm_bufio_client *c = b->c;
  372. adjust_total_allocated(b->data_mode, -(long)c->block_size);
  373. free_buffer_data(c, b->data, b->data_mode);
  374. kmem_cache_free(c->slab_buffer, b);
  375. }
  376. /*
  377. * Link buffer to the hash list and clean or dirty queue.
  378. */
  379. static void __link_buffer(struct dm_buffer *b, sector_t block, int dirty)
  380. {
  381. struct dm_bufio_client *c = b->c;
  382. c->n_buffers[dirty]++;
  383. b->block = block;
  384. b->list_mode = dirty;
  385. list_add(&b->lru_list, &c->lru[dirty]);
  386. __insert(b->c, b);
  387. b->last_accessed = jiffies;
  388. }
  389. /*
  390. * Unlink buffer from the hash list and dirty or clean queue.
  391. */
  392. static void __unlink_buffer(struct dm_buffer *b)
  393. {
  394. struct dm_bufio_client *c = b->c;
  395. BUG_ON(!c->n_buffers[b->list_mode]);
  396. c->n_buffers[b->list_mode]--;
  397. __remove(b->c, b);
  398. list_del(&b->lru_list);
  399. }
  400. /*
  401. * Place the buffer to the head of dirty or clean LRU queue.
  402. */
  403. static void __relink_lru(struct dm_buffer *b, int dirty)
  404. {
  405. struct dm_bufio_client *c = b->c;
  406. BUG_ON(!c->n_buffers[b->list_mode]);
  407. c->n_buffers[b->list_mode]--;
  408. c->n_buffers[dirty]++;
  409. b->list_mode = dirty;
  410. list_move(&b->lru_list, &c->lru[dirty]);
  411. b->last_accessed = jiffies;
  412. }
  413. /*----------------------------------------------------------------
  414. * Submit I/O on the buffer.
  415. *
  416. * Bio interface is faster but it has some problems:
  417. * the vector list is limited (increasing this limit increases
  418. * memory-consumption per buffer, so it is not viable);
  419. *
  420. * the memory must be direct-mapped, not vmalloced;
  421. *
  422. * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and
  423. * it is not vmalloced, try using the bio interface.
  424. *
  425. * If the buffer is big, if it is vmalloced or if the underlying device
  426. * rejects the bio because it is too large, use dm-io layer to do the I/O.
  427. * The dm-io layer splits the I/O into multiple requests, avoiding the above
  428. * shortcomings.
  429. *--------------------------------------------------------------*/
  430. /*
  431. * dm-io completion routine. It just calls b->bio.bi_end_io, pretending
  432. * that the request was handled directly with bio interface.
  433. */
  434. static void dmio_complete(unsigned long error, void *context)
  435. {
  436. struct dm_buffer *b = context;
  437. b->end_io(b, unlikely(error != 0) ? BLK_STS_IOERR : 0);
  438. }
  439. static void use_dmio(struct dm_buffer *b, int rw, sector_t sector,
  440. unsigned n_sectors, unsigned offset)
  441. {
  442. int r;
  443. struct dm_io_request io_req = {
  444. .bi_op = rw,
  445. .bi_op_flags = 0,
  446. .notify.fn = dmio_complete,
  447. .notify.context = b,
  448. .client = b->c->dm_io,
  449. };
  450. struct dm_io_region region = {
  451. .bdev = b->c->bdev,
  452. .sector = sector,
  453. .count = n_sectors,
  454. };
  455. if (b->data_mode != DATA_MODE_VMALLOC) {
  456. io_req.mem.type = DM_IO_KMEM;
  457. io_req.mem.ptr.addr = (char *)b->data + offset;
  458. } else {
  459. io_req.mem.type = DM_IO_VMA;
  460. io_req.mem.ptr.vma = (char *)b->data + offset;
  461. }
  462. r = dm_io(&io_req, 1, &region, NULL);
  463. if (unlikely(r))
  464. b->end_io(b, errno_to_blk_status(r));
  465. }
  466. static void bio_complete(struct bio *bio)
  467. {
  468. struct dm_buffer *b = bio->bi_private;
  469. blk_status_t status = bio->bi_status;
  470. bio_put(bio);
  471. b->end_io(b, status);
  472. }
  473. static void use_bio(struct dm_buffer *b, int rw, sector_t sector,
  474. unsigned n_sectors, unsigned offset)
  475. {
  476. struct bio *bio;
  477. char *ptr;
  478. unsigned vec_size, len;
  479. vec_size = b->c->block_size >> PAGE_SHIFT;
  480. if (unlikely(b->c->sectors_per_block_bits < PAGE_SHIFT - SECTOR_SHIFT))
  481. vec_size += 2;
  482. bio = bio_kmalloc(GFP_NOWAIT | __GFP_NORETRY | __GFP_NOWARN, vec_size);
  483. if (!bio) {
  484. dmio:
  485. use_dmio(b, rw, sector, n_sectors, offset);
  486. return;
  487. }
  488. bio->bi_iter.bi_sector = sector;
  489. bio_set_dev(bio, b->c->bdev);
  490. bio_set_op_attrs(bio, rw, 0);
  491. bio->bi_end_io = bio_complete;
  492. bio->bi_private = b;
  493. ptr = (char *)b->data + offset;
  494. len = n_sectors << SECTOR_SHIFT;
  495. do {
  496. unsigned this_step = min((unsigned)(PAGE_SIZE - offset_in_page(ptr)), len);
  497. if (!bio_add_page(bio, virt_to_page(ptr), this_step,
  498. offset_in_page(ptr))) {
  499. bio_put(bio);
  500. goto dmio;
  501. }
  502. len -= this_step;
  503. ptr += this_step;
  504. } while (len > 0);
  505. submit_bio(bio);
  506. }
  507. static void submit_io(struct dm_buffer *b, int rw, void (*end_io)(struct dm_buffer *, blk_status_t))
  508. {
  509. unsigned n_sectors;
  510. sector_t sector;
  511. unsigned offset, end;
  512. b->end_io = end_io;
  513. if (likely(b->c->sectors_per_block_bits >= 0))
  514. sector = b->block << b->c->sectors_per_block_bits;
  515. else
  516. sector = b->block * (b->c->block_size >> SECTOR_SHIFT);
  517. sector += b->c->start;
  518. if (rw != REQ_OP_WRITE) {
  519. n_sectors = b->c->block_size >> SECTOR_SHIFT;
  520. offset = 0;
  521. } else {
  522. if (b->c->write_callback)
  523. b->c->write_callback(b);
  524. offset = b->write_start;
  525. end = b->write_end;
  526. offset &= -DM_BUFIO_WRITE_ALIGN;
  527. end += DM_BUFIO_WRITE_ALIGN - 1;
  528. end &= -DM_BUFIO_WRITE_ALIGN;
  529. if (unlikely(end > b->c->block_size))
  530. end = b->c->block_size;
  531. sector += offset >> SECTOR_SHIFT;
  532. n_sectors = (end - offset) >> SECTOR_SHIFT;
  533. }
  534. if (b->data_mode != DATA_MODE_VMALLOC)
  535. use_bio(b, rw, sector, n_sectors, offset);
  536. else
  537. use_dmio(b, rw, sector, n_sectors, offset);
  538. }
  539. /*----------------------------------------------------------------
  540. * Writing dirty buffers
  541. *--------------------------------------------------------------*/
  542. /*
  543. * The endio routine for write.
  544. *
  545. * Set the error, clear B_WRITING bit and wake anyone who was waiting on
  546. * it.
  547. */
  548. static void write_endio(struct dm_buffer *b, blk_status_t status)
  549. {
  550. b->write_error = status;
  551. if (unlikely(status)) {
  552. struct dm_bufio_client *c = b->c;
  553. (void)cmpxchg(&c->async_write_error, 0,
  554. blk_status_to_errno(status));
  555. }
  556. BUG_ON(!test_bit(B_WRITING, &b->state));
  557. smp_mb__before_atomic();
  558. clear_bit(B_WRITING, &b->state);
  559. smp_mb__after_atomic();
  560. wake_up_bit(&b->state, B_WRITING);
  561. }
  562. /*
  563. * Initiate a write on a dirty buffer, but don't wait for it.
  564. *
  565. * - If the buffer is not dirty, exit.
  566. * - If there some previous write going on, wait for it to finish (we can't
  567. * have two writes on the same buffer simultaneously).
  568. * - Submit our write and don't wait on it. We set B_WRITING indicating
  569. * that there is a write in progress.
  570. */
  571. static void __write_dirty_buffer(struct dm_buffer *b,
  572. struct list_head *write_list)
  573. {
  574. if (!test_bit(B_DIRTY, &b->state))
  575. return;
  576. clear_bit(B_DIRTY, &b->state);
  577. wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
  578. b->write_start = b->dirty_start;
  579. b->write_end = b->dirty_end;
  580. if (!write_list)
  581. submit_io(b, REQ_OP_WRITE, write_endio);
  582. else
  583. list_add_tail(&b->write_list, write_list);
  584. }
  585. static void __flush_write_list(struct list_head *write_list)
  586. {
  587. struct blk_plug plug;
  588. blk_start_plug(&plug);
  589. while (!list_empty(write_list)) {
  590. struct dm_buffer *b =
  591. list_entry(write_list->next, struct dm_buffer, write_list);
  592. list_del(&b->write_list);
  593. submit_io(b, REQ_OP_WRITE, write_endio);
  594. cond_resched();
  595. }
  596. blk_finish_plug(&plug);
  597. }
  598. /*
  599. * Wait until any activity on the buffer finishes. Possibly write the
  600. * buffer if it is dirty. When this function finishes, there is no I/O
  601. * running on the buffer and the buffer is not dirty.
  602. */
  603. static void __make_buffer_clean(struct dm_buffer *b)
  604. {
  605. BUG_ON(b->hold_count);
  606. if (!b->state) /* fast case */
  607. return;
  608. wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
  609. __write_dirty_buffer(b, NULL);
  610. wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
  611. }
  612. /*
  613. * Find some buffer that is not held by anybody, clean it, unlink it and
  614. * return it.
  615. */
  616. static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
  617. {
  618. struct dm_buffer *b;
  619. list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
  620. BUG_ON(test_bit(B_WRITING, &b->state));
  621. BUG_ON(test_bit(B_DIRTY, &b->state));
  622. if (!b->hold_count) {
  623. __make_buffer_clean(b);
  624. __unlink_buffer(b);
  625. return b;
  626. }
  627. cond_resched();
  628. }
  629. list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
  630. BUG_ON(test_bit(B_READING, &b->state));
  631. if (!b->hold_count) {
  632. __make_buffer_clean(b);
  633. __unlink_buffer(b);
  634. return b;
  635. }
  636. cond_resched();
  637. }
  638. return NULL;
  639. }
  640. /*
  641. * Wait until some other threads free some buffer or release hold count on
  642. * some buffer.
  643. *
  644. * This function is entered with c->lock held, drops it and regains it
  645. * before exiting.
  646. */
  647. static void __wait_for_free_buffer(struct dm_bufio_client *c)
  648. {
  649. DECLARE_WAITQUEUE(wait, current);
  650. add_wait_queue(&c->free_buffer_wait, &wait);
  651. set_current_state(TASK_UNINTERRUPTIBLE);
  652. dm_bufio_unlock(c);
  653. io_schedule();
  654. remove_wait_queue(&c->free_buffer_wait, &wait);
  655. dm_bufio_lock(c);
  656. }
  657. enum new_flag {
  658. NF_FRESH = 0,
  659. NF_READ = 1,
  660. NF_GET = 2,
  661. NF_PREFETCH = 3
  662. };
  663. /*
  664. * Allocate a new buffer. If the allocation is not possible, wait until
  665. * some other thread frees a buffer.
  666. *
  667. * May drop the lock and regain it.
  668. */
  669. static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
  670. {
  671. struct dm_buffer *b;
  672. bool tried_noio_alloc = false;
  673. /*
  674. * dm-bufio is resistant to allocation failures (it just keeps
  675. * one buffer reserved in cases all the allocations fail).
  676. * So set flags to not try too hard:
  677. * GFP_NOWAIT: don't wait; if we need to sleep we'll release our
  678. * mutex and wait ourselves.
  679. * __GFP_NORETRY: don't retry and rather return failure
  680. * __GFP_NOMEMALLOC: don't use emergency reserves
  681. * __GFP_NOWARN: don't print a warning in case of failure
  682. *
  683. * For debugging, if we set the cache size to 1, no new buffers will
  684. * be allocated.
  685. */
  686. while (1) {
  687. if (dm_bufio_cache_size_latch != 1) {
  688. b = alloc_buffer(c, GFP_NOWAIT | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  689. if (b)
  690. return b;
  691. }
  692. if (nf == NF_PREFETCH)
  693. return NULL;
  694. if (dm_bufio_cache_size_latch != 1 && !tried_noio_alloc) {
  695. dm_bufio_unlock(c);
  696. b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  697. dm_bufio_lock(c);
  698. if (b)
  699. return b;
  700. tried_noio_alloc = true;
  701. }
  702. if (!list_empty(&c->reserved_buffers)) {
  703. b = list_entry(c->reserved_buffers.next,
  704. struct dm_buffer, lru_list);
  705. list_del(&b->lru_list);
  706. c->need_reserved_buffers++;
  707. return b;
  708. }
  709. b = __get_unclaimed_buffer(c);
  710. if (b)
  711. return b;
  712. __wait_for_free_buffer(c);
  713. }
  714. }
  715. static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
  716. {
  717. struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
  718. if (!b)
  719. return NULL;
  720. if (c->alloc_callback)
  721. c->alloc_callback(b);
  722. return b;
  723. }
  724. /*
  725. * Free a buffer and wake other threads waiting for free buffers.
  726. */
  727. static void __free_buffer_wake(struct dm_buffer *b)
  728. {
  729. struct dm_bufio_client *c = b->c;
  730. if (!c->need_reserved_buffers)
  731. free_buffer(b);
  732. else {
  733. list_add(&b->lru_list, &c->reserved_buffers);
  734. c->need_reserved_buffers--;
  735. }
  736. wake_up(&c->free_buffer_wait);
  737. }
  738. static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
  739. struct list_head *write_list)
  740. {
  741. struct dm_buffer *b, *tmp;
  742. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  743. BUG_ON(test_bit(B_READING, &b->state));
  744. if (!test_bit(B_DIRTY, &b->state) &&
  745. !test_bit(B_WRITING, &b->state)) {
  746. __relink_lru(b, LIST_CLEAN);
  747. continue;
  748. }
  749. if (no_wait && test_bit(B_WRITING, &b->state))
  750. return;
  751. __write_dirty_buffer(b, write_list);
  752. cond_resched();
  753. }
  754. }
  755. /*
  756. * Get writeback threshold and buffer limit for a given client.
  757. */
  758. static void __get_memory_limit(struct dm_bufio_client *c,
  759. unsigned long *threshold_buffers,
  760. unsigned long *limit_buffers)
  761. {
  762. unsigned long buffers;
  763. if (unlikely(READ_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch)) {
  764. if (mutex_trylock(&dm_bufio_clients_lock)) {
  765. __cache_size_refresh();
  766. mutex_unlock(&dm_bufio_clients_lock);
  767. }
  768. }
  769. buffers = dm_bufio_cache_size_per_client;
  770. if (likely(c->sectors_per_block_bits >= 0))
  771. buffers >>= c->sectors_per_block_bits + SECTOR_SHIFT;
  772. else
  773. buffers /= c->block_size;
  774. if (buffers < c->minimum_buffers)
  775. buffers = c->minimum_buffers;
  776. *limit_buffers = buffers;
  777. *threshold_buffers = mult_frac(buffers,
  778. DM_BUFIO_WRITEBACK_PERCENT, 100);
  779. }
  780. /*
  781. * Check if we're over watermark.
  782. * If we are over threshold_buffers, start freeing buffers.
  783. * If we're over "limit_buffers", block until we get under the limit.
  784. */
  785. static void __check_watermark(struct dm_bufio_client *c,
  786. struct list_head *write_list)
  787. {
  788. unsigned long threshold_buffers, limit_buffers;
  789. __get_memory_limit(c, &threshold_buffers, &limit_buffers);
  790. while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
  791. limit_buffers) {
  792. struct dm_buffer *b = __get_unclaimed_buffer(c);
  793. if (!b)
  794. return;
  795. __free_buffer_wake(b);
  796. cond_resched();
  797. }
  798. if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
  799. __write_dirty_buffers_async(c, 1, write_list);
  800. }
  801. /*----------------------------------------------------------------
  802. * Getting a buffer
  803. *--------------------------------------------------------------*/
  804. static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
  805. enum new_flag nf, int *need_submit,
  806. struct list_head *write_list)
  807. {
  808. struct dm_buffer *b, *new_b = NULL;
  809. *need_submit = 0;
  810. b = __find(c, block);
  811. if (b)
  812. goto found_buffer;
  813. if (nf == NF_GET)
  814. return NULL;
  815. new_b = __alloc_buffer_wait(c, nf);
  816. if (!new_b)
  817. return NULL;
  818. /*
  819. * We've had a period where the mutex was unlocked, so need to
  820. * recheck the hash table.
  821. */
  822. b = __find(c, block);
  823. if (b) {
  824. __free_buffer_wake(new_b);
  825. goto found_buffer;
  826. }
  827. __check_watermark(c, write_list);
  828. b = new_b;
  829. b->hold_count = 1;
  830. b->read_error = 0;
  831. b->write_error = 0;
  832. __link_buffer(b, block, LIST_CLEAN);
  833. if (nf == NF_FRESH) {
  834. b->state = 0;
  835. return b;
  836. }
  837. b->state = 1 << B_READING;
  838. *need_submit = 1;
  839. return b;
  840. found_buffer:
  841. if (nf == NF_PREFETCH)
  842. return NULL;
  843. /*
  844. * Note: it is essential that we don't wait for the buffer to be
  845. * read if dm_bufio_get function is used. Both dm_bufio_get and
  846. * dm_bufio_prefetch can be used in the driver request routine.
  847. * If the user called both dm_bufio_prefetch and dm_bufio_get on
  848. * the same buffer, it would deadlock if we waited.
  849. */
  850. if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
  851. return NULL;
  852. b->hold_count++;
  853. __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
  854. test_bit(B_WRITING, &b->state));
  855. return b;
  856. }
  857. /*
  858. * The endio routine for reading: set the error, clear the bit and wake up
  859. * anyone waiting on the buffer.
  860. */
  861. static void read_endio(struct dm_buffer *b, blk_status_t status)
  862. {
  863. b->read_error = status;
  864. BUG_ON(!test_bit(B_READING, &b->state));
  865. smp_mb__before_atomic();
  866. clear_bit(B_READING, &b->state);
  867. smp_mb__after_atomic();
  868. wake_up_bit(&b->state, B_READING);
  869. }
  870. /*
  871. * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
  872. * functions is similar except that dm_bufio_new doesn't read the
  873. * buffer from the disk (assuming that the caller overwrites all the data
  874. * and uses dm_bufio_mark_buffer_dirty to write new data back).
  875. */
  876. static void *new_read(struct dm_bufio_client *c, sector_t block,
  877. enum new_flag nf, struct dm_buffer **bp)
  878. {
  879. int need_submit;
  880. struct dm_buffer *b;
  881. LIST_HEAD(write_list);
  882. dm_bufio_lock(c);
  883. b = __bufio_new(c, block, nf, &need_submit, &write_list);
  884. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  885. if (b && b->hold_count == 1)
  886. buffer_record_stack(b);
  887. #endif
  888. dm_bufio_unlock(c);
  889. __flush_write_list(&write_list);
  890. if (!b)
  891. return NULL;
  892. if (need_submit)
  893. submit_io(b, REQ_OP_READ, read_endio);
  894. wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
  895. if (b->read_error) {
  896. int error = blk_status_to_errno(b->read_error);
  897. dm_bufio_release(b);
  898. return ERR_PTR(error);
  899. }
  900. *bp = b;
  901. return b->data;
  902. }
  903. void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
  904. struct dm_buffer **bp)
  905. {
  906. return new_read(c, block, NF_GET, bp);
  907. }
  908. EXPORT_SYMBOL_GPL(dm_bufio_get);
  909. void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
  910. struct dm_buffer **bp)
  911. {
  912. BUG_ON(dm_bufio_in_request());
  913. return new_read(c, block, NF_READ, bp);
  914. }
  915. EXPORT_SYMBOL_GPL(dm_bufio_read);
  916. void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
  917. struct dm_buffer **bp)
  918. {
  919. BUG_ON(dm_bufio_in_request());
  920. return new_read(c, block, NF_FRESH, bp);
  921. }
  922. EXPORT_SYMBOL_GPL(dm_bufio_new);
  923. void dm_bufio_prefetch(struct dm_bufio_client *c,
  924. sector_t block, unsigned n_blocks)
  925. {
  926. struct blk_plug plug;
  927. LIST_HEAD(write_list);
  928. BUG_ON(dm_bufio_in_request());
  929. blk_start_plug(&plug);
  930. dm_bufio_lock(c);
  931. for (; n_blocks--; block++) {
  932. int need_submit;
  933. struct dm_buffer *b;
  934. b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
  935. &write_list);
  936. if (unlikely(!list_empty(&write_list))) {
  937. dm_bufio_unlock(c);
  938. blk_finish_plug(&plug);
  939. __flush_write_list(&write_list);
  940. blk_start_plug(&plug);
  941. dm_bufio_lock(c);
  942. }
  943. if (unlikely(b != NULL)) {
  944. dm_bufio_unlock(c);
  945. if (need_submit)
  946. submit_io(b, REQ_OP_READ, read_endio);
  947. dm_bufio_release(b);
  948. cond_resched();
  949. if (!n_blocks)
  950. goto flush_plug;
  951. dm_bufio_lock(c);
  952. }
  953. }
  954. dm_bufio_unlock(c);
  955. flush_plug:
  956. blk_finish_plug(&plug);
  957. }
  958. EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
  959. void dm_bufio_release(struct dm_buffer *b)
  960. {
  961. struct dm_bufio_client *c = b->c;
  962. dm_bufio_lock(c);
  963. BUG_ON(!b->hold_count);
  964. b->hold_count--;
  965. if (!b->hold_count) {
  966. wake_up(&c->free_buffer_wait);
  967. /*
  968. * If there were errors on the buffer, and the buffer is not
  969. * to be written, free the buffer. There is no point in caching
  970. * invalid buffer.
  971. */
  972. if ((b->read_error || b->write_error) &&
  973. !test_bit(B_READING, &b->state) &&
  974. !test_bit(B_WRITING, &b->state) &&
  975. !test_bit(B_DIRTY, &b->state)) {
  976. __unlink_buffer(b);
  977. __free_buffer_wake(b);
  978. }
  979. }
  980. dm_bufio_unlock(c);
  981. }
  982. EXPORT_SYMBOL_GPL(dm_bufio_release);
  983. void dm_bufio_mark_partial_buffer_dirty(struct dm_buffer *b,
  984. unsigned start, unsigned end)
  985. {
  986. struct dm_bufio_client *c = b->c;
  987. BUG_ON(start >= end);
  988. BUG_ON(end > b->c->block_size);
  989. dm_bufio_lock(c);
  990. BUG_ON(test_bit(B_READING, &b->state));
  991. if (!test_and_set_bit(B_DIRTY, &b->state)) {
  992. b->dirty_start = start;
  993. b->dirty_end = end;
  994. __relink_lru(b, LIST_DIRTY);
  995. } else {
  996. if (start < b->dirty_start)
  997. b->dirty_start = start;
  998. if (end > b->dirty_end)
  999. b->dirty_end = end;
  1000. }
  1001. dm_bufio_unlock(c);
  1002. }
  1003. EXPORT_SYMBOL_GPL(dm_bufio_mark_partial_buffer_dirty);
  1004. void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
  1005. {
  1006. dm_bufio_mark_partial_buffer_dirty(b, 0, b->c->block_size);
  1007. }
  1008. EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
  1009. void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
  1010. {
  1011. LIST_HEAD(write_list);
  1012. BUG_ON(dm_bufio_in_request());
  1013. dm_bufio_lock(c);
  1014. __write_dirty_buffers_async(c, 0, &write_list);
  1015. dm_bufio_unlock(c);
  1016. __flush_write_list(&write_list);
  1017. }
  1018. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
  1019. /*
  1020. * For performance, it is essential that the buffers are written asynchronously
  1021. * and simultaneously (so that the block layer can merge the writes) and then
  1022. * waited upon.
  1023. *
  1024. * Finally, we flush hardware disk cache.
  1025. */
  1026. int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
  1027. {
  1028. int a, f;
  1029. unsigned long buffers_processed = 0;
  1030. struct dm_buffer *b, *tmp;
  1031. LIST_HEAD(write_list);
  1032. dm_bufio_lock(c);
  1033. __write_dirty_buffers_async(c, 0, &write_list);
  1034. dm_bufio_unlock(c);
  1035. __flush_write_list(&write_list);
  1036. dm_bufio_lock(c);
  1037. again:
  1038. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
  1039. int dropped_lock = 0;
  1040. if (buffers_processed < c->n_buffers[LIST_DIRTY])
  1041. buffers_processed++;
  1042. BUG_ON(test_bit(B_READING, &b->state));
  1043. if (test_bit(B_WRITING, &b->state)) {
  1044. if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
  1045. dropped_lock = 1;
  1046. b->hold_count++;
  1047. dm_bufio_unlock(c);
  1048. wait_on_bit_io(&b->state, B_WRITING,
  1049. TASK_UNINTERRUPTIBLE);
  1050. dm_bufio_lock(c);
  1051. b->hold_count--;
  1052. } else
  1053. wait_on_bit_io(&b->state, B_WRITING,
  1054. TASK_UNINTERRUPTIBLE);
  1055. }
  1056. if (!test_bit(B_DIRTY, &b->state) &&
  1057. !test_bit(B_WRITING, &b->state))
  1058. __relink_lru(b, LIST_CLEAN);
  1059. cond_resched();
  1060. /*
  1061. * If we dropped the lock, the list is no longer consistent,
  1062. * so we must restart the search.
  1063. *
  1064. * In the most common case, the buffer just processed is
  1065. * relinked to the clean list, so we won't loop scanning the
  1066. * same buffer again and again.
  1067. *
  1068. * This may livelock if there is another thread simultaneously
  1069. * dirtying buffers, so we count the number of buffers walked
  1070. * and if it exceeds the total number of buffers, it means that
  1071. * someone is doing some writes simultaneously with us. In
  1072. * this case, stop, dropping the lock.
  1073. */
  1074. if (dropped_lock)
  1075. goto again;
  1076. }
  1077. wake_up(&c->free_buffer_wait);
  1078. dm_bufio_unlock(c);
  1079. a = xchg(&c->async_write_error, 0);
  1080. f = dm_bufio_issue_flush(c);
  1081. if (a)
  1082. return a;
  1083. return f;
  1084. }
  1085. EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
  1086. /*
  1087. * Use dm-io to send and empty barrier flush the device.
  1088. */
  1089. int dm_bufio_issue_flush(struct dm_bufio_client *c)
  1090. {
  1091. struct dm_io_request io_req = {
  1092. .bi_op = REQ_OP_WRITE,
  1093. .bi_op_flags = REQ_PREFLUSH | REQ_SYNC,
  1094. .mem.type = DM_IO_KMEM,
  1095. .mem.ptr.addr = NULL,
  1096. .client = c->dm_io,
  1097. };
  1098. struct dm_io_region io_reg = {
  1099. .bdev = c->bdev,
  1100. .sector = 0,
  1101. .count = 0,
  1102. };
  1103. BUG_ON(dm_bufio_in_request());
  1104. return dm_io(&io_req, 1, &io_reg, NULL);
  1105. }
  1106. EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
  1107. /*
  1108. * We first delete any other buffer that may be at that new location.
  1109. *
  1110. * Then, we write the buffer to the original location if it was dirty.
  1111. *
  1112. * Then, if we are the only one who is holding the buffer, relink the buffer
  1113. * in the hash queue for the new location.
  1114. *
  1115. * If there was someone else holding the buffer, we write it to the new
  1116. * location but not relink it, because that other user needs to have the buffer
  1117. * at the same place.
  1118. */
  1119. void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
  1120. {
  1121. struct dm_bufio_client *c = b->c;
  1122. struct dm_buffer *new;
  1123. BUG_ON(dm_bufio_in_request());
  1124. dm_bufio_lock(c);
  1125. retry:
  1126. new = __find(c, new_block);
  1127. if (new) {
  1128. if (new->hold_count) {
  1129. __wait_for_free_buffer(c);
  1130. goto retry;
  1131. }
  1132. /*
  1133. * FIXME: Is there any point waiting for a write that's going
  1134. * to be overwritten in a bit?
  1135. */
  1136. __make_buffer_clean(new);
  1137. __unlink_buffer(new);
  1138. __free_buffer_wake(new);
  1139. }
  1140. BUG_ON(!b->hold_count);
  1141. BUG_ON(test_bit(B_READING, &b->state));
  1142. __write_dirty_buffer(b, NULL);
  1143. if (b->hold_count == 1) {
  1144. wait_on_bit_io(&b->state, B_WRITING,
  1145. TASK_UNINTERRUPTIBLE);
  1146. set_bit(B_DIRTY, &b->state);
  1147. b->dirty_start = 0;
  1148. b->dirty_end = c->block_size;
  1149. __unlink_buffer(b);
  1150. __link_buffer(b, new_block, LIST_DIRTY);
  1151. } else {
  1152. sector_t old_block;
  1153. wait_on_bit_lock_io(&b->state, B_WRITING,
  1154. TASK_UNINTERRUPTIBLE);
  1155. /*
  1156. * Relink buffer to "new_block" so that write_callback
  1157. * sees "new_block" as a block number.
  1158. * After the write, link the buffer back to old_block.
  1159. * All this must be done in bufio lock, so that block number
  1160. * change isn't visible to other threads.
  1161. */
  1162. old_block = b->block;
  1163. __unlink_buffer(b);
  1164. __link_buffer(b, new_block, b->list_mode);
  1165. submit_io(b, REQ_OP_WRITE, write_endio);
  1166. wait_on_bit_io(&b->state, B_WRITING,
  1167. TASK_UNINTERRUPTIBLE);
  1168. __unlink_buffer(b);
  1169. __link_buffer(b, old_block, b->list_mode);
  1170. }
  1171. dm_bufio_unlock(c);
  1172. dm_bufio_release(b);
  1173. }
  1174. EXPORT_SYMBOL_GPL(dm_bufio_release_move);
  1175. /*
  1176. * Free the given buffer.
  1177. *
  1178. * This is just a hint, if the buffer is in use or dirty, this function
  1179. * does nothing.
  1180. */
  1181. void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
  1182. {
  1183. struct dm_buffer *b;
  1184. dm_bufio_lock(c);
  1185. b = __find(c, block);
  1186. if (b && likely(!b->hold_count) && likely(!b->state)) {
  1187. __unlink_buffer(b);
  1188. __free_buffer_wake(b);
  1189. }
  1190. dm_bufio_unlock(c);
  1191. }
  1192. EXPORT_SYMBOL_GPL(dm_bufio_forget);
  1193. void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
  1194. {
  1195. c->minimum_buffers = n;
  1196. }
  1197. EXPORT_SYMBOL_GPL(dm_bufio_set_minimum_buffers);
  1198. unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
  1199. {
  1200. return c->block_size;
  1201. }
  1202. EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
  1203. sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
  1204. {
  1205. sector_t s = i_size_read(c->bdev->bd_inode) >> SECTOR_SHIFT;
  1206. if (likely(c->sectors_per_block_bits >= 0))
  1207. s >>= c->sectors_per_block_bits;
  1208. else
  1209. sector_div(s, c->block_size >> SECTOR_SHIFT);
  1210. return s;
  1211. }
  1212. EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
  1213. sector_t dm_bufio_get_block_number(struct dm_buffer *b)
  1214. {
  1215. return b->block;
  1216. }
  1217. EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
  1218. void *dm_bufio_get_block_data(struct dm_buffer *b)
  1219. {
  1220. return b->data;
  1221. }
  1222. EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
  1223. void *dm_bufio_get_aux_data(struct dm_buffer *b)
  1224. {
  1225. return b + 1;
  1226. }
  1227. EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
  1228. struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
  1229. {
  1230. return b->c;
  1231. }
  1232. EXPORT_SYMBOL_GPL(dm_bufio_get_client);
  1233. static void drop_buffers(struct dm_bufio_client *c)
  1234. {
  1235. struct dm_buffer *b;
  1236. int i;
  1237. bool warned = false;
  1238. BUG_ON(dm_bufio_in_request());
  1239. /*
  1240. * An optimization so that the buffers are not written one-by-one.
  1241. */
  1242. dm_bufio_write_dirty_buffers_async(c);
  1243. dm_bufio_lock(c);
  1244. while ((b = __get_unclaimed_buffer(c)))
  1245. __free_buffer_wake(b);
  1246. for (i = 0; i < LIST_SIZE; i++)
  1247. list_for_each_entry(b, &c->lru[i], lru_list) {
  1248. WARN_ON(!warned);
  1249. warned = true;
  1250. DMERR("leaked buffer %llx, hold count %u, list %d",
  1251. (unsigned long long)b->block, b->hold_count, i);
  1252. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  1253. print_stack_trace(&b->stack_trace, 1);
  1254. b->hold_count = 0; /* mark unclaimed to avoid BUG_ON below */
  1255. #endif
  1256. }
  1257. #ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
  1258. while ((b = __get_unclaimed_buffer(c)))
  1259. __free_buffer_wake(b);
  1260. #endif
  1261. for (i = 0; i < LIST_SIZE; i++)
  1262. BUG_ON(!list_empty(&c->lru[i]));
  1263. dm_bufio_unlock(c);
  1264. }
  1265. /*
  1266. * We may not be able to evict this buffer if IO pending or the client
  1267. * is still using it. Caller is expected to know buffer is too old.
  1268. *
  1269. * And if GFP_NOFS is used, we must not do any I/O because we hold
  1270. * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
  1271. * rerouted to different bufio client.
  1272. */
  1273. static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
  1274. {
  1275. if (!(gfp & __GFP_FS)) {
  1276. if (test_bit(B_READING, &b->state) ||
  1277. test_bit(B_WRITING, &b->state) ||
  1278. test_bit(B_DIRTY, &b->state))
  1279. return false;
  1280. }
  1281. if (b->hold_count)
  1282. return false;
  1283. __make_buffer_clean(b);
  1284. __unlink_buffer(b);
  1285. __free_buffer_wake(b);
  1286. return true;
  1287. }
  1288. static unsigned long get_retain_buffers(struct dm_bufio_client *c)
  1289. {
  1290. unsigned long retain_bytes = READ_ONCE(dm_bufio_retain_bytes);
  1291. if (likely(c->sectors_per_block_bits >= 0))
  1292. retain_bytes >>= c->sectors_per_block_bits + SECTOR_SHIFT;
  1293. else
  1294. retain_bytes /= c->block_size;
  1295. return retain_bytes;
  1296. }
  1297. static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
  1298. gfp_t gfp_mask)
  1299. {
  1300. int l;
  1301. struct dm_buffer *b, *tmp;
  1302. unsigned long freed = 0;
  1303. unsigned long count = c->n_buffers[LIST_CLEAN] +
  1304. c->n_buffers[LIST_DIRTY];
  1305. unsigned long retain_target = get_retain_buffers(c);
  1306. for (l = 0; l < LIST_SIZE; l++) {
  1307. list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
  1308. if (__try_evict_buffer(b, gfp_mask))
  1309. freed++;
  1310. if (!--nr_to_scan || ((count - freed) <= retain_target))
  1311. return freed;
  1312. cond_resched();
  1313. }
  1314. }
  1315. return freed;
  1316. }
  1317. static unsigned long
  1318. dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  1319. {
  1320. struct dm_bufio_client *c;
  1321. unsigned long freed;
  1322. c = container_of(shrink, struct dm_bufio_client, shrinker);
  1323. if (sc->gfp_mask & __GFP_FS)
  1324. dm_bufio_lock(c);
  1325. else if (!dm_bufio_trylock(c))
  1326. return SHRINK_STOP;
  1327. freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
  1328. dm_bufio_unlock(c);
  1329. return freed;
  1330. }
  1331. static unsigned long
  1332. dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  1333. {
  1334. struct dm_bufio_client *c = container_of(shrink, struct dm_bufio_client, shrinker);
  1335. unsigned long count = READ_ONCE(c->n_buffers[LIST_CLEAN]) +
  1336. READ_ONCE(c->n_buffers[LIST_DIRTY]);
  1337. unsigned long retain_target = get_retain_buffers(c);
  1338. return (count < retain_target) ? 0 : (count - retain_target);
  1339. }
  1340. /*
  1341. * Create the buffering interface
  1342. */
  1343. struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
  1344. unsigned reserved_buffers, unsigned aux_size,
  1345. void (*alloc_callback)(struct dm_buffer *),
  1346. void (*write_callback)(struct dm_buffer *))
  1347. {
  1348. int r;
  1349. struct dm_bufio_client *c;
  1350. unsigned i;
  1351. char slab_name[27];
  1352. if (!block_size || block_size & ((1 << SECTOR_SHIFT) - 1)) {
  1353. DMERR("%s: block size not specified or is not multiple of 512b", __func__);
  1354. r = -EINVAL;
  1355. goto bad_client;
  1356. }
  1357. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1358. if (!c) {
  1359. r = -ENOMEM;
  1360. goto bad_client;
  1361. }
  1362. c->buffer_tree = RB_ROOT;
  1363. c->bdev = bdev;
  1364. c->block_size = block_size;
  1365. if (is_power_of_2(block_size))
  1366. c->sectors_per_block_bits = __ffs(block_size) - SECTOR_SHIFT;
  1367. else
  1368. c->sectors_per_block_bits = -1;
  1369. c->alloc_callback = alloc_callback;
  1370. c->write_callback = write_callback;
  1371. for (i = 0; i < LIST_SIZE; i++) {
  1372. INIT_LIST_HEAD(&c->lru[i]);
  1373. c->n_buffers[i] = 0;
  1374. }
  1375. mutex_init(&c->lock);
  1376. INIT_LIST_HEAD(&c->reserved_buffers);
  1377. c->need_reserved_buffers = reserved_buffers;
  1378. dm_bufio_set_minimum_buffers(c, DM_BUFIO_MIN_BUFFERS);
  1379. init_waitqueue_head(&c->free_buffer_wait);
  1380. c->async_write_error = 0;
  1381. c->dm_io = dm_io_client_create();
  1382. if (IS_ERR(c->dm_io)) {
  1383. r = PTR_ERR(c->dm_io);
  1384. goto bad_dm_io;
  1385. }
  1386. if (block_size <= KMALLOC_MAX_SIZE &&
  1387. (block_size < PAGE_SIZE || !is_power_of_2(block_size))) {
  1388. unsigned align = min(1U << __ffs(block_size), (unsigned)PAGE_SIZE);
  1389. snprintf(slab_name, sizeof slab_name, "dm_bufio_cache-%u", block_size);
  1390. c->slab_cache = kmem_cache_create(slab_name, block_size, align,
  1391. SLAB_RECLAIM_ACCOUNT, NULL);
  1392. if (!c->slab_cache) {
  1393. r = -ENOMEM;
  1394. goto bad;
  1395. }
  1396. }
  1397. if (aux_size)
  1398. snprintf(slab_name, sizeof slab_name, "dm_bufio_buffer-%u", aux_size);
  1399. else
  1400. snprintf(slab_name, sizeof slab_name, "dm_bufio_buffer");
  1401. c->slab_buffer = kmem_cache_create(slab_name, sizeof(struct dm_buffer) + aux_size,
  1402. 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1403. if (!c->slab_buffer) {
  1404. r = -ENOMEM;
  1405. goto bad;
  1406. }
  1407. while (c->need_reserved_buffers) {
  1408. struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
  1409. if (!b) {
  1410. r = -ENOMEM;
  1411. goto bad;
  1412. }
  1413. __free_buffer_wake(b);
  1414. }
  1415. c->shrinker.count_objects = dm_bufio_shrink_count;
  1416. c->shrinker.scan_objects = dm_bufio_shrink_scan;
  1417. c->shrinker.seeks = 1;
  1418. c->shrinker.batch = 0;
  1419. r = register_shrinker(&c->shrinker);
  1420. if (r)
  1421. goto bad;
  1422. mutex_lock(&dm_bufio_clients_lock);
  1423. dm_bufio_client_count++;
  1424. list_add(&c->client_list, &dm_bufio_all_clients);
  1425. __cache_size_refresh();
  1426. mutex_unlock(&dm_bufio_clients_lock);
  1427. return c;
  1428. bad:
  1429. while (!list_empty(&c->reserved_buffers)) {
  1430. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1431. struct dm_buffer, lru_list);
  1432. list_del(&b->lru_list);
  1433. free_buffer(b);
  1434. }
  1435. kmem_cache_destroy(c->slab_cache);
  1436. kmem_cache_destroy(c->slab_buffer);
  1437. dm_io_client_destroy(c->dm_io);
  1438. bad_dm_io:
  1439. mutex_destroy(&c->lock);
  1440. kfree(c);
  1441. bad_client:
  1442. return ERR_PTR(r);
  1443. }
  1444. EXPORT_SYMBOL_GPL(dm_bufio_client_create);
  1445. /*
  1446. * Free the buffering interface.
  1447. * It is required that there are no references on any buffers.
  1448. */
  1449. void dm_bufio_client_destroy(struct dm_bufio_client *c)
  1450. {
  1451. unsigned i;
  1452. drop_buffers(c);
  1453. unregister_shrinker(&c->shrinker);
  1454. mutex_lock(&dm_bufio_clients_lock);
  1455. list_del(&c->client_list);
  1456. dm_bufio_client_count--;
  1457. __cache_size_refresh();
  1458. mutex_unlock(&dm_bufio_clients_lock);
  1459. BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
  1460. BUG_ON(c->need_reserved_buffers);
  1461. while (!list_empty(&c->reserved_buffers)) {
  1462. struct dm_buffer *b = list_entry(c->reserved_buffers.next,
  1463. struct dm_buffer, lru_list);
  1464. list_del(&b->lru_list);
  1465. free_buffer(b);
  1466. }
  1467. for (i = 0; i < LIST_SIZE; i++)
  1468. if (c->n_buffers[i])
  1469. DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
  1470. for (i = 0; i < LIST_SIZE; i++)
  1471. BUG_ON(c->n_buffers[i]);
  1472. kmem_cache_destroy(c->slab_cache);
  1473. kmem_cache_destroy(c->slab_buffer);
  1474. dm_io_client_destroy(c->dm_io);
  1475. mutex_destroy(&c->lock);
  1476. kfree(c);
  1477. }
  1478. EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
  1479. void dm_bufio_set_sector_offset(struct dm_bufio_client *c, sector_t start)
  1480. {
  1481. c->start = start;
  1482. }
  1483. EXPORT_SYMBOL_GPL(dm_bufio_set_sector_offset);
  1484. static unsigned get_max_age_hz(void)
  1485. {
  1486. unsigned max_age = READ_ONCE(dm_bufio_max_age);
  1487. if (max_age > UINT_MAX / HZ)
  1488. max_age = UINT_MAX / HZ;
  1489. return max_age * HZ;
  1490. }
  1491. static bool older_than(struct dm_buffer *b, unsigned long age_hz)
  1492. {
  1493. return time_after_eq(jiffies, b->last_accessed + age_hz);
  1494. }
  1495. static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
  1496. {
  1497. struct dm_buffer *b, *tmp;
  1498. unsigned long retain_target = get_retain_buffers(c);
  1499. unsigned long count;
  1500. LIST_HEAD(write_list);
  1501. dm_bufio_lock(c);
  1502. __check_watermark(c, &write_list);
  1503. if (unlikely(!list_empty(&write_list))) {
  1504. dm_bufio_unlock(c);
  1505. __flush_write_list(&write_list);
  1506. dm_bufio_lock(c);
  1507. }
  1508. count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
  1509. list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
  1510. if (count <= retain_target)
  1511. break;
  1512. if (!older_than(b, age_hz))
  1513. break;
  1514. if (__try_evict_buffer(b, 0))
  1515. count--;
  1516. cond_resched();
  1517. }
  1518. dm_bufio_unlock(c);
  1519. }
  1520. static void cleanup_old_buffers(void)
  1521. {
  1522. unsigned long max_age_hz = get_max_age_hz();
  1523. struct dm_bufio_client *c;
  1524. mutex_lock(&dm_bufio_clients_lock);
  1525. __cache_size_refresh();
  1526. list_for_each_entry(c, &dm_bufio_all_clients, client_list)
  1527. __evict_old_buffers(c, max_age_hz);
  1528. mutex_unlock(&dm_bufio_clients_lock);
  1529. }
  1530. static struct workqueue_struct *dm_bufio_wq;
  1531. static struct delayed_work dm_bufio_work;
  1532. static void work_fn(struct work_struct *w)
  1533. {
  1534. cleanup_old_buffers();
  1535. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1536. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1537. }
  1538. /*----------------------------------------------------------------
  1539. * Module setup
  1540. *--------------------------------------------------------------*/
  1541. /*
  1542. * This is called only once for the whole dm_bufio module.
  1543. * It initializes memory limit.
  1544. */
  1545. static int __init dm_bufio_init(void)
  1546. {
  1547. __u64 mem;
  1548. dm_bufio_allocated_kmem_cache = 0;
  1549. dm_bufio_allocated_get_free_pages = 0;
  1550. dm_bufio_allocated_vmalloc = 0;
  1551. dm_bufio_current_allocated = 0;
  1552. mem = (__u64)mult_frac(totalram_pages - totalhigh_pages,
  1553. DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT;
  1554. if (mem > ULONG_MAX)
  1555. mem = ULONG_MAX;
  1556. #ifdef CONFIG_MMU
  1557. if (mem > mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100))
  1558. mem = mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100);
  1559. #endif
  1560. dm_bufio_default_cache_size = mem;
  1561. mutex_lock(&dm_bufio_clients_lock);
  1562. __cache_size_refresh();
  1563. mutex_unlock(&dm_bufio_clients_lock);
  1564. dm_bufio_wq = alloc_workqueue("dm_bufio_cache", WQ_MEM_RECLAIM, 0);
  1565. if (!dm_bufio_wq)
  1566. return -ENOMEM;
  1567. INIT_DELAYED_WORK(&dm_bufio_work, work_fn);
  1568. queue_delayed_work(dm_bufio_wq, &dm_bufio_work,
  1569. DM_BUFIO_WORK_TIMER_SECS * HZ);
  1570. return 0;
  1571. }
  1572. /*
  1573. * This is called once when unloading the dm_bufio module.
  1574. */
  1575. static void __exit dm_bufio_exit(void)
  1576. {
  1577. int bug = 0;
  1578. cancel_delayed_work_sync(&dm_bufio_work);
  1579. destroy_workqueue(dm_bufio_wq);
  1580. if (dm_bufio_client_count) {
  1581. DMCRIT("%s: dm_bufio_client_count leaked: %d",
  1582. __func__, dm_bufio_client_count);
  1583. bug = 1;
  1584. }
  1585. if (dm_bufio_current_allocated) {
  1586. DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
  1587. __func__, dm_bufio_current_allocated);
  1588. bug = 1;
  1589. }
  1590. if (dm_bufio_allocated_get_free_pages) {
  1591. DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
  1592. __func__, dm_bufio_allocated_get_free_pages);
  1593. bug = 1;
  1594. }
  1595. if (dm_bufio_allocated_vmalloc) {
  1596. DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
  1597. __func__, dm_bufio_allocated_vmalloc);
  1598. bug = 1;
  1599. }
  1600. BUG_ON(bug);
  1601. }
  1602. module_init(dm_bufio_init)
  1603. module_exit(dm_bufio_exit)
  1604. module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
  1605. MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
  1606. module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
  1607. MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
  1608. module_param_named(retain_bytes, dm_bufio_retain_bytes, ulong, S_IRUGO | S_IWUSR);
  1609. MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
  1610. module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
  1611. MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
  1612. module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
  1613. MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
  1614. module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
  1615. MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
  1616. module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
  1617. MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
  1618. module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
  1619. MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
  1620. MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
  1621. MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
  1622. MODULE_LICENSE("GPL");