blob: 48bb5a879e6f40fedcb35a44502cd2661c6936ac [file] [log] [blame]
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001/*
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
9#include "dm-bufio.h"
10
11#include <linux/device-mapper.h>
12#include <linux/dm-io.h>
13#include <linux/slab.h>
Asaf Vertzf4953392015-01-06 15:44:15 +020014#include <linux/jiffies.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000015#include <linux/vmalloc.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000016#include <linux/shrinker.h>
Stephen Rothwell6f662632011-11-01 18:30:49 +110017#include <linux/module.h>
Joe Thornber4e420c42014-10-06 13:48:51 +010018#include <linux/rbtree.h>
Mikulas Patocka86bad0c2015-11-23 19:20:06 -050019#include <linux/stacktrace.h>
Mikulas Patocka95d402f2011-10-31 20:19:09 +000020
21#define DM_MSG_PREFIX "bufio"
22
23/*
24 * Memory management policy:
25 * Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory
26 * or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower).
27 * Always allocate at least DM_BUFIO_MIN_BUFFERS buffers.
28 * Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT
29 * dirty buffers.
30 */
31#define DM_BUFIO_MIN_BUFFERS 8
32
33#define DM_BUFIO_MEMORY_PERCENT 2
34#define DM_BUFIO_VMALLOC_PERCENT 25
Mikulas Patockab9fd9eb2019-09-12 10:44:47 +020035#define DM_BUFIO_WRITEBACK_RATIO 3
Mikulas Patocka7dc56e32019-09-12 12:07:23 -040036#define DM_BUFIO_LOW_WATERMARK_RATIO 16
Mikulas Patocka95d402f2011-10-31 20:19:09 +000037
38/*
39 * Check buffer ages in this interval (seconds)
40 */
Joe Thornber33096a72014-10-09 11:10:25 +010041#define DM_BUFIO_WORK_TIMER_SECS 30
Mikulas Patocka95d402f2011-10-31 20:19:09 +000042
43/*
44 * Free buffers when they are older than this (seconds)
45 */
Joe Thornber33096a72014-10-09 11:10:25 +010046#define DM_BUFIO_DEFAULT_AGE_SECS 300
47
48/*
49 * The nr of bytes of cached data to keep around.
50 */
51#define DM_BUFIO_DEFAULT_RETAIN_BYTES (256 * 1024)
Mikulas Patocka95d402f2011-10-31 20:19:09 +000052
53/*
54 * The number of bvec entries that are embedded directly in the buffer.
55 * If the chunk size is larger, dm-io is used to do the io.
56 */
57#define DM_BUFIO_INLINE_VECS 16
58
59/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +000060 * Don't try to use kmem_cache_alloc for blocks larger than this.
61 * For explanation, see alloc_buffer_data below.
62 */
63#define DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT (PAGE_SIZE >> 1)
64#define DM_BUFIO_BLOCK_SIZE_GFP_LIMIT (PAGE_SIZE << (MAX_ORDER - 1))
65
66/*
67 * dm_buffer->list_mode
68 */
69#define LIST_CLEAN 0
70#define LIST_DIRTY 1
71#define LIST_SIZE 2
72
73/*
74 * Linking of buffers:
75 * All buffers are linked to cache_hash with their hash_list field.
76 *
77 * Clean buffers that are not being written (B_WRITING not set)
78 * are linked to lru[LIST_CLEAN] with their lru_list field.
79 *
80 * Dirty and clean buffers that are being written are linked to
81 * lru[LIST_DIRTY] with their lru_list field. When the write
82 * finishes, the buffer cannot be relinked immediately (because we
83 * are in an interrupt context and relinking requires process
84 * context), so some clean-not-writing buffers can be held on
85 * dirty_lru too. They are later added to lru in the process
86 * context.
87 */
88struct dm_bufio_client {
89 struct mutex lock;
90
91 struct list_head lru[LIST_SIZE];
92 unsigned long n_buffers[LIST_SIZE];
93
94 struct block_device *bdev;
95 unsigned block_size;
96 unsigned char sectors_per_block_bits;
97 unsigned char pages_per_block_bits;
98 unsigned char blocks_per_page_bits;
99 unsigned aux_size;
100 void (*alloc_callback)(struct dm_buffer *);
101 void (*write_callback)(struct dm_buffer *);
102
103 struct dm_io_client *dm_io;
104
105 struct list_head reserved_buffers;
106 unsigned need_reserved_buffers;
107
Mikulas Patocka55b082e2014-01-13 19:13:05 -0500108 unsigned minimum_buffers;
109
Joe Thornber4e420c42014-10-06 13:48:51 +0100110 struct rb_root buffer_tree;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000111 wait_queue_head_t free_buffer_wait;
112
113 int async_write_error;
114
115 struct list_head client_list;
116 struct shrinker shrinker;
117};
118
119/*
120 * Buffer state bits.
121 */
122#define B_READING 0
123#define B_WRITING 1
124#define B_DIRTY 2
125
126/*
127 * Describes how the block was allocated:
128 * kmem_cache_alloc(), __get_free_pages() or vmalloc().
129 * See the comment at alloc_buffer_data.
130 */
131enum data_mode {
132 DATA_MODE_SLAB = 0,
133 DATA_MODE_GET_FREE_PAGES = 1,
134 DATA_MODE_VMALLOC = 2,
135 DATA_MODE_LIMIT = 3
136};
137
138struct dm_buffer {
Joe Thornber4e420c42014-10-06 13:48:51 +0100139 struct rb_node node;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000140 struct list_head lru_list;
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200141 struct list_head global_list;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000142 sector_t block;
143 void *data;
144 enum data_mode data_mode;
145 unsigned char list_mode; /* LIST_* */
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400146 unsigned accessed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000147 unsigned hold_count;
148 int read_error;
149 int write_error;
150 unsigned long state;
151 unsigned long last_accessed;
152 struct dm_bufio_client *c;
Mikulas Patocka24809452013-07-10 23:41:18 +0100153 struct list_head write_list;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000154 struct bio bio;
155 struct bio_vec bio_vec[DM_BUFIO_INLINE_VECS];
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500156#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
157#define MAX_STACK 10
158 struct stack_trace stack_trace;
159 unsigned long stack_entries[MAX_STACK];
160#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000161};
162
163/*----------------------------------------------------------------*/
164
165static struct kmem_cache *dm_bufio_caches[PAGE_SHIFT - SECTOR_SHIFT];
166static char *dm_bufio_cache_names[PAGE_SHIFT - SECTOR_SHIFT];
167
168static inline int dm_bufio_cache_index(struct dm_bufio_client *c)
169{
170 unsigned ret = c->blocks_per_page_bits - 1;
171
172 BUG_ON(ret >= ARRAY_SIZE(dm_bufio_caches));
173
174 return ret;
175}
176
177#define DM_BUFIO_CACHE(c) (dm_bufio_caches[dm_bufio_cache_index(c)])
178#define DM_BUFIO_CACHE_NAME(c) (dm_bufio_cache_names[dm_bufio_cache_index(c)])
179
180#define dm_bufio_in_request() (!!current->bio_list)
181
182static void dm_bufio_lock(struct dm_bufio_client *c)
183{
184 mutex_lock_nested(&c->lock, dm_bufio_in_request());
185}
186
187static int dm_bufio_trylock(struct dm_bufio_client *c)
188{
189 return mutex_trylock(&c->lock);
190}
191
192static void dm_bufio_unlock(struct dm_bufio_client *c)
193{
194 mutex_unlock(&c->lock);
195}
196
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000197/*----------------------------------------------------------------*/
198
199/*
200 * Default cache size: available memory divided by the ratio.
201 */
202static unsigned long dm_bufio_default_cache_size;
203
204/*
205 * Total cache size set by the user.
206 */
207static unsigned long dm_bufio_cache_size;
208
209/*
210 * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change
211 * at any time. If it disagrees, the user has changed cache size.
212 */
213static unsigned long dm_bufio_cache_size_latch;
214
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200215static DEFINE_SPINLOCK(global_spinlock);
216
217static LIST_HEAD(global_queue);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000218
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400219static unsigned long global_num = 0;
220
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000221/*
222 * Buffers are freed after this timeout
223 */
224static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS;
Mikulas Patockaeeaf1332017-04-30 17:32:28 -0400225static unsigned long dm_bufio_retain_bytes = DM_BUFIO_DEFAULT_RETAIN_BYTES;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000226
227static unsigned long dm_bufio_peak_allocated;
228static unsigned long dm_bufio_allocated_kmem_cache;
229static unsigned long dm_bufio_allocated_get_free_pages;
230static unsigned long dm_bufio_allocated_vmalloc;
231static unsigned long dm_bufio_current_allocated;
232
233/*----------------------------------------------------------------*/
234
235/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000236 * The current number of clients.
237 */
238static int dm_bufio_client_count;
239
240/*
241 * The list of all clients.
242 */
243static LIST_HEAD(dm_bufio_all_clients);
244
245/*
Mikulas Patockab9fd9eb2019-09-12 10:44:47 +0200246 * This mutex protects dm_bufio_cache_size_latch and dm_bufio_client_count
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000247 */
248static DEFINE_MUTEX(dm_bufio_clients_lock);
249
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400250static struct workqueue_struct *dm_bufio_wq;
251static struct delayed_work dm_bufio_cleanup_old_work;
252static struct work_struct dm_bufio_replacement_work;
253
254
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500255#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
256static void buffer_record_stack(struct dm_buffer *b)
257{
258 b->stack_trace.nr_entries = 0;
259 b->stack_trace.max_entries = MAX_STACK;
260 b->stack_trace.entries = b->stack_entries;
261 b->stack_trace.skip = 2;
262 save_stack_trace(&b->stack_trace);
263}
264#endif
265
Joe Thornber4e420c42014-10-06 13:48:51 +0100266/*----------------------------------------------------------------
267 * A red/black tree acts as an index for all the buffers.
268 *--------------------------------------------------------------*/
269static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block)
270{
271 struct rb_node *n = c->buffer_tree.rb_node;
272 struct dm_buffer *b;
273
274 while (n) {
275 b = container_of(n, struct dm_buffer, node);
276
277 if (b->block == block)
278 return b;
279
280 n = (b->block < block) ? n->rb_left : n->rb_right;
281 }
282
283 return NULL;
284}
285
286static void __insert(struct dm_bufio_client *c, struct dm_buffer *b)
287{
288 struct rb_node **new = &c->buffer_tree.rb_node, *parent = NULL;
289 struct dm_buffer *found;
290
291 while (*new) {
292 found = container_of(*new, struct dm_buffer, node);
293
294 if (found->block == b->block) {
295 BUG_ON(found != b);
296 return;
297 }
298
299 parent = *new;
300 new = (found->block < b->block) ?
301 &((*new)->rb_left) : &((*new)->rb_right);
302 }
303
304 rb_link_node(&b->node, parent, new);
305 rb_insert_color(&b->node, &c->buffer_tree);
306}
307
308static void __remove(struct dm_bufio_client *c, struct dm_buffer *b)
309{
310 rb_erase(&b->node, &c->buffer_tree);
311}
312
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000313/*----------------------------------------------------------------*/
314
Mikulas Patocka76ad8852019-09-12 10:44:45 +0200315static void adjust_total_allocated(struct dm_buffer *b, bool unlink)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000316{
Mikulas Patocka76ad8852019-09-12 10:44:45 +0200317 enum data_mode data_mode;
318 long diff;
319
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000320 static unsigned long * const class_ptr[DATA_MODE_LIMIT] = {
321 &dm_bufio_allocated_kmem_cache,
322 &dm_bufio_allocated_get_free_pages,
323 &dm_bufio_allocated_vmalloc,
324 };
325
Mikulas Patocka76ad8852019-09-12 10:44:45 +0200326 data_mode = b->data_mode;
327 diff = (long)b->c->block_size;
328 if (unlink)
329 diff = -diff;
330
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200331 spin_lock(&global_spinlock);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000332
333 *class_ptr[data_mode] += diff;
334
335 dm_bufio_current_allocated += diff;
336
337 if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
338 dm_bufio_peak_allocated = dm_bufio_current_allocated;
339
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400340 b->accessed = 1;
341
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200342 if (!unlink) {
343 list_add(&b->global_list, &global_queue);
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400344 global_num++;
345 if (dm_bufio_current_allocated > dm_bufio_cache_size)
346 queue_work(dm_bufio_wq, &dm_bufio_replacement_work);
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200347 } else {
348 list_del(&b->global_list);
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400349 global_num--;
Mikulas Patocka6d453eb2019-09-12 10:44:46 +0200350 }
351
352 spin_unlock(&global_spinlock);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000353}
354
355/*
356 * Change the number of clients and recalculate per-client limit.
357 */
358static void __cache_size_refresh(void)
359{
360 BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock));
361 BUG_ON(dm_bufio_client_count < 0);
362
Mikulas Patockafe5fe902012-10-12 16:59:46 +0100363 dm_bufio_cache_size_latch = ACCESS_ONCE(dm_bufio_cache_size);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000364
365 /*
366 * Use default if set to 0 and report the actual cache size used.
367 */
368 if (!dm_bufio_cache_size_latch) {
369 (void)cmpxchg(&dm_bufio_cache_size, 0,
370 dm_bufio_default_cache_size);
371 dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
372 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000373}
374
375/*
376 * Allocating buffer data.
377 *
378 * Small buffers are allocated with kmem_cache, to use space optimally.
379 *
380 * For large buffers, we choose between get_free_pages and vmalloc.
381 * Each has advantages and disadvantages.
382 *
383 * __get_free_pages can randomly fail if the memory is fragmented.
384 * __vmalloc won't randomly fail, but vmalloc space is limited (it may be
385 * as low as 128M) so using it for caching is not appropriate.
386 *
387 * If the allocation may fail we use __get_free_pages. Memory fragmentation
388 * won't have a fatal effect here, but it just causes flushes of some other
389 * buffers and more I/O will be performed. Don't use __get_free_pages if it
390 * always fails (i.e. order >= MAX_ORDER).
391 *
392 * If the allocation shouldn't fail we use __vmalloc. This is only for the
393 * initial reserve allocation, so there's no risk of wasting all vmalloc
394 * space.
395 */
396static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
397 enum data_mode *data_mode)
398{
399 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT) {
400 *data_mode = DATA_MODE_SLAB;
401 return kmem_cache_alloc(DM_BUFIO_CACHE(c), gfp_mask);
402 }
403
404 if (c->block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
405 gfp_mask & __GFP_NORETRY) {
406 *data_mode = DATA_MODE_GET_FREE_PAGES;
407 return (void *)__get_free_pages(gfp_mask,
408 c->pages_per_block_bits);
409 }
410
411 *data_mode = DATA_MODE_VMALLOC;
Mikulas Patocka502624b2013-05-10 14:37:15 +0100412
413 /*
414 * __vmalloc allocates the data pages and auxiliary structures with
415 * gfp_flags that were specified, but pagetables are always allocated
416 * with GFP_KERNEL, no matter what was specified as gfp_mask.
417 *
418 * Consequently, we must set per-process flag PF_MEMALLOC_NOIO so that
419 * all allocations done by this process (including pagetables) are done
420 * as if GFP_NOIO was specified.
421 */
Arnd Bergmanne1fba172018-02-22 16:56:16 +0100422 if (gfp_mask & __GFP_NORETRY) {
423 unsigned noio_flag = memalloc_noio_save();
424 void *ptr = __vmalloc(c->block_size, gfp_mask | __GFP_HIGHMEM,
425 PAGE_KERNEL);
Mikulas Patocka502624b2013-05-10 14:37:15 +0100426
Mikulas Patocka502624b2013-05-10 14:37:15 +0100427 memalloc_noio_restore(noio_flag);
Arnd Bergmanne1fba172018-02-22 16:56:16 +0100428 return ptr;
429 }
Mikulas Patocka502624b2013-05-10 14:37:15 +0100430
Arnd Bergmanne1fba172018-02-22 16:56:16 +0100431 return __vmalloc(c->block_size, gfp_mask | __GFP_HIGHMEM, PAGE_KERNEL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000432}
433
434/*
435 * Free buffer's data.
436 */
437static void free_buffer_data(struct dm_bufio_client *c,
438 void *data, enum data_mode data_mode)
439{
440 switch (data_mode) {
441 case DATA_MODE_SLAB:
442 kmem_cache_free(DM_BUFIO_CACHE(c), data);
443 break;
444
445 case DATA_MODE_GET_FREE_PAGES:
446 free_pages((unsigned long)data, c->pages_per_block_bits);
447 break;
448
449 case DATA_MODE_VMALLOC:
450 vfree(data);
451 break;
452
453 default:
454 DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d",
455 data_mode);
456 BUG();
457 }
458}
459
460/*
461 * Allocate buffer and its data.
462 */
463static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask)
464{
465 struct dm_buffer *b = kmalloc(sizeof(struct dm_buffer) + c->aux_size,
466 gfp_mask);
467
468 if (!b)
469 return NULL;
470
471 b->c = c;
472
473 b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode);
474 if (!b->data) {
475 kfree(b);
476 return NULL;
477 }
478
Mikulas Patocka86bad0c2015-11-23 19:20:06 -0500479#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
480 memset(&b->stack_trace, 0, sizeof(b->stack_trace));
481#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000482 return b;
483}
484
485/*
486 * Free buffer and its data.
487 */
488static void free_buffer(struct dm_buffer *b)
489{
490 struct dm_bufio_client *c = b->c;
491
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000492 free_buffer_data(c, b->data, b->data_mode);
493 kfree(b);
494}
495
496/*
497 * Link buffer to the hash list and clean or dirty queue.
498 */
499static void __link_buffer(struct dm_buffer *b, sector_t block, int dirty)
500{
501 struct dm_bufio_client *c = b->c;
502
503 c->n_buffers[dirty]++;
504 b->block = block;
505 b->list_mode = dirty;
506 list_add(&b->lru_list, &c->lru[dirty]);
Joe Thornber4e420c42014-10-06 13:48:51 +0100507 __insert(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000508 b->last_accessed = jiffies;
Mikulas Patocka24d0cad2019-09-12 10:44:44 +0200509
Mikulas Patocka76ad8852019-09-12 10:44:45 +0200510 adjust_total_allocated(b, false);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000511}
512
513/*
514 * Unlink buffer from the hash list and dirty or clean queue.
515 */
516static void __unlink_buffer(struct dm_buffer *b)
517{
518 struct dm_bufio_client *c = b->c;
519
520 BUG_ON(!c->n_buffers[b->list_mode]);
521
522 c->n_buffers[b->list_mode]--;
Joe Thornber4e420c42014-10-06 13:48:51 +0100523 __remove(b->c, b);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000524 list_del(&b->lru_list);
Mikulas Patocka24d0cad2019-09-12 10:44:44 +0200525
Mikulas Patocka76ad8852019-09-12 10:44:45 +0200526 adjust_total_allocated(b, true);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000527}
528
529/*
530 * Place the buffer to the head of dirty or clean LRU queue.
531 */
532static void __relink_lru(struct dm_buffer *b, int dirty)
533{
534 struct dm_bufio_client *c = b->c;
535
Mikulas Patocka7dc56e32019-09-12 12:07:23 -0400536 b->accessed = 1;
537
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000538 BUG_ON(!c->n_buffers[b->list_mode]);
539
540 c->n_buffers[b->list_mode]--;
541 c->n_buffers[dirty]++;
542 b->list_mode = dirty;
Wei Yongjun54499af2012-10-12 16:59:44 +0100543 list_move(&b->lru_list, &c->lru[dirty]);
Joe Thornbereb76faf2014-09-30 09:32:46 +0100544 b->last_accessed = jiffies;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000545}
546
547/*----------------------------------------------------------------
548 * Submit I/O on the buffer.
549 *
550 * Bio interface is faster but it has some problems:
551 * the vector list is limited (increasing this limit increases
552 * memory-consumption per buffer, so it is not viable);
553 *
554 * the memory must be direct-mapped, not vmalloced;
555 *
556 * the I/O driver can reject requests spuriously if it thinks that
557 * the requests are too big for the device or if they cross a
558 * controller-defined memory boundary.
559 *
560 * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and
561 * it is not vmalloced, try using the bio interface.
562 *
563 * If the buffer is big, if it is vmalloced or if the underlying device
564 * rejects the bio because it is too large, use dm-io layer to do the I/O.
565 * The dm-io layer splits the I/O into multiple requests, avoiding the above
566 * shortcomings.
567 *--------------------------------------------------------------*/
568
569/*
570 * dm-io completion routine. It just calls b->bio.bi_end_io, pretending
571 * that the request was handled directly with bio interface.
572 */
573static void dmio_complete(unsigned long error, void *context)
574{
575 struct dm_buffer *b = context;
576
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200577 b->bio.bi_error = error ? -EIO : 0;
578 b->bio.bi_end_io(&b->bio);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000579}
580
581static void use_dmio(struct dm_buffer *b, int rw, sector_t block,
582 bio_end_io_t *end_io)
583{
584 int r;
585 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -0500586 .bi_op = rw,
587 .bi_op_flags = 0,
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000588 .notify.fn = dmio_complete,
589 .notify.context = b,
590 .client = b->c->dm_io,
591 };
592 struct dm_io_region region = {
593 .bdev = b->c->bdev,
594 .sector = block << b->c->sectors_per_block_bits,
595 .count = b->c->block_size >> SECTOR_SHIFT,
596 };
597
598 if (b->data_mode != DATA_MODE_VMALLOC) {
599 io_req.mem.type = DM_IO_KMEM;
600 io_req.mem.ptr.addr = b->data;
601 } else {
602 io_req.mem.type = DM_IO_VMA;
603 io_req.mem.ptr.vma = b->data;
604 }
605
606 b->bio.bi_end_io = end_io;
607
608 r = dm_io(&io_req, 1, &region, NULL);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200609 if (r) {
610 b->bio.bi_error = r;
611 end_io(&b->bio);
612 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000613}
614
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200615static void inline_endio(struct bio *bio)
Darrick J. Wong445559c2014-11-25 17:45:15 -0800616{
617 bio_end_io_t *end_fn = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200618 int error = bio->bi_error;
Darrick J. Wong445559c2014-11-25 17:45:15 -0800619
620 /*
621 * Reset the bio to free any attached resources
622 * (e.g. bio integrity profiles).
623 */
624 bio_reset(bio);
625
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200626 bio->bi_error = error;
627 end_fn(bio);
Darrick J. Wong445559c2014-11-25 17:45:15 -0800628}
629
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000630static void use_inline_bio(struct dm_buffer *b, int rw, sector_t block,
631 bio_end_io_t *end_io)
632{
633 char *ptr;
634 int len;
635
636 bio_init(&b->bio);
637 b->bio.bi_io_vec = b->bio_vec;
638 b->bio.bi_max_vecs = DM_BUFIO_INLINE_VECS;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700639 b->bio.bi_iter.bi_sector = block << b->c->sectors_per_block_bits;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000640 b->bio.bi_bdev = b->c->bdev;
Darrick J. Wong445559c2014-11-25 17:45:15 -0800641 b->bio.bi_end_io = inline_endio;
642 /*
643 * Use of .bi_private isn't a problem here because
644 * the dm_buffer's inline bio is local to bufio.
645 */
646 b->bio.bi_private = end_io;
Mike Christiee6047142016-06-05 14:32:04 -0500647 bio_set_op_attrs(&b->bio, rw, 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000648
649 /*
650 * We assume that if len >= PAGE_SIZE ptr is page-aligned.
651 * If len < PAGE_SIZE the buffer doesn't cross page boundary.
652 */
653 ptr = b->data;
654 len = b->c->block_size;
655
656 if (len >= PAGE_SIZE)
657 BUG_ON((unsigned long)ptr & (PAGE_SIZE - 1));
658 else
659 BUG_ON((unsigned long)ptr & (len - 1));
660
661 do {
662 if (!bio_add_page(&b->bio, virt_to_page(ptr),
663 len < PAGE_SIZE ? len : PAGE_SIZE,
Al Viro756d0972016-01-02 12:45:27 -0500664 offset_in_page(ptr))) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000665 BUG_ON(b->c->block_size <= PAGE_SIZE);
666 use_dmio(b, rw, block, end_io);
667 return;
668 }
669
670 len -= PAGE_SIZE;
671 ptr += PAGE_SIZE;
672 } while (len > 0);
673
Mike Christie4e49ea42016-06-05 14:31:41 -0500674 submit_bio(&b->bio);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000675}
676
677static void submit_io(struct dm_buffer *b, int rw, sector_t block,
678 bio_end_io_t *end_io)
679{
680 if (rw == WRITE && b->c->write_callback)
681 b->c->write_callback(b);
682
683 if (b->c->block_size <= DM_BUFIO_INLINE_VECS * PAGE_SIZE &&
684 b->data_mode != DATA_MODE_VMALLOC)
685 use_inline_bio(b, rw, block, end_io);
686 else
687 use_dmio(b, rw, block, end_io);
688}
689
690/*----------------------------------------------------------------
691 * Writing dirty buffers
692 *--------------------------------------------------------------*/
693
694/*
695 * The endio routine for write.
696 *
697 * Set the error, clear B_WRITING bit and wake anyone who was waiting on
698 * it.
699 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200700static void write_endio(struct bio *bio)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000701{
702 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
703
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200704 b->write_error = bio->bi_error;
705 if (unlikely(bio->bi_error)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000706 struct dm_bufio_client *c = b->c;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200707 int error = bio->bi_error;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000708 (void)cmpxchg(&c->async_write_error, 0, error);
709 }
710
711 BUG_ON(!test_bit(B_WRITING, &b->state));
712
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100713 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000714 clear_bit(B_WRITING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100715 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000716
717 wake_up_bit(&b->state, B_WRITING);
718}
719
720/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000721 * Initiate a write on a dirty buffer, but don't wait for it.
722 *
723 * - If the buffer is not dirty, exit.
724 * - If there some previous write going on, wait for it to finish (we can't
725 * have two writes on the same buffer simultaneously).
726 * - Submit our write and don't wait on it. We set B_WRITING indicating
727 * that there is a write in progress.
728 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100729static void __write_dirty_buffer(struct dm_buffer *b,
730 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000731{
732 if (!test_bit(B_DIRTY, &b->state))
733 return;
734
735 clear_bit(B_DIRTY, &b->state);
NeilBrown74316202014-07-07 15:16:04 +1000736 wait_on_bit_lock_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000737
Mikulas Patocka24809452013-07-10 23:41:18 +0100738 if (!write_list)
739 submit_io(b, WRITE, b->block, write_endio);
740 else
741 list_add_tail(&b->write_list, write_list);
742}
743
744static void __flush_write_list(struct list_head *write_list)
745{
746 struct blk_plug plug;
747 blk_start_plug(&plug);
748 while (!list_empty(write_list)) {
749 struct dm_buffer *b =
750 list_entry(write_list->next, struct dm_buffer, write_list);
751 list_del(&b->write_list);
752 submit_io(b, WRITE, b->block, write_endio);
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200753 cond_resched();
Mikulas Patocka24809452013-07-10 23:41:18 +0100754 }
755 blk_finish_plug(&plug);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000756}
757
758/*
759 * Wait until any activity on the buffer finishes. Possibly write the
760 * buffer if it is dirty. When this function finishes, there is no I/O
761 * running on the buffer and the buffer is not dirty.
762 */
763static void __make_buffer_clean(struct dm_buffer *b)
764{
765 BUG_ON(b->hold_count);
766
767 if (!b->state) /* fast case */
768 return;
769
NeilBrown74316202014-07-07 15:16:04 +1000770 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka24809452013-07-10 23:41:18 +0100771 __write_dirty_buffer(b, NULL);
NeilBrown74316202014-07-07 15:16:04 +1000772 wait_on_bit_io(&b->state, B_WRITING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000773}
774
775/*
776 * Find some buffer that is not held by anybody, clean it, unlink it and
777 * return it.
778 */
779static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
780{
781 struct dm_buffer *b;
782
783 list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
784 BUG_ON(test_bit(B_WRITING, &b->state));
785 BUG_ON(test_bit(B_DIRTY, &b->state));
786
787 if (!b->hold_count) {
788 __make_buffer_clean(b);
789 __unlink_buffer(b);
790 return b;
791 }
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200792 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000793 }
794
795 list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
796 BUG_ON(test_bit(B_READING, &b->state));
797
798 if (!b->hold_count) {
799 __make_buffer_clean(b);
800 __unlink_buffer(b);
801 return b;
802 }
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200803 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000804 }
805
806 return NULL;
807}
808
809/*
810 * Wait until some other threads free some buffer or release hold count on
811 * some buffer.
812 *
813 * This function is entered with c->lock held, drops it and regains it
814 * before exiting.
815 */
816static void __wait_for_free_buffer(struct dm_bufio_client *c)
817{
818 DECLARE_WAITQUEUE(wait, current);
819
820 add_wait_queue(&c->free_buffer_wait, &wait);
821 set_task_state(current, TASK_UNINTERRUPTIBLE);
822 dm_bufio_unlock(c);
823
824 io_schedule();
825
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000826 remove_wait_queue(&c->free_buffer_wait, &wait);
827
828 dm_bufio_lock(c);
829}
830
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100831enum new_flag {
832 NF_FRESH = 0,
833 NF_READ = 1,
834 NF_GET = 2,
835 NF_PREFETCH = 3
836};
837
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000838/*
839 * Allocate a new buffer. If the allocation is not possible, wait until
840 * some other thread frees a buffer.
841 *
842 * May drop the lock and regain it.
843 */
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100844static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000845{
846 struct dm_buffer *b;
Mikulas Patocka34d2fe72016-11-23 17:04:00 -0500847 bool tried_noio_alloc = false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000848
849 /*
850 * dm-bufio is resistant to allocation failures (it just keeps
851 * one buffer reserved in cases all the allocations fail).
852 * So set flags to not try too hard:
Douglas Anderson0758c352016-11-17 11:24:20 -0800853 * GFP_NOWAIT: don't wait; if we need to sleep we'll release our
854 * mutex and wait ourselves.
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000855 * __GFP_NORETRY: don't retry and rather return failure
856 * __GFP_NOMEMALLOC: don't use emergency reserves
857 * __GFP_NOWARN: don't print a warning in case of failure
858 *
859 * For debugging, if we set the cache size to 1, no new buffers will
860 * be allocated.
861 */
862 while (1) {
863 if (dm_bufio_cache_size_latch != 1) {
Douglas Anderson0758c352016-11-17 11:24:20 -0800864 b = alloc_buffer(c, GFP_NOWAIT | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000865 if (b)
866 return b;
867 }
868
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100869 if (nf == NF_PREFETCH)
870 return NULL;
871
Mikulas Patocka34d2fe72016-11-23 17:04:00 -0500872 if (dm_bufio_cache_size_latch != 1 && !tried_noio_alloc) {
873 dm_bufio_unlock(c);
874 b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
875 dm_bufio_lock(c);
876 if (b)
877 return b;
878 tried_noio_alloc = true;
879 }
880
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000881 if (!list_empty(&c->reserved_buffers)) {
882 b = list_entry(c->reserved_buffers.next,
883 struct dm_buffer, lru_list);
884 list_del(&b->lru_list);
885 c->need_reserved_buffers++;
886
887 return b;
888 }
889
890 b = __get_unclaimed_buffer(c);
891 if (b)
892 return b;
893
894 __wait_for_free_buffer(c);
895 }
896}
897
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100898static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000899{
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100900 struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
901
902 if (!b)
903 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000904
905 if (c->alloc_callback)
906 c->alloc_callback(b);
907
908 return b;
909}
910
911/*
912 * Free a buffer and wake other threads waiting for free buffers.
913 */
914static void __free_buffer_wake(struct dm_buffer *b)
915{
916 struct dm_bufio_client *c = b->c;
917
918 if (!c->need_reserved_buffers)
919 free_buffer(b);
920 else {
921 list_add(&b->lru_list, &c->reserved_buffers);
922 c->need_reserved_buffers--;
923 }
924
925 wake_up(&c->free_buffer_wait);
926}
927
Mikulas Patocka24809452013-07-10 23:41:18 +0100928static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait,
929 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000930{
931 struct dm_buffer *b, *tmp;
932
933 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
934 BUG_ON(test_bit(B_READING, &b->state));
935
936 if (!test_bit(B_DIRTY, &b->state) &&
937 !test_bit(B_WRITING, &b->state)) {
938 __relink_lru(b, LIST_CLEAN);
939 continue;
940 }
941
942 if (no_wait && test_bit(B_WRITING, &b->state))
943 return;
944
Mikulas Patocka24809452013-07-10 23:41:18 +0100945 __write_dirty_buffer(b, write_list);
Peter Zijlstra7cd32672016-09-13 10:45:20 +0200946 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000947 }
948}
949
950/*
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000951 * Check if we're over watermark.
952 * If we are over threshold_buffers, start freeing buffers.
953 * If we're over "limit_buffers", block until we get under the limit.
954 */
Mikulas Patocka24809452013-07-10 23:41:18 +0100955static void __check_watermark(struct dm_bufio_client *c,
956 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000957{
Mikulas Patockab9fd9eb2019-09-12 10:44:47 +0200958 if (c->n_buffers[LIST_DIRTY] > c->n_buffers[LIST_CLEAN] * DM_BUFIO_WRITEBACK_RATIO)
Mikulas Patocka24809452013-07-10 23:41:18 +0100959 __write_dirty_buffers_async(c, 1, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000960}
961
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000962/*----------------------------------------------------------------
963 * Getting a buffer
964 *--------------------------------------------------------------*/
965
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000966static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
Mikulas Patocka24809452013-07-10 23:41:18 +0100967 enum new_flag nf, int *need_submit,
968 struct list_head *write_list)
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000969{
970 struct dm_buffer *b, *new_b = NULL;
971
972 *need_submit = 0;
973
974 b = __find(c, block);
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100975 if (b)
976 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000977
978 if (nf == NF_GET)
979 return NULL;
980
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100981 new_b = __alloc_buffer_wait(c, nf);
982 if (!new_b)
983 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000984
985 /*
986 * We've had a period where the mutex was unlocked, so need to
987 * recheck the hash table.
988 */
989 b = __find(c, block);
990 if (b) {
991 __free_buffer_wake(new_b);
Mikulas Patockaa66cc282012-03-28 18:41:29 +0100992 goto found_buffer;
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000993 }
994
Mikulas Patocka24809452013-07-10 23:41:18 +0100995 __check_watermark(c, write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +0000996
997 b = new_b;
998 b->hold_count = 1;
999 b->read_error = 0;
1000 b->write_error = 0;
1001 __link_buffer(b, block, LIST_CLEAN);
1002
1003 if (nf == NF_FRESH) {
1004 b->state = 0;
1005 return b;
1006 }
1007
1008 b->state = 1 << B_READING;
1009 *need_submit = 1;
1010
1011 return b;
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001012
1013found_buffer:
1014 if (nf == NF_PREFETCH)
1015 return NULL;
1016 /*
1017 * Note: it is essential that we don't wait for the buffer to be
1018 * read if dm_bufio_get function is used. Both dm_bufio_get and
1019 * dm_bufio_prefetch can be used in the driver request routine.
1020 * If the user called both dm_bufio_prefetch and dm_bufio_get on
1021 * the same buffer, it would deadlock if we waited.
1022 */
1023 if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
1024 return NULL;
1025
1026 b->hold_count++;
1027 __relink_lru(b, test_bit(B_DIRTY, &b->state) ||
1028 test_bit(B_WRITING, &b->state));
1029 return b;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001030}
1031
1032/*
1033 * The endio routine for reading: set the error, clear the bit and wake up
1034 * anyone waiting on the buffer.
1035 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001036static void read_endio(struct bio *bio)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001037{
1038 struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
1039
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001040 b->read_error = bio->bi_error;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001041
1042 BUG_ON(!test_bit(B_READING, &b->state));
1043
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001044 smp_mb__before_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001045 clear_bit(B_READING, &b->state);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001046 smp_mb__after_atomic();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001047
1048 wake_up_bit(&b->state, B_READING);
1049}
1050
1051/*
1052 * A common routine for dm_bufio_new and dm_bufio_read. Operation of these
1053 * functions is similar except that dm_bufio_new doesn't read the
1054 * buffer from the disk (assuming that the caller overwrites all the data
1055 * and uses dm_bufio_mark_buffer_dirty to write new data back).
1056 */
1057static void *new_read(struct dm_bufio_client *c, sector_t block,
1058 enum new_flag nf, struct dm_buffer **bp)
1059{
1060 int need_submit;
1061 struct dm_buffer *b;
1062
Mikulas Patocka24809452013-07-10 23:41:18 +01001063 LIST_HEAD(write_list);
1064
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001065 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001066 b = __bufio_new(c, block, nf, &need_submit, &write_list);
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001067#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1068 if (b && b->hold_count == 1)
1069 buffer_record_stack(b);
1070#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001071 dm_bufio_unlock(c);
1072
Mikulas Patocka24809452013-07-10 23:41:18 +01001073 __flush_write_list(&write_list);
1074
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001075 if (!b)
Mikulas Patockaf98c8f72015-11-23 19:11:32 -05001076 return NULL;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001077
1078 if (need_submit)
1079 submit_io(b, READ, b->block, read_endio);
1080
NeilBrown74316202014-07-07 15:16:04 +10001081 wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001082
1083 if (b->read_error) {
1084 int error = b->read_error;
1085
1086 dm_bufio_release(b);
1087
1088 return ERR_PTR(error);
1089 }
1090
1091 *bp = b;
1092
1093 return b->data;
1094}
1095
1096void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
1097 struct dm_buffer **bp)
1098{
1099 return new_read(c, block, NF_GET, bp);
1100}
1101EXPORT_SYMBOL_GPL(dm_bufio_get);
1102
1103void *dm_bufio_read(struct dm_bufio_client *c, sector_t block,
1104 struct dm_buffer **bp)
1105{
1106 BUG_ON(dm_bufio_in_request());
1107
1108 return new_read(c, block, NF_READ, bp);
1109}
1110EXPORT_SYMBOL_GPL(dm_bufio_read);
1111
1112void *dm_bufio_new(struct dm_bufio_client *c, sector_t block,
1113 struct dm_buffer **bp)
1114{
1115 BUG_ON(dm_bufio_in_request());
1116
1117 return new_read(c, block, NF_FRESH, bp);
1118}
1119EXPORT_SYMBOL_GPL(dm_bufio_new);
1120
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001121void dm_bufio_prefetch(struct dm_bufio_client *c,
1122 sector_t block, unsigned n_blocks)
1123{
1124 struct blk_plug plug;
1125
Mikulas Patocka24809452013-07-10 23:41:18 +01001126 LIST_HEAD(write_list);
1127
Mikulas Patocka3b6b7812013-03-20 17:21:25 +00001128 BUG_ON(dm_bufio_in_request());
1129
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001130 blk_start_plug(&plug);
1131 dm_bufio_lock(c);
1132
1133 for (; n_blocks--; block++) {
1134 int need_submit;
1135 struct dm_buffer *b;
Mikulas Patocka24809452013-07-10 23:41:18 +01001136 b = __bufio_new(c, block, NF_PREFETCH, &need_submit,
1137 &write_list);
1138 if (unlikely(!list_empty(&write_list))) {
1139 dm_bufio_unlock(c);
1140 blk_finish_plug(&plug);
1141 __flush_write_list(&write_list);
1142 blk_start_plug(&plug);
1143 dm_bufio_lock(c);
1144 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001145 if (unlikely(b != NULL)) {
1146 dm_bufio_unlock(c);
1147
1148 if (need_submit)
1149 submit_io(b, READ, b->block, read_endio);
1150 dm_bufio_release(b);
1151
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001152 cond_resched();
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001153
1154 if (!n_blocks)
1155 goto flush_plug;
1156 dm_bufio_lock(c);
1157 }
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001158 }
1159
1160 dm_bufio_unlock(c);
1161
1162flush_plug:
1163 blk_finish_plug(&plug);
1164}
1165EXPORT_SYMBOL_GPL(dm_bufio_prefetch);
1166
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001167void dm_bufio_release(struct dm_buffer *b)
1168{
1169 struct dm_bufio_client *c = b->c;
1170
1171 dm_bufio_lock(c);
1172
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001173 BUG_ON(!b->hold_count);
1174
1175 b->hold_count--;
1176 if (!b->hold_count) {
1177 wake_up(&c->free_buffer_wait);
1178
1179 /*
1180 * If there were errors on the buffer, and the buffer is not
1181 * to be written, free the buffer. There is no point in caching
1182 * invalid buffer.
1183 */
1184 if ((b->read_error || b->write_error) &&
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001185 !test_bit(B_READING, &b->state) &&
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001186 !test_bit(B_WRITING, &b->state) &&
1187 !test_bit(B_DIRTY, &b->state)) {
1188 __unlink_buffer(b);
1189 __free_buffer_wake(b);
1190 }
1191 }
1192
1193 dm_bufio_unlock(c);
1194}
1195EXPORT_SYMBOL_GPL(dm_bufio_release);
1196
1197void dm_bufio_mark_buffer_dirty(struct dm_buffer *b)
1198{
1199 struct dm_bufio_client *c = b->c;
1200
1201 dm_bufio_lock(c);
1202
Mikulas Patockaa66cc282012-03-28 18:41:29 +01001203 BUG_ON(test_bit(B_READING, &b->state));
1204
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001205 if (!test_and_set_bit(B_DIRTY, &b->state))
1206 __relink_lru(b, LIST_DIRTY);
1207
1208 dm_bufio_unlock(c);
1209}
1210EXPORT_SYMBOL_GPL(dm_bufio_mark_buffer_dirty);
1211
1212void dm_bufio_write_dirty_buffers_async(struct dm_bufio_client *c)
1213{
Mikulas Patocka24809452013-07-10 23:41:18 +01001214 LIST_HEAD(write_list);
1215
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001216 BUG_ON(dm_bufio_in_request());
1217
1218 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001219 __write_dirty_buffers_async(c, 0, &write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001220 dm_bufio_unlock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001221 __flush_write_list(&write_list);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001222}
1223EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers_async);
1224
1225/*
1226 * For performance, it is essential that the buffers are written asynchronously
1227 * and simultaneously (so that the block layer can merge the writes) and then
1228 * waited upon.
1229 *
1230 * Finally, we flush hardware disk cache.
1231 */
1232int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
1233{
1234 int a, f;
1235 unsigned long buffers_processed = 0;
1236 struct dm_buffer *b, *tmp;
1237
Mikulas Patocka24809452013-07-10 23:41:18 +01001238 LIST_HEAD(write_list);
1239
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001240 dm_bufio_lock(c);
Mikulas Patocka24809452013-07-10 23:41:18 +01001241 __write_dirty_buffers_async(c, 0, &write_list);
1242 dm_bufio_unlock(c);
1243 __flush_write_list(&write_list);
1244 dm_bufio_lock(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001245
1246again:
1247 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
1248 int dropped_lock = 0;
1249
1250 if (buffers_processed < c->n_buffers[LIST_DIRTY])
1251 buffers_processed++;
1252
1253 BUG_ON(test_bit(B_READING, &b->state));
1254
1255 if (test_bit(B_WRITING, &b->state)) {
1256 if (buffers_processed < c->n_buffers[LIST_DIRTY]) {
1257 dropped_lock = 1;
1258 b->hold_count++;
1259 dm_bufio_unlock(c);
NeilBrown74316202014-07-07 15:16:04 +10001260 wait_on_bit_io(&b->state, B_WRITING,
1261 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001262 dm_bufio_lock(c);
1263 b->hold_count--;
1264 } else
NeilBrown74316202014-07-07 15:16:04 +10001265 wait_on_bit_io(&b->state, B_WRITING,
1266 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001267 }
1268
1269 if (!test_bit(B_DIRTY, &b->state) &&
1270 !test_bit(B_WRITING, &b->state))
1271 __relink_lru(b, LIST_CLEAN);
1272
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001273 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001274
1275 /*
1276 * If we dropped the lock, the list is no longer consistent,
1277 * so we must restart the search.
1278 *
1279 * In the most common case, the buffer just processed is
1280 * relinked to the clean list, so we won't loop scanning the
1281 * same buffer again and again.
1282 *
1283 * This may livelock if there is another thread simultaneously
1284 * dirtying buffers, so we count the number of buffers walked
1285 * and if it exceeds the total number of buffers, it means that
1286 * someone is doing some writes simultaneously with us. In
1287 * this case, stop, dropping the lock.
1288 */
1289 if (dropped_lock)
1290 goto again;
1291 }
1292 wake_up(&c->free_buffer_wait);
1293 dm_bufio_unlock(c);
1294
1295 a = xchg(&c->async_write_error, 0);
1296 f = dm_bufio_issue_flush(c);
1297 if (a)
1298 return a;
1299
1300 return f;
1301}
1302EXPORT_SYMBOL_GPL(dm_bufio_write_dirty_buffers);
1303
1304/*
1305 * Use dm-io to send and empty barrier flush the device.
1306 */
1307int dm_bufio_issue_flush(struct dm_bufio_client *c)
1308{
1309 struct dm_io_request io_req = {
Mike Christiee6047142016-06-05 14:32:04 -05001310 .bi_op = REQ_OP_WRITE,
1311 .bi_op_flags = WRITE_FLUSH,
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001312 .mem.type = DM_IO_KMEM,
1313 .mem.ptr.addr = NULL,
1314 .client = c->dm_io,
1315 };
1316 struct dm_io_region io_reg = {
1317 .bdev = c->bdev,
1318 .sector = 0,
1319 .count = 0,
1320 };
1321
1322 BUG_ON(dm_bufio_in_request());
1323
1324 return dm_io(&io_req, 1, &io_reg, NULL);
1325}
1326EXPORT_SYMBOL_GPL(dm_bufio_issue_flush);
1327
1328/*
1329 * We first delete any other buffer that may be at that new location.
1330 *
1331 * Then, we write the buffer to the original location if it was dirty.
1332 *
1333 * Then, if we are the only one who is holding the buffer, relink the buffer
1334 * in the hash queue for the new location.
1335 *
1336 * If there was someone else holding the buffer, we write it to the new
1337 * location but not relink it, because that other user needs to have the buffer
1338 * at the same place.
1339 */
1340void dm_bufio_release_move(struct dm_buffer *b, sector_t new_block)
1341{
1342 struct dm_bufio_client *c = b->c;
1343 struct dm_buffer *new;
1344
1345 BUG_ON(dm_bufio_in_request());
1346
1347 dm_bufio_lock(c);
1348
1349retry:
1350 new = __find(c, new_block);
1351 if (new) {
1352 if (new->hold_count) {
1353 __wait_for_free_buffer(c);
1354 goto retry;
1355 }
1356
1357 /*
1358 * FIXME: Is there any point waiting for a write that's going
1359 * to be overwritten in a bit?
1360 */
1361 __make_buffer_clean(new);
1362 __unlink_buffer(new);
1363 __free_buffer_wake(new);
1364 }
1365
1366 BUG_ON(!b->hold_count);
1367 BUG_ON(test_bit(B_READING, &b->state));
1368
Mikulas Patocka24809452013-07-10 23:41:18 +01001369 __write_dirty_buffer(b, NULL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001370 if (b->hold_count == 1) {
NeilBrown74316202014-07-07 15:16:04 +10001371 wait_on_bit_io(&b->state, B_WRITING,
1372 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001373 set_bit(B_DIRTY, &b->state);
1374 __unlink_buffer(b);
1375 __link_buffer(b, new_block, LIST_DIRTY);
1376 } else {
1377 sector_t old_block;
NeilBrown74316202014-07-07 15:16:04 +10001378 wait_on_bit_lock_io(&b->state, B_WRITING,
1379 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001380 /*
1381 * Relink buffer to "new_block" so that write_callback
1382 * sees "new_block" as a block number.
1383 * After the write, link the buffer back to old_block.
1384 * All this must be done in bufio lock, so that block number
1385 * change isn't visible to other threads.
1386 */
1387 old_block = b->block;
1388 __unlink_buffer(b);
1389 __link_buffer(b, new_block, b->list_mode);
1390 submit_io(b, WRITE, new_block, write_endio);
NeilBrown74316202014-07-07 15:16:04 +10001391 wait_on_bit_io(&b->state, B_WRITING,
1392 TASK_UNINTERRUPTIBLE);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001393 __unlink_buffer(b);
1394 __link_buffer(b, old_block, b->list_mode);
1395 }
1396
1397 dm_bufio_unlock(c);
1398 dm_bufio_release(b);
1399}
1400EXPORT_SYMBOL_GPL(dm_bufio_release_move);
1401
Mikulas Patocka55494bf2014-01-13 19:12:36 -05001402/*
1403 * Free the given buffer.
1404 *
1405 * This is just a hint, if the buffer is in use or dirty, this function
1406 * does nothing.
1407 */
1408void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
1409{
1410 struct dm_buffer *b;
1411
1412 dm_bufio_lock(c);
1413
1414 b = __find(c, block);
1415 if (b && likely(!b->hold_count) && likely(!b->state)) {
1416 __unlink_buffer(b);
1417 __free_buffer_wake(b);
1418 }
1419
1420 dm_bufio_unlock(c);
1421}
1422EXPORT_SYMBOL(dm_bufio_forget);
1423
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001424void dm_bufio_set_minimum_buffers(struct dm_bufio_client *c, unsigned n)
1425{
1426 c->minimum_buffers = n;
1427}
1428EXPORT_SYMBOL(dm_bufio_set_minimum_buffers);
1429
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001430unsigned dm_bufio_get_block_size(struct dm_bufio_client *c)
1431{
1432 return c->block_size;
1433}
1434EXPORT_SYMBOL_GPL(dm_bufio_get_block_size);
1435
1436sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
1437{
1438 return i_size_read(c->bdev->bd_inode) >>
1439 (SECTOR_SHIFT + c->sectors_per_block_bits);
1440}
1441EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
1442
1443sector_t dm_bufio_get_block_number(struct dm_buffer *b)
1444{
1445 return b->block;
1446}
1447EXPORT_SYMBOL_GPL(dm_bufio_get_block_number);
1448
1449void *dm_bufio_get_block_data(struct dm_buffer *b)
1450{
1451 return b->data;
1452}
1453EXPORT_SYMBOL_GPL(dm_bufio_get_block_data);
1454
1455void *dm_bufio_get_aux_data(struct dm_buffer *b)
1456{
1457 return b + 1;
1458}
1459EXPORT_SYMBOL_GPL(dm_bufio_get_aux_data);
1460
1461struct dm_bufio_client *dm_bufio_get_client(struct dm_buffer *b)
1462{
1463 return b->c;
1464}
1465EXPORT_SYMBOL_GPL(dm_bufio_get_client);
1466
1467static void drop_buffers(struct dm_bufio_client *c)
1468{
1469 struct dm_buffer *b;
1470 int i;
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001471 bool warned = false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001472
1473 BUG_ON(dm_bufio_in_request());
1474
1475 /*
1476 * An optimization so that the buffers are not written one-by-one.
1477 */
1478 dm_bufio_write_dirty_buffers_async(c);
1479
1480 dm_bufio_lock(c);
1481
1482 while ((b = __get_unclaimed_buffer(c)))
1483 __free_buffer_wake(b);
1484
1485 for (i = 0; i < LIST_SIZE; i++)
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001486 list_for_each_entry(b, &c->lru[i], lru_list) {
1487 WARN_ON(!warned);
1488 warned = true;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001489 DMERR("leaked buffer %llx, hold count %u, list %d",
1490 (unsigned long long)b->block, b->hold_count, i);
Mikulas Patocka86bad0c2015-11-23 19:20:06 -05001491#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1492 print_stack_trace(&b->stack_trace, 1);
1493 b->hold_count = 0; /* mark unclaimed to avoid BUG_ON below */
1494#endif
1495 }
1496
1497#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING
1498 while ((b = __get_unclaimed_buffer(c)))
1499 __free_buffer_wake(b);
1500#endif
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001501
1502 for (i = 0; i < LIST_SIZE; i++)
1503 BUG_ON(!list_empty(&c->lru[i]));
1504
1505 dm_bufio_unlock(c);
1506}
1507
1508/*
Joe Thornber33096a72014-10-09 11:10:25 +01001509 * We may not be able to evict this buffer if IO pending or the client
1510 * is still using it. Caller is expected to know buffer is too old.
1511 *
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001512 * And if GFP_NOFS is used, we must not do any I/O because we hold
1513 * dm_bufio_clients_lock and we would risk deadlock if the I/O gets
1514 * rerouted to different bufio client.
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001515 */
Joe Thornber33096a72014-10-09 11:10:25 +01001516static bool __try_evict_buffer(struct dm_buffer *b, gfp_t gfp)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001517{
Mikulas Patocka9d28eb12014-10-16 14:45:20 -04001518 if (!(gfp & __GFP_FS)) {
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001519 if (test_bit(B_READING, &b->state) ||
1520 test_bit(B_WRITING, &b->state) ||
1521 test_bit(B_DIRTY, &b->state))
Joe Thornber33096a72014-10-09 11:10:25 +01001522 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001523 }
1524
1525 if (b->hold_count)
Joe Thornber33096a72014-10-09 11:10:25 +01001526 return false;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001527
1528 __make_buffer_clean(b);
1529 __unlink_buffer(b);
1530 __free_buffer_wake(b);
1531
Joe Thornber33096a72014-10-09 11:10:25 +01001532 return true;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001533}
1534
Mikulas Patockaeeaf1332017-04-30 17:32:28 -04001535static unsigned long get_retain_buffers(struct dm_bufio_client *c)
Joe Thornber33096a72014-10-09 11:10:25 +01001536{
Mikulas Patockaeeaf1332017-04-30 17:32:28 -04001537 unsigned long retain_bytes = ACCESS_ONCE(dm_bufio_retain_bytes);
1538 return retain_bytes >> (c->sectors_per_block_bits + SECTOR_SHIFT);
Joe Thornber33096a72014-10-09 11:10:25 +01001539}
1540
1541static unsigned long __scan(struct dm_bufio_client *c, unsigned long nr_to_scan,
1542 gfp_t gfp_mask)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001543{
1544 int l;
1545 struct dm_buffer *b, *tmp;
Joe Thornber33096a72014-10-09 11:10:25 +01001546 unsigned long freed = 0;
Suren Baghdasaryanb58aa242017-12-06 09:27:30 -08001547 unsigned long count = c->n_buffers[LIST_CLEAN] +
1548 c->n_buffers[LIST_DIRTY];
Mikulas Patockaeeaf1332017-04-30 17:32:28 -04001549 unsigned long retain_target = get_retain_buffers(c);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001550
1551 for (l = 0; l < LIST_SIZE; l++) {
Dave Chinner7dc19d52013-08-28 10:18:11 +10001552 list_for_each_entry_safe_reverse(b, tmp, &c->lru[l], lru_list) {
Joe Thornber33096a72014-10-09 11:10:25 +01001553 if (__try_evict_buffer(b, gfp_mask))
1554 freed++;
1555 if (!--nr_to_scan || ((count - freed) <= retain_target))
Mikulas Patocka0e825862014-10-01 13:29:48 -04001556 return freed;
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001557 cond_resched();
Dave Chinner7dc19d52013-08-28 10:18:11 +10001558 }
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001559 }
Dave Chinner7dc19d52013-08-28 10:18:11 +10001560 return freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001561}
1562
Dave Chinner7dc19d52013-08-28 10:18:11 +10001563static unsigned long
1564dm_bufio_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001565{
Dave Chinner7dc19d52013-08-28 10:18:11 +10001566 struct dm_bufio_client *c;
1567 unsigned long freed;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001568
Dave Chinner7dc19d52013-08-28 10:18:11 +10001569 c = container_of(shrink, struct dm_bufio_client, shrinker);
Mikulas Patockab9059452019-08-08 05:40:04 -04001570 if (sc->gfp_mask & __GFP_FS)
1571 dm_bufio_lock(c);
1572 else if (!dm_bufio_trylock(c))
Dave Chinner7dc19d52013-08-28 10:18:11 +10001573 return SHRINK_STOP;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001574
Dave Chinner7dc19d52013-08-28 10:18:11 +10001575 freed = __scan(c, sc->nr_to_scan, sc->gfp_mask);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001576 dm_bufio_unlock(c);
Dave Chinner7dc19d52013-08-28 10:18:11 +10001577 return freed;
1578}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001579
Dave Chinner7dc19d52013-08-28 10:18:11 +10001580static unsigned long
1581dm_bufio_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1582{
Mikulas Patocka47791842016-11-23 16:52:01 -05001583 struct dm_bufio_client *c = container_of(shrink, struct dm_bufio_client, shrinker);
1584 unsigned long count = READ_ONCE(c->n_buffers[LIST_CLEAN]) +
1585 READ_ONCE(c->n_buffers[LIST_DIRTY]);
1586 unsigned long retain_target = get_retain_buffers(c);
Dave Chinner7dc19d52013-08-28 10:18:11 +10001587
Suren Baghdasaryanb58aa242017-12-06 09:27:30 -08001588 return (count < retain_target) ? 0 : (count - retain_target);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001589}
1590
1591/*
1592 * Create the buffering interface
1593 */
1594struct dm_bufio_client *dm_bufio_client_create(struct block_device *bdev, unsigned block_size,
1595 unsigned reserved_buffers, unsigned aux_size,
1596 void (*alloc_callback)(struct dm_buffer *),
1597 void (*write_callback)(struct dm_buffer *))
1598{
1599 int r;
1600 struct dm_bufio_client *c;
1601 unsigned i;
1602
1603 BUG_ON(block_size < 1 << SECTOR_SHIFT ||
1604 (block_size & (block_size - 1)));
1605
Greg Thelend8c712e2014-07-31 09:07:19 -07001606 c = kzalloc(sizeof(*c), GFP_KERNEL);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001607 if (!c) {
1608 r = -ENOMEM;
1609 goto bad_client;
1610 }
Joe Thornber4e420c42014-10-06 13:48:51 +01001611 c->buffer_tree = RB_ROOT;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001612
1613 c->bdev = bdev;
1614 c->block_size = block_size;
Mikulas Patockaa3d939a2015-10-02 11:21:24 -04001615 c->sectors_per_block_bits = __ffs(block_size) - SECTOR_SHIFT;
1616 c->pages_per_block_bits = (__ffs(block_size) >= PAGE_SHIFT) ?
1617 __ffs(block_size) - PAGE_SHIFT : 0;
1618 c->blocks_per_page_bits = (__ffs(block_size) < PAGE_SHIFT ?
1619 PAGE_SHIFT - __ffs(block_size) : 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001620
1621 c->aux_size = aux_size;
1622 c->alloc_callback = alloc_callback;
1623 c->write_callback = write_callback;
1624
1625 for (i = 0; i < LIST_SIZE; i++) {
1626 INIT_LIST_HEAD(&c->lru[i]);
1627 c->n_buffers[i] = 0;
1628 }
1629
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001630 mutex_init(&c->lock);
1631 INIT_LIST_HEAD(&c->reserved_buffers);
1632 c->need_reserved_buffers = reserved_buffers;
1633
Mikulas Patocka55b082e2014-01-13 19:13:05 -05001634 c->minimum_buffers = DM_BUFIO_MIN_BUFFERS;
1635
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001636 init_waitqueue_head(&c->free_buffer_wait);
1637 c->async_write_error = 0;
1638
1639 c->dm_io = dm_io_client_create();
1640 if (IS_ERR(c->dm_io)) {
1641 r = PTR_ERR(c->dm_io);
1642 goto bad_dm_io;
1643 }
1644
1645 mutex_lock(&dm_bufio_clients_lock);
1646 if (c->blocks_per_page_bits) {
1647 if (!DM_BUFIO_CACHE_NAME(c)) {
1648 DM_BUFIO_CACHE_NAME(c) = kasprintf(GFP_KERNEL, "dm_bufio_cache-%u", c->block_size);
1649 if (!DM_BUFIO_CACHE_NAME(c)) {
1650 r = -ENOMEM;
1651 mutex_unlock(&dm_bufio_clients_lock);
1652 goto bad_cache;
1653 }
1654 }
1655
1656 if (!DM_BUFIO_CACHE(c)) {
1657 DM_BUFIO_CACHE(c) = kmem_cache_create(DM_BUFIO_CACHE_NAME(c),
1658 c->block_size,
1659 c->block_size, 0, NULL);
1660 if (!DM_BUFIO_CACHE(c)) {
1661 r = -ENOMEM;
1662 mutex_unlock(&dm_bufio_clients_lock);
1663 goto bad_cache;
1664 }
1665 }
1666 }
1667 mutex_unlock(&dm_bufio_clients_lock);
1668
1669 while (c->need_reserved_buffers) {
1670 struct dm_buffer *b = alloc_buffer(c, GFP_KERNEL);
1671
1672 if (!b) {
1673 r = -ENOMEM;
1674 goto bad_buffer;
1675 }
1676 __free_buffer_wake(b);
1677 }
1678
1679 mutex_lock(&dm_bufio_clients_lock);
1680 dm_bufio_client_count++;
1681 list_add(&c->client_list, &dm_bufio_all_clients);
1682 __cache_size_refresh();
1683 mutex_unlock(&dm_bufio_clients_lock);
1684
Dave Chinner7dc19d52013-08-28 10:18:11 +10001685 c->shrinker.count_objects = dm_bufio_shrink_count;
1686 c->shrinker.scan_objects = dm_bufio_shrink_scan;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001687 c->shrinker.seeks = 1;
1688 c->shrinker.batch = 0;
1689 register_shrinker(&c->shrinker);
1690
1691 return c;
1692
1693bad_buffer:
1694bad_cache:
1695 while (!list_empty(&c->reserved_buffers)) {
1696 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1697 struct dm_buffer, lru_list);
1698 list_del(&b->lru_list);
1699 free_buffer(b);
1700 }
1701 dm_io_client_destroy(c->dm_io);
1702bad_dm_io:
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001703 kfree(c);
1704bad_client:
1705 return ERR_PTR(r);
1706}
1707EXPORT_SYMBOL_GPL(dm_bufio_client_create);
1708
1709/*
1710 * Free the buffering interface.
1711 * It is required that there are no references on any buffers.
1712 */
1713void dm_bufio_client_destroy(struct dm_bufio_client *c)
1714{
1715 unsigned i;
1716
1717 drop_buffers(c);
1718
1719 unregister_shrinker(&c->shrinker);
1720
1721 mutex_lock(&dm_bufio_clients_lock);
1722
1723 list_del(&c->client_list);
1724 dm_bufio_client_count--;
1725 __cache_size_refresh();
1726
1727 mutex_unlock(&dm_bufio_clients_lock);
1728
Joe Thornber4e420c42014-10-06 13:48:51 +01001729 BUG_ON(!RB_EMPTY_ROOT(&c->buffer_tree));
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001730 BUG_ON(c->need_reserved_buffers);
1731
1732 while (!list_empty(&c->reserved_buffers)) {
1733 struct dm_buffer *b = list_entry(c->reserved_buffers.next,
1734 struct dm_buffer, lru_list);
1735 list_del(&b->lru_list);
1736 free_buffer(b);
1737 }
1738
1739 for (i = 0; i < LIST_SIZE; i++)
1740 if (c->n_buffers[i])
1741 DMERR("leaked buffer count %d: %ld", i, c->n_buffers[i]);
1742
1743 for (i = 0; i < LIST_SIZE; i++)
1744 BUG_ON(c->n_buffers[i]);
1745
1746 dm_io_client_destroy(c->dm_io);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001747 kfree(c);
1748}
1749EXPORT_SYMBOL_GPL(dm_bufio_client_destroy);
1750
Joe Thornber33096a72014-10-09 11:10:25 +01001751static unsigned get_max_age_hz(void)
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001752{
Joe Thornber33096a72014-10-09 11:10:25 +01001753 unsigned max_age = ACCESS_ONCE(dm_bufio_max_age);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001754
Joe Thornber33096a72014-10-09 11:10:25 +01001755 if (max_age > UINT_MAX / HZ)
1756 max_age = UINT_MAX / HZ;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001757
Joe Thornber33096a72014-10-09 11:10:25 +01001758 return max_age * HZ;
1759}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001760
Joe Thornber33096a72014-10-09 11:10:25 +01001761static bool older_than(struct dm_buffer *b, unsigned long age_hz)
1762{
Asaf Vertzf4953392015-01-06 15:44:15 +02001763 return time_after_eq(jiffies, b->last_accessed + age_hz);
Joe Thornber33096a72014-10-09 11:10:25 +01001764}
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001765
Joe Thornber33096a72014-10-09 11:10:25 +01001766static void __evict_old_buffers(struct dm_bufio_client *c, unsigned long age_hz)
1767{
1768 struct dm_buffer *b, *tmp;
Mikulas Patockaeeaf1332017-04-30 17:32:28 -04001769 unsigned long retain_target = get_retain_buffers(c);
1770 unsigned long count;
Mikulas Patockae08047c2017-04-30 17:34:53 -04001771 LIST_HEAD(write_list);
Joe Thornber33096a72014-10-09 11:10:25 +01001772
1773 dm_bufio_lock(c);
1774
Mikulas Patockae08047c2017-04-30 17:34:53 -04001775 __check_watermark(c, &write_list);
1776 if (unlikely(!list_empty(&write_list))) {
1777 dm_bufio_unlock(c);
1778 __flush_write_list(&write_list);
1779 dm_bufio_lock(c);
1780 }
1781
Joe Thornber33096a72014-10-09 11:10:25 +01001782 count = c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY];
1783 list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_CLEAN], lru_list) {
1784 if (count <= retain_target)
1785 break;
1786
1787 if (!older_than(b, age_hz))
1788 break;
1789
1790 if (__try_evict_buffer(b, 0))
1791 count--;
1792
Peter Zijlstra7cd32672016-09-13 10:45:20 +02001793 cond_resched();
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001794 }
Joe Thornber33096a72014-10-09 11:10:25 +01001795
1796 dm_bufio_unlock(c);
1797}
1798
Mikulas Patocka7dc56e32019-09-12 12:07:23 -04001799static void do_global_cleanup(struct work_struct *w)
1800{
1801 struct dm_bufio_client *locked_client = NULL;
1802 struct dm_bufio_client *current_client;
1803 struct dm_buffer *b;
1804 unsigned spinlock_hold_count;
1805 unsigned long threshold = dm_bufio_cache_size -
1806 dm_bufio_cache_size / DM_BUFIO_LOW_WATERMARK_RATIO;
1807 unsigned long loops = global_num * 2;
1808
1809 mutex_lock(&dm_bufio_clients_lock);
1810
1811 while (1) {
1812 cond_resched();
1813
1814 spin_lock(&global_spinlock);
1815 if (unlikely(dm_bufio_current_allocated <= threshold))
1816 break;
1817
1818 spinlock_hold_count = 0;
1819get_next:
1820 if (!loops--)
1821 break;
1822 if (unlikely(list_empty(&global_queue)))
1823 break;
1824 b = list_entry(global_queue.prev, struct dm_buffer, global_list);
1825
1826 if (b->accessed) {
1827 b->accessed = 0;
1828 list_move(&b->global_list, &global_queue);
1829 if (likely(++spinlock_hold_count < 16))
1830 goto get_next;
1831 spin_unlock(&global_spinlock);
1832 continue;
1833 }
1834
1835 current_client = b->c;
1836 if (unlikely(current_client != locked_client)) {
1837 if (locked_client)
1838 dm_bufio_unlock(locked_client);
1839
1840 if (!dm_bufio_trylock(current_client)) {
1841 spin_unlock(&global_spinlock);
1842 dm_bufio_lock(current_client);
1843 locked_client = current_client;
1844 continue;
1845 }
1846
1847 locked_client = current_client;
1848 }
1849
1850 spin_unlock(&global_spinlock);
1851
1852 if (unlikely(!__try_evict_buffer(b, GFP_KERNEL))) {
1853 spin_lock(&global_spinlock);
1854 list_move(&b->global_list, &global_queue);
1855 spin_unlock(&global_spinlock);
1856 }
1857 }
1858
1859 spin_unlock(&global_spinlock);
1860
1861 if (locked_client)
1862 dm_bufio_unlock(locked_client);
1863
1864 mutex_unlock(&dm_bufio_clients_lock);
1865}
1866
Joe Thornber33096a72014-10-09 11:10:25 +01001867static void cleanup_old_buffers(void)
1868{
1869 unsigned long max_age_hz = get_max_age_hz();
1870 struct dm_bufio_client *c;
1871
1872 mutex_lock(&dm_bufio_clients_lock);
1873
Mikulas Patockae08047c2017-04-30 17:34:53 -04001874 __cache_size_refresh();
1875
Joe Thornber33096a72014-10-09 11:10:25 +01001876 list_for_each_entry(c, &dm_bufio_all_clients, client_list)
1877 __evict_old_buffers(c, max_age_hz);
1878
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001879 mutex_unlock(&dm_bufio_clients_lock);
1880}
1881
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001882static void work_fn(struct work_struct *w)
1883{
1884 cleanup_old_buffers();
1885
Mikulas Patocka7dc56e32019-09-12 12:07:23 -04001886 queue_delayed_work(dm_bufio_wq, &dm_bufio_cleanup_old_work,
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001887 DM_BUFIO_WORK_TIMER_SECS * HZ);
1888}
1889
1890/*----------------------------------------------------------------
1891 * Module setup
1892 *--------------------------------------------------------------*/
1893
1894/*
1895 * This is called only once for the whole dm_bufio module.
1896 * It initializes memory limit.
1897 */
1898static int __init dm_bufio_init(void)
1899{
1900 __u64 mem;
1901
Mikulas Patocka4cb57ab2013-12-05 17:33:29 -05001902 dm_bufio_allocated_kmem_cache = 0;
1903 dm_bufio_allocated_get_free_pages = 0;
1904 dm_bufio_allocated_vmalloc = 0;
1905 dm_bufio_current_allocated = 0;
1906
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001907 memset(&dm_bufio_caches, 0, sizeof dm_bufio_caches);
1908 memset(&dm_bufio_cache_names, 0, sizeof dm_bufio_cache_names);
1909
Eric Biggers6609a3c2017-11-15 16:38:09 -08001910 mem = (__u64)mult_frac(totalram_pages - totalhigh_pages,
1911 DM_BUFIO_MEMORY_PERCENT, 100) << PAGE_SHIFT;
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001912
1913 if (mem > ULONG_MAX)
1914 mem = ULONG_MAX;
1915
1916#ifdef CONFIG_MMU
Eric Biggers6609a3c2017-11-15 16:38:09 -08001917 if (mem > mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100))
1918 mem = mult_frac(VMALLOC_TOTAL, DM_BUFIO_VMALLOC_PERCENT, 100);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001919#endif
1920
1921 dm_bufio_default_cache_size = mem;
1922
1923 mutex_lock(&dm_bufio_clients_lock);
1924 __cache_size_refresh();
1925 mutex_unlock(&dm_bufio_clients_lock);
1926
Bhaktipriya Shridharedd1ea22016-08-30 22:19:11 +05301927 dm_bufio_wq = alloc_workqueue("dm_bufio_cache", WQ_MEM_RECLAIM, 0);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001928 if (!dm_bufio_wq)
1929 return -ENOMEM;
1930
Mikulas Patocka7dc56e32019-09-12 12:07:23 -04001931 INIT_DELAYED_WORK(&dm_bufio_cleanup_old_work, work_fn);
1932 INIT_WORK(&dm_bufio_replacement_work, do_global_cleanup);
1933 queue_delayed_work(dm_bufio_wq, &dm_bufio_cleanup_old_work,
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001934 DM_BUFIO_WORK_TIMER_SECS * HZ);
1935
1936 return 0;
1937}
1938
1939/*
1940 * This is called once when unloading the dm_bufio module.
1941 */
1942static void __exit dm_bufio_exit(void)
1943{
1944 int bug = 0;
1945 int i;
1946
Mikulas Patocka7dc56e32019-09-12 12:07:23 -04001947 cancel_delayed_work_sync(&dm_bufio_cleanup_old_work);
1948 flush_workqueue(dm_bufio_wq);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001949 destroy_workqueue(dm_bufio_wq);
1950
Julia Lawall6f659852015-09-13 14:15:05 +02001951 for (i = 0; i < ARRAY_SIZE(dm_bufio_caches); i++)
1952 kmem_cache_destroy(dm_bufio_caches[i]);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001953
1954 for (i = 0; i < ARRAY_SIZE(dm_bufio_cache_names); i++)
1955 kfree(dm_bufio_cache_names[i]);
1956
1957 if (dm_bufio_client_count) {
1958 DMCRIT("%s: dm_bufio_client_count leaked: %d",
1959 __func__, dm_bufio_client_count);
1960 bug = 1;
1961 }
1962
1963 if (dm_bufio_current_allocated) {
1964 DMCRIT("%s: dm_bufio_current_allocated leaked: %lu",
1965 __func__, dm_bufio_current_allocated);
1966 bug = 1;
1967 }
1968
1969 if (dm_bufio_allocated_get_free_pages) {
1970 DMCRIT("%s: dm_bufio_allocated_get_free_pages leaked: %lu",
1971 __func__, dm_bufio_allocated_get_free_pages);
1972 bug = 1;
1973 }
1974
1975 if (dm_bufio_allocated_vmalloc) {
1976 DMCRIT("%s: dm_bufio_vmalloc leaked: %lu",
1977 __func__, dm_bufio_allocated_vmalloc);
1978 bug = 1;
1979 }
1980
Anup Limbu86a49e22015-11-25 15:46:05 +05301981 BUG_ON(bug);
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001982}
1983
1984module_init(dm_bufio_init)
1985module_exit(dm_bufio_exit)
1986
1987module_param_named(max_cache_size_bytes, dm_bufio_cache_size, ulong, S_IRUGO | S_IWUSR);
1988MODULE_PARM_DESC(max_cache_size_bytes, "Size of metadata cache");
1989
1990module_param_named(max_age_seconds, dm_bufio_max_age, uint, S_IRUGO | S_IWUSR);
1991MODULE_PARM_DESC(max_age_seconds, "Max age of a buffer in seconds");
1992
Mikulas Patockaeeaf1332017-04-30 17:32:28 -04001993module_param_named(retain_bytes, dm_bufio_retain_bytes, ulong, S_IRUGO | S_IWUSR);
Joe Thornber33096a72014-10-09 11:10:25 +01001994MODULE_PARM_DESC(retain_bytes, "Try to keep at least this many bytes cached in memory");
1995
Mikulas Patocka95d402f2011-10-31 20:19:09 +00001996module_param_named(peak_allocated_bytes, dm_bufio_peak_allocated, ulong, S_IRUGO | S_IWUSR);
1997MODULE_PARM_DESC(peak_allocated_bytes, "Tracks the maximum allocated memory");
1998
1999module_param_named(allocated_kmem_cache_bytes, dm_bufio_allocated_kmem_cache, ulong, S_IRUGO);
2000MODULE_PARM_DESC(allocated_kmem_cache_bytes, "Memory allocated with kmem_cache_alloc");
2001
2002module_param_named(allocated_get_free_pages_bytes, dm_bufio_allocated_get_free_pages, ulong, S_IRUGO);
2003MODULE_PARM_DESC(allocated_get_free_pages_bytes, "Memory allocated with get_free_pages");
2004
2005module_param_named(allocated_vmalloc_bytes, dm_bufio_allocated_vmalloc, ulong, S_IRUGO);
2006MODULE_PARM_DESC(allocated_vmalloc_bytes, "Memory allocated with vmalloc");
2007
2008module_param_named(current_allocated_bytes, dm_bufio_current_allocated, ulong, S_IRUGO);
2009MODULE_PARM_DESC(current_allocated_bytes, "Memory currently used by the cache");
2010
2011MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2012MODULE_DESCRIPTION(DM_NAME " buffered I/O library");
2013MODULE_LICENSE("GPL");