dm-bufio.c 48 KB

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