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 unsigned page_offset
;
195 struct wc_entry
*wc_list_inline
[WB_LIST_INLINE
];
200 struct list_head endio_entry
;
201 struct dm_writecache
*wc
;
207 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle
,
208 "A percentage of time allocated for data copying");
210 static void wc_lock(struct dm_writecache
*wc
)
212 mutex_lock(&wc
->lock
);
215 static void wc_unlock(struct dm_writecache
*wc
)
217 mutex_unlock(&wc
->lock
);
220 #ifdef DM_WRITECACHE_HAS_PMEM
221 static int persistent_memory_claim(struct dm_writecache
*wc
)
230 wc
->memory_vmapped
= false;
232 if (!wc
->ssd_dev
->dax_dev
) {
236 s
= wc
->memory_map_size
;
242 if (p
!= s
>> PAGE_SHIFT
) {
247 id
= dax_read_lock();
249 da
= dax_direct_access(wc
->ssd_dev
->dax_dev
, 0, p
, &wc
->memory_map
, &pfn
);
251 wc
->memory_map
= NULL
;
255 if (!pfn_t_has_page(pfn
)) {
256 wc
->memory_map
= NULL
;
262 wc
->memory_map
= NULL
;
263 pages
= kvmalloc_array(p
, sizeof(struct page
*), GFP_KERNEL
);
271 daa
= dax_direct_access(wc
->ssd_dev
->dax_dev
, i
, p
- i
,
274 r
= daa
? daa
: -EINVAL
;
277 if (!pfn_t_has_page(pfn
)) {
281 while (daa
-- && i
< p
) {
282 pages
[i
++] = pfn_t_to_page(pfn
);
288 wc
->memory_map
= vmap(pages
, p
, VM_MAP
, PAGE_KERNEL
);
289 if (!wc
->memory_map
) {
294 wc
->memory_vmapped
= true;
299 wc
->memory_map
+= (size_t)wc
->start_sector
<< SECTOR_SHIFT
;
300 wc
->memory_map_size
-= (size_t)wc
->start_sector
<< SECTOR_SHIFT
;
311 static int persistent_memory_claim(struct dm_writecache
*wc
)
317 static void persistent_memory_release(struct dm_writecache
*wc
)
319 if (wc
->memory_vmapped
)
320 vunmap(wc
->memory_map
- ((size_t)wc
->start_sector
<< SECTOR_SHIFT
));
323 static struct page
*persistent_memory_page(void *addr
)
325 if (is_vmalloc_addr(addr
))
326 return vmalloc_to_page(addr
);
328 return virt_to_page(addr
);
331 static unsigned persistent_memory_page_offset(void *addr
)
333 return (unsigned long)addr
& (PAGE_SIZE
- 1);
336 static void persistent_memory_flush_cache(void *ptr
, size_t size
)
338 if (is_vmalloc_addr(ptr
))
339 flush_kernel_vmap_range(ptr
, size
);
342 static void persistent_memory_invalidate_cache(void *ptr
, size_t size
)
344 if (is_vmalloc_addr(ptr
))
345 invalidate_kernel_vmap_range(ptr
, size
);
348 static struct wc_memory_superblock
*sb(struct dm_writecache
*wc
)
350 return wc
->memory_map
;
353 static struct wc_memory_entry
*memory_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
355 if (is_power_of_2(sizeof(struct wc_entry
)) && 0)
356 return &sb(wc
)->entries
[e
- wc
->entries
];
358 return &sb(wc
)->entries
[e
->index
];
361 static void *memory_data(struct dm_writecache
*wc
, struct wc_entry
*e
)
363 return (char *)wc
->block_start
+ (e
->index
<< wc
->block_size_bits
);
366 static sector_t
cache_sector(struct dm_writecache
*wc
, struct wc_entry
*e
)
368 return wc
->start_sector
+ wc
->metadata_sectors
+
369 ((sector_t
)e
->index
<< (wc
->block_size_bits
- SECTOR_SHIFT
));
372 static uint64_t read_original_sector(struct dm_writecache
*wc
, struct wc_entry
*e
)
374 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
375 return e
->original_sector
;
377 return le64_to_cpu(memory_entry(wc
, e
)->original_sector
);
381 static uint64_t read_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
)
383 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
386 return le64_to_cpu(memory_entry(wc
, e
)->seq_count
);
390 static void clear_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
)
392 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
395 pmem_assign(memory_entry(wc
, e
)->seq_count
, cpu_to_le64(-1));
398 static void write_original_sector_seq_count(struct dm_writecache
*wc
, struct wc_entry
*e
,
399 uint64_t original_sector
, uint64_t seq_count
)
401 struct wc_memory_entry me
;
402 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
403 e
->original_sector
= original_sector
;
404 e
->seq_count
= seq_count
;
406 me
.original_sector
= cpu_to_le64(original_sector
);
407 me
.seq_count
= cpu_to_le64(seq_count
);
408 pmem_assign(*memory_entry(wc
, e
), me
);
411 #define writecache_error(wc, err, msg, arg...) \
413 if (!cmpxchg(&(wc)->error, 0, err)) \
415 wake_up(&(wc)->freelist_wait); \
418 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
420 static void writecache_flush_all_metadata(struct dm_writecache
*wc
)
422 if (!WC_MODE_PMEM(wc
))
423 memset(wc
->dirty_bitmap
, -1, wc
->dirty_bitmap_size
);
426 static void writecache_flush_region(struct dm_writecache
*wc
, void *ptr
, size_t size
)
428 if (!WC_MODE_PMEM(wc
))
429 __set_bit(((char *)ptr
- (char *)wc
->memory_map
) / BITMAP_GRANULARITY
,
433 static void writecache_disk_flush(struct dm_writecache
*wc
, struct dm_dev
*dev
);
436 struct dm_writecache
*wc
;
441 static void writecache_notify_io(unsigned long error
, void *context
)
443 struct io_notify
*endio
= context
;
445 if (unlikely(error
!= 0))
446 writecache_error(endio
->wc
, -EIO
, "error writing metadata");
447 BUG_ON(atomic_read(&endio
->count
) <= 0);
448 if (atomic_dec_and_test(&endio
->count
))
452 static void writecache_wait_for_ios(struct dm_writecache
*wc
, int direction
)
454 wait_event(wc
->bio_in_progress_wait
[direction
],
455 !atomic_read(&wc
->bio_in_progress
[direction
]));
458 static void ssd_commit_flushed(struct dm_writecache
*wc
, bool wait_for_ios
)
460 struct dm_io_region region
;
461 struct dm_io_request req
;
462 struct io_notify endio
= {
464 COMPLETION_INITIALIZER_ONSTACK(endio
.c
),
467 unsigned bitmap_bits
= wc
->dirty_bitmap_size
* 8;
472 i
= find_next_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
473 if (unlikely(i
== bitmap_bits
))
475 j
= find_next_zero_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
477 region
.bdev
= wc
->ssd_dev
->bdev
;
478 region
.sector
= (sector_t
)i
* (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
479 region
.count
= (sector_t
)(j
- i
) * (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
481 if (unlikely(region
.sector
>= wc
->metadata_sectors
))
483 if (unlikely(region
.sector
+ region
.count
> wc
->metadata_sectors
))
484 region
.count
= wc
->metadata_sectors
- region
.sector
;
486 region
.sector
+= wc
->start_sector
;
487 atomic_inc(&endio
.count
);
488 req
.bi_op
= REQ_OP_WRITE
;
489 req
.bi_op_flags
= REQ_SYNC
;
490 req
.mem
.type
= DM_IO_VMA
;
491 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
+ (size_t)i
* BITMAP_GRANULARITY
;
492 req
.client
= wc
->dm_io
;
493 req
.notify
.fn
= writecache_notify_io
;
494 req
.notify
.context
= &endio
;
496 /* writing via async dm-io (implied by notify.fn above) won't return an error */
497 (void) dm_io(&req
, 1, ®ion
, NULL
);
501 writecache_notify_io(0, &endio
);
502 wait_for_completion_io(&endio
.c
);
505 writecache_wait_for_ios(wc
, WRITE
);
507 writecache_disk_flush(wc
, wc
->ssd_dev
);
509 memset(wc
->dirty_bitmap
, 0, wc
->dirty_bitmap_size
);
512 static void writecache_commit_flushed(struct dm_writecache
*wc
, bool wait_for_ios
)
514 if (WC_MODE_PMEM(wc
))
517 ssd_commit_flushed(wc
, wait_for_ios
);
520 static void writecache_disk_flush(struct dm_writecache
*wc
, struct dm_dev
*dev
)
523 struct dm_io_region region
;
524 struct dm_io_request req
;
526 region
.bdev
= dev
->bdev
;
529 req
.bi_op
= REQ_OP_WRITE
;
530 req
.bi_op_flags
= REQ_PREFLUSH
;
531 req
.mem
.type
= DM_IO_KMEM
;
532 req
.mem
.ptr
.addr
= NULL
;
533 req
.client
= wc
->dm_io
;
534 req
.notify
.fn
= NULL
;
536 r
= dm_io(&req
, 1, ®ion
, NULL
);
538 writecache_error(wc
, r
, "error flushing metadata: %d", r
);
541 #define WFE_RETURN_FOLLOWING 1
542 #define WFE_LOWEST_SEQ 2
544 static struct wc_entry
*writecache_find_entry(struct dm_writecache
*wc
,
545 uint64_t block
, int flags
)
548 struct rb_node
*node
= wc
->tree
.rb_node
;
554 e
= container_of(node
, struct wc_entry
, rb_node
);
555 if (read_original_sector(wc
, e
) == block
)
557 node
= (read_original_sector(wc
, e
) >= block
?
558 e
->rb_node
.rb_left
: e
->rb_node
.rb_right
);
559 if (unlikely(!node
)) {
560 if (!(flags
& WFE_RETURN_FOLLOWING
)) {
563 if (read_original_sector(wc
, e
) >= block
) {
566 node
= rb_next(&e
->rb_node
);
567 if (unlikely(!node
)) {
570 e
= container_of(node
, struct wc_entry
, rb_node
);
578 if (flags
& WFE_LOWEST_SEQ
)
579 node
= rb_prev(&e
->rb_node
);
581 node
= rb_next(&e
->rb_node
);
584 e2
= container_of(node
, struct wc_entry
, rb_node
);
585 if (read_original_sector(wc
, e2
) != block
)
591 static void writecache_insert_entry(struct dm_writecache
*wc
, struct wc_entry
*ins
)
594 struct rb_node
**node
= &wc
->tree
.rb_node
, *parent
= NULL
;
597 e
= container_of(*node
, struct wc_entry
, rb_node
);
598 parent
= &e
->rb_node
;
599 if (read_original_sector(wc
, e
) > read_original_sector(wc
, ins
))
600 node
= &parent
->rb_left
;
602 node
= &parent
->rb_right
;
604 rb_link_node(&ins
->rb_node
, parent
, node
);
605 rb_insert_color(&ins
->rb_node
, &wc
->tree
);
606 list_add(&ins
->lru
, &wc
->lru
);
609 static void writecache_unlink(struct dm_writecache
*wc
, struct wc_entry
*e
)
612 rb_erase(&e
->rb_node
, &wc
->tree
);
615 static void writecache_add_to_freelist(struct dm_writecache
*wc
, struct wc_entry
*e
)
617 if (WC_MODE_SORT_FREELIST(wc
)) {
618 struct rb_node
**node
= &wc
->freetree
.rb_node
, *parent
= NULL
;
619 if (unlikely(!*node
))
620 wc
->current_free
= e
;
623 if (&e
->rb_node
< *node
)
624 node
= &parent
->rb_left
;
626 node
= &parent
->rb_right
;
628 rb_link_node(&e
->rb_node
, parent
, node
);
629 rb_insert_color(&e
->rb_node
, &wc
->freetree
);
631 list_add_tail(&e
->lru
, &wc
->freelist
);
636 static inline void writecache_verify_watermark(struct dm_writecache
*wc
)
638 if (unlikely(wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_high_watermark
))
639 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
642 static struct wc_entry
*writecache_pop_from_freelist(struct dm_writecache
*wc
)
646 if (WC_MODE_SORT_FREELIST(wc
)) {
647 struct rb_node
*next
;
648 if (unlikely(!wc
->current_free
))
650 e
= wc
->current_free
;
651 next
= rb_next(&e
->rb_node
);
652 rb_erase(&e
->rb_node
, &wc
->freetree
);
654 next
= rb_first(&wc
->freetree
);
655 wc
->current_free
= next
? container_of(next
, struct wc_entry
, rb_node
) : NULL
;
657 if (unlikely(list_empty(&wc
->freelist
)))
659 e
= container_of(wc
->freelist
.next
, struct wc_entry
, lru
);
664 writecache_verify_watermark(wc
);
669 static void writecache_free_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
671 writecache_unlink(wc
, e
);
672 writecache_add_to_freelist(wc
, e
);
673 clear_seq_count(wc
, e
);
674 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
675 if (unlikely(waitqueue_active(&wc
->freelist_wait
)))
676 wake_up(&wc
->freelist_wait
);
679 static void writecache_wait_on_freelist(struct dm_writecache
*wc
)
683 prepare_to_wait(&wc
->freelist_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
686 finish_wait(&wc
->freelist_wait
, &wait
);
690 static void writecache_poison_lists(struct dm_writecache
*wc
)
693 * Catch incorrect access to these values while the device is suspended.
695 memset(&wc
->tree
, -1, sizeof wc
->tree
);
696 wc
->lru
.next
= LIST_POISON1
;
697 wc
->lru
.prev
= LIST_POISON2
;
698 wc
->freelist
.next
= LIST_POISON1
;
699 wc
->freelist
.prev
= LIST_POISON2
;
702 static void writecache_flush_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
704 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
705 if (WC_MODE_PMEM(wc
))
706 writecache_flush_region(wc
, memory_data(wc
, e
), wc
->block_size
);
709 static bool writecache_entry_is_committed(struct dm_writecache
*wc
, struct wc_entry
*e
)
711 return read_seq_count(wc
, e
) < wc
->seq_count
;
714 static void writecache_flush(struct dm_writecache
*wc
)
716 struct wc_entry
*e
, *e2
;
717 bool need_flush_after_free
;
719 wc
->uncommitted_blocks
= 0;
720 del_timer(&wc
->autocommit_timer
);
722 if (list_empty(&wc
->lru
))
725 e
= container_of(wc
->lru
.next
, struct wc_entry
, lru
);
726 if (writecache_entry_is_committed(wc
, e
)) {
727 if (wc
->overwrote_committed
) {
728 writecache_wait_for_ios(wc
, WRITE
);
729 writecache_disk_flush(wc
, wc
->ssd_dev
);
730 wc
->overwrote_committed
= false;
735 writecache_flush_entry(wc
, e
);
736 if (unlikely(e
->lru
.next
== &wc
->lru
))
738 e2
= container_of(e
->lru
.next
, struct wc_entry
, lru
);
739 if (writecache_entry_is_committed(wc
, e2
))
744 writecache_commit_flushed(wc
, true);
747 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(wc
->seq_count
));
748 writecache_flush_region(wc
, &sb(wc
)->seq_count
, sizeof sb(wc
)->seq_count
);
749 writecache_commit_flushed(wc
, false);
751 wc
->overwrote_committed
= false;
753 need_flush_after_free
= false;
755 /* Free another committed entry with lower seq-count */
756 struct rb_node
*rb_node
= rb_prev(&e
->rb_node
);
759 e2
= container_of(rb_node
, struct wc_entry
, rb_node
);
760 if (read_original_sector(wc
, e2
) == read_original_sector(wc
, e
) &&
761 likely(!e2
->write_in_progress
)) {
762 writecache_free_entry(wc
, e2
);
763 need_flush_after_free
= true;
766 if (unlikely(e
->lru
.prev
== &wc
->lru
))
768 e
= container_of(e
->lru
.prev
, struct wc_entry
, lru
);
772 if (need_flush_after_free
)
773 writecache_commit_flushed(wc
, false);
776 static void writecache_flush_work(struct work_struct
*work
)
778 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, flush_work
);
781 writecache_flush(wc
);
785 static void writecache_autocommit_timer(struct timer_list
*t
)
787 struct dm_writecache
*wc
= from_timer(wc
, t
, autocommit_timer
);
788 if (!writecache_has_error(wc
))
789 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
792 static void writecache_schedule_autocommit(struct dm_writecache
*wc
)
794 if (!timer_pending(&wc
->autocommit_timer
))
795 mod_timer(&wc
->autocommit_timer
, jiffies
+ wc
->autocommit_jiffies
);
798 static void writecache_discard(struct dm_writecache
*wc
, sector_t start
, sector_t end
)
801 bool discarded_something
= false;
803 e
= writecache_find_entry(wc
, start
, WFE_RETURN_FOLLOWING
| WFE_LOWEST_SEQ
);
807 while (read_original_sector(wc
, e
) < end
) {
808 struct rb_node
*node
= rb_next(&e
->rb_node
);
810 if (likely(!e
->write_in_progress
)) {
811 if (!discarded_something
) {
812 writecache_wait_for_ios(wc
, READ
);
813 writecache_wait_for_ios(wc
, WRITE
);
814 discarded_something
= true;
816 if (!writecache_entry_is_committed(wc
, e
))
817 wc
->uncommitted_blocks
--;
818 writecache_free_entry(wc
, e
);
824 e
= container_of(node
, struct wc_entry
, rb_node
);
827 if (discarded_something
)
828 writecache_commit_flushed(wc
, false);
831 static bool writecache_wait_for_writeback(struct dm_writecache
*wc
)
833 if (wc
->writeback_size
) {
834 writecache_wait_on_freelist(wc
);
840 static void writecache_suspend(struct dm_target
*ti
)
842 struct dm_writecache
*wc
= ti
->private;
843 bool flush_on_suspend
;
845 del_timer_sync(&wc
->autocommit_timer
);
848 writecache_flush(wc
);
849 flush_on_suspend
= wc
->flush_on_suspend
;
850 if (flush_on_suspend
) {
851 wc
->flush_on_suspend
= false;
853 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
857 drain_workqueue(wc
->writeback_wq
);
860 if (flush_on_suspend
)
862 while (writecache_wait_for_writeback(wc
));
864 if (WC_MODE_PMEM(wc
))
865 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
867 writecache_poison_lists(wc
);
872 static int writecache_alloc_entries(struct dm_writecache
*wc
)
878 wc
->entries
= vmalloc(array_size(sizeof(struct wc_entry
), wc
->n_blocks
));
881 for (b
= 0; b
< wc
->n_blocks
; b
++) {
882 struct wc_entry
*e
= &wc
->entries
[b
];
884 e
->write_in_progress
= false;
891 static int writecache_read_metadata(struct dm_writecache
*wc
, sector_t n_sectors
)
893 struct dm_io_region region
;
894 struct dm_io_request req
;
896 region
.bdev
= wc
->ssd_dev
->bdev
;
897 region
.sector
= wc
->start_sector
;
898 region
.count
= n_sectors
;
899 req
.bi_op
= REQ_OP_READ
;
900 req
.bi_op_flags
= REQ_SYNC
;
901 req
.mem
.type
= DM_IO_VMA
;
902 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
;
903 req
.client
= wc
->dm_io
;
904 req
.notify
.fn
= NULL
;
906 return dm_io(&req
, 1, ®ion
, NULL
);
909 static void writecache_resume(struct dm_target
*ti
)
911 struct dm_writecache
*wc
= ti
->private;
913 bool need_flush
= false;
919 if (WC_MODE_PMEM(wc
)) {
920 persistent_memory_invalidate_cache(wc
->memory_map
, wc
->memory_map_size
);
922 r
= writecache_read_metadata(wc
, wc
->metadata_sectors
);
924 size_t sb_entries_offset
;
925 writecache_error(wc
, r
, "unable to read metadata: %d", r
);
926 sb_entries_offset
= offsetof(struct wc_memory_superblock
, entries
);
927 memset((char *)wc
->memory_map
+ sb_entries_offset
, -1,
928 (wc
->metadata_sectors
<< SECTOR_SHIFT
) - sb_entries_offset
);
933 INIT_LIST_HEAD(&wc
->lru
);
934 if (WC_MODE_SORT_FREELIST(wc
)) {
935 wc
->freetree
= RB_ROOT
;
936 wc
->current_free
= NULL
;
938 INIT_LIST_HEAD(&wc
->freelist
);
940 wc
->freelist_size
= 0;
942 r
= memcpy_mcsafe(&sb_seq_count
, &sb(wc
)->seq_count
, sizeof(uint64_t));
944 writecache_error(wc
, r
, "hardware memory error when reading superblock: %d", r
);
945 sb_seq_count
= cpu_to_le64(0);
947 wc
->seq_count
= le64_to_cpu(sb_seq_count
);
949 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
950 for (b
= 0; b
< wc
->n_blocks
; b
++) {
951 struct wc_entry
*e
= &wc
->entries
[b
];
952 struct wc_memory_entry wme
;
953 if (writecache_has_error(wc
)) {
954 e
->original_sector
= -1;
958 r
= memcpy_mcsafe(&wme
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
960 writecache_error(wc
, r
, "hardware memory error when reading metadata entry %lu: %d",
961 (unsigned long)b
, r
);
962 e
->original_sector
= -1;
965 e
->original_sector
= le64_to_cpu(wme
.original_sector
);
966 e
->seq_count
= le64_to_cpu(wme
.seq_count
);
971 for (b
= 0; b
< wc
->n_blocks
; b
++) {
972 struct wc_entry
*e
= &wc
->entries
[b
];
973 if (!writecache_entry_is_committed(wc
, e
)) {
974 if (read_seq_count(wc
, e
) != -1) {
976 clear_seq_count(wc
, e
);
979 writecache_add_to_freelist(wc
, e
);
981 struct wc_entry
*old
;
983 old
= writecache_find_entry(wc
, read_original_sector(wc
, e
), 0);
985 writecache_insert_entry(wc
, e
);
987 if (read_seq_count(wc
, old
) == read_seq_count(wc
, e
)) {
988 writecache_error(wc
, -EINVAL
,
989 "two identical entries, position %llu, sector %llu, sequence %llu",
990 (unsigned long long)b
, (unsigned long long)read_original_sector(wc
, e
),
991 (unsigned long long)read_seq_count(wc
, e
));
993 if (read_seq_count(wc
, old
) > read_seq_count(wc
, e
)) {
996 writecache_free_entry(wc
, old
);
997 writecache_insert_entry(wc
, e
);
1006 writecache_flush_all_metadata(wc
);
1007 writecache_commit_flushed(wc
, false);
1010 writecache_verify_watermark(wc
);
1015 static int process_flush_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
1021 if (dm_suspended(wc
->ti
)) {
1025 if (writecache_has_error(wc
)) {
1030 writecache_flush(wc
);
1031 wc
->writeback_all
++;
1032 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
1035 flush_workqueue(wc
->writeback_wq
);
1038 wc
->writeback_all
--;
1039 if (writecache_has_error(wc
)) {
1048 static int process_flush_on_suspend_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
1054 wc
->flush_on_suspend
= true;
1060 static int writecache_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
1061 char *result
, unsigned maxlen
)
1064 struct dm_writecache
*wc
= ti
->private;
1066 if (!strcasecmp(argv
[0], "flush"))
1067 r
= process_flush_mesg(argc
, argv
, wc
);
1068 else if (!strcasecmp(argv
[0], "flush_on_suspend"))
1069 r
= process_flush_on_suspend_mesg(argc
, argv
, wc
);
1071 DMERR("unrecognised message received: %s", argv
[0]);
1076 static void bio_copy_block(struct dm_writecache
*wc
, struct bio
*bio
, void *data
)
1079 unsigned long flags
;
1081 int rw
= bio_data_dir(bio
);
1082 unsigned remaining_size
= wc
->block_size
;
1085 struct bio_vec bv
= bio_iter_iovec(bio
, bio
->bi_iter
);
1086 buf
= bvec_kmap_irq(&bv
, &flags
);
1088 if (unlikely(size
> remaining_size
))
1089 size
= remaining_size
;
1093 r
= memcpy_mcsafe(buf
, data
, size
);
1094 flush_dcache_page(bio_page(bio
));
1096 writecache_error(wc
, r
, "hardware memory error when reading data: %d", r
);
1097 bio
->bi_status
= BLK_STS_IOERR
;
1100 flush_dcache_page(bio_page(bio
));
1101 memcpy_flushcache(data
, buf
, size
);
1104 bvec_kunmap_irq(buf
, &flags
);
1106 data
= (char *)data
+ size
;
1107 remaining_size
-= size
;
1108 bio_advance(bio
, size
);
1109 } while (unlikely(remaining_size
));
1112 static int writecache_flush_thread(void *data
)
1114 struct dm_writecache
*wc
= data
;
1120 bio
= bio_list_pop(&wc
->flush_list
);
1122 set_current_state(TASK_INTERRUPTIBLE
);
1125 if (unlikely(kthread_should_stop())) {
1126 set_current_state(TASK_RUNNING
);
1134 if (bio_op(bio
) == REQ_OP_DISCARD
) {
1135 writecache_discard(wc
, bio
->bi_iter
.bi_sector
,
1136 bio_end_sector(bio
));
1138 bio_set_dev(bio
, wc
->dev
->bdev
);
1139 generic_make_request(bio
);
1141 writecache_flush(wc
);
1143 if (writecache_has_error(wc
))
1144 bio
->bi_status
= BLK_STS_IOERR
;
1152 static void writecache_offload_bio(struct dm_writecache
*wc
, struct bio
*bio
)
1154 if (bio_list_empty(&wc
->flush_list
))
1155 wake_up_process(wc
->flush_thread
);
1156 bio_list_add(&wc
->flush_list
, bio
);
1159 static int writecache_map(struct dm_target
*ti
, struct bio
*bio
)
1162 struct dm_writecache
*wc
= ti
->private;
1164 bio
->bi_private
= NULL
;
1168 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1169 if (writecache_has_error(wc
))
1171 if (WC_MODE_PMEM(wc
)) {
1172 writecache_flush(wc
);
1173 if (writecache_has_error(wc
))
1177 writecache_offload_bio(wc
, bio
);
1182 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
1184 if (unlikely((((unsigned)bio
->bi_iter
.bi_sector
| bio_sectors(bio
)) &
1185 (wc
->block_size
/ 512 - 1)) != 0)) {
1186 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1187 (unsigned long long)bio
->bi_iter
.bi_sector
,
1188 bio
->bi_iter
.bi_size
, wc
->block_size
);
1192 if (unlikely(bio_op(bio
) == REQ_OP_DISCARD
)) {
1193 if (writecache_has_error(wc
))
1195 if (WC_MODE_PMEM(wc
)) {
1196 writecache_discard(wc
, bio
->bi_iter
.bi_sector
, bio_end_sector(bio
));
1197 goto unlock_remap_origin
;
1199 writecache_offload_bio(wc
, bio
);
1204 if (bio_data_dir(bio
) == READ
) {
1206 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, WFE_RETURN_FOLLOWING
);
1207 if (e
&& read_original_sector(wc
, e
) == bio
->bi_iter
.bi_sector
) {
1208 if (WC_MODE_PMEM(wc
)) {
1209 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1210 if (bio
->bi_iter
.bi_size
)
1211 goto read_next_block
;
1214 dm_accept_partial_bio(bio
, wc
->block_size
>> SECTOR_SHIFT
);
1215 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1216 bio
->bi_iter
.bi_sector
= cache_sector(wc
, e
);
1217 if (!writecache_entry_is_committed(wc
, e
))
1218 writecache_wait_for_ios(wc
, WRITE
);
1223 sector_t next_boundary
=
1224 read_original_sector(wc
, e
) - bio
->bi_iter
.bi_sector
;
1225 if (next_boundary
< bio
->bi_iter
.bi_size
>> SECTOR_SHIFT
) {
1226 dm_accept_partial_bio(bio
, next_boundary
);
1229 goto unlock_remap_origin
;
1233 if (writecache_has_error(wc
))
1235 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, 0);
1237 if (!writecache_entry_is_committed(wc
, e
))
1239 if (!WC_MODE_PMEM(wc
) && !e
->write_in_progress
) {
1240 wc
->overwrote_committed
= true;
1244 e
= writecache_pop_from_freelist(wc
);
1246 writecache_wait_on_freelist(wc
);
1249 write_original_sector_seq_count(wc
, e
, bio
->bi_iter
.bi_sector
, wc
->seq_count
);
1250 writecache_insert_entry(wc
, e
);
1251 wc
->uncommitted_blocks
++;
1253 if (WC_MODE_PMEM(wc
)) {
1254 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1256 dm_accept_partial_bio(bio
, wc
->block_size
>> SECTOR_SHIFT
);
1257 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1258 bio
->bi_iter
.bi_sector
= cache_sector(wc
, e
);
1259 if (unlikely(wc
->uncommitted_blocks
>= wc
->autocommit_blocks
)) {
1260 wc
->uncommitted_blocks
= 0;
1261 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
1263 writecache_schedule_autocommit(wc
);
1267 } while (bio
->bi_iter
.bi_size
);
1269 if (unlikely(bio
->bi_opf
& REQ_FUA
||
1270 wc
->uncommitted_blocks
>= wc
->autocommit_blocks
))
1271 writecache_flush(wc
);
1273 writecache_schedule_autocommit(wc
);
1277 unlock_remap_origin
:
1278 bio_set_dev(bio
, wc
->dev
->bdev
);
1280 return DM_MAPIO_REMAPPED
;
1283 /* make sure that writecache_end_io decrements bio_in_progress: */
1284 bio
->bi_private
= (void *)1;
1285 atomic_inc(&wc
->bio_in_progress
[bio_data_dir(bio
)]);
1287 return DM_MAPIO_REMAPPED
;
1292 return DM_MAPIO_SUBMITTED
;
1296 return DM_MAPIO_SUBMITTED
;
1301 return DM_MAPIO_SUBMITTED
;
1304 static int writecache_end_io(struct dm_target
*ti
, struct bio
*bio
, blk_status_t
*status
)
1306 struct dm_writecache
*wc
= ti
->private;
1308 if (bio
->bi_private
!= NULL
) {
1309 int dir
= bio_data_dir(bio
);
1310 if (atomic_dec_and_test(&wc
->bio_in_progress
[dir
]))
1311 if (unlikely(waitqueue_active(&wc
->bio_in_progress_wait
[dir
])))
1312 wake_up(&wc
->bio_in_progress_wait
[dir
]);
1317 static int writecache_iterate_devices(struct dm_target
*ti
,
1318 iterate_devices_callout_fn fn
, void *data
)
1320 struct dm_writecache
*wc
= ti
->private;
1322 return fn(ti
, wc
->dev
, 0, ti
->len
, data
);
1325 static void writecache_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1327 struct dm_writecache
*wc
= ti
->private;
1329 if (limits
->logical_block_size
< wc
->block_size
)
1330 limits
->logical_block_size
= wc
->block_size
;
1332 if (limits
->physical_block_size
< wc
->block_size
)
1333 limits
->physical_block_size
= wc
->block_size
;
1335 if (limits
->io_min
< wc
->block_size
)
1336 limits
->io_min
= wc
->block_size
;
1340 static void writecache_writeback_endio(struct bio
*bio
)
1342 struct writeback_struct
*wb
= container_of(bio
, struct writeback_struct
, bio
);
1343 struct dm_writecache
*wc
= wb
->wc
;
1344 unsigned long flags
;
1346 raw_spin_lock_irqsave(&wc
->endio_list_lock
, flags
);
1347 if (unlikely(list_empty(&wc
->endio_list
)))
1348 wake_up_process(wc
->endio_thread
);
1349 list_add_tail(&wb
->endio_entry
, &wc
->endio_list
);
1350 raw_spin_unlock_irqrestore(&wc
->endio_list_lock
, flags
);
1353 static void writecache_copy_endio(int read_err
, unsigned long write_err
, void *ptr
)
1355 struct copy_struct
*c
= ptr
;
1356 struct dm_writecache
*wc
= c
->wc
;
1358 c
->error
= likely(!(read_err
| write_err
)) ? 0 : -EIO
;
1360 raw_spin_lock_irq(&wc
->endio_list_lock
);
1361 if (unlikely(list_empty(&wc
->endio_list
)))
1362 wake_up_process(wc
->endio_thread
);
1363 list_add_tail(&c
->endio_entry
, &wc
->endio_list
);
1364 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1367 static void __writecache_endio_pmem(struct dm_writecache
*wc
, struct list_head
*list
)
1370 struct writeback_struct
*wb
;
1372 unsigned long n_walked
= 0;
1375 wb
= list_entry(list
->next
, struct writeback_struct
, endio_entry
);
1376 list_del(&wb
->endio_entry
);
1378 if (unlikely(wb
->bio
.bi_status
!= BLK_STS_OK
))
1379 writecache_error(wc
, blk_status_to_errno(wb
->bio
.bi_status
),
1380 "write error %d", wb
->bio
.bi_status
);
1384 BUG_ON(!e
->write_in_progress
);
1385 e
->write_in_progress
= false;
1386 INIT_LIST_HEAD(&e
->lru
);
1387 if (!writecache_has_error(wc
))
1388 writecache_free_entry(wc
, e
);
1389 BUG_ON(!wc
->writeback_size
);
1390 wc
->writeback_size
--;
1392 if (unlikely(n_walked
>= ENDIO_LATENCY
)) {
1393 writecache_commit_flushed(wc
, false);
1398 } while (++i
< wb
->wc_list_n
);
1400 if (wb
->wc_list
!= wb
->wc_list_inline
)
1403 } while (!list_empty(list
));
1406 static void __writecache_endio_ssd(struct dm_writecache
*wc
, struct list_head
*list
)
1408 struct copy_struct
*c
;
1412 c
= list_entry(list
->next
, struct copy_struct
, endio_entry
);
1413 list_del(&c
->endio_entry
);
1415 if (unlikely(c
->error
))
1416 writecache_error(wc
, c
->error
, "copy error");
1420 BUG_ON(!e
->write_in_progress
);
1421 e
->write_in_progress
= false;
1422 INIT_LIST_HEAD(&e
->lru
);
1423 if (!writecache_has_error(wc
))
1424 writecache_free_entry(wc
, e
);
1426 BUG_ON(!wc
->writeback_size
);
1427 wc
->writeback_size
--;
1429 } while (--c
->n_entries
);
1430 mempool_free(c
, &wc
->copy_pool
);
1431 } while (!list_empty(list
));
1434 static int writecache_endio_thread(void *data
)
1436 struct dm_writecache
*wc
= data
;
1439 struct list_head list
;
1441 raw_spin_lock_irq(&wc
->endio_list_lock
);
1442 if (!list_empty(&wc
->endio_list
))
1444 set_current_state(TASK_INTERRUPTIBLE
);
1445 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1447 if (unlikely(kthread_should_stop())) {
1448 set_current_state(TASK_RUNNING
);
1457 list
= wc
->endio_list
;
1458 list
.next
->prev
= list
.prev
->next
= &list
;
1459 INIT_LIST_HEAD(&wc
->endio_list
);
1460 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1462 if (!WC_MODE_FUA(wc
))
1463 writecache_disk_flush(wc
, wc
->dev
);
1467 if (WC_MODE_PMEM(wc
)) {
1468 __writecache_endio_pmem(wc
, &list
);
1470 __writecache_endio_ssd(wc
, &list
);
1471 writecache_wait_for_ios(wc
, READ
);
1474 writecache_commit_flushed(wc
, false);
1482 static bool wc_add_block(struct writeback_struct
*wb
, struct wc_entry
*e
, gfp_t gfp
)
1484 struct dm_writecache
*wc
= wb
->wc
;
1485 unsigned block_size
= wc
->block_size
;
1486 void *address
= memory_data(wc
, e
);
1488 persistent_memory_flush_cache(address
, block_size
);
1489 return bio_add_page(&wb
->bio
, persistent_memory_page(address
),
1490 block_size
, persistent_memory_page_offset(address
)) != 0;
1493 struct writeback_list
{
1494 struct list_head list
;
1498 static void __writeback_throttle(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1500 if (unlikely(wc
->max_writeback_jobs
)) {
1501 if (READ_ONCE(wc
->writeback_size
) - wbl
->size
>= wc
->max_writeback_jobs
) {
1503 while (wc
->writeback_size
- wbl
->size
>= wc
->max_writeback_jobs
)
1504 writecache_wait_on_freelist(wc
);
1511 static void __writecache_writeback_pmem(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1513 struct wc_entry
*e
, *f
;
1515 struct writeback_struct
*wb
;
1520 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1523 max_pages
= e
->wc_list_contiguous
;
1525 bio
= bio_alloc_bioset(GFP_NOIO
, max_pages
, &wc
->bio_set
);
1526 wb
= container_of(bio
, struct writeback_struct
, bio
);
1528 wb
->bio
.bi_end_io
= writecache_writeback_endio
;
1529 bio_set_dev(&wb
->bio
, wc
->dev
->bdev
);
1530 wb
->bio
.bi_iter
.bi_sector
= read_original_sector(wc
, e
);
1531 wb
->page_offset
= PAGE_SIZE
;
1532 if (max_pages
<= WB_LIST_INLINE
||
1533 unlikely(!(wb
->wc_list
= kmalloc_array(max_pages
, sizeof(struct wc_entry
*),
1534 GFP_NOIO
| __GFP_NORETRY
|
1535 __GFP_NOMEMALLOC
| __GFP_NOWARN
)))) {
1536 wb
->wc_list
= wb
->wc_list_inline
;
1537 max_pages
= WB_LIST_INLINE
;
1540 BUG_ON(!wc_add_block(wb
, e
, GFP_NOIO
));
1545 while (wbl
->size
&& wb
->wc_list_n
< max_pages
) {
1546 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1547 if (read_original_sector(wc
, f
) !=
1548 read_original_sector(wc
, e
) + (wc
->block_size
>> SECTOR_SHIFT
))
1550 if (!wc_add_block(wb
, f
, GFP_NOWAIT
| __GFP_NOWARN
))
1554 wb
->wc_list
[wb
->wc_list_n
++] = f
;
1557 bio_set_op_attrs(&wb
->bio
, REQ_OP_WRITE
, WC_MODE_FUA(wc
) * REQ_FUA
);
1558 if (writecache_has_error(wc
)) {
1559 bio
->bi_status
= BLK_STS_IOERR
;
1560 bio_endio(&wb
->bio
);
1562 submit_bio(&wb
->bio
);
1565 __writeback_throttle(wc
, wbl
);
1569 static void __writecache_writeback_ssd(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1571 struct wc_entry
*e
, *f
;
1572 struct dm_io_region from
, to
;
1573 struct copy_struct
*c
;
1579 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1582 n_sectors
= e
->wc_list_contiguous
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
1584 from
.bdev
= wc
->ssd_dev
->bdev
;
1585 from
.sector
= cache_sector(wc
, e
);
1586 from
.count
= n_sectors
;
1587 to
.bdev
= wc
->dev
->bdev
;
1588 to
.sector
= read_original_sector(wc
, e
);
1589 to
.count
= n_sectors
;
1591 c
= mempool_alloc(&wc
->copy_pool
, GFP_NOIO
);
1594 c
->n_entries
= e
->wc_list_contiguous
;
1596 while ((n_sectors
-= wc
->block_size
>> SECTOR_SHIFT
)) {
1598 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1604 dm_kcopyd_copy(wc
->dm_kcopyd
, &from
, 1, &to
, 0, writecache_copy_endio
, c
);
1606 __writeback_throttle(wc
, wbl
);
1610 static void writecache_writeback(struct work_struct
*work
)
1612 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, writeback_work
);
1613 struct blk_plug plug
;
1614 struct wc_entry
*e
, *f
, *g
;
1615 struct rb_node
*node
, *next_node
;
1616 struct list_head skipped
;
1617 struct writeback_list wbl
;
1618 unsigned long n_walked
;
1622 if (writecache_has_error(wc
)) {
1627 if (unlikely(wc
->writeback_all
)) {
1628 if (writecache_wait_for_writeback(wc
))
1632 if (wc
->overwrote_committed
) {
1633 writecache_wait_for_ios(wc
, WRITE
);
1637 INIT_LIST_HEAD(&skipped
);
1638 INIT_LIST_HEAD(&wbl
.list
);
1640 while (!list_empty(&wc
->lru
) &&
1641 (wc
->writeback_all
||
1642 wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_low_watermark
)) {
1645 if (unlikely(n_walked
> WRITEBACK_LATENCY
) &&
1646 likely(!wc
->writeback_all
) && likely(!dm_suspended(wc
->ti
))) {
1647 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
1651 e
= container_of(wc
->lru
.prev
, struct wc_entry
, lru
);
1652 BUG_ON(e
->write_in_progress
);
1653 if (unlikely(!writecache_entry_is_committed(wc
, e
))) {
1654 writecache_flush(wc
);
1656 node
= rb_prev(&e
->rb_node
);
1658 f
= container_of(node
, struct wc_entry
, rb_node
);
1659 if (unlikely(read_original_sector(wc
, f
) ==
1660 read_original_sector(wc
, e
))) {
1661 BUG_ON(!f
->write_in_progress
);
1663 list_add(&e
->lru
, &skipped
);
1668 wc
->writeback_size
++;
1670 list_add(&e
->lru
, &wbl
.list
);
1672 e
->write_in_progress
= true;
1673 e
->wc_list_contiguous
= 1;
1678 next_node
= rb_next(&f
->rb_node
);
1679 if (unlikely(!next_node
))
1681 g
= container_of(next_node
, struct wc_entry
, rb_node
);
1682 if (read_original_sector(wc
, g
) ==
1683 read_original_sector(wc
, f
)) {
1687 if (read_original_sector(wc
, g
) !=
1688 read_original_sector(wc
, f
) + (wc
->block_size
>> SECTOR_SHIFT
))
1690 if (unlikely(g
->write_in_progress
))
1692 if (unlikely(!writecache_entry_is_committed(wc
, g
)))
1695 if (!WC_MODE_PMEM(wc
)) {
1701 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1704 wc
->writeback_size
++;
1706 list_add(&g
->lru
, &wbl
.list
);
1708 g
->write_in_progress
= true;
1709 g
->wc_list_contiguous
= BIO_MAX_PAGES
;
1711 e
->wc_list_contiguous
++;
1712 if (unlikely(e
->wc_list_contiguous
== BIO_MAX_PAGES
))
1718 if (!list_empty(&skipped
)) {
1719 list_splice_tail(&skipped
, &wc
->lru
);
1721 * If we didn't do any progress, we must wait until some
1722 * writeback finishes to avoid burning CPU in a loop
1724 if (unlikely(!wbl
.size
))
1725 writecache_wait_for_writeback(wc
);
1730 blk_start_plug(&plug
);
1732 if (WC_MODE_PMEM(wc
))
1733 __writecache_writeback_pmem(wc
, &wbl
);
1735 __writecache_writeback_ssd(wc
, &wbl
);
1737 blk_finish_plug(&plug
);
1739 if (unlikely(wc
->writeback_all
)) {
1741 while (writecache_wait_for_writeback(wc
));
1746 static int calculate_memory_size(uint64_t device_size
, unsigned block_size
,
1747 size_t *n_blocks_p
, size_t *n_metadata_blocks_p
)
1749 uint64_t n_blocks
, offset
;
1752 n_blocks
= device_size
;
1753 do_div(n_blocks
, block_size
+ sizeof(struct wc_memory_entry
));
1758 /* Verify the following entries[n_blocks] won't overflow */
1759 if (n_blocks
>= ((size_t)-sizeof(struct wc_memory_superblock
) /
1760 sizeof(struct wc_memory_entry
)))
1762 offset
= offsetof(struct wc_memory_superblock
, entries
[n_blocks
]);
1763 offset
= (offset
+ block_size
- 1) & ~(uint64_t)(block_size
- 1);
1764 if (offset
+ n_blocks
* block_size
<= device_size
)
1769 /* check if the bit field overflows */
1771 if (e
.index
!= n_blocks
)
1775 *n_blocks_p
= n_blocks
;
1776 if (n_metadata_blocks_p
)
1777 *n_metadata_blocks_p
= offset
>> __ffs(block_size
);
1781 static int init_memory(struct dm_writecache
*wc
)
1786 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
, &wc
->n_blocks
, NULL
);
1790 r
= writecache_alloc_entries(wc
);
1794 for (b
= 0; b
< ARRAY_SIZE(sb(wc
)->padding
); b
++)
1795 pmem_assign(sb(wc
)->padding
[b
], cpu_to_le64(0));
1796 pmem_assign(sb(wc
)->version
, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION
));
1797 pmem_assign(sb(wc
)->block_size
, cpu_to_le32(wc
->block_size
));
1798 pmem_assign(sb(wc
)->n_blocks
, cpu_to_le64(wc
->n_blocks
));
1799 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(0));
1801 for (b
= 0; b
< wc
->n_blocks
; b
++) {
1802 write_original_sector_seq_count(wc
, &wc
->entries
[b
], -1, -1);
1806 writecache_flush_all_metadata(wc
);
1807 writecache_commit_flushed(wc
, false);
1808 pmem_assign(sb(wc
)->magic
, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC
));
1809 writecache_flush_region(wc
, &sb(wc
)->magic
, sizeof sb(wc
)->magic
);
1810 writecache_commit_flushed(wc
, false);
1815 static void writecache_dtr(struct dm_target
*ti
)
1817 struct dm_writecache
*wc
= ti
->private;
1822 if (wc
->endio_thread
)
1823 kthread_stop(wc
->endio_thread
);
1825 if (wc
->flush_thread
)
1826 kthread_stop(wc
->flush_thread
);
1828 bioset_exit(&wc
->bio_set
);
1830 mempool_exit(&wc
->copy_pool
);
1832 if (wc
->writeback_wq
)
1833 destroy_workqueue(wc
->writeback_wq
);
1836 dm_put_device(ti
, wc
->dev
);
1839 dm_put_device(ti
, wc
->ssd_dev
);
1844 if (wc
->memory_map
) {
1845 if (WC_MODE_PMEM(wc
))
1846 persistent_memory_release(wc
);
1848 vfree(wc
->memory_map
);
1852 dm_kcopyd_client_destroy(wc
->dm_kcopyd
);
1855 dm_io_client_destroy(wc
->dm_io
);
1857 if (wc
->dirty_bitmap
)
1858 vfree(wc
->dirty_bitmap
);
1863 static int writecache_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1865 struct dm_writecache
*wc
;
1866 struct dm_arg_set as
;
1868 unsigned opt_params
;
1869 size_t offset
, data_size
;
1872 int high_wm_percent
= HIGH_WATERMARK
;
1873 int low_wm_percent
= LOW_WATERMARK
;
1875 struct wc_memory_superblock s
;
1877 static struct dm_arg _args
[] = {
1878 {0, 10, "Invalid number of feature args"},
1884 wc
= kzalloc(sizeof(struct dm_writecache
), GFP_KERNEL
);
1886 ti
->error
= "Cannot allocate writecache structure";
1893 mutex_init(&wc
->lock
);
1894 writecache_poison_lists(wc
);
1895 init_waitqueue_head(&wc
->freelist_wait
);
1896 timer_setup(&wc
->autocommit_timer
, writecache_autocommit_timer
, 0);
1898 for (i
= 0; i
< 2; i
++) {
1899 atomic_set(&wc
->bio_in_progress
[i
], 0);
1900 init_waitqueue_head(&wc
->bio_in_progress_wait
[i
]);
1903 wc
->dm_io
= dm_io_client_create();
1904 if (IS_ERR(wc
->dm_io
)) {
1905 r
= PTR_ERR(wc
->dm_io
);
1906 ti
->error
= "Unable to allocate dm-io client";
1911 wc
->writeback_wq
= alloc_workqueue("writecache-writeabck", WQ_MEM_RECLAIM
, 1);
1912 if (!wc
->writeback_wq
) {
1914 ti
->error
= "Could not allocate writeback workqueue";
1917 INIT_WORK(&wc
->writeback_work
, writecache_writeback
);
1918 INIT_WORK(&wc
->flush_work
, writecache_flush_work
);
1920 raw_spin_lock_init(&wc
->endio_list_lock
);
1921 INIT_LIST_HEAD(&wc
->endio_list
);
1922 wc
->endio_thread
= kthread_create(writecache_endio_thread
, wc
, "writecache_endio");
1923 if (IS_ERR(wc
->endio_thread
)) {
1924 r
= PTR_ERR(wc
->endio_thread
);
1925 wc
->endio_thread
= NULL
;
1926 ti
->error
= "Couldn't spawn endio thread";
1929 wake_up_process(wc
->endio_thread
);
1932 * Parse the mode (pmem or ssd)
1934 string
= dm_shift_arg(&as
);
1938 if (!strcasecmp(string
, "s")) {
1939 wc
->pmem_mode
= false;
1940 } else if (!strcasecmp(string
, "p")) {
1941 #ifdef DM_WRITECACHE_HAS_PMEM
1942 wc
->pmem_mode
= true;
1943 wc
->writeback_fua
= true;
1946 * If the architecture doesn't support persistent memory or
1947 * the kernel doesn't support any DAX drivers, this driver can
1948 * only be used in SSD-only mode.
1951 ti
->error
= "Persistent memory or DAX not supported on this system";
1958 if (WC_MODE_PMEM(wc
)) {
1959 r
= bioset_init(&wc
->bio_set
, BIO_POOL_SIZE
,
1960 offsetof(struct writeback_struct
, bio
),
1963 ti
->error
= "Could not allocate bio set";
1967 r
= mempool_init_kmalloc_pool(&wc
->copy_pool
, 1, sizeof(struct copy_struct
));
1969 ti
->error
= "Could not allocate mempool";
1975 * Parse the origin data device
1977 string
= dm_shift_arg(&as
);
1980 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->dev
);
1982 ti
->error
= "Origin data device lookup failed";
1987 * Parse cache data device (be it pmem or ssd)
1989 string
= dm_shift_arg(&as
);
1993 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->ssd_dev
);
1995 ti
->error
= "Cache data device lookup failed";
1998 wc
->memory_map_size
= i_size_read(wc
->ssd_dev
->bdev
->bd_inode
);
2001 * Parse the cache block size
2003 string
= dm_shift_arg(&as
);
2006 if (sscanf(string
, "%u%c", &wc
->block_size
, &dummy
) != 1 ||
2007 wc
->block_size
< 512 || wc
->block_size
> PAGE_SIZE
||
2008 (wc
->block_size
& (wc
->block_size
- 1))) {
2010 ti
->error
= "Invalid block size";
2013 if (wc
->block_size
< bdev_logical_block_size(wc
->dev
->bdev
) ||
2014 wc
->block_size
< bdev_logical_block_size(wc
->ssd_dev
->bdev
)) {
2016 ti
->error
= "Block size is smaller than device logical block size";
2019 wc
->block_size_bits
= __ffs(wc
->block_size
);
2021 wc
->max_writeback_jobs
= MAX_WRITEBACK_JOBS
;
2022 wc
->autocommit_blocks
= !WC_MODE_PMEM(wc
) ? AUTOCOMMIT_BLOCKS_SSD
: AUTOCOMMIT_BLOCKS_PMEM
;
2023 wc
->autocommit_jiffies
= msecs_to_jiffies(AUTOCOMMIT_MSEC
);
2026 * Parse optional arguments
2028 r
= dm_read_arg_group(_args
, &as
, &opt_params
, &ti
->error
);
2032 while (opt_params
) {
2033 string
= dm_shift_arg(&as
), opt_params
--;
2034 if (!strcasecmp(string
, "start_sector") && opt_params
>= 1) {
2035 unsigned long long start_sector
;
2036 string
= dm_shift_arg(&as
), opt_params
--;
2037 if (sscanf(string
, "%llu%c", &start_sector
, &dummy
) != 1)
2038 goto invalid_optional
;
2039 wc
->start_sector
= start_sector
;
2040 if (wc
->start_sector
!= start_sector
||
2041 wc
->start_sector
>= wc
->memory_map_size
>> SECTOR_SHIFT
)
2042 goto invalid_optional
;
2043 } else if (!strcasecmp(string
, "high_watermark") && opt_params
>= 1) {
2044 string
= dm_shift_arg(&as
), opt_params
--;
2045 if (sscanf(string
, "%d%c", &high_wm_percent
, &dummy
) != 1)
2046 goto invalid_optional
;
2047 if (high_wm_percent
< 0 || high_wm_percent
> 100)
2048 goto invalid_optional
;
2049 wc
->high_wm_percent_set
= true;
2050 } else if (!strcasecmp(string
, "low_watermark") && opt_params
>= 1) {
2051 string
= dm_shift_arg(&as
), opt_params
--;
2052 if (sscanf(string
, "%d%c", &low_wm_percent
, &dummy
) != 1)
2053 goto invalid_optional
;
2054 if (low_wm_percent
< 0 || low_wm_percent
> 100)
2055 goto invalid_optional
;
2056 wc
->low_wm_percent_set
= true;
2057 } else if (!strcasecmp(string
, "writeback_jobs") && opt_params
>= 1) {
2058 string
= dm_shift_arg(&as
), opt_params
--;
2059 if (sscanf(string
, "%u%c", &wc
->max_writeback_jobs
, &dummy
) != 1)
2060 goto invalid_optional
;
2061 wc
->max_writeback_jobs_set
= true;
2062 } else if (!strcasecmp(string
, "autocommit_blocks") && opt_params
>= 1) {
2063 string
= dm_shift_arg(&as
), opt_params
--;
2064 if (sscanf(string
, "%u%c", &wc
->autocommit_blocks
, &dummy
) != 1)
2065 goto invalid_optional
;
2066 wc
->autocommit_blocks_set
= true;
2067 } else if (!strcasecmp(string
, "autocommit_time") && opt_params
>= 1) {
2068 unsigned autocommit_msecs
;
2069 string
= dm_shift_arg(&as
), opt_params
--;
2070 if (sscanf(string
, "%u%c", &autocommit_msecs
, &dummy
) != 1)
2071 goto invalid_optional
;
2072 if (autocommit_msecs
> 3600000)
2073 goto invalid_optional
;
2074 wc
->autocommit_jiffies
= msecs_to_jiffies(autocommit_msecs
);
2075 wc
->autocommit_time_set
= true;
2076 } else if (!strcasecmp(string
, "fua")) {
2077 if (WC_MODE_PMEM(wc
)) {
2078 wc
->writeback_fua
= true;
2079 wc
->writeback_fua_set
= true;
2080 } else goto invalid_optional
;
2081 } else if (!strcasecmp(string
, "nofua")) {
2082 if (WC_MODE_PMEM(wc
)) {
2083 wc
->writeback_fua
= false;
2084 wc
->writeback_fua_set
= true;
2085 } else goto invalid_optional
;
2089 ti
->error
= "Invalid optional argument";
2094 if (high_wm_percent
< low_wm_percent
) {
2096 ti
->error
= "High watermark must be greater than or equal to low watermark";
2100 if (WC_MODE_PMEM(wc
)) {
2101 r
= persistent_memory_claim(wc
);
2103 ti
->error
= "Unable to map persistent memory for cache";
2107 size_t n_blocks
, n_metadata_blocks
;
2108 uint64_t n_bitmap_bits
;
2110 wc
->memory_map_size
-= (uint64_t)wc
->start_sector
<< SECTOR_SHIFT
;
2112 bio_list_init(&wc
->flush_list
);
2113 wc
->flush_thread
= kthread_create(writecache_flush_thread
, wc
, "dm_writecache_flush");
2114 if (IS_ERR(wc
->flush_thread
)) {
2115 r
= PTR_ERR(wc
->flush_thread
);
2116 wc
->flush_thread
= NULL
;
2117 ti
->error
= "Couldn't spawn endio thread";
2120 wake_up_process(wc
->flush_thread
);
2122 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
,
2123 &n_blocks
, &n_metadata_blocks
);
2125 ti
->error
= "Invalid device size";
2129 n_bitmap_bits
= (((uint64_t)n_metadata_blocks
<< wc
->block_size_bits
) +
2130 BITMAP_GRANULARITY
- 1) / BITMAP_GRANULARITY
;
2131 /* this is limitation of test_bit functions */
2132 if (n_bitmap_bits
> 1U << 31) {
2134 ti
->error
= "Invalid device size";
2138 wc
->memory_map
= vmalloc(n_metadata_blocks
<< wc
->block_size_bits
);
2139 if (!wc
->memory_map
) {
2141 ti
->error
= "Unable to allocate memory for metadata";
2145 wc
->dm_kcopyd
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2146 if (IS_ERR(wc
->dm_kcopyd
)) {
2147 r
= PTR_ERR(wc
->dm_kcopyd
);
2148 ti
->error
= "Unable to allocate dm-kcopyd client";
2149 wc
->dm_kcopyd
= NULL
;
2153 wc
->metadata_sectors
= n_metadata_blocks
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
2154 wc
->dirty_bitmap_size
= (n_bitmap_bits
+ BITS_PER_LONG
- 1) /
2155 BITS_PER_LONG
* sizeof(unsigned long);
2156 wc
->dirty_bitmap
= vzalloc(wc
->dirty_bitmap_size
);
2157 if (!wc
->dirty_bitmap
) {
2159 ti
->error
= "Unable to allocate dirty bitmap";
2163 r
= writecache_read_metadata(wc
, wc
->block_size
>> SECTOR_SHIFT
);
2165 ti
->error
= "Unable to read first block of metadata";
2170 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2172 ti
->error
= "Hardware memory error when reading superblock";
2175 if (!le32_to_cpu(s
.magic
) && !le32_to_cpu(s
.version
)) {
2176 r
= init_memory(wc
);
2178 ti
->error
= "Unable to initialize device";
2181 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2183 ti
->error
= "Hardware memory error when reading superblock";
2188 if (le32_to_cpu(s
.magic
) != MEMORY_SUPERBLOCK_MAGIC
) {
2189 ti
->error
= "Invalid magic in the superblock";
2194 if (le32_to_cpu(s
.version
) != MEMORY_SUPERBLOCK_VERSION
) {
2195 ti
->error
= "Invalid version in the superblock";
2200 if (le32_to_cpu(s
.block_size
) != wc
->block_size
) {
2201 ti
->error
= "Block size does not match superblock";
2206 wc
->n_blocks
= le64_to_cpu(s
.n_blocks
);
2208 offset
= wc
->n_blocks
* sizeof(struct wc_memory_entry
);
2209 if (offset
/ sizeof(struct wc_memory_entry
) != le64_to_cpu(sb(wc
)->n_blocks
)) {
2211 ti
->error
= "Overflow in size calculation";
2215 offset
+= sizeof(struct wc_memory_superblock
);
2216 if (offset
< sizeof(struct wc_memory_superblock
))
2218 offset
= (offset
+ wc
->block_size
- 1) & ~(size_t)(wc
->block_size
- 1);
2219 data_size
= wc
->n_blocks
* (size_t)wc
->block_size
;
2220 if (!offset
|| (data_size
/ wc
->block_size
!= wc
->n_blocks
) ||
2221 (offset
+ data_size
< offset
))
2223 if (offset
+ data_size
> wc
->memory_map_size
) {
2224 ti
->error
= "Memory area is too small";
2229 wc
->metadata_sectors
= offset
>> SECTOR_SHIFT
;
2230 wc
->block_start
= (char *)sb(wc
) + offset
;
2232 x
= (uint64_t)wc
->n_blocks
* (100 - high_wm_percent
);
2235 wc
->freelist_high_watermark
= x
;
2236 x
= (uint64_t)wc
->n_blocks
* (100 - low_wm_percent
);
2239 wc
->freelist_low_watermark
= x
;
2241 r
= writecache_alloc_entries(wc
);
2243 ti
->error
= "Cannot allocate memory";
2247 ti
->num_flush_bios
= 1;
2248 ti
->flush_supported
= true;
2249 ti
->num_discard_bios
= 1;
2251 if (WC_MODE_PMEM(wc
))
2252 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
2258 ti
->error
= "Bad arguments";
2264 static void writecache_status(struct dm_target
*ti
, status_type_t type
,
2265 unsigned status_flags
, char *result
, unsigned maxlen
)
2267 struct dm_writecache
*wc
= ti
->private;
2268 unsigned extra_args
;
2273 case STATUSTYPE_INFO
:
2274 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc
),
2275 (unsigned long long)wc
->n_blocks
, (unsigned long long)wc
->freelist_size
,
2276 (unsigned long long)wc
->writeback_size
);
2278 case STATUSTYPE_TABLE
:
2279 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc
) ? 'p' : 's',
2280 wc
->dev
->name
, wc
->ssd_dev
->name
, wc
->block_size
);
2282 if (wc
->start_sector
)
2284 if (wc
->high_wm_percent_set
)
2286 if (wc
->low_wm_percent_set
)
2288 if (wc
->max_writeback_jobs_set
)
2290 if (wc
->autocommit_blocks_set
)
2292 if (wc
->autocommit_time_set
)
2294 if (wc
->writeback_fua_set
)
2297 DMEMIT("%u", extra_args
);
2298 if (wc
->start_sector
)
2299 DMEMIT(" start_sector %llu", (unsigned long long)wc
->start_sector
);
2300 if (wc
->high_wm_percent_set
) {
2301 x
= (uint64_t)wc
->freelist_high_watermark
* 100;
2302 x
+= wc
->n_blocks
/ 2;
2303 do_div(x
, (size_t)wc
->n_blocks
);
2304 DMEMIT(" high_watermark %u", 100 - (unsigned)x
);
2306 if (wc
->low_wm_percent_set
) {
2307 x
= (uint64_t)wc
->freelist_low_watermark
* 100;
2308 x
+= wc
->n_blocks
/ 2;
2309 do_div(x
, (size_t)wc
->n_blocks
);
2310 DMEMIT(" low_watermark %u", 100 - (unsigned)x
);
2312 if (wc
->max_writeback_jobs_set
)
2313 DMEMIT(" writeback_jobs %u", wc
->max_writeback_jobs
);
2314 if (wc
->autocommit_blocks_set
)
2315 DMEMIT(" autocommit_blocks %u", wc
->autocommit_blocks
);
2316 if (wc
->autocommit_time_set
)
2317 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc
->autocommit_jiffies
));
2318 if (wc
->writeback_fua_set
)
2319 DMEMIT(" %sfua", wc
->writeback_fua
? "" : "no");
2324 static struct target_type writecache_target
= {
2325 .name
= "writecache",
2326 .version
= {1, 1, 1},
2327 .module
= THIS_MODULE
,
2328 .ctr
= writecache_ctr
,
2329 .dtr
= writecache_dtr
,
2330 .status
= writecache_status
,
2331 .postsuspend
= writecache_suspend
,
2332 .resume
= writecache_resume
,
2333 .message
= writecache_message
,
2334 .map
= writecache_map
,
2335 .end_io
= writecache_end_io
,
2336 .iterate_devices
= writecache_iterate_devices
,
2337 .io_hints
= writecache_io_hints
,
2340 static int __init
dm_writecache_init(void)
2344 r
= dm_register_target(&writecache_target
);
2346 DMERR("register failed %d", r
);
2353 static void __exit
dm_writecache_exit(void)
2355 dm_unregister_target(&writecache_target
);
2358 module_init(dm_writecache_init
);
2359 module_exit(dm_writecache_exit
);
2361 MODULE_DESCRIPTION(DM_NAME
" writecache target");
2362 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2363 MODULE_LICENSE("GPL");