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