forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dm-bufio.c
1750 lines (1421 loc) · 41.7 KB
/
dm-bufio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2009-2011 Red Hat, Inc.
*
* Author: Mikulas Patocka <[email protected]>
*
* This file is released under the GPL.
*/
#include "dm-bufio.h"
#include <linux/device-mapper.h>
#include <linux/dm-io.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/shrinker.h>
#include <linux/module.h>
#define DM_MSG_PREFIX "bufio"
/*
* Memory management policy:
* Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory
* or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower).
* Always allocate at least DM_BUFIO_MIN_BUFFERS buffers.
* Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT
* dirty buffers.
*/
#define DM_BUFIO_MIN_BUFFERS 8
#define DM_BUFIO_MEMORY_PERCENT 2
#define DM_BUFIO_VMALLOC_PERCENT 25
#define DM_BUFIO_WRITEBACK_PERCENT 75
/*
* Check buffer ages in this interval (seconds)
*/
#define DM_BUFIO_WORK_TIMER_SECS 10
/*
* Free buffers when they are older than this (seconds)
*/
#define DM_BUFIO_DEFAULT_AGE_SECS 60
/*
* The number of bvec entries that are embedded directly in the buffer.
* If the chunk size is larger, dm-io is used to do the io.
*/
#define DM_BUFIO_INLINE_VECS 16
/*
* Buffer hash
*/
#define DM_BUFIO_HASH_BITS 20
#define DM_BUFIO_HASH(block) \
((((block) >> DM_BUFIO_HASH_BITS) ^ (block)) & \
((1 << DM_BUFIO_HASH_BITS) - 1))
/*
* Don't try to use kmem_cache_alloc for blocks larger than this.
* For explanation, see alloc_buffer_data below.
*/
#define DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT (PAGE_SIZE >> 1)
#define DM_BUFIO_BLOCK_SIZE_GFP_LIMIT (PAGE_SIZE << (MAX_ORDER - 1))
/*
* dm_buffer->list_mode
*/
#define LIST_CLEAN 0
#define LIST_DIRTY 1
#define LIST_SIZE 2
/*
* Linking of buffers:
* All buffers are linked to cache_hash with their hash_list field.
*
* Clean buffers that are not being written (B_WRITING not set)
* are linked to lru[LIST_CLEAN] with their lru_list field.
*
* Dirty and clean buffers that are being written are linked to
* lru[LIST_DIRTY] with their lru_list field. When the write
* finishes, the buffer cannot be relinked immediately (because we
* are in an interrupt context and relinking requires process
* context), so some clean-not-writing buffers can be held on
* dirty_lru too. They are later added to lru in the process
* context.
*/
struct dm_bufio_client {
struct mutex lock;
struct list_head lru[LIST_SIZE];
unsigned long n_buffers[LIST_SIZE];
struct block_device *bdev;
unsigned block_size;
unsigned char sectors_per_block_bits;
unsigned char pages_per_block_bits;
unsigned char blocks_per_page_bits;
unsigned aux_size;
void (*alloc_callback)(struct dm_buffer *);
void (*write_callback)(struct dm_buffer *);
struct dm_io_client *dm_io;
struct list_head reserved_buffers;
unsigned need_reserved_buffers;
struct hlist_head *cache_hash;
wait_queue_head_t free_buffer_wait;
int async_write_error;
struct list_head client_list;
struct shrinker shrinker;
};
/*
* Buffer state bits.
*/
#define B_READING 0
#define B_WRITING 1
#define B_DIRTY 2
/*
* Describes how the block was allocated:
* kmem_cache_alloc(), __get_free_pages() or vmalloc().
* See the comment at alloc_buffer_data.
*/
enum data_mode {
DATA_MODE_SLAB = 0,
DATA_MODE_GET_FREE_PAGES = 1,
DATA_MODE_VMALLOC = 2,
DATA_MODE_LIMIT = 3
};
struct dm_buffer {
struct hlist_node hash_list;
struct list_head lru_list;
sector_t block;
void *data;
enum data_mode data_mode;
unsigned char list_mode; /* LIST_* */
unsigned hold_count;
int read_error;
int write_error;
unsigned long state;
unsigned long last_accessed;
struct dm_bufio_client *c;
struct bio bio;
struct bio_vec bio_vec[DM_BUFIO_INLINE_VECS];
};
/*----------------------------------------------------------------*/
static struct kmem_cache *dm_bufio_caches[PAGE_SHIFT - SECTOR_SHIFT];
static char *dm_bufio_cache_names[PAGE_SHIFT - SECTOR_SHIFT];
static inline int dm_bufio_cache_index(struct dm_bufio_client *c)
{
unsigned ret = c->blocks_per_page_bits - 1;
BUG_ON(ret >= ARRAY_SIZE(dm_bufio_caches));
return ret;
}
#define DM_BUFIO_CACHE(c) (dm_bufio_caches[dm_bufio_cache_index(c)])
#define DM_BUFIO_CACHE_NAME(c) (dm_bufio_cache_names[dm_bufio_cache_index(c)])
#define dm_bufio_in_request() (!!current->bio_list)
static void dm_bufio_lock(struct dm_bufio_client *c)
{
mutex_lock_nested(&c->lock, dm_bufio_in_request());
}
static int dm_bufio_trylock(struct dm_bufio_client *c)
{
return mutex_trylock(&c->lock);
}
static void dm_bufio_unlock(struct dm_bufio_client *c)
{
mutex_unlock(&c->lock);
}
/*
* FIXME Move to sched.h?
*/
#ifdef CONFIG_PREEMPT_VOLUNTARY
# define dm_bufio_cond_resched() \
do { \
if (unlikely(need_resched())) \
_cond_resched(); \
} while (0)
#else
# define dm_bufio_cond_resched() do { } while (0)
#endif
/*----------------------------------------------------------------*/
/*
* Default cache size: available memory divided by the ratio.
*/
static unsigned long dm_bufio_default_cache_size;
/*
* Total cache size set by the user.
*/
static unsigned long dm_bufio_cache_size;
/*
* A copy of dm_bufio_cache_size because dm_bufio_cache_size can change
* at any time. If it disagrees, the user has changed cache size.
*/
static unsigned long dm_bufio_cache_size_latch;
static DEFINE_SPINLOCK(param_spinlock);
/*
* Buffers are freed after this timeout
*/
static unsigned dm_bufio_max_age = DM_BUFIO_DEFAULT_AGE_SECS;
static unsigned long dm_bufio_peak_allocated;
static unsigned long dm_bufio_allocated_kmem_cache;
static unsigned long dm_bufio_allocated_get_free_pages;
static unsigned long dm_bufio_allocated_vmalloc;
static unsigned long dm_bufio_current_allocated;
/*----------------------------------------------------------------*/
/*
* Per-client cache: dm_bufio_cache_size / dm_bufio_client_count
*/
static unsigned long dm_bufio_cache_size_per_client;
/*
* The current number of clients.
*/
static int dm_bufio_client_count;
/*
* The list of all clients.
*/
static LIST_HEAD(dm_bufio_all_clients);
/*
* This mutex protects dm_bufio_cache_size_latch,
* dm_bufio_cache_size_per_client and dm_bufio_client_count
*/
static DEFINE_MUTEX(dm_bufio_clients_lock);
/*----------------------------------------------------------------*/
static void adjust_total_allocated(enum data_mode data_mode, long diff)
{
static unsigned long * const class_ptr[DATA_MODE_LIMIT] = {
&dm_bufio_allocated_kmem_cache,
&dm_bufio_allocated_get_free_pages,
&dm_bufio_allocated_vmalloc,
};
spin_lock(¶m_spinlock);
*class_ptr[data_mode] += diff;
dm_bufio_current_allocated += diff;
if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
dm_bufio_peak_allocated = dm_bufio_current_allocated;
spin_unlock(¶m_spinlock);
}
/*
* Change the number of clients and recalculate per-client limit.
*/
static void __cache_size_refresh(void)
{
BUG_ON(!mutex_is_locked(&dm_bufio_clients_lock));
BUG_ON(dm_bufio_client_count < 0);
dm_bufio_cache_size_latch = ACCESS_ONCE(dm_bufio_cache_size);
/*
* Use default if set to 0 and report the actual cache size used.
*/
if (!dm_bufio_cache_size_latch) {
(void)cmpxchg(&dm_bufio_cache_size, 0,
dm_bufio_default_cache_size);
dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
}
dm_bufio_cache_size_per_client = dm_bufio_cache_size_latch /
(dm_bufio_client_count ? : 1);
}
/*
* Allocating buffer data.
*
* Small buffers are allocated with kmem_cache, to use space optimally.
*
* For large buffers, we choose between get_free_pages and vmalloc.
* Each has advantages and disadvantages.
*
* __get_free_pages can randomly fail if the memory is fragmented.
* __vmalloc won't randomly fail, but vmalloc space is limited (it may be
* as low as 128M) so using it for caching is not appropriate.
*
* If the allocation may fail we use __get_free_pages. Memory fragmentation
* won't have a fatal effect here, but it just causes flushes of some other
* buffers and more I/O will be performed. Don't use __get_free_pages if it
* always fails (i.e. order >= MAX_ORDER).
*
* If the allocation shouldn't fail we use __vmalloc. This is only for the
* initial reserve allocation, so there's no risk of wasting all vmalloc
* space.
*/
static void *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask,
enum data_mode *data_mode)
{
if (c->block_size <= DM_BUFIO_BLOCK_SIZE_SLAB_LIMIT) {
*data_mode = DATA_MODE_SLAB;
return kmem_cache_alloc(DM_BUFIO_CACHE(c), gfp_mask);
}
if (c->block_size <= DM_BUFIO_BLOCK_SIZE_GFP_LIMIT &&
gfp_mask & __GFP_NORETRY) {
*data_mode = DATA_MODE_GET_FREE_PAGES;
return (void *)__get_free_pages(gfp_mask,
c->pages_per_block_bits);
}
*data_mode = DATA_MODE_VMALLOC;
return __vmalloc(c->block_size, gfp_mask, PAGE_KERNEL);
}
/*
* Free buffer's data.
*/
static void free_buffer_data(struct dm_bufio_client *c,
void *data, enum data_mode data_mode)
{
switch (data_mode) {
case DATA_MODE_SLAB:
kmem_cache_free(DM_BUFIO_CACHE(c), data);
break;
case DATA_MODE_GET_FREE_PAGES:
free_pages((unsigned long)data, c->pages_per_block_bits);
break;
case DATA_MODE_VMALLOC:
vfree(data);
break;
default:
DMCRIT("dm_bufio_free_buffer_data: bad data mode: %d",
data_mode);
BUG();
}
}
/*
* Allocate buffer and its data.
*/
static struct dm_buffer *alloc_buffer(struct dm_bufio_client *c, gfp_t gfp_mask)
{
struct dm_buffer *b = kmalloc(sizeof(struct dm_buffer) + c->aux_size,
gfp_mask);
if (!b)
return NULL;
b->c = c;
b->data = alloc_buffer_data(c, gfp_mask, &b->data_mode);
if (!b->data) {
kfree(b);
return NULL;
}
adjust_total_allocated(b->data_mode, (long)c->block_size);
return b;
}
/*
* Free buffer and its data.
*/
static void free_buffer(struct dm_buffer *b)
{
struct dm_bufio_client *c = b->c;
adjust_total_allocated(b->data_mode, -(long)c->block_size);
free_buffer_data(c, b->data, b->data_mode);
kfree(b);
}
/*
* Link buffer to the hash list and clean or dirty queue.
*/
static void __link_buffer(struct dm_buffer *b, sector_t block, int dirty)
{
struct dm_bufio_client *c = b->c;
c->n_buffers[dirty]++;
b->block = block;
b->list_mode = dirty;
list_add(&b->lru_list, &c->lru[dirty]);
hlist_add_head(&b->hash_list, &c->cache_hash[DM_BUFIO_HASH(block)]);
b->last_accessed = jiffies;
}
/*
* Unlink buffer from the hash list and dirty or clean queue.
*/
static void __unlink_buffer(struct dm_buffer *b)
{
struct dm_bufio_client *c = b->c;
BUG_ON(!c->n_buffers[b->list_mode]);
c->n_buffers[b->list_mode]--;
hlist_del(&b->hash_list);
list_del(&b->lru_list);
}
/*
* Place the buffer to the head of dirty or clean LRU queue.
*/
static void __relink_lru(struct dm_buffer *b, int dirty)
{
struct dm_bufio_client *c = b->c;
BUG_ON(!c->n_buffers[b->list_mode]);
c->n_buffers[b->list_mode]--;
c->n_buffers[dirty]++;
b->list_mode = dirty;
list_move(&b->lru_list, &c->lru[dirty]);
}
/*----------------------------------------------------------------
* Submit I/O on the buffer.
*
* Bio interface is faster but it has some problems:
* the vector list is limited (increasing this limit increases
* memory-consumption per buffer, so it is not viable);
*
* the memory must be direct-mapped, not vmalloced;
*
* the I/O driver can reject requests spuriously if it thinks that
* the requests are too big for the device or if they cross a
* controller-defined memory boundary.
*
* If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and
* it is not vmalloced, try using the bio interface.
*
* If the buffer is big, if it is vmalloced or if the underlying device
* rejects the bio because it is too large, use dm-io layer to do the I/O.
* The dm-io layer splits the I/O into multiple requests, avoiding the above
* shortcomings.
*--------------------------------------------------------------*/
/*
* dm-io completion routine. It just calls b->bio.bi_end_io, pretending
* that the request was handled directly with bio interface.
*/
static void dmio_complete(unsigned long error, void *context)
{
struct dm_buffer *b = context;
b->bio.bi_end_io(&b->bio, error ? -EIO : 0);
}
static void use_dmio(struct dm_buffer *b, int rw, sector_t block,
bio_end_io_t *end_io)
{
int r;
struct dm_io_request io_req = {
.bi_rw = rw,
.notify.fn = dmio_complete,
.notify.context = b,
.client = b->c->dm_io,
};
struct dm_io_region region = {
.bdev = b->c->bdev,
.sector = block << b->c->sectors_per_block_bits,
.count = b->c->block_size >> SECTOR_SHIFT,
};
if (b->data_mode != DATA_MODE_VMALLOC) {
io_req.mem.type = DM_IO_KMEM;
io_req.mem.ptr.addr = b->data;
} else {
io_req.mem.type = DM_IO_VMA;
io_req.mem.ptr.vma = b->data;
}
b->bio.bi_end_io = end_io;
r = dm_io(&io_req, 1, ®ion, NULL);
if (r)
end_io(&b->bio, r);
}
static void use_inline_bio(struct dm_buffer *b, int rw, sector_t block,
bio_end_io_t *end_io)
{
char *ptr;
int len;
bio_init(&b->bio);
b->bio.bi_io_vec = b->bio_vec;
b->bio.bi_max_vecs = DM_BUFIO_INLINE_VECS;
b->bio.bi_sector = block << b->c->sectors_per_block_bits;
b->bio.bi_bdev = b->c->bdev;
b->bio.bi_end_io = end_io;
/*
* We assume that if len >= PAGE_SIZE ptr is page-aligned.
* If len < PAGE_SIZE the buffer doesn't cross page boundary.
*/
ptr = b->data;
len = b->c->block_size;
if (len >= PAGE_SIZE)
BUG_ON((unsigned long)ptr & (PAGE_SIZE - 1));
else
BUG_ON((unsigned long)ptr & (len - 1));
do {
if (!bio_add_page(&b->bio, virt_to_page(ptr),
len < PAGE_SIZE ? len : PAGE_SIZE,
virt_to_phys(ptr) & (PAGE_SIZE - 1))) {
BUG_ON(b->c->block_size <= PAGE_SIZE);
use_dmio(b, rw, block, end_io);
return;
}
len -= PAGE_SIZE;
ptr += PAGE_SIZE;
} while (len > 0);
submit_bio(rw, &b->bio);
}
static void submit_io(struct dm_buffer *b, int rw, sector_t block,
bio_end_io_t *end_io)
{
if (rw == WRITE && b->c->write_callback)
b->c->write_callback(b);
if (b->c->block_size <= DM_BUFIO_INLINE_VECS * PAGE_SIZE &&
b->data_mode != DATA_MODE_VMALLOC)
use_inline_bio(b, rw, block, end_io);
else
use_dmio(b, rw, block, end_io);
}
/*----------------------------------------------------------------
* Writing dirty buffers
*--------------------------------------------------------------*/
/*
* The endio routine for write.
*
* Set the error, clear B_WRITING bit and wake anyone who was waiting on
* it.
*/
static void write_endio(struct bio *bio, int error)
{
struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
b->write_error = error;
if (unlikely(error)) {
struct dm_bufio_client *c = b->c;
(void)cmpxchg(&c->async_write_error, 0, error);
}
BUG_ON(!test_bit(B_WRITING, &b->state));
smp_mb__before_clear_bit();
clear_bit(B_WRITING, &b->state);
smp_mb__after_clear_bit();
wake_up_bit(&b->state, B_WRITING);
}
/*
* This function is called when wait_on_bit is actually waiting.
*/
static int do_io_schedule(void *word)
{
io_schedule();
return 0;
}
/*
* Initiate a write on a dirty buffer, but don't wait for it.
*
* - If the buffer is not dirty, exit.
* - If there some previous write going on, wait for it to finish (we can't
* have two writes on the same buffer simultaneously).
* - Submit our write and don't wait on it. We set B_WRITING indicating
* that there is a write in progress.
*/
static void __write_dirty_buffer(struct dm_buffer *b)
{
if (!test_bit(B_DIRTY, &b->state))
return;
clear_bit(B_DIRTY, &b->state);
wait_on_bit_lock(&b->state, B_WRITING,
do_io_schedule, TASK_UNINTERRUPTIBLE);
submit_io(b, WRITE, b->block, write_endio);
}
/*
* Wait until any activity on the buffer finishes. Possibly write the
* buffer if it is dirty. When this function finishes, there is no I/O
* running on the buffer and the buffer is not dirty.
*/
static void __make_buffer_clean(struct dm_buffer *b)
{
BUG_ON(b->hold_count);
if (!b->state) /* fast case */
return;
wait_on_bit(&b->state, B_READING, do_io_schedule, TASK_UNINTERRUPTIBLE);
__write_dirty_buffer(b);
wait_on_bit(&b->state, B_WRITING, do_io_schedule, TASK_UNINTERRUPTIBLE);
}
/*
* Find some buffer that is not held by anybody, clean it, unlink it and
* return it.
*/
static struct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
{
struct dm_buffer *b;
list_for_each_entry_reverse(b, &c->lru[LIST_CLEAN], lru_list) {
BUG_ON(test_bit(B_WRITING, &b->state));
BUG_ON(test_bit(B_DIRTY, &b->state));
if (!b->hold_count) {
__make_buffer_clean(b);
__unlink_buffer(b);
return b;
}
dm_bufio_cond_resched();
}
list_for_each_entry_reverse(b, &c->lru[LIST_DIRTY], lru_list) {
BUG_ON(test_bit(B_READING, &b->state));
if (!b->hold_count) {
__make_buffer_clean(b);
__unlink_buffer(b);
return b;
}
dm_bufio_cond_resched();
}
return NULL;
}
/*
* Wait until some other threads free some buffer or release hold count on
* some buffer.
*
* This function is entered with c->lock held, drops it and regains it
* before exiting.
*/
static void __wait_for_free_buffer(struct dm_bufio_client *c)
{
DECLARE_WAITQUEUE(wait, current);
add_wait_queue(&c->free_buffer_wait, &wait);
set_task_state(current, TASK_UNINTERRUPTIBLE);
dm_bufio_unlock(c);
io_schedule();
set_task_state(current, TASK_RUNNING);
remove_wait_queue(&c->free_buffer_wait, &wait);
dm_bufio_lock(c);
}
enum new_flag {
NF_FRESH = 0,
NF_READ = 1,
NF_GET = 2,
NF_PREFETCH = 3
};
/*
* Allocate a new buffer. If the allocation is not possible, wait until
* some other thread frees a buffer.
*
* May drop the lock and regain it.
*/
static struct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
{
struct dm_buffer *b;
/*
* dm-bufio is resistant to allocation failures (it just keeps
* one buffer reserved in cases all the allocations fail).
* So set flags to not try too hard:
* GFP_NOIO: don't recurse into the I/O layer
* __GFP_NORETRY: don't retry and rather return failure
* __GFP_NOMEMALLOC: don't use emergency reserves
* __GFP_NOWARN: don't print a warning in case of failure
*
* For debugging, if we set the cache size to 1, no new buffers will
* be allocated.
*/
while (1) {
if (dm_bufio_cache_size_latch != 1) {
b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
if (b)
return b;
}
if (nf == NF_PREFETCH)
return NULL;
if (!list_empty(&c->reserved_buffers)) {
b = list_entry(c->reserved_buffers.next,
struct dm_buffer, lru_list);
list_del(&b->lru_list);
c->need_reserved_buffers++;
return b;
}
b = __get_unclaimed_buffer(c);
if (b)
return b;
__wait_for_free_buffer(c);
}
}
static struct dm_buffer *__alloc_buffer_wait(struct dm_bufio_client *c, enum new_flag nf)
{
struct dm_buffer *b = __alloc_buffer_wait_no_callback(c, nf);
if (!b)
return NULL;
if (c->alloc_callback)
c->alloc_callback(b);
return b;
}
/*
* Free a buffer and wake other threads waiting for free buffers.
*/
static void __free_buffer_wake(struct dm_buffer *b)
{
struct dm_bufio_client *c = b->c;
if (!c->need_reserved_buffers)
free_buffer(b);
else {
list_add(&b->lru_list, &c->reserved_buffers);
c->need_reserved_buffers--;
}
wake_up(&c->free_buffer_wait);
}
static void __write_dirty_buffers_async(struct dm_bufio_client *c, int no_wait)
{
struct dm_buffer *b, *tmp;
list_for_each_entry_safe_reverse(b, tmp, &c->lru[LIST_DIRTY], lru_list) {
BUG_ON(test_bit(B_READING, &b->state));
if (!test_bit(B_DIRTY, &b->state) &&
!test_bit(B_WRITING, &b->state)) {
__relink_lru(b, LIST_CLEAN);
continue;
}
if (no_wait && test_bit(B_WRITING, &b->state))
return;
__write_dirty_buffer(b);
dm_bufio_cond_resched();
}
}
/*
* Get writeback threshold and buffer limit for a given client.
*/
static void __get_memory_limit(struct dm_bufio_client *c,
unsigned long *threshold_buffers,
unsigned long *limit_buffers)
{
unsigned long buffers;
if (ACCESS_ONCE(dm_bufio_cache_size) != dm_bufio_cache_size_latch) {
mutex_lock(&dm_bufio_clients_lock);
__cache_size_refresh();
mutex_unlock(&dm_bufio_clients_lock);
}
buffers = dm_bufio_cache_size_per_client >>
(c->sectors_per_block_bits + SECTOR_SHIFT);
if (buffers < DM_BUFIO_MIN_BUFFERS)
buffers = DM_BUFIO_MIN_BUFFERS;
*limit_buffers = buffers;
*threshold_buffers = buffers * DM_BUFIO_WRITEBACK_PERCENT / 100;
}
/*
* Check if we're over watermark.
* If we are over threshold_buffers, start freeing buffers.
* If we're over "limit_buffers", block until we get under the limit.
*/
static void __check_watermark(struct dm_bufio_client *c)
{
unsigned long threshold_buffers, limit_buffers;
__get_memory_limit(c, &threshold_buffers, &limit_buffers);
while (c->n_buffers[LIST_CLEAN] + c->n_buffers[LIST_DIRTY] >
limit_buffers) {
struct dm_buffer *b = __get_unclaimed_buffer(c);
if (!b)
return;
__free_buffer_wake(b);
dm_bufio_cond_resched();
}
if (c->n_buffers[LIST_DIRTY] > threshold_buffers)
__write_dirty_buffers_async(c, 1);
}
/*
* Find a buffer in the hash.
*/
static struct dm_buffer *__find(struct dm_bufio_client *c, sector_t block)
{
struct dm_buffer *b;
struct hlist_node *hn;
hlist_for_each_entry(b, hn, &c->cache_hash[DM_BUFIO_HASH(block)],
hash_list) {
dm_bufio_cond_resched();
if (b->block == block)
return b;
}
return NULL;
}
/*----------------------------------------------------------------
* Getting a buffer
*--------------------------------------------------------------*/
static struct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block,
enum new_flag nf, int *need_submit)
{
struct dm_buffer *b, *new_b = NULL;
*need_submit = 0;
b = __find(c, block);
if (b)
goto found_buffer;
if (nf == NF_GET)
return NULL;
new_b = __alloc_buffer_wait(c, nf);
if (!new_b)
return NULL;
/*
* We've had a period where the mutex was unlocked, so need to
* recheck the hash table.
*/
b = __find(c, block);
if (b) {
__free_buffer_wake(new_b);
goto found_buffer;
}
__check_watermark(c);
b = new_b;
b->hold_count = 1;
b->read_error = 0;
b->write_error = 0;
__link_buffer(b, block, LIST_CLEAN);
if (nf == NF_FRESH) {
b->state = 0;
return b;
}
b->state = 1 << B_READING;
*need_submit = 1;
return b;
found_buffer:
if (nf == NF_PREFETCH)
return NULL;
/*
* Note: it is essential that we don't wait for the buffer to be
* read if dm_bufio_get function is used. Both dm_bufio_get and
* dm_bufio_prefetch can be used in the driver request routine.
* If the user called both dm_bufio_prefetch and dm_bufio_get on
* the same buffer, it would deadlock if we waited.
*/
if (nf == NF_GET && unlikely(test_bit(B_READING, &b->state)))
return NULL;
b->hold_count++;
__relink_lru(b, test_bit(B_DIRTY, &b->state) ||
test_bit(B_WRITING, &b->state));
return b;
}
/*
* The endio routine for reading: set the error, clear the bit and wake up
* anyone waiting on the buffer.
*/
static void read_endio(struct bio *bio, int error)
{
struct dm_buffer *b = container_of(bio, struct dm_buffer, bio);
b->read_error = error;
BUG_ON(!test_bit(B_READING, &b->state));
smp_mb__before_clear_bit();
clear_bit(B_READING, &b->state);
smp_mb__after_clear_bit();
wake_up_bit(&b->state, B_READING);
}
/*
* A common routine for dm_bufio_new and dm_bufio_read. Operation of these
* functions is similar except that dm_bufio_new doesn't read the
* buffer from the disk (assuming that the caller overwrites all the data
* and uses dm_bufio_mark_buffer_dirty to write new data back).
*/
static void *new_read(struct dm_bufio_client *c, sector_t block,
enum new_flag nf, struct dm_buffer **bp)
{
int need_submit;
struct dm_buffer *b;
dm_bufio_lock(c);
b = __bufio_new(c, block, nf, &need_submit);
dm_bufio_unlock(c);
if (!b)
return b;
if (need_submit)
submit_io(b, READ, b->block, read_endio);
wait_on_bit(&b->state, B_READING, do_io_schedule, TASK_UNINTERRUPTIBLE);
if (b->read_error) {
int error = b->read_error;
dm_bufio_release(b);
return ERR_PTR(error);
}
*bp = b;
return b->data;
}
void *dm_bufio_get(struct dm_bufio_client *c, sector_t block,
struct dm_buffer **bp)