dm-bufio.c 50 KB

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