1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Red Hat. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/dax.h>
16 #include <linux/pfn_t.h>
17 #include <linux/libnvdimm.h>
19 #define DM_MSG_PREFIX "writecache"
21 #define HIGH_WATERMARK 50
22 #define LOW_WATERMARK 45
23 #define MAX_WRITEBACK_JOBS 0
24 #define ENDIO_LATENCY 16
25 #define WRITEBACK_LATENCY 64
26 #define AUTOCOMMIT_BLOCKS_SSD 65536
27 #define AUTOCOMMIT_BLOCKS_PMEM 64
28 #define AUTOCOMMIT_MSEC 1000
30 #define BITMAP_GRANULARITY 65536
31 #if BITMAP_GRANULARITY < PAGE_SIZE
32 #undef BITMAP_GRANULARITY
33 #define BITMAP_GRANULARITY PAGE_SIZE
36 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
37 #define DM_WRITECACHE_HAS_PMEM
40 #ifdef DM_WRITECACHE_HAS_PMEM
41 #define pmem_assign(dest, src) \
43 typeof(dest) uniq = (src); \
44 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
47 #define pmem_assign(dest, src) ((dest) = (src))
50 #if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
51 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
54 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
55 #define MEMORY_SUPERBLOCK_VERSION 1
57 struct wc_memory_entry
{
58 __le64 original_sector
;
62 struct wc_memory_superblock
{
74 struct wc_memory_entry entries
[0];
78 struct rb_node rb_node
;
80 unsigned short wc_list_contiguous
;
81 bool write_in_progress
82 #if BITS_PER_LONG == 64
87 #if BITS_PER_LONG == 64
91 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
92 uint64_t original_sector
;
97 #ifdef DM_WRITECACHE_HAS_PMEM
98 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
99 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
101 #define WC_MODE_PMEM(wc) false
102 #define WC_MODE_FUA(wc) false
104 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
106 struct dm_writecache
{
108 struct list_head lru
;
110 struct list_head freelist
;
112 struct rb_root freetree
;
113 struct wc_entry
*current_free
;
118 size_t freelist_size
;
119 size_t writeback_size
;
120 size_t freelist_high_watermark
;
121 size_t freelist_low_watermark
;
123 unsigned uncommitted_blocks
;
124 unsigned autocommit_blocks
;
125 unsigned max_writeback_jobs
;
129 unsigned long autocommit_jiffies
;
130 struct timer_list autocommit_timer
;
131 struct wait_queue_head freelist_wait
;
133 atomic_t bio_in_progress
[2];
134 struct wait_queue_head bio_in_progress_wait
[2];
136 struct dm_target
*ti
;
138 struct dm_dev
*ssd_dev
;
139 sector_t start_sector
;
141 uint64_t memory_map_size
;
142 size_t metadata_sectors
;
146 struct wc_entry
*entries
;
148 unsigned char block_size_bits
;
151 bool writeback_fua
:1;
153 bool overwrote_committed
:1;
154 bool memory_vmapped
:1;
156 bool high_wm_percent_set
:1;
157 bool low_wm_percent_set
:1;
158 bool max_writeback_jobs_set
:1;
159 bool autocommit_blocks_set
:1;
160 bool autocommit_time_set
:1;
161 bool writeback_fua_set
:1;
162 bool flush_on_suspend
:1;
164 unsigned writeback_all
;
165 struct workqueue_struct
*writeback_wq
;
166 struct work_struct writeback_work
;
167 struct work_struct flush_work
;
169 struct dm_io_client
*dm_io
;
171 raw_spinlock_t endio_list_lock
;
172 struct list_head endio_list
;
173 struct task_struct
*endio_thread
;
175 struct task_struct
*flush_thread
;
176 struct bio_list flush_list
;
178 struct dm_kcopyd_client
*dm_kcopyd
;
179 unsigned long *dirty_bitmap
;
180 unsigned dirty_bitmap_size
;
182 struct bio_set bio_set
;
186 #define WB_LIST_INLINE 16
188 struct writeback_struct
{
189 struct list_head endio_entry
;
190 struct dm_writecache
*wc
;
191 struct wc_entry
**wc_list
;
193 struct wc_entry
*wc_list_inline
[WB_LIST_INLINE
];
198 struct list_head endio_entry
;
199 struct dm_writecache
*wc
;
205 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle
,
206 "A percentage of time allocated for data copying");
208 static void wc_lock(struct dm_writecache
*wc
)
210 mutex_lock(&wc
->lock
);
213 static void wc_unlock(struct dm_writecache
*wc
)
215 mutex_unlock(&wc
->lock
);
218 #ifdef DM_WRITECACHE_HAS_PMEM
219 static int persistent_memory_claim(struct dm_writecache
*wc
)
228 wc
->memory_vmapped
= false;
230 if (!wc
->ssd_dev
->dax_dev
) {
234 s
= wc
->memory_map_size
;
240 if (p
!= s
>> PAGE_SHIFT
) {
245 id
= dax_read_lock();
247 da
= dax_direct_access(wc
->ssd_dev
->dax_dev
, 0, p
, &wc
->memory_map
, &pfn
);
249 wc
->memory_map
= NULL
;
253 if (!pfn_t_has_page(pfn
)) {
254 wc
->memory_map
= NULL
;
260 wc
->memory_map
= NULL
;
261 pages
= kvmalloc_array(p
, sizeof(struct page
*), GFP_KERNEL
);
269 daa
= dax_direct_access(wc
->ssd_dev
->dax_dev
, i
, p
- i
,
272 r
= daa
? daa
: -EINVAL
;
275 if (!pfn_t_has_page(pfn
)) {
279 while (daa
-- && i
< p
) {
280 pages
[i
++] = pfn_t_to_page(pfn
);
284 wc
->memory_map
= vmap(pages
, p
, VM_MAP
, PAGE_KERNEL
);
285 if (!wc
->memory_map
) {
290 wc
->memory_vmapped
= true;
295 wc
->memory_map
+= (size_t)wc
->start_sector
<< SECTOR_SHIFT
;
296 wc
->memory_map_size
-= (size_t)wc
->start_sector
<< SECTOR_SHIFT
;
307 static int persistent_memory_claim(struct dm_writecache
*wc
)
313 static void persistent_memory_release(struct dm_writecache
*wc
)
315 if (wc
->memory_vmapped
)
316 vunmap(wc
->memory_map
- ((size_t)wc
->start_sector
<< SECTOR_SHIFT
));
319 static struct page
*persistent_memory_page(void *addr
)
321 if (is_vmalloc_addr(addr
))
322 return vmalloc_to_page(addr
);
324 return virt_to_page(addr
);
327 static unsigned persistent_memory_page_offset(void *addr
)
329 return (unsigned long)addr
& (PAGE_SIZE
- 1);
332 static void persistent_memory_flush_cache(void *ptr
, size_t size
)
334 if (is_vmalloc_addr(ptr
))
335 flush_kernel_vmap_range(ptr
, size
);
338 static void persistent_memory_invalidate_cache(void *ptr
, size_t size
)
340 if (is_vmalloc_addr(ptr
))
341 invalidate_kernel_vmap_range(ptr
, size
);
344 static struct wc_memory_superblock
*sb(struct dm_writecache
*wc
)
346 return wc
->memory_map
;
349 static struct wc_memory_entry
*memory_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
351 return &sb(wc
)->entries
[e
->index
];
354 static void *memory_data(struct dm_writecache
*wc
, struct wc_entry
*e
)
356 return (char *)wc
->block_start
+ (e
->index
<< wc
->block_size_bits
);
359 static sector_t
cache_sector(struct dm_writecache
*wc
, struct wc_entry
*e
)
361 return wc
->start_sector
+ wc
->metadata_sectors
+
362 ((sector_t
)e
->index
<< (wc
->block_size_bits
- SECTOR_SHIFT
));
365 static uint64_t read_original_sector(struct dm_writecache
*wc
, struct wc_entry
*e
)
367 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
368 return e
->original_sector
;
370 return le64_to_cpu(memory_entry(wc
, e
)->original_sector
);
374 static uint64_t read_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
)
376 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
379 return le64_to_cpu(memory_entry(wc
, e
)->seq_count
);
383 static void clear_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
)
385 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
388 pmem_assign(memory_entry(wc
, e
)->seq_count
, cpu_to_le64(-1));
391 static void write_original_sector_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
,
392 uint64_t original_sector
, uint64_t seq_count
)
394 struct wc_memory_entry me
;
395 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
396 e
->original_sector
= original_sector
;
397 e
->seq_count
= seq_count
;
399 me
.original_sector
= cpu_to_le64(original_sector
);
400 me
.seq_count
= cpu_to_le64(seq_count
);
401 pmem_assign(*memory_entry(wc
, e
), me
);
404 #define writecache_error(wc, err, msg, arg...) \
406 if (!cmpxchg(&(wc)->error, 0, err)) \
408 wake_up(&(wc)->freelist_wait); \
411 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
413 static void writecache_flush_all_metadata(struct dm_writecache
*wc
)
415 if (!WC_MODE_PMEM(wc
))
416 memset(wc
->dirty_bitmap
, -1, wc
->dirty_bitmap_size
);
419 static void writecache_flush_region(struct dm_writecache
*wc
, void *ptr
, size_t size
)
421 if (!WC_MODE_PMEM(wc
))
422 __set_bit(((char *)ptr
- (char *)wc
->memory_map
) / BITMAP_GRANULARITY
,
426 static void writecache_disk_flush(struct dm_writecache
*wc
, struct dm_dev
*dev
);
429 struct dm_writecache
*wc
;
434 static void writecache_notify_io(unsigned long error
, void *context
)
436 struct io_notify
*endio
= context
;
438 if (unlikely(error
!= 0))
439 writecache_error(endio
->wc
, -EIO
, "error writing metadata");
440 BUG_ON(atomic_read(&endio
->count
) <= 0);
441 if (atomic_dec_and_test(&endio
->count
))
445 static void ssd_commit_flushed(struct dm_writecache
*wc
)
447 struct dm_io_region region
;
448 struct dm_io_request req
;
449 struct io_notify endio
= {
451 COMPLETION_INITIALIZER_ONSTACK(endio
.c
),
454 unsigned bitmap_bits
= wc
->dirty_bitmap_size
* 8;
459 i
= find_next_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
460 if (unlikely(i
== bitmap_bits
))
462 j
= find_next_zero_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
464 region
.bdev
= wc
->ssd_dev
->bdev
;
465 region
.sector
= (sector_t
)i
* (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
466 region
.count
= (sector_t
)(j
- i
) * (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
468 if (unlikely(region
.sector
>= wc
->metadata_sectors
))
470 if (unlikely(region
.sector
+ region
.count
> wc
->metadata_sectors
))
471 region
.count
= wc
->metadata_sectors
- region
.sector
;
473 region
.sector
+= wc
->start_sector
;
474 atomic_inc(&endio
.count
);
475 req
.bi_op
= REQ_OP_WRITE
;
476 req
.bi_op_flags
= REQ_SYNC
;
477 req
.mem
.type
= DM_IO_VMA
;
478 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
+ (size_t)i
* BITMAP_GRANULARITY
;
479 req
.client
= wc
->dm_io
;
480 req
.notify
.fn
= writecache_notify_io
;
481 req
.notify
.context
= &endio
;
483 /* writing via async dm-io (implied by notify.fn above) won't return an error */
484 (void) dm_io(&req
, 1, ®ion
, NULL
);
488 writecache_notify_io(0, &endio
);
489 wait_for_completion_io(&endio
.c
);
491 writecache_disk_flush(wc
, wc
->ssd_dev
);
493 memset(wc
->dirty_bitmap
, 0, wc
->dirty_bitmap_size
);
496 static void writecache_commit_flushed(struct dm_writecache
*wc
)
498 if (WC_MODE_PMEM(wc
))
501 ssd_commit_flushed(wc
);
504 static void writecache_disk_flush(struct dm_writecache
*wc
, struct dm_dev
*dev
)
507 struct dm_io_region region
;
508 struct dm_io_request req
;
510 region
.bdev
= dev
->bdev
;
513 req
.bi_op
= REQ_OP_WRITE
;
514 req
.bi_op_flags
= REQ_PREFLUSH
;
515 req
.mem
.type
= DM_IO_KMEM
;
516 req
.mem
.ptr
.addr
= NULL
;
517 req
.client
= wc
->dm_io
;
518 req
.notify
.fn
= NULL
;
520 r
= dm_io(&req
, 1, ®ion
, NULL
);
522 writecache_error(wc
, r
, "error flushing metadata: %d", r
);
525 static void writecache_wait_for_ios(struct dm_writecache
*wc
, int direction
)
527 wait_event(wc
->bio_in_progress_wait
[direction
],
528 !atomic_read(&wc
->bio_in_progress
[direction
]));
531 #define WFE_RETURN_FOLLOWING 1
532 #define WFE_LOWEST_SEQ 2
534 static struct wc_entry
*writecache_find_entry(struct dm_writecache
*wc
,
535 uint64_t block
, int flags
)
538 struct rb_node
*node
= wc
->tree
.rb_node
;
544 e
= container_of(node
, struct wc_entry
, rb_node
);
545 if (read_original_sector(wc
, e
) == block
)
548 node
= (read_original_sector(wc
, e
) >= block
?
549 e
->rb_node
.rb_left
: e
->rb_node
.rb_right
);
550 if (unlikely(!node
)) {
551 if (!(flags
& WFE_RETURN_FOLLOWING
))
553 if (read_original_sector(wc
, e
) >= block
) {
556 node
= rb_next(&e
->rb_node
);
559 e
= container_of(node
, struct wc_entry
, rb_node
);
567 if (flags
& WFE_LOWEST_SEQ
)
568 node
= rb_prev(&e
->rb_node
);
570 node
= rb_next(&e
->rb_node
);
573 e2
= container_of(node
, struct wc_entry
, rb_node
);
574 if (read_original_sector(wc
, e2
) != block
)
580 static void writecache_insert_entry(struct dm_writecache
*wc
, struct wc_entry
*ins
)
583 struct rb_node
**node
= &wc
->tree
.rb_node
, *parent
= NULL
;
586 e
= container_of(*node
, struct wc_entry
, rb_node
);
587 parent
= &e
->rb_node
;
588 if (read_original_sector(wc
, e
) > read_original_sector(wc
, ins
))
589 node
= &parent
->rb_left
;
591 node
= &parent
->rb_right
;
593 rb_link_node(&ins
->rb_node
, parent
, node
);
594 rb_insert_color(&ins
->rb_node
, &wc
->tree
);
595 list_add(&ins
->lru
, &wc
->lru
);
598 static void writecache_unlink(struct dm_writecache
*wc
, struct wc_entry
*e
)
601 rb_erase(&e
->rb_node
, &wc
->tree
);
604 static void writecache_add_to_freelist(struct dm_writecache
*wc
, struct wc_entry
*e
)
606 if (WC_MODE_SORT_FREELIST(wc
)) {
607 struct rb_node
**node
= &wc
->freetree
.rb_node
, *parent
= NULL
;
608 if (unlikely(!*node
))
609 wc
->current_free
= e
;
612 if (&e
->rb_node
< *node
)
613 node
= &parent
->rb_left
;
615 node
= &parent
->rb_right
;
617 rb_link_node(&e
->rb_node
, parent
, node
);
618 rb_insert_color(&e
->rb_node
, &wc
->freetree
);
620 list_add_tail(&e
->lru
, &wc
->freelist
);
625 static struct wc_entry
*writecache_pop_from_freelist(struct dm_writecache
*wc
)
629 if (WC_MODE_SORT_FREELIST(wc
)) {
630 struct rb_node
*next
;
631 if (unlikely(!wc
->current_free
))
633 e
= wc
->current_free
;
634 next
= rb_next(&e
->rb_node
);
635 rb_erase(&e
->rb_node
, &wc
->freetree
);
637 next
= rb_first(&wc
->freetree
);
638 wc
->current_free
= next
? container_of(next
, struct wc_entry
, rb_node
) : NULL
;
640 if (unlikely(list_empty(&wc
->freelist
)))
642 e
= container_of(wc
->freelist
.next
, struct wc_entry
, lru
);
646 if (unlikely(wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_high_watermark
))
647 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
652 static void writecache_free_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
654 writecache_unlink(wc
, e
);
655 writecache_add_to_freelist(wc
, e
);
656 clear_seq_count(wc
, e
);
657 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
658 if (unlikely(waitqueue_active(&wc
->freelist_wait
)))
659 wake_up(&wc
->freelist_wait
);
662 static void writecache_wait_on_freelist(struct dm_writecache
*wc
)
666 prepare_to_wait(&wc
->freelist_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
669 finish_wait(&wc
->freelist_wait
, &wait
);
673 static void writecache_poison_lists(struct dm_writecache
*wc
)
676 * Catch incorrect access to these values while the device is suspended.
678 memset(&wc
->tree
, -1, sizeof wc
->tree
);
679 wc
->lru
.next
= LIST_POISON1
;
680 wc
->lru
.prev
= LIST_POISON2
;
681 wc
->freelist
.next
= LIST_POISON1
;
682 wc
->freelist
.prev
= LIST_POISON2
;
685 static void writecache_flush_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
687 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
688 if (WC_MODE_PMEM(wc
))
689 writecache_flush_region(wc
, memory_data(wc
, e
), wc
->block_size
);
692 static bool writecache_entry_is_committed(struct dm_writecache
*wc
, struct wc_entry
*e
)
694 return read_seq_count(wc
, e
) < wc
->seq_count
;
697 static void writecache_flush(struct dm_writecache
*wc
)
699 struct wc_entry
*e
, *e2
;
700 bool need_flush_after_free
;
702 wc
->uncommitted_blocks
= 0;
703 del_timer(&wc
->autocommit_timer
);
705 if (list_empty(&wc
->lru
))
708 e
= container_of(wc
->lru
.next
, struct wc_entry
, lru
);
709 if (writecache_entry_is_committed(wc
, e
)) {
710 if (wc
->overwrote_committed
) {
711 writecache_wait_for_ios(wc
, WRITE
);
712 writecache_disk_flush(wc
, wc
->ssd_dev
);
713 wc
->overwrote_committed
= false;
718 writecache_flush_entry(wc
, e
);
719 if (unlikely(e
->lru
.next
== &wc
->lru
))
721 e2
= container_of(e
->lru
.next
, struct wc_entry
, lru
);
722 if (writecache_entry_is_committed(wc
, e2
))
727 writecache_commit_flushed(wc
);
729 if (!WC_MODE_PMEM(wc
))
730 writecache_wait_for_ios(wc
, WRITE
);
733 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(wc
->seq_count
));
734 writecache_flush_region(wc
, &sb(wc
)->seq_count
, sizeof sb(wc
)->seq_count
);
735 writecache_commit_flushed(wc
);
737 wc
->overwrote_committed
= false;
739 need_flush_after_free
= false;
741 /* Free another committed entry with lower seq-count */
742 struct rb_node
*rb_node
= rb_prev(&e
->rb_node
);
745 e2
= container_of(rb_node
, struct wc_entry
, rb_node
);
746 if (read_original_sector(wc
, e2
) == read_original_sector(wc
, e
) &&
747 likely(!e2
->write_in_progress
)) {
748 writecache_free_entry(wc
, e2
);
749 need_flush_after_free
= true;
752 if (unlikely(e
->lru
.prev
== &wc
->lru
))
754 e
= container_of(e
->lru
.prev
, struct wc_entry
, lru
);
758 if (need_flush_after_free
)
759 writecache_commit_flushed(wc
);
762 static void writecache_flush_work(struct work_struct
*work
)
764 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, flush_work
);
767 writecache_flush(wc
);
771 static void writecache_autocommit_timer(struct timer_list
*t
)
773 struct dm_writecache
*wc
= from_timer(wc
, t
, autocommit_timer
);
774 if (!writecache_has_error(wc
))
775 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
778 static void writecache_schedule_autocommit(struct dm_writecache
*wc
)
780 if (!timer_pending(&wc
->autocommit_timer
))
781 mod_timer(&wc
->autocommit_timer
, jiffies
+ wc
->autocommit_jiffies
);
784 static void writecache_discard(struct dm_writecache
*wc
, sector_t start
, sector_t end
)
787 bool discarded_something
= false;
789 e
= writecache_find_entry(wc
, start
, WFE_RETURN_FOLLOWING
| WFE_LOWEST_SEQ
);
793 while (read_original_sector(wc
, e
) < end
) {
794 struct rb_node
*node
= rb_next(&e
->rb_node
);
796 if (likely(!e
->write_in_progress
)) {
797 if (!discarded_something
) {
798 writecache_wait_for_ios(wc
, READ
);
799 writecache_wait_for_ios(wc
, WRITE
);
800 discarded_something
= true;
802 writecache_free_entry(wc
, e
);
808 e
= container_of(node
, struct wc_entry
, rb_node
);
811 if (discarded_something
)
812 writecache_commit_flushed(wc
);
815 static bool writecache_wait_for_writeback(struct dm_writecache
*wc
)
817 if (wc
->writeback_size
) {
818 writecache_wait_on_freelist(wc
);
824 static void writecache_suspend(struct dm_target
*ti
)
826 struct dm_writecache
*wc
= ti
->private;
827 bool flush_on_suspend
;
829 del_timer_sync(&wc
->autocommit_timer
);
832 writecache_flush(wc
);
833 flush_on_suspend
= wc
->flush_on_suspend
;
834 if (flush_on_suspend
) {
835 wc
->flush_on_suspend
= false;
837 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
841 flush_workqueue(wc
->writeback_wq
);
844 if (flush_on_suspend
)
846 while (writecache_wait_for_writeback(wc
));
848 if (WC_MODE_PMEM(wc
))
849 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
851 writecache_poison_lists(wc
);
856 static int writecache_alloc_entries(struct dm_writecache
*wc
)
862 wc
->entries
= vmalloc(array_size(sizeof(struct wc_entry
), wc
->n_blocks
));
865 for (b
= 0; b
< wc
->n_blocks
; b
++) {
866 struct wc_entry
*e
= &wc
->entries
[b
];
868 e
->write_in_progress
= false;
874 static void writecache_resume(struct dm_target
*ti
)
876 struct dm_writecache
*wc
= ti
->private;
878 bool need_flush
= false;
884 if (WC_MODE_PMEM(wc
))
885 persistent_memory_invalidate_cache(wc
->memory_map
, wc
->memory_map_size
);
888 INIT_LIST_HEAD(&wc
->lru
);
889 if (WC_MODE_SORT_FREELIST(wc
)) {
890 wc
->freetree
= RB_ROOT
;
891 wc
->current_free
= NULL
;
893 INIT_LIST_HEAD(&wc
->freelist
);
895 wc
->freelist_size
= 0;
897 r
= memcpy_mcsafe(&sb_seq_count
, &sb(wc
)->seq_count
, sizeof(uint64_t));
899 writecache_error(wc
, r
, "hardware memory error when reading superblock: %d", r
);
900 sb_seq_count
= cpu_to_le64(0);
902 wc
->seq_count
= le64_to_cpu(sb_seq_count
);
904 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
905 for (b
= 0; b
< wc
->n_blocks
; b
++) {
906 struct wc_entry
*e
= &wc
->entries
[b
];
907 struct wc_memory_entry wme
;
908 if (writecache_has_error(wc
)) {
909 e
->original_sector
= -1;
913 r
= memcpy_mcsafe(&wme
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
915 writecache_error(wc
, r
, "hardware memory error when reading metadata entry %lu: %d",
916 (unsigned long)b
, r
);
917 e
->original_sector
= -1;
920 e
->original_sector
= le64_to_cpu(wme
.original_sector
);
921 e
->seq_count
= le64_to_cpu(wme
.seq_count
);
925 for (b
= 0; b
< wc
->n_blocks
; b
++) {
926 struct wc_entry
*e
= &wc
->entries
[b
];
927 if (!writecache_entry_is_committed(wc
, e
)) {
928 if (read_seq_count(wc
, e
) != -1) {
930 clear_seq_count(wc
, e
);
933 writecache_add_to_freelist(wc
, e
);
935 struct wc_entry
*old
;
937 old
= writecache_find_entry(wc
, read_original_sector(wc
, e
), 0);
939 writecache_insert_entry(wc
, e
);
941 if (read_seq_count(wc
, old
) == read_seq_count(wc
, e
)) {
942 writecache_error(wc
, -EINVAL
,
943 "two identical entries, position %llu, sector %llu, sequence %llu",
944 (unsigned long long)b
, (unsigned long long)read_original_sector(wc
, e
),
945 (unsigned long long)read_seq_count(wc
, e
));
947 if (read_seq_count(wc
, old
) > read_seq_count(wc
, e
)) {
950 writecache_free_entry(wc
, old
);
951 writecache_insert_entry(wc
, e
);
960 writecache_flush_all_metadata(wc
);
961 writecache_commit_flushed(wc
);
967 static int process_flush_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
973 if (dm_suspended(wc
->ti
)) {
977 if (writecache_has_error(wc
)) {
982 writecache_flush(wc
);
984 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
987 flush_workqueue(wc
->writeback_wq
);
991 if (writecache_has_error(wc
)) {
1000 static int process_flush_on_suspend_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
1006 wc
->flush_on_suspend
= true;
1012 static int writecache_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
1013 char *result
, unsigned maxlen
)
1016 struct dm_writecache
*wc
= ti
->private;
1018 if (!strcasecmp(argv
[0], "flush"))
1019 r
= process_flush_mesg(argc
, argv
, wc
);
1020 else if (!strcasecmp(argv
[0], "flush_on_suspend"))
1021 r
= process_flush_on_suspend_mesg(argc
, argv
, wc
);
1023 DMERR("unrecognised message received: %s", argv
[0]);
1028 static void bio_copy_block(struct dm_writecache
*wc
, struct bio
*bio
, void *data
)
1031 unsigned long flags
;
1033 int rw
= bio_data_dir(bio
);
1034 unsigned remaining_size
= wc
->block_size
;
1037 struct bio_vec bv
= bio_iter_iovec(bio
, bio
->bi_iter
);
1038 buf
= bvec_kmap_irq(&bv
, &flags
);
1040 if (unlikely(size
> remaining_size
))
1041 size
= remaining_size
;
1045 r
= memcpy_mcsafe(buf
, data
, size
);
1046 flush_dcache_page(bio_page(bio
));
1048 writecache_error(wc
, r
, "hardware memory error when reading data: %d", r
);
1049 bio
->bi_status
= BLK_STS_IOERR
;
1052 flush_dcache_page(bio_page(bio
));
1053 memcpy_flushcache(data
, buf
, size
);
1056 bvec_kunmap_irq(buf
, &flags
);
1058 data
= (char *)data
+ size
;
1059 remaining_size
-= size
;
1060 bio_advance(bio
, size
);
1061 } while (unlikely(remaining_size
));
1064 static int writecache_flush_thread(void *data
)
1066 struct dm_writecache
*wc
= data
;
1072 bio
= bio_list_pop(&wc
->flush_list
);
1074 set_current_state(TASK_INTERRUPTIBLE
);
1077 if (unlikely(kthread_should_stop())) {
1078 set_current_state(TASK_RUNNING
);
1086 if (bio_op(bio
) == REQ_OP_DISCARD
) {
1087 writecache_discard(wc
, bio
->bi_iter
.bi_sector
,
1088 bio_end_sector(bio
));
1090 bio_set_dev(bio
, wc
->dev
->bdev
);
1091 generic_make_request(bio
);
1093 writecache_flush(wc
);
1095 if (writecache_has_error(wc
))
1096 bio
->bi_status
= BLK_STS_IOERR
;
1104 static void writecache_offload_bio(struct dm_writecache
*wc
, struct bio
*bio
)
1106 if (bio_list_empty(&wc
->flush_list
))
1107 wake_up_process(wc
->flush_thread
);
1108 bio_list_add(&wc
->flush_list
, bio
);
1111 static int writecache_map(struct dm_target
*ti
, struct bio
*bio
)
1114 struct dm_writecache
*wc
= ti
->private;
1116 bio
->bi_private
= NULL
;
1120 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1121 if (writecache_has_error(wc
))
1123 if (WC_MODE_PMEM(wc
)) {
1124 writecache_flush(wc
);
1125 if (writecache_has_error(wc
))
1129 writecache_offload_bio(wc
, bio
);
1134 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
1136 if (unlikely((((unsigned)bio
->bi_iter
.bi_sector
| bio_sectors(bio
)) &
1137 (wc
->block_size
/ 512 - 1)) != 0)) {
1138 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1139 (unsigned long long)bio
->bi_iter
.bi_sector
,
1140 bio
->bi_iter
.bi_size
, wc
->block_size
);
1144 if (unlikely(bio_op(bio
) == REQ_OP_DISCARD
)) {
1145 if (writecache_has_error(wc
))
1147 if (WC_MODE_PMEM(wc
)) {
1148 writecache_discard(wc
, bio
->bi_iter
.bi_sector
, bio_end_sector(bio
));
1149 goto unlock_remap_origin
;
1151 writecache_offload_bio(wc
, bio
);
1156 if (bio_data_dir(bio
) == READ
) {
1158 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, WFE_RETURN_FOLLOWING
);
1159 if (e
&& read_original_sector(wc
, e
) == bio
->bi_iter
.bi_sector
) {
1160 if (WC_MODE_PMEM(wc
)) {
1161 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1162 if (bio
->bi_iter
.bi_size
)
1163 goto read_next_block
;
1166 dm_accept_partial_bio(bio
, wc
->block_size
>> SECTOR_SHIFT
);
1167 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1168 bio
->bi_iter
.bi_sector
= cache_sector(wc
, e
);
1169 if (!writecache_entry_is_committed(wc
, e
))
1170 writecache_wait_for_ios(wc
, WRITE
);
1175 sector_t next_boundary
=
1176 read_original_sector(wc
, e
) - bio
->bi_iter
.bi_sector
;
1177 if (next_boundary
< bio
->bi_iter
.bi_size
>> SECTOR_SHIFT
) {
1178 dm_accept_partial_bio(bio
, next_boundary
);
1181 goto unlock_remap_origin
;
1185 if (writecache_has_error(wc
))
1187 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, 0);
1189 if (!writecache_entry_is_committed(wc
, e
))
1191 if (!WC_MODE_PMEM(wc
) && !e
->write_in_progress
) {
1192 wc
->overwrote_committed
= true;
1196 e
= writecache_pop_from_freelist(wc
);
1198 writecache_wait_on_freelist(wc
);
1201 write_original_sector_seq_count(wc
, e
, bio
->bi_iter
.bi_sector
, wc
->seq_count
);
1202 writecache_insert_entry(wc
, e
);
1203 wc
->uncommitted_blocks
++;
1205 if (WC_MODE_PMEM(wc
)) {
1206 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1208 dm_accept_partial_bio(bio
, wc
->block_size
>> SECTOR_SHIFT
);
1209 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1210 bio
->bi_iter
.bi_sector
= cache_sector(wc
, e
);
1211 if (unlikely(wc
->uncommitted_blocks
>= wc
->autocommit_blocks
)) {
1212 wc
->uncommitted_blocks
= 0;
1213 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
1215 writecache_schedule_autocommit(wc
);
1219 } while (bio
->bi_iter
.bi_size
);
1221 if (unlikely(bio
->bi_opf
& REQ_FUA
||
1222 wc
->uncommitted_blocks
>= wc
->autocommit_blocks
))
1223 writecache_flush(wc
);
1225 writecache_schedule_autocommit(wc
);
1229 unlock_remap_origin
:
1230 bio_set_dev(bio
, wc
->dev
->bdev
);
1232 return DM_MAPIO_REMAPPED
;
1235 /* make sure that writecache_end_io decrements bio_in_progress: */
1236 bio
->bi_private
= (void *)1;
1237 atomic_inc(&wc
->bio_in_progress
[bio_data_dir(bio
)]);
1239 return DM_MAPIO_REMAPPED
;
1244 return DM_MAPIO_SUBMITTED
;
1248 return DM_MAPIO_SUBMITTED
;
1253 return DM_MAPIO_SUBMITTED
;
1256 static int writecache_end_io(struct dm_target
*ti
, struct bio
*bio
, blk_status_t
*status
)
1258 struct dm_writecache
*wc
= ti
->private;
1260 if (bio
->bi_private
!= NULL
) {
1261 int dir
= bio_data_dir(bio
);
1262 if (atomic_dec_and_test(&wc
->bio_in_progress
[dir
]))
1263 if (unlikely(waitqueue_active(&wc
->bio_in_progress_wait
[dir
])))
1264 wake_up(&wc
->bio_in_progress_wait
[dir
]);
1269 static int writecache_iterate_devices(struct dm_target
*ti
,
1270 iterate_devices_callout_fn fn
, void *data
)
1272 struct dm_writecache
*wc
= ti
->private;
1274 return fn(ti
, wc
->dev
, 0, ti
->len
, data
);
1277 static void writecache_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1279 struct dm_writecache
*wc
= ti
->private;
1281 if (limits
->logical_block_size
< wc
->block_size
)
1282 limits
->logical_block_size
= wc
->block_size
;
1284 if (limits
->physical_block_size
< wc
->block_size
)
1285 limits
->physical_block_size
= wc
->block_size
;
1287 if (limits
->io_min
< wc
->block_size
)
1288 limits
->io_min
= wc
->block_size
;
1292 static void writecache_writeback_endio(struct bio
*bio
)
1294 struct writeback_struct
*wb
= container_of(bio
, struct writeback_struct
, bio
);
1295 struct dm_writecache
*wc
= wb
->wc
;
1296 unsigned long flags
;
1298 raw_spin_lock_irqsave(&wc
->endio_list_lock
, flags
);
1299 if (unlikely(list_empty(&wc
->endio_list
)))
1300 wake_up_process(wc
->endio_thread
);
1301 list_add_tail(&wb
->endio_entry
, &wc
->endio_list
);
1302 raw_spin_unlock_irqrestore(&wc
->endio_list_lock
, flags
);
1305 static void writecache_copy_endio(int read_err
, unsigned long write_err
, void *ptr
)
1307 struct copy_struct
*c
= ptr
;
1308 struct dm_writecache
*wc
= c
->wc
;
1310 c
->error
= likely(!(read_err
| write_err
)) ? 0 : -EIO
;
1312 raw_spin_lock_irq(&wc
->endio_list_lock
);
1313 if (unlikely(list_empty(&wc
->endio_list
)))
1314 wake_up_process(wc
->endio_thread
);
1315 list_add_tail(&c
->endio_entry
, &wc
->endio_list
);
1316 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1319 static void __writecache_endio_pmem(struct dm_writecache
*wc
, struct list_head
*list
)
1322 struct writeback_struct
*wb
;
1324 unsigned long n_walked
= 0;
1327 wb
= list_entry(list
->next
, struct writeback_struct
, endio_entry
);
1328 list_del(&wb
->endio_entry
);
1330 if (unlikely(wb
->bio
.bi_status
!= BLK_STS_OK
))
1331 writecache_error(wc
, blk_status_to_errno(wb
->bio
.bi_status
),
1332 "write error %d", wb
->bio
.bi_status
);
1336 BUG_ON(!e
->write_in_progress
);
1337 e
->write_in_progress
= false;
1338 INIT_LIST_HEAD(&e
->lru
);
1339 if (!writecache_has_error(wc
))
1340 writecache_free_entry(wc
, e
);
1341 BUG_ON(!wc
->writeback_size
);
1342 wc
->writeback_size
--;
1344 if (unlikely(n_walked
>= ENDIO_LATENCY
)) {
1345 writecache_commit_flushed(wc
);
1350 } while (++i
< wb
->wc_list_n
);
1352 if (wb
->wc_list
!= wb
->wc_list_inline
)
1355 } while (!list_empty(list
));
1358 static void __writecache_endio_ssd(struct dm_writecache
*wc
, struct list_head
*list
)
1360 struct copy_struct
*c
;
1364 c
= list_entry(list
->next
, struct copy_struct
, endio_entry
);
1365 list_del(&c
->endio_entry
);
1367 if (unlikely(c
->error
))
1368 writecache_error(wc
, c
->error
, "copy error");
1372 BUG_ON(!e
->write_in_progress
);
1373 e
->write_in_progress
= false;
1374 INIT_LIST_HEAD(&e
->lru
);
1375 if (!writecache_has_error(wc
))
1376 writecache_free_entry(wc
, e
);
1378 BUG_ON(!wc
->writeback_size
);
1379 wc
->writeback_size
--;
1381 } while (--c
->n_entries
);
1382 mempool_free(c
, &wc
->copy_pool
);
1383 } while (!list_empty(list
));
1386 static int writecache_endio_thread(void *data
)
1388 struct dm_writecache
*wc
= data
;
1391 struct list_head list
;
1393 raw_spin_lock_irq(&wc
->endio_list_lock
);
1394 if (!list_empty(&wc
->endio_list
))
1396 set_current_state(TASK_INTERRUPTIBLE
);
1397 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1399 if (unlikely(kthread_should_stop())) {
1400 set_current_state(TASK_RUNNING
);
1409 list
= wc
->endio_list
;
1410 list
.next
->prev
= list
.prev
->next
= &list
;
1411 INIT_LIST_HEAD(&wc
->endio_list
);
1412 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1414 if (!WC_MODE_FUA(wc
))
1415 writecache_disk_flush(wc
, wc
->dev
);
1419 if (WC_MODE_PMEM(wc
)) {
1420 __writecache_endio_pmem(wc
, &list
);
1422 __writecache_endio_ssd(wc
, &list
);
1423 writecache_wait_for_ios(wc
, READ
);
1426 writecache_commit_flushed(wc
);
1434 static bool wc_add_block(struct writeback_struct
*wb
, struct wc_entry
*e
, gfp_t gfp
)
1436 struct dm_writecache
*wc
= wb
->wc
;
1437 unsigned block_size
= wc
->block_size
;
1438 void *address
= memory_data(wc
, e
);
1440 persistent_memory_flush_cache(address
, block_size
);
1441 return bio_add_page(&wb
->bio
, persistent_memory_page(address
),
1442 block_size
, persistent_memory_page_offset(address
)) != 0;
1445 struct writeback_list
{
1446 struct list_head list
;
1450 static void __writeback_throttle(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1452 if (unlikely(wc
->max_writeback_jobs
)) {
1453 if (READ_ONCE(wc
->writeback_size
) - wbl
->size
>= wc
->max_writeback_jobs
) {
1455 while (wc
->writeback_size
- wbl
->size
>= wc
->max_writeback_jobs
)
1456 writecache_wait_on_freelist(wc
);
1463 static void __writecache_writeback_pmem(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1465 struct wc_entry
*e
, *f
;
1467 struct writeback_struct
*wb
;
1472 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1475 max_pages
= e
->wc_list_contiguous
;
1477 bio
= bio_alloc_bioset(GFP_NOIO
, max_pages
, &wc
->bio_set
);
1478 wb
= container_of(bio
, struct writeback_struct
, bio
);
1480 bio
->bi_end_io
= writecache_writeback_endio
;
1481 bio_set_dev(bio
, wc
->dev
->bdev
);
1482 bio
->bi_iter
.bi_sector
= read_original_sector(wc
, e
);
1483 if (max_pages
<= WB_LIST_INLINE
||
1484 unlikely(!(wb
->wc_list
= kmalloc_array(max_pages
, sizeof(struct wc_entry
*),
1485 GFP_NOIO
| __GFP_NORETRY
|
1486 __GFP_NOMEMALLOC
| __GFP_NOWARN
)))) {
1487 wb
->wc_list
= wb
->wc_list_inline
;
1488 max_pages
= WB_LIST_INLINE
;
1491 BUG_ON(!wc_add_block(wb
, e
, GFP_NOIO
));
1496 while (wbl
->size
&& wb
->wc_list_n
< max_pages
) {
1497 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1498 if (read_original_sector(wc
, f
) !=
1499 read_original_sector(wc
, e
) + (wc
->block_size
>> SECTOR_SHIFT
))
1501 if (!wc_add_block(wb
, f
, GFP_NOWAIT
| __GFP_NOWARN
))
1505 wb
->wc_list
[wb
->wc_list_n
++] = f
;
1508 bio_set_op_attrs(bio
, REQ_OP_WRITE
, WC_MODE_FUA(wc
) * REQ_FUA
);
1509 if (writecache_has_error(wc
)) {
1510 bio
->bi_status
= BLK_STS_IOERR
;
1516 __writeback_throttle(wc
, wbl
);
1520 static void __writecache_writeback_ssd(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1522 struct wc_entry
*e
, *f
;
1523 struct dm_io_region from
, to
;
1524 struct copy_struct
*c
;
1530 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1533 n_sectors
= e
->wc_list_contiguous
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
1535 from
.bdev
= wc
->ssd_dev
->bdev
;
1536 from
.sector
= cache_sector(wc
, e
);
1537 from
.count
= n_sectors
;
1538 to
.bdev
= wc
->dev
->bdev
;
1539 to
.sector
= read_original_sector(wc
, e
);
1540 to
.count
= n_sectors
;
1542 c
= mempool_alloc(&wc
->copy_pool
, GFP_NOIO
);
1545 c
->n_entries
= e
->wc_list_contiguous
;
1547 while ((n_sectors
-= wc
->block_size
>> SECTOR_SHIFT
)) {
1549 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1555 dm_kcopyd_copy(wc
->dm_kcopyd
, &from
, 1, &to
, 0, writecache_copy_endio
, c
);
1557 __writeback_throttle(wc
, wbl
);
1561 static void writecache_writeback(struct work_struct
*work
)
1563 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, writeback_work
);
1564 struct blk_plug plug
;
1565 struct wc_entry
*f
, *uninitialized_var(g
), *e
= NULL
;
1566 struct rb_node
*node
, *next_node
;
1567 struct list_head skipped
;
1568 struct writeback_list wbl
;
1569 unsigned long n_walked
;
1573 if (writecache_has_error(wc
)) {
1578 if (unlikely(wc
->writeback_all
)) {
1579 if (writecache_wait_for_writeback(wc
))
1583 if (wc
->overwrote_committed
) {
1584 writecache_wait_for_ios(wc
, WRITE
);
1588 INIT_LIST_HEAD(&skipped
);
1589 INIT_LIST_HEAD(&wbl
.list
);
1591 while (!list_empty(&wc
->lru
) &&
1592 (wc
->writeback_all
||
1593 wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_low_watermark
)) {
1596 if (unlikely(n_walked
> WRITEBACK_LATENCY
) &&
1597 likely(!wc
->writeback_all
) && likely(!dm_suspended(wc
->ti
))) {
1598 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
1602 if (unlikely(wc
->writeback_all
)) {
1604 writecache_flush(wc
);
1605 e
= container_of(rb_first(&wc
->tree
), struct wc_entry
, rb_node
);
1609 e
= container_of(wc
->lru
.prev
, struct wc_entry
, lru
);
1610 BUG_ON(e
->write_in_progress
);
1611 if (unlikely(!writecache_entry_is_committed(wc
, e
))) {
1612 writecache_flush(wc
);
1614 node
= rb_prev(&e
->rb_node
);
1616 f
= container_of(node
, struct wc_entry
, rb_node
);
1617 if (unlikely(read_original_sector(wc
, f
) ==
1618 read_original_sector(wc
, e
))) {
1619 BUG_ON(!f
->write_in_progress
);
1621 list_add(&e
->lru
, &skipped
);
1626 wc
->writeback_size
++;
1628 list_add(&e
->lru
, &wbl
.list
);
1630 e
->write_in_progress
= true;
1631 e
->wc_list_contiguous
= 1;
1636 next_node
= rb_next(&f
->rb_node
);
1637 if (unlikely(!next_node
))
1639 g
= container_of(next_node
, struct wc_entry
, rb_node
);
1640 if (unlikely(read_original_sector(wc
, g
) ==
1641 read_original_sector(wc
, f
))) {
1645 if (read_original_sector(wc
, g
) !=
1646 read_original_sector(wc
, f
) + (wc
->block_size
>> SECTOR_SHIFT
))
1648 if (unlikely(g
->write_in_progress
))
1650 if (unlikely(!writecache_entry_is_committed(wc
, g
)))
1653 if (!WC_MODE_PMEM(wc
)) {
1659 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1662 wc
->writeback_size
++;
1664 list_add(&g
->lru
, &wbl
.list
);
1666 g
->write_in_progress
= true;
1667 g
->wc_list_contiguous
= BIO_MAX_PAGES
;
1669 e
->wc_list_contiguous
++;
1670 if (unlikely(e
->wc_list_contiguous
== BIO_MAX_PAGES
)) {
1671 if (unlikely(wc
->writeback_all
)) {
1672 next_node
= rb_next(&f
->rb_node
);
1673 if (likely(next_node
))
1674 g
= container_of(next_node
, struct wc_entry
, rb_node
);
1682 if (!list_empty(&skipped
)) {
1683 list_splice_tail(&skipped
, &wc
->lru
);
1685 * If we didn't do any progress, we must wait until some
1686 * writeback finishes to avoid burning CPU in a loop
1688 if (unlikely(!wbl
.size
))
1689 writecache_wait_for_writeback(wc
);
1694 blk_start_plug(&plug
);
1696 if (WC_MODE_PMEM(wc
))
1697 __writecache_writeback_pmem(wc
, &wbl
);
1699 __writecache_writeback_ssd(wc
, &wbl
);
1701 blk_finish_plug(&plug
);
1703 if (unlikely(wc
->writeback_all
)) {
1705 while (writecache_wait_for_writeback(wc
));
1710 static int calculate_memory_size(uint64_t device_size
, unsigned block_size
,
1711 size_t *n_blocks_p
, size_t *n_metadata_blocks_p
)
1713 uint64_t n_blocks
, offset
;
1716 n_blocks
= device_size
;
1717 do_div(n_blocks
, block_size
+ sizeof(struct wc_memory_entry
));
1722 /* Verify the following entries[n_blocks] won't overflow */
1723 if (n_blocks
>= ((size_t)-sizeof(struct wc_memory_superblock
) /
1724 sizeof(struct wc_memory_entry
)))
1726 offset
= offsetof(struct wc_memory_superblock
, entries
[n_blocks
]);
1727 offset
= (offset
+ block_size
- 1) & ~(uint64_t)(block_size
- 1);
1728 if (offset
+ n_blocks
* block_size
<= device_size
)
1733 /* check if the bit field overflows */
1735 if (e
.index
!= n_blocks
)
1739 *n_blocks_p
= n_blocks
;
1740 if (n_metadata_blocks_p
)
1741 *n_metadata_blocks_p
= offset
>> __ffs(block_size
);
1745 static int init_memory(struct dm_writecache
*wc
)
1750 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
, &wc
->n_blocks
, NULL
);
1754 r
= writecache_alloc_entries(wc
);
1758 for (b
= 0; b
< ARRAY_SIZE(sb(wc
)->padding
); b
++)
1759 pmem_assign(sb(wc
)->padding
[b
], cpu_to_le64(0));
1760 pmem_assign(sb(wc
)->version
, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION
));
1761 pmem_assign(sb(wc
)->block_size
, cpu_to_le32(wc
->block_size
));
1762 pmem_assign(sb(wc
)->n_blocks
, cpu_to_le64(wc
->n_blocks
));
1763 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(0));
1765 for (b
= 0; b
< wc
->n_blocks
; b
++)
1766 write_original_sector_seq_count(wc
, &wc
->entries
[b
], -1, -1);
1768 writecache_flush_all_metadata(wc
);
1769 writecache_commit_flushed(wc
);
1770 pmem_assign(sb(wc
)->magic
, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC
));
1771 writecache_flush_region(wc
, &sb(wc
)->magic
, sizeof sb(wc
)->magic
);
1772 writecache_commit_flushed(wc
);
1777 static void writecache_dtr(struct dm_target
*ti
)
1779 struct dm_writecache
*wc
= ti
->private;
1784 if (wc
->endio_thread
)
1785 kthread_stop(wc
->endio_thread
);
1787 if (wc
->flush_thread
)
1788 kthread_stop(wc
->flush_thread
);
1790 bioset_exit(&wc
->bio_set
);
1792 mempool_exit(&wc
->copy_pool
);
1794 if (wc
->writeback_wq
)
1795 destroy_workqueue(wc
->writeback_wq
);
1798 dm_put_device(ti
, wc
->dev
);
1801 dm_put_device(ti
, wc
->ssd_dev
);
1806 if (wc
->memory_map
) {
1807 if (WC_MODE_PMEM(wc
))
1808 persistent_memory_release(wc
);
1810 vfree(wc
->memory_map
);
1814 dm_kcopyd_client_destroy(wc
->dm_kcopyd
);
1817 dm_io_client_destroy(wc
->dm_io
);
1819 if (wc
->dirty_bitmap
)
1820 vfree(wc
->dirty_bitmap
);
1825 static int writecache_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1827 struct dm_writecache
*wc
;
1828 struct dm_arg_set as
;
1830 unsigned opt_params
;
1831 size_t offset
, data_size
;
1834 int high_wm_percent
= HIGH_WATERMARK
;
1835 int low_wm_percent
= LOW_WATERMARK
;
1837 struct wc_memory_superblock s
;
1839 static struct dm_arg _args
[] = {
1840 {0, 10, "Invalid number of feature args"},
1846 wc
= kzalloc(sizeof(struct dm_writecache
), GFP_KERNEL
);
1848 ti
->error
= "Cannot allocate writecache structure";
1855 mutex_init(&wc
->lock
);
1856 writecache_poison_lists(wc
);
1857 init_waitqueue_head(&wc
->freelist_wait
);
1858 timer_setup(&wc
->autocommit_timer
, writecache_autocommit_timer
, 0);
1860 for (i
= 0; i
< 2; i
++) {
1861 atomic_set(&wc
->bio_in_progress
[i
], 0);
1862 init_waitqueue_head(&wc
->bio_in_progress_wait
[i
]);
1865 wc
->dm_io
= dm_io_client_create();
1866 if (IS_ERR(wc
->dm_io
)) {
1867 r
= PTR_ERR(wc
->dm_io
);
1868 ti
->error
= "Unable to allocate dm-io client";
1873 wc
->writeback_wq
= alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM
, 1);
1874 if (!wc
->writeback_wq
) {
1876 ti
->error
= "Could not allocate writeback workqueue";
1879 INIT_WORK(&wc
->writeback_work
, writecache_writeback
);
1880 INIT_WORK(&wc
->flush_work
, writecache_flush_work
);
1882 raw_spin_lock_init(&wc
->endio_list_lock
);
1883 INIT_LIST_HEAD(&wc
->endio_list
);
1884 wc
->endio_thread
= kthread_create(writecache_endio_thread
, wc
, "writecache_endio");
1885 if (IS_ERR(wc
->endio_thread
)) {
1886 r
= PTR_ERR(wc
->endio_thread
);
1887 wc
->endio_thread
= NULL
;
1888 ti
->error
= "Couldn't spawn endio thread";
1891 wake_up_process(wc
->endio_thread
);
1894 * Parse the mode (pmem or ssd)
1896 string
= dm_shift_arg(&as
);
1900 if (!strcasecmp(string
, "s")) {
1901 wc
->pmem_mode
= false;
1902 } else if (!strcasecmp(string
, "p")) {
1903 #ifdef DM_WRITECACHE_HAS_PMEM
1904 wc
->pmem_mode
= true;
1905 wc
->writeback_fua
= true;
1908 * If the architecture doesn't support persistent memory or
1909 * the kernel doesn't support any DAX drivers, this driver can
1910 * only be used in SSD-only mode.
1913 ti
->error
= "Persistent memory or DAX not supported on this system";
1920 if (WC_MODE_PMEM(wc
)) {
1921 r
= bioset_init(&wc
->bio_set
, BIO_POOL_SIZE
,
1922 offsetof(struct writeback_struct
, bio
),
1925 ti
->error
= "Could not allocate bio set";
1929 r
= mempool_init_kmalloc_pool(&wc
->copy_pool
, 1, sizeof(struct copy_struct
));
1931 ti
->error
= "Could not allocate mempool";
1937 * Parse the origin data device
1939 string
= dm_shift_arg(&as
);
1942 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->dev
);
1944 ti
->error
= "Origin data device lookup failed";
1949 * Parse cache data device (be it pmem or ssd)
1951 string
= dm_shift_arg(&as
);
1955 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->ssd_dev
);
1957 ti
->error
= "Cache data device lookup failed";
1960 wc
->memory_map_size
= i_size_read(wc
->ssd_dev
->bdev
->bd_inode
);
1963 * Parse the cache block size
1965 string
= dm_shift_arg(&as
);
1968 if (sscanf(string
, "%u%c", &wc
->block_size
, &dummy
) != 1 ||
1969 wc
->block_size
< 512 || wc
->block_size
> PAGE_SIZE
||
1970 (wc
->block_size
& (wc
->block_size
- 1))) {
1972 ti
->error
= "Invalid block size";
1975 wc
->block_size_bits
= __ffs(wc
->block_size
);
1977 wc
->max_writeback_jobs
= MAX_WRITEBACK_JOBS
;
1978 wc
->autocommit_blocks
= !WC_MODE_PMEM(wc
) ? AUTOCOMMIT_BLOCKS_SSD
: AUTOCOMMIT_BLOCKS_PMEM
;
1979 wc
->autocommit_jiffies
= msecs_to_jiffies(AUTOCOMMIT_MSEC
);
1982 * Parse optional arguments
1984 r
= dm_read_arg_group(_args
, &as
, &opt_params
, &ti
->error
);
1988 while (opt_params
) {
1989 string
= dm_shift_arg(&as
), opt_params
--;
1990 if (!strcasecmp(string
, "start_sector") && opt_params
>= 1) {
1991 unsigned long long start_sector
;
1992 string
= dm_shift_arg(&as
), opt_params
--;
1993 if (sscanf(string
, "%llu%c", &start_sector
, &dummy
) != 1)
1994 goto invalid_optional
;
1995 wc
->start_sector
= start_sector
;
1996 if (wc
->start_sector
!= start_sector
||
1997 wc
->start_sector
>= wc
->memory_map_size
>> SECTOR_SHIFT
)
1998 goto invalid_optional
;
1999 } else if (!strcasecmp(string
, "high_watermark") && opt_params
>= 1) {
2000 string
= dm_shift_arg(&as
), opt_params
--;
2001 if (sscanf(string
, "%d%c", &high_wm_percent
, &dummy
) != 1)
2002 goto invalid_optional
;
2003 if (high_wm_percent
< 0 || high_wm_percent
> 100)
2004 goto invalid_optional
;
2005 wc
->high_wm_percent_set
= true;
2006 } else if (!strcasecmp(string
, "low_watermark") && opt_params
>= 1) {
2007 string
= dm_shift_arg(&as
), opt_params
--;
2008 if (sscanf(string
, "%d%c", &low_wm_percent
, &dummy
) != 1)
2009 goto invalid_optional
;
2010 if (low_wm_percent
< 0 || low_wm_percent
> 100)
2011 goto invalid_optional
;
2012 wc
->low_wm_percent_set
= true;
2013 } else if (!strcasecmp(string
, "writeback_jobs") && opt_params
>= 1) {
2014 string
= dm_shift_arg(&as
), opt_params
--;
2015 if (sscanf(string
, "%u%c", &wc
->max_writeback_jobs
, &dummy
) != 1)
2016 goto invalid_optional
;
2017 wc
->max_writeback_jobs_set
= true;
2018 } else if (!strcasecmp(string
, "autocommit_blocks") && opt_params
>= 1) {
2019 string
= dm_shift_arg(&as
), opt_params
--;
2020 if (sscanf(string
, "%u%c", &wc
->autocommit_blocks
, &dummy
) != 1)
2021 goto invalid_optional
;
2022 wc
->autocommit_blocks_set
= true;
2023 } else if (!strcasecmp(string
, "autocommit_time") && opt_params
>= 1) {
2024 unsigned autocommit_msecs
;
2025 string
= dm_shift_arg(&as
), opt_params
--;
2026 if (sscanf(string
, "%u%c", &autocommit_msecs
, &dummy
) != 1)
2027 goto invalid_optional
;
2028 if (autocommit_msecs
> 3600000)
2029 goto invalid_optional
;
2030 wc
->autocommit_jiffies
= msecs_to_jiffies(autocommit_msecs
);
2031 wc
->autocommit_time_set
= true;
2032 } else if (!strcasecmp(string
, "fua")) {
2033 if (WC_MODE_PMEM(wc
)) {
2034 wc
->writeback_fua
= true;
2035 wc
->writeback_fua_set
= true;
2036 } else goto invalid_optional
;
2037 } else if (!strcasecmp(string
, "nofua")) {
2038 if (WC_MODE_PMEM(wc
)) {
2039 wc
->writeback_fua
= false;
2040 wc
->writeback_fua_set
= true;
2041 } else goto invalid_optional
;
2045 ti
->error
= "Invalid optional argument";
2050 if (high_wm_percent
< low_wm_percent
) {
2052 ti
->error
= "High watermark must be greater than or equal to low watermark";
2056 if (WC_MODE_PMEM(wc
)) {
2057 r
= persistent_memory_claim(wc
);
2059 ti
->error
= "Unable to map persistent memory for cache";
2063 struct dm_io_region region
;
2064 struct dm_io_request req
;
2065 size_t n_blocks
, n_metadata_blocks
;
2066 uint64_t n_bitmap_bits
;
2068 wc
->memory_map_size
-= (uint64_t)wc
->start_sector
<< SECTOR_SHIFT
;
2070 bio_list_init(&wc
->flush_list
);
2071 wc
->flush_thread
= kthread_create(writecache_flush_thread
, wc
, "dm_writecache_flush");
2072 if (IS_ERR(wc
->flush_thread
)) {
2073 r
= PTR_ERR(wc
->flush_thread
);
2074 wc
->flush_thread
= NULL
;
2075 ti
->error
= "Couldn't spawn flush thread";
2078 wake_up_process(wc
->flush_thread
);
2080 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
,
2081 &n_blocks
, &n_metadata_blocks
);
2083 ti
->error
= "Invalid device size";
2087 n_bitmap_bits
= (((uint64_t)n_metadata_blocks
<< wc
->block_size_bits
) +
2088 BITMAP_GRANULARITY
- 1) / BITMAP_GRANULARITY
;
2089 /* this is limitation of test_bit functions */
2090 if (n_bitmap_bits
> 1U << 31) {
2092 ti
->error
= "Invalid device size";
2096 wc
->memory_map
= vmalloc(n_metadata_blocks
<< wc
->block_size_bits
);
2097 if (!wc
->memory_map
) {
2099 ti
->error
= "Unable to allocate memory for metadata";
2103 wc
->dm_kcopyd
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2104 if (IS_ERR(wc
->dm_kcopyd
)) {
2105 r
= PTR_ERR(wc
->dm_kcopyd
);
2106 ti
->error
= "Unable to allocate dm-kcopyd client";
2107 wc
->dm_kcopyd
= NULL
;
2111 wc
->metadata_sectors
= n_metadata_blocks
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
2112 wc
->dirty_bitmap_size
= (n_bitmap_bits
+ BITS_PER_LONG
- 1) /
2113 BITS_PER_LONG
* sizeof(unsigned long);
2114 wc
->dirty_bitmap
= vzalloc(wc
->dirty_bitmap_size
);
2115 if (!wc
->dirty_bitmap
) {
2117 ti
->error
= "Unable to allocate dirty bitmap";
2121 region
.bdev
= wc
->ssd_dev
->bdev
;
2122 region
.sector
= wc
->start_sector
;
2123 region
.count
= wc
->metadata_sectors
;
2124 req
.bi_op
= REQ_OP_READ
;
2125 req
.bi_op_flags
= REQ_SYNC
;
2126 req
.mem
.type
= DM_IO_VMA
;
2127 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
;
2128 req
.client
= wc
->dm_io
;
2129 req
.notify
.fn
= NULL
;
2131 r
= dm_io(&req
, 1, ®ion
, NULL
);
2133 ti
->error
= "Unable to read metadata";
2138 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2140 ti
->error
= "Hardware memory error when reading superblock";
2143 if (!le32_to_cpu(s
.magic
) && !le32_to_cpu(s
.version
)) {
2144 r
= init_memory(wc
);
2146 ti
->error
= "Unable to initialize device";
2149 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2151 ti
->error
= "Hardware memory error when reading superblock";
2156 if (le32_to_cpu(s
.magic
) != MEMORY_SUPERBLOCK_MAGIC
) {
2157 ti
->error
= "Invalid magic in the superblock";
2162 if (le32_to_cpu(s
.version
) != MEMORY_SUPERBLOCK_VERSION
) {
2163 ti
->error
= "Invalid version in the superblock";
2168 if (le32_to_cpu(s
.block_size
) != wc
->block_size
) {
2169 ti
->error
= "Block size does not match superblock";
2174 wc
->n_blocks
= le64_to_cpu(s
.n_blocks
);
2176 offset
= wc
->n_blocks
* sizeof(struct wc_memory_entry
);
2177 if (offset
/ sizeof(struct wc_memory_entry
) != le64_to_cpu(sb(wc
)->n_blocks
)) {
2179 ti
->error
= "Overflow in size calculation";
2183 offset
+= sizeof(struct wc_memory_superblock
);
2184 if (offset
< sizeof(struct wc_memory_superblock
))
2186 offset
= (offset
+ wc
->block_size
- 1) & ~(size_t)(wc
->block_size
- 1);
2187 data_size
= wc
->n_blocks
* (size_t)wc
->block_size
;
2188 if (!offset
|| (data_size
/ wc
->block_size
!= wc
->n_blocks
) ||
2189 (offset
+ data_size
< offset
))
2191 if (offset
+ data_size
> wc
->memory_map_size
) {
2192 ti
->error
= "Memory area is too small";
2197 wc
->metadata_sectors
= offset
>> SECTOR_SHIFT
;
2198 wc
->block_start
= (char *)sb(wc
) + offset
;
2200 x
= (uint64_t)wc
->n_blocks
* (100 - high_wm_percent
);
2203 wc
->freelist_high_watermark
= x
;
2204 x
= (uint64_t)wc
->n_blocks
* (100 - low_wm_percent
);
2207 wc
->freelist_low_watermark
= x
;
2209 r
= writecache_alloc_entries(wc
);
2211 ti
->error
= "Cannot allocate memory";
2215 ti
->num_flush_bios
= 1;
2216 ti
->flush_supported
= true;
2217 ti
->num_discard_bios
= 1;
2219 if (WC_MODE_PMEM(wc
))
2220 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
2226 ti
->error
= "Bad arguments";
2232 static void writecache_status(struct dm_target
*ti
, status_type_t type
,
2233 unsigned status_flags
, char *result
, unsigned maxlen
)
2235 struct dm_writecache
*wc
= ti
->private;
2236 unsigned extra_args
;
2241 case STATUSTYPE_INFO
:
2242 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc
),
2243 (unsigned long long)wc
->n_blocks
, (unsigned long long)wc
->freelist_size
,
2244 (unsigned long long)wc
->writeback_size
);
2246 case STATUSTYPE_TABLE
:
2247 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc
) ? 'p' : 's',
2248 wc
->dev
->name
, wc
->ssd_dev
->name
, wc
->block_size
);
2250 if (wc
->start_sector
)
2252 if (wc
->high_wm_percent_set
)
2254 if (wc
->low_wm_percent_set
)
2256 if (wc
->max_writeback_jobs_set
)
2258 if (wc
->autocommit_blocks_set
)
2260 if (wc
->autocommit_time_set
)
2262 if (wc
->writeback_fua_set
)
2265 DMEMIT("%u", extra_args
);
2266 if (wc
->start_sector
)
2267 DMEMIT(" start_sector %llu", (unsigned long long)wc
->start_sector
);
2268 if (wc
->high_wm_percent_set
) {
2269 x
= (uint64_t)wc
->freelist_high_watermark
* 100;
2270 x
+= wc
->n_blocks
/ 2;
2271 do_div(x
, (size_t)wc
->n_blocks
);
2272 DMEMIT(" high_watermark %u", 100 - (unsigned)x
);
2274 if (wc
->low_wm_percent_set
) {
2275 x
= (uint64_t)wc
->freelist_low_watermark
* 100;
2276 x
+= wc
->n_blocks
/ 2;
2277 do_div(x
, (size_t)wc
->n_blocks
);
2278 DMEMIT(" low_watermark %u", 100 - (unsigned)x
);
2280 if (wc
->max_writeback_jobs_set
)
2281 DMEMIT(" writeback_jobs %u", wc
->max_writeback_jobs
);
2282 if (wc
->autocommit_blocks_set
)
2283 DMEMIT(" autocommit_blocks %u", wc
->autocommit_blocks
);
2284 if (wc
->autocommit_time_set
)
2285 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc
->autocommit_jiffies
));
2286 if (wc
->writeback_fua_set
)
2287 DMEMIT(" %sfua", wc
->writeback_fua
? "" : "no");
2292 static struct target_type writecache_target
= {
2293 .name
= "writecache",
2294 .version
= {1, 1, 1},
2295 .module
= THIS_MODULE
,
2296 .ctr
= writecache_ctr
,
2297 .dtr
= writecache_dtr
,
2298 .status
= writecache_status
,
2299 .postsuspend
= writecache_suspend
,
2300 .resume
= writecache_resume
,
2301 .message
= writecache_message
,
2302 .map
= writecache_map
,
2303 .end_io
= writecache_end_io
,
2304 .iterate_devices
= writecache_iterate_devices
,
2305 .io_hints
= writecache_io_hints
,
2308 static int __init
dm_writecache_init(void)
2312 r
= dm_register_target(&writecache_target
);
2314 DMERR("register failed %d", r
);
2321 static void __exit
dm_writecache_exit(void)
2323 dm_unregister_target(&writecache_target
);
2326 module_init(dm_writecache_init
);
2327 module_exit(dm_writecache_exit
);
2329 MODULE_DESCRIPTION(DM_NAME
" writecache target");
2330 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2331 MODULE_LICENSE("GPL");