dm-bufio.c 47 KB

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