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 writecache_wait_for_ios(struct dm_writecache
*wc
, int direction
)
447 wait_event(wc
->bio_in_progress_wait
[direction
],
448 !atomic_read(&wc
->bio_in_progress
[direction
]));
451 static void ssd_commit_flushed(struct dm_writecache
*wc
, bool wait_for_ios
)
453 struct dm_io_region region
;
454 struct dm_io_request req
;
455 struct io_notify endio
= {
457 COMPLETION_INITIALIZER_ONSTACK(endio
.c
),
460 unsigned bitmap_bits
= wc
->dirty_bitmap_size
* 8;
465 i
= find_next_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
466 if (unlikely(i
== bitmap_bits
))
468 j
= find_next_zero_bit(wc
->dirty_bitmap
, bitmap_bits
, i
);
470 region
.bdev
= wc
->ssd_dev
->bdev
;
471 region
.sector
= (sector_t
)i
* (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
472 region
.count
= (sector_t
)(j
- i
) * (BITMAP_GRANULARITY
>> SECTOR_SHIFT
);
474 if (unlikely(region
.sector
>= wc
->metadata_sectors
))
476 if (unlikely(region
.sector
+ region
.count
> wc
->metadata_sectors
))
477 region
.count
= wc
->metadata_sectors
- region
.sector
;
479 region
.sector
+= wc
->start_sector
;
480 atomic_inc(&endio
.count
);
481 req
.bi_op
= REQ_OP_WRITE
;
482 req
.bi_op_flags
= REQ_SYNC
;
483 req
.mem
.type
= DM_IO_VMA
;
484 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
+ (size_t)i
* BITMAP_GRANULARITY
;
485 req
.client
= wc
->dm_io
;
486 req
.notify
.fn
= writecache_notify_io
;
487 req
.notify
.context
= &endio
;
489 /* writing via async dm-io (implied by notify.fn above) won't return an error */
490 (void) dm_io(&req
, 1, ®ion
, NULL
);
494 writecache_notify_io(0, &endio
);
495 wait_for_completion_io(&endio
.c
);
498 writecache_wait_for_ios(wc
, WRITE
);
500 writecache_disk_flush(wc
, wc
->ssd_dev
);
502 memset(wc
->dirty_bitmap
, 0, wc
->dirty_bitmap_size
);
505 static void writecache_commit_flushed(struct dm_writecache
*wc
, bool wait_for_ios
)
507 if (WC_MODE_PMEM(wc
))
510 ssd_commit_flushed(wc
, wait_for_ios
);
513 static void writecache_disk_flush(struct dm_writecache
*wc
, struct dm_dev
*dev
)
516 struct dm_io_region region
;
517 struct dm_io_request req
;
519 region
.bdev
= dev
->bdev
;
522 req
.bi_op
= REQ_OP_WRITE
;
523 req
.bi_op_flags
= REQ_PREFLUSH
;
524 req
.mem
.type
= DM_IO_KMEM
;
525 req
.mem
.ptr
.addr
= NULL
;
526 req
.client
= wc
->dm_io
;
527 req
.notify
.fn
= NULL
;
529 r
= dm_io(&req
, 1, ®ion
, NULL
);
531 writecache_error(wc
, r
, "error flushing metadata: %d", r
);
534 #define WFE_RETURN_FOLLOWING 1
535 #define WFE_LOWEST_SEQ 2
537 static struct wc_entry
*writecache_find_entry(struct dm_writecache
*wc
,
538 uint64_t block
, int flags
)
541 struct rb_node
*node
= wc
->tree
.rb_node
;
547 e
= container_of(node
, struct wc_entry
, rb_node
);
548 if (read_original_sector(wc
, e
) == block
)
551 node
= (read_original_sector(wc
, e
) >= block
?
552 e
->rb_node
.rb_left
: e
->rb_node
.rb_right
);
553 if (unlikely(!node
)) {
554 if (!(flags
& WFE_RETURN_FOLLOWING
))
556 if (read_original_sector(wc
, e
) >= block
) {
559 node
= rb_next(&e
->rb_node
);
562 e
= container_of(node
, struct wc_entry
, rb_node
);
570 if (flags
& WFE_LOWEST_SEQ
)
571 node
= rb_prev(&e
->rb_node
);
573 node
= rb_next(&e
->rb_node
);
576 e2
= container_of(node
, struct wc_entry
, rb_node
);
577 if (read_original_sector(wc
, e2
) != block
)
583 static void writecache_insert_entry(struct dm_writecache
*wc
, struct wc_entry
*ins
)
586 struct rb_node
**node
= &wc
->tree
.rb_node
, *parent
= NULL
;
589 e
= container_of(*node
, struct wc_entry
, rb_node
);
590 parent
= &e
->rb_node
;
591 if (read_original_sector(wc
, e
) > read_original_sector(wc
, ins
))
592 node
= &parent
->rb_left
;
594 node
= &parent
->rb_right
;
596 rb_link_node(&ins
->rb_node
, parent
, node
);
597 rb_insert_color(&ins
->rb_node
, &wc
->tree
);
598 list_add(&ins
->lru
, &wc
->lru
);
601 static void writecache_unlink(struct dm_writecache
*wc
, struct wc_entry
*e
)
604 rb_erase(&e
->rb_node
, &wc
->tree
);
607 static void writecache_add_to_freelist(struct dm_writecache
*wc
, struct wc_entry
*e
)
609 if (WC_MODE_SORT_FREELIST(wc
)) {
610 struct rb_node
**node
= &wc
->freetree
.rb_node
, *parent
= NULL
;
611 if (unlikely(!*node
))
612 wc
->current_free
= e
;
615 if (&e
->rb_node
< *node
)
616 node
= &parent
->rb_left
;
618 node
= &parent
->rb_right
;
620 rb_link_node(&e
->rb_node
, parent
, node
);
621 rb_insert_color(&e
->rb_node
, &wc
->freetree
);
623 list_add_tail(&e
->lru
, &wc
->freelist
);
628 static struct wc_entry
*writecache_pop_from_freelist(struct dm_writecache
*wc
, sector_t expected_sector
)
632 if (WC_MODE_SORT_FREELIST(wc
)) {
633 struct rb_node
*next
;
634 if (unlikely(!wc
->current_free
))
636 e
= wc
->current_free
;
637 if (expected_sector
!= (sector_t
)-1 && unlikely(cache_sector(wc
, e
) != expected_sector
))
639 next
= rb_next(&e
->rb_node
);
640 rb_erase(&e
->rb_node
, &wc
->freetree
);
642 next
= rb_first(&wc
->freetree
);
643 wc
->current_free
= next
? container_of(next
, struct wc_entry
, rb_node
) : NULL
;
645 if (unlikely(list_empty(&wc
->freelist
)))
647 e
= container_of(wc
->freelist
.next
, struct wc_entry
, lru
);
648 if (expected_sector
!= (sector_t
)-1 && unlikely(cache_sector(wc
, e
) != expected_sector
))
653 if (unlikely(wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_high_watermark
))
654 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
659 static void writecache_free_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
661 writecache_unlink(wc
, e
);
662 writecache_add_to_freelist(wc
, e
);
663 clear_seq_count(wc
, e
);
664 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
665 if (unlikely(waitqueue_active(&wc
->freelist_wait
)))
666 wake_up(&wc
->freelist_wait
);
669 static void writecache_wait_on_freelist(struct dm_writecache
*wc
)
673 prepare_to_wait(&wc
->freelist_wait
, &wait
, TASK_UNINTERRUPTIBLE
);
676 finish_wait(&wc
->freelist_wait
, &wait
);
680 static void writecache_poison_lists(struct dm_writecache
*wc
)
683 * Catch incorrect access to these values while the device is suspended.
685 memset(&wc
->tree
, -1, sizeof wc
->tree
);
686 wc
->lru
.next
= LIST_POISON1
;
687 wc
->lru
.prev
= LIST_POISON2
;
688 wc
->freelist
.next
= LIST_POISON1
;
689 wc
->freelist
.prev
= LIST_POISON2
;
692 static void writecache_flush_entry(struct dm_writecache
*wc
, struct wc_entry
*e
)
694 writecache_flush_region(wc
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
695 if (WC_MODE_PMEM(wc
))
696 writecache_flush_region(wc
, memory_data(wc
, e
), wc
->block_size
);
699 static bool writecache_entry_is_committed(struct dm_writecache
*wc
, struct wc_entry
*e
)
701 return read_seq_count(wc
, e
) < wc
->seq_count
;
704 static void writecache_flush(struct dm_writecache
*wc
)
706 struct wc_entry
*e
, *e2
;
707 bool need_flush_after_free
;
709 wc
->uncommitted_blocks
= 0;
710 del_timer(&wc
->autocommit_timer
);
712 if (list_empty(&wc
->lru
))
715 e
= container_of(wc
->lru
.next
, struct wc_entry
, lru
);
716 if (writecache_entry_is_committed(wc
, e
)) {
717 if (wc
->overwrote_committed
) {
718 writecache_wait_for_ios(wc
, WRITE
);
719 writecache_disk_flush(wc
, wc
->ssd_dev
);
720 wc
->overwrote_committed
= false;
725 writecache_flush_entry(wc
, e
);
726 if (unlikely(e
->lru
.next
== &wc
->lru
))
728 e2
= container_of(e
->lru
.next
, struct wc_entry
, lru
);
729 if (writecache_entry_is_committed(wc
, e2
))
734 writecache_commit_flushed(wc
, true);
737 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(wc
->seq_count
));
738 writecache_flush_region(wc
, &sb(wc
)->seq_count
, sizeof sb(wc
)->seq_count
);
739 writecache_commit_flushed(wc
, false);
741 wc
->overwrote_committed
= false;
743 need_flush_after_free
= false;
745 /* Free another committed entry with lower seq-count */
746 struct rb_node
*rb_node
= rb_prev(&e
->rb_node
);
749 e2
= container_of(rb_node
, struct wc_entry
, rb_node
);
750 if (read_original_sector(wc
, e2
) == read_original_sector(wc
, e
) &&
751 likely(!e2
->write_in_progress
)) {
752 writecache_free_entry(wc
, e2
);
753 need_flush_after_free
= true;
756 if (unlikely(e
->lru
.prev
== &wc
->lru
))
758 e
= container_of(e
->lru
.prev
, struct wc_entry
, lru
);
762 if (need_flush_after_free
)
763 writecache_commit_flushed(wc
, false);
766 static void writecache_flush_work(struct work_struct
*work
)
768 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, flush_work
);
771 writecache_flush(wc
);
775 static void writecache_autocommit_timer(struct timer_list
*t
)
777 struct dm_writecache
*wc
= from_timer(wc
, t
, autocommit_timer
);
778 if (!writecache_has_error(wc
))
779 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
782 static void writecache_schedule_autocommit(struct dm_writecache
*wc
)
784 if (!timer_pending(&wc
->autocommit_timer
))
785 mod_timer(&wc
->autocommit_timer
, jiffies
+ wc
->autocommit_jiffies
);
788 static void writecache_discard(struct dm_writecache
*wc
, sector_t start
, sector_t end
)
791 bool discarded_something
= false;
793 e
= writecache_find_entry(wc
, start
, WFE_RETURN_FOLLOWING
| WFE_LOWEST_SEQ
);
797 while (read_original_sector(wc
, e
) < end
) {
798 struct rb_node
*node
= rb_next(&e
->rb_node
);
800 if (likely(!e
->write_in_progress
)) {
801 if (!discarded_something
) {
802 writecache_wait_for_ios(wc
, READ
);
803 writecache_wait_for_ios(wc
, WRITE
);
804 discarded_something
= true;
806 writecache_free_entry(wc
, e
);
812 e
= container_of(node
, struct wc_entry
, rb_node
);
815 if (discarded_something
)
816 writecache_commit_flushed(wc
, false);
819 static bool writecache_wait_for_writeback(struct dm_writecache
*wc
)
821 if (wc
->writeback_size
) {
822 writecache_wait_on_freelist(wc
);
828 static void writecache_suspend(struct dm_target
*ti
)
830 struct dm_writecache
*wc
= ti
->private;
831 bool flush_on_suspend
;
833 del_timer_sync(&wc
->autocommit_timer
);
836 writecache_flush(wc
);
837 flush_on_suspend
= wc
->flush_on_suspend
;
838 if (flush_on_suspend
) {
839 wc
->flush_on_suspend
= false;
841 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
845 flush_workqueue(wc
->writeback_wq
);
848 if (flush_on_suspend
)
850 while (writecache_wait_for_writeback(wc
));
852 if (WC_MODE_PMEM(wc
))
853 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
855 writecache_poison_lists(wc
);
860 static int writecache_alloc_entries(struct dm_writecache
*wc
)
866 wc
->entries
= vmalloc(array_size(sizeof(struct wc_entry
), wc
->n_blocks
));
869 for (b
= 0; b
< wc
->n_blocks
; b
++) {
870 struct wc_entry
*e
= &wc
->entries
[b
];
872 e
->write_in_progress
= false;
878 static void writecache_resume(struct dm_target
*ti
)
880 struct dm_writecache
*wc
= ti
->private;
882 bool need_flush
= false;
888 if (WC_MODE_PMEM(wc
))
889 persistent_memory_invalidate_cache(wc
->memory_map
, wc
->memory_map_size
);
892 INIT_LIST_HEAD(&wc
->lru
);
893 if (WC_MODE_SORT_FREELIST(wc
)) {
894 wc
->freetree
= RB_ROOT
;
895 wc
->current_free
= NULL
;
897 INIT_LIST_HEAD(&wc
->freelist
);
899 wc
->freelist_size
= 0;
901 r
= memcpy_mcsafe(&sb_seq_count
, &sb(wc
)->seq_count
, sizeof(uint64_t));
903 writecache_error(wc
, r
, "hardware memory error when reading superblock: %d", r
);
904 sb_seq_count
= cpu_to_le64(0);
906 wc
->seq_count
= le64_to_cpu(sb_seq_count
);
908 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
909 for (b
= 0; b
< wc
->n_blocks
; b
++) {
910 struct wc_entry
*e
= &wc
->entries
[b
];
911 struct wc_memory_entry wme
;
912 if (writecache_has_error(wc
)) {
913 e
->original_sector
= -1;
917 r
= memcpy_mcsafe(&wme
, memory_entry(wc
, e
), sizeof(struct wc_memory_entry
));
919 writecache_error(wc
, r
, "hardware memory error when reading metadata entry %lu: %d",
920 (unsigned long)b
, r
);
921 e
->original_sector
= -1;
924 e
->original_sector
= le64_to_cpu(wme
.original_sector
);
925 e
->seq_count
= le64_to_cpu(wme
.seq_count
);
929 for (b
= 0; b
< wc
->n_blocks
; b
++) {
930 struct wc_entry
*e
= &wc
->entries
[b
];
931 if (!writecache_entry_is_committed(wc
, e
)) {
932 if (read_seq_count(wc
, e
) != -1) {
934 clear_seq_count(wc
, e
);
937 writecache_add_to_freelist(wc
, e
);
939 struct wc_entry
*old
;
941 old
= writecache_find_entry(wc
, read_original_sector(wc
, e
), 0);
943 writecache_insert_entry(wc
, e
);
945 if (read_seq_count(wc
, old
) == read_seq_count(wc
, e
)) {
946 writecache_error(wc
, -EINVAL
,
947 "two identical entries, position %llu, sector %llu, sequence %llu",
948 (unsigned long long)b
, (unsigned long long)read_original_sector(wc
, e
),
949 (unsigned long long)read_seq_count(wc
, e
));
951 if (read_seq_count(wc
, old
) > read_seq_count(wc
, e
)) {
954 writecache_free_entry(wc
, old
);
955 writecache_insert_entry(wc
, e
);
964 writecache_flush_all_metadata(wc
);
965 writecache_commit_flushed(wc
, false);
971 static int process_flush_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
977 if (dm_suspended(wc
->ti
)) {
981 if (writecache_has_error(wc
)) {
986 writecache_flush(wc
);
988 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
991 flush_workqueue(wc
->writeback_wq
);
995 if (writecache_has_error(wc
)) {
1004 static int process_flush_on_suspend_mesg(unsigned argc
, char **argv
, struct dm_writecache
*wc
)
1010 wc
->flush_on_suspend
= true;
1016 static int writecache_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
1017 char *result
, unsigned maxlen
)
1020 struct dm_writecache
*wc
= ti
->private;
1022 if (!strcasecmp(argv
[0], "flush"))
1023 r
= process_flush_mesg(argc
, argv
, wc
);
1024 else if (!strcasecmp(argv
[0], "flush_on_suspend"))
1025 r
= process_flush_on_suspend_mesg(argc
, argv
, wc
);
1027 DMERR("unrecognised message received: %s", argv
[0]);
1032 static void bio_copy_block(struct dm_writecache
*wc
, struct bio
*bio
, void *data
)
1035 unsigned long flags
;
1037 int rw
= bio_data_dir(bio
);
1038 unsigned remaining_size
= wc
->block_size
;
1041 struct bio_vec bv
= bio_iter_iovec(bio
, bio
->bi_iter
);
1042 buf
= bvec_kmap_irq(&bv
, &flags
);
1044 if (unlikely(size
> remaining_size
))
1045 size
= remaining_size
;
1049 r
= memcpy_mcsafe(buf
, data
, size
);
1050 flush_dcache_page(bio_page(bio
));
1052 writecache_error(wc
, r
, "hardware memory error when reading data: %d", r
);
1053 bio
->bi_status
= BLK_STS_IOERR
;
1056 flush_dcache_page(bio_page(bio
));
1057 memcpy_flushcache(data
, buf
, size
);
1060 bvec_kunmap_irq(buf
, &flags
);
1062 data
= (char *)data
+ size
;
1063 remaining_size
-= size
;
1064 bio_advance(bio
, size
);
1065 } while (unlikely(remaining_size
));
1068 static int writecache_flush_thread(void *data
)
1070 struct dm_writecache
*wc
= data
;
1076 bio
= bio_list_pop(&wc
->flush_list
);
1078 set_current_state(TASK_INTERRUPTIBLE
);
1081 if (unlikely(kthread_should_stop())) {
1082 set_current_state(TASK_RUNNING
);
1090 if (bio_op(bio
) == REQ_OP_DISCARD
) {
1091 writecache_discard(wc
, bio
->bi_iter
.bi_sector
,
1092 bio_end_sector(bio
));
1094 bio_set_dev(bio
, wc
->dev
->bdev
);
1095 generic_make_request(bio
);
1097 writecache_flush(wc
);
1099 if (writecache_has_error(wc
))
1100 bio
->bi_status
= BLK_STS_IOERR
;
1108 static void writecache_offload_bio(struct dm_writecache
*wc
, struct bio
*bio
)
1110 if (bio_list_empty(&wc
->flush_list
))
1111 wake_up_process(wc
->flush_thread
);
1112 bio_list_add(&wc
->flush_list
, bio
);
1115 static int writecache_map(struct dm_target
*ti
, struct bio
*bio
)
1118 struct dm_writecache
*wc
= ti
->private;
1120 bio
->bi_private
= NULL
;
1124 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1125 if (writecache_has_error(wc
))
1127 if (WC_MODE_PMEM(wc
)) {
1128 writecache_flush(wc
);
1129 if (writecache_has_error(wc
))
1133 writecache_offload_bio(wc
, bio
);
1138 bio
->bi_iter
.bi_sector
= dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
1140 if (unlikely((((unsigned)bio
->bi_iter
.bi_sector
| bio_sectors(bio
)) &
1141 (wc
->block_size
/ 512 - 1)) != 0)) {
1142 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1143 (unsigned long long)bio
->bi_iter
.bi_sector
,
1144 bio
->bi_iter
.bi_size
, wc
->block_size
);
1148 if (unlikely(bio_op(bio
) == REQ_OP_DISCARD
)) {
1149 if (writecache_has_error(wc
))
1151 if (WC_MODE_PMEM(wc
)) {
1152 writecache_discard(wc
, bio
->bi_iter
.bi_sector
, bio_end_sector(bio
));
1153 goto unlock_remap_origin
;
1155 writecache_offload_bio(wc
, bio
);
1160 if (bio_data_dir(bio
) == READ
) {
1162 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, WFE_RETURN_FOLLOWING
);
1163 if (e
&& read_original_sector(wc
, e
) == bio
->bi_iter
.bi_sector
) {
1164 if (WC_MODE_PMEM(wc
)) {
1165 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1166 if (bio
->bi_iter
.bi_size
)
1167 goto read_next_block
;
1170 dm_accept_partial_bio(bio
, wc
->block_size
>> SECTOR_SHIFT
);
1171 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1172 bio
->bi_iter
.bi_sector
= cache_sector(wc
, e
);
1173 if (!writecache_entry_is_committed(wc
, e
))
1174 writecache_wait_for_ios(wc
, WRITE
);
1179 sector_t next_boundary
=
1180 read_original_sector(wc
, e
) - bio
->bi_iter
.bi_sector
;
1181 if (next_boundary
< bio
->bi_iter
.bi_size
>> SECTOR_SHIFT
) {
1182 dm_accept_partial_bio(bio
, next_boundary
);
1185 goto unlock_remap_origin
;
1189 if (writecache_has_error(wc
))
1191 e
= writecache_find_entry(wc
, bio
->bi_iter
.bi_sector
, 0);
1193 if (!writecache_entry_is_committed(wc
, e
))
1195 if (!WC_MODE_PMEM(wc
) && !e
->write_in_progress
) {
1196 wc
->overwrote_committed
= true;
1200 e
= writecache_pop_from_freelist(wc
, (sector_t
)-1);
1202 writecache_wait_on_freelist(wc
);
1205 write_original_sector_seq_count(wc
, e
, bio
->bi_iter
.bi_sector
, wc
->seq_count
);
1206 writecache_insert_entry(wc
, e
);
1207 wc
->uncommitted_blocks
++;
1209 if (WC_MODE_PMEM(wc
)) {
1210 bio_copy_block(wc
, bio
, memory_data(wc
, e
));
1212 unsigned bio_size
= wc
->block_size
;
1213 sector_t start_cache_sec
= cache_sector(wc
, e
);
1214 sector_t current_cache_sec
= start_cache_sec
+ (bio_size
>> SECTOR_SHIFT
);
1216 while (bio_size
< bio
->bi_iter
.bi_size
) {
1217 struct wc_entry
*f
= writecache_pop_from_freelist(wc
, current_cache_sec
);
1220 write_original_sector_seq_count(wc
, f
, bio
->bi_iter
.bi_sector
+
1221 (bio_size
>> SECTOR_SHIFT
), wc
->seq_count
);
1222 writecache_insert_entry(wc
, f
);
1223 wc
->uncommitted_blocks
++;
1224 bio_size
+= wc
->block_size
;
1225 current_cache_sec
+= wc
->block_size
>> SECTOR_SHIFT
;
1228 bio_set_dev(bio
, wc
->ssd_dev
->bdev
);
1229 bio
->bi_iter
.bi_sector
= start_cache_sec
;
1230 dm_accept_partial_bio(bio
, bio_size
>> SECTOR_SHIFT
);
1232 if (unlikely(wc
->uncommitted_blocks
>= wc
->autocommit_blocks
)) {
1233 wc
->uncommitted_blocks
= 0;
1234 queue_work(wc
->writeback_wq
, &wc
->flush_work
);
1236 writecache_schedule_autocommit(wc
);
1240 } while (bio
->bi_iter
.bi_size
);
1242 if (unlikely(bio
->bi_opf
& REQ_FUA
||
1243 wc
->uncommitted_blocks
>= wc
->autocommit_blocks
))
1244 writecache_flush(wc
);
1246 writecache_schedule_autocommit(wc
);
1250 unlock_remap_origin
:
1251 bio_set_dev(bio
, wc
->dev
->bdev
);
1253 return DM_MAPIO_REMAPPED
;
1256 /* make sure that writecache_end_io decrements bio_in_progress: */
1257 bio
->bi_private
= (void *)1;
1258 atomic_inc(&wc
->bio_in_progress
[bio_data_dir(bio
)]);
1260 return DM_MAPIO_REMAPPED
;
1265 return DM_MAPIO_SUBMITTED
;
1269 return DM_MAPIO_SUBMITTED
;
1274 return DM_MAPIO_SUBMITTED
;
1277 static int writecache_end_io(struct dm_target
*ti
, struct bio
*bio
, blk_status_t
*status
)
1279 struct dm_writecache
*wc
= ti
->private;
1281 if (bio
->bi_private
!= NULL
) {
1282 int dir
= bio_data_dir(bio
);
1283 if (atomic_dec_and_test(&wc
->bio_in_progress
[dir
]))
1284 if (unlikely(waitqueue_active(&wc
->bio_in_progress_wait
[dir
])))
1285 wake_up(&wc
->bio_in_progress_wait
[dir
]);
1290 static int writecache_iterate_devices(struct dm_target
*ti
,
1291 iterate_devices_callout_fn fn
, void *data
)
1293 struct dm_writecache
*wc
= ti
->private;
1295 return fn(ti
, wc
->dev
, 0, ti
->len
, data
);
1298 static void writecache_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
1300 struct dm_writecache
*wc
= ti
->private;
1302 if (limits
->logical_block_size
< wc
->block_size
)
1303 limits
->logical_block_size
= wc
->block_size
;
1305 if (limits
->physical_block_size
< wc
->block_size
)
1306 limits
->physical_block_size
= wc
->block_size
;
1308 if (limits
->io_min
< wc
->block_size
)
1309 limits
->io_min
= wc
->block_size
;
1313 static void writecache_writeback_endio(struct bio
*bio
)
1315 struct writeback_struct
*wb
= container_of(bio
, struct writeback_struct
, bio
);
1316 struct dm_writecache
*wc
= wb
->wc
;
1317 unsigned long flags
;
1319 raw_spin_lock_irqsave(&wc
->endio_list_lock
, flags
);
1320 if (unlikely(list_empty(&wc
->endio_list
)))
1321 wake_up_process(wc
->endio_thread
);
1322 list_add_tail(&wb
->endio_entry
, &wc
->endio_list
);
1323 raw_spin_unlock_irqrestore(&wc
->endio_list_lock
, flags
);
1326 static void writecache_copy_endio(int read_err
, unsigned long write_err
, void *ptr
)
1328 struct copy_struct
*c
= ptr
;
1329 struct dm_writecache
*wc
= c
->wc
;
1331 c
->error
= likely(!(read_err
| write_err
)) ? 0 : -EIO
;
1333 raw_spin_lock_irq(&wc
->endio_list_lock
);
1334 if (unlikely(list_empty(&wc
->endio_list
)))
1335 wake_up_process(wc
->endio_thread
);
1336 list_add_tail(&c
->endio_entry
, &wc
->endio_list
);
1337 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1340 static void __writecache_endio_pmem(struct dm_writecache
*wc
, struct list_head
*list
)
1343 struct writeback_struct
*wb
;
1345 unsigned long n_walked
= 0;
1348 wb
= list_entry(list
->next
, struct writeback_struct
, endio_entry
);
1349 list_del(&wb
->endio_entry
);
1351 if (unlikely(wb
->bio
.bi_status
!= BLK_STS_OK
))
1352 writecache_error(wc
, blk_status_to_errno(wb
->bio
.bi_status
),
1353 "write error %d", wb
->bio
.bi_status
);
1357 BUG_ON(!e
->write_in_progress
);
1358 e
->write_in_progress
= false;
1359 INIT_LIST_HEAD(&e
->lru
);
1360 if (!writecache_has_error(wc
))
1361 writecache_free_entry(wc
, e
);
1362 BUG_ON(!wc
->writeback_size
);
1363 wc
->writeback_size
--;
1365 if (unlikely(n_walked
>= ENDIO_LATENCY
)) {
1366 writecache_commit_flushed(wc
, false);
1371 } while (++i
< wb
->wc_list_n
);
1373 if (wb
->wc_list
!= wb
->wc_list_inline
)
1376 } while (!list_empty(list
));
1379 static void __writecache_endio_ssd(struct dm_writecache
*wc
, struct list_head
*list
)
1381 struct copy_struct
*c
;
1385 c
= list_entry(list
->next
, struct copy_struct
, endio_entry
);
1386 list_del(&c
->endio_entry
);
1388 if (unlikely(c
->error
))
1389 writecache_error(wc
, c
->error
, "copy error");
1393 BUG_ON(!e
->write_in_progress
);
1394 e
->write_in_progress
= false;
1395 INIT_LIST_HEAD(&e
->lru
);
1396 if (!writecache_has_error(wc
))
1397 writecache_free_entry(wc
, e
);
1399 BUG_ON(!wc
->writeback_size
);
1400 wc
->writeback_size
--;
1402 } while (--c
->n_entries
);
1403 mempool_free(c
, &wc
->copy_pool
);
1404 } while (!list_empty(list
));
1407 static int writecache_endio_thread(void *data
)
1409 struct dm_writecache
*wc
= data
;
1412 struct list_head list
;
1414 raw_spin_lock_irq(&wc
->endio_list_lock
);
1415 if (!list_empty(&wc
->endio_list
))
1417 set_current_state(TASK_INTERRUPTIBLE
);
1418 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1420 if (unlikely(kthread_should_stop())) {
1421 set_current_state(TASK_RUNNING
);
1430 list
= wc
->endio_list
;
1431 list
.next
->prev
= list
.prev
->next
= &list
;
1432 INIT_LIST_HEAD(&wc
->endio_list
);
1433 raw_spin_unlock_irq(&wc
->endio_list_lock
);
1435 if (!WC_MODE_FUA(wc
))
1436 writecache_disk_flush(wc
, wc
->dev
);
1440 if (WC_MODE_PMEM(wc
)) {
1441 __writecache_endio_pmem(wc
, &list
);
1443 __writecache_endio_ssd(wc
, &list
);
1444 writecache_wait_for_ios(wc
, READ
);
1447 writecache_commit_flushed(wc
, false);
1455 static bool wc_add_block(struct writeback_struct
*wb
, struct wc_entry
*e
, gfp_t gfp
)
1457 struct dm_writecache
*wc
= wb
->wc
;
1458 unsigned block_size
= wc
->block_size
;
1459 void *address
= memory_data(wc
, e
);
1461 persistent_memory_flush_cache(address
, block_size
);
1462 return bio_add_page(&wb
->bio
, persistent_memory_page(address
),
1463 block_size
, persistent_memory_page_offset(address
)) != 0;
1466 struct writeback_list
{
1467 struct list_head list
;
1471 static void __writeback_throttle(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1473 if (unlikely(wc
->max_writeback_jobs
)) {
1474 if (READ_ONCE(wc
->writeback_size
) - wbl
->size
>= wc
->max_writeback_jobs
) {
1476 while (wc
->writeback_size
- wbl
->size
>= wc
->max_writeback_jobs
)
1477 writecache_wait_on_freelist(wc
);
1484 static void __writecache_writeback_pmem(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1486 struct wc_entry
*e
, *f
;
1488 struct writeback_struct
*wb
;
1493 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1496 max_pages
= e
->wc_list_contiguous
;
1498 bio
= bio_alloc_bioset(GFP_NOIO
, max_pages
, &wc
->bio_set
);
1499 wb
= container_of(bio
, struct writeback_struct
, bio
);
1501 bio
->bi_end_io
= writecache_writeback_endio
;
1502 bio_set_dev(bio
, wc
->dev
->bdev
);
1503 bio
->bi_iter
.bi_sector
= read_original_sector(wc
, e
);
1504 if (max_pages
<= WB_LIST_INLINE
||
1505 unlikely(!(wb
->wc_list
= kmalloc_array(max_pages
, sizeof(struct wc_entry
*),
1506 GFP_NOIO
| __GFP_NORETRY
|
1507 __GFP_NOMEMALLOC
| __GFP_NOWARN
)))) {
1508 wb
->wc_list
= wb
->wc_list_inline
;
1509 max_pages
= WB_LIST_INLINE
;
1512 BUG_ON(!wc_add_block(wb
, e
, GFP_NOIO
));
1517 while (wbl
->size
&& wb
->wc_list_n
< max_pages
) {
1518 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1519 if (read_original_sector(wc
, f
) !=
1520 read_original_sector(wc
, e
) + (wc
->block_size
>> SECTOR_SHIFT
))
1522 if (!wc_add_block(wb
, f
, GFP_NOWAIT
| __GFP_NOWARN
))
1526 wb
->wc_list
[wb
->wc_list_n
++] = f
;
1529 bio_set_op_attrs(bio
, REQ_OP_WRITE
, WC_MODE_FUA(wc
) * REQ_FUA
);
1530 if (writecache_has_error(wc
)) {
1531 bio
->bi_status
= BLK_STS_IOERR
;
1537 __writeback_throttle(wc
, wbl
);
1541 static void __writecache_writeback_ssd(struct dm_writecache
*wc
, struct writeback_list
*wbl
)
1543 struct wc_entry
*e
, *f
;
1544 struct dm_io_region from
, to
;
1545 struct copy_struct
*c
;
1551 e
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1554 n_sectors
= e
->wc_list_contiguous
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
1556 from
.bdev
= wc
->ssd_dev
->bdev
;
1557 from
.sector
= cache_sector(wc
, e
);
1558 from
.count
= n_sectors
;
1559 to
.bdev
= wc
->dev
->bdev
;
1560 to
.sector
= read_original_sector(wc
, e
);
1561 to
.count
= n_sectors
;
1563 c
= mempool_alloc(&wc
->copy_pool
, GFP_NOIO
);
1566 c
->n_entries
= e
->wc_list_contiguous
;
1568 while ((n_sectors
-= wc
->block_size
>> SECTOR_SHIFT
)) {
1570 f
= container_of(wbl
->list
.prev
, struct wc_entry
, lru
);
1576 dm_kcopyd_copy(wc
->dm_kcopyd
, &from
, 1, &to
, 0, writecache_copy_endio
, c
);
1578 __writeback_throttle(wc
, wbl
);
1582 static void writecache_writeback(struct work_struct
*work
)
1584 struct dm_writecache
*wc
= container_of(work
, struct dm_writecache
, writeback_work
);
1585 struct blk_plug plug
;
1586 struct wc_entry
*f
, *uninitialized_var(g
), *e
= NULL
;
1587 struct rb_node
*node
, *next_node
;
1588 struct list_head skipped
;
1589 struct writeback_list wbl
;
1590 unsigned long n_walked
;
1594 if (writecache_has_error(wc
)) {
1599 if (unlikely(wc
->writeback_all
)) {
1600 if (writecache_wait_for_writeback(wc
))
1604 if (wc
->overwrote_committed
) {
1605 writecache_wait_for_ios(wc
, WRITE
);
1609 INIT_LIST_HEAD(&skipped
);
1610 INIT_LIST_HEAD(&wbl
.list
);
1612 while (!list_empty(&wc
->lru
) &&
1613 (wc
->writeback_all
||
1614 wc
->freelist_size
+ wc
->writeback_size
<= wc
->freelist_low_watermark
)) {
1617 if (unlikely(n_walked
> WRITEBACK_LATENCY
) &&
1618 likely(!wc
->writeback_all
) && likely(!dm_suspended(wc
->ti
))) {
1619 queue_work(wc
->writeback_wq
, &wc
->writeback_work
);
1623 if (unlikely(wc
->writeback_all
)) {
1625 writecache_flush(wc
);
1626 e
= container_of(rb_first(&wc
->tree
), struct wc_entry
, rb_node
);
1630 e
= container_of(wc
->lru
.prev
, struct wc_entry
, lru
);
1631 BUG_ON(e
->write_in_progress
);
1632 if (unlikely(!writecache_entry_is_committed(wc
, e
))) {
1633 writecache_flush(wc
);
1635 node
= rb_prev(&e
->rb_node
);
1637 f
= container_of(node
, struct wc_entry
, rb_node
);
1638 if (unlikely(read_original_sector(wc
, f
) ==
1639 read_original_sector(wc
, e
))) {
1640 BUG_ON(!f
->write_in_progress
);
1642 list_add(&e
->lru
, &skipped
);
1647 wc
->writeback_size
++;
1649 list_add(&e
->lru
, &wbl
.list
);
1651 e
->write_in_progress
= true;
1652 e
->wc_list_contiguous
= 1;
1657 next_node
= rb_next(&f
->rb_node
);
1658 if (unlikely(!next_node
))
1660 g
= container_of(next_node
, struct wc_entry
, rb_node
);
1661 if (unlikely(read_original_sector(wc
, g
) ==
1662 read_original_sector(wc
, f
))) {
1666 if (read_original_sector(wc
, g
) !=
1667 read_original_sector(wc
, f
) + (wc
->block_size
>> SECTOR_SHIFT
))
1669 if (unlikely(g
->write_in_progress
))
1671 if (unlikely(!writecache_entry_is_committed(wc
, g
)))
1674 if (!WC_MODE_PMEM(wc
)) {
1680 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1683 wc
->writeback_size
++;
1685 list_add(&g
->lru
, &wbl
.list
);
1687 g
->write_in_progress
= true;
1688 g
->wc_list_contiguous
= BIO_MAX_PAGES
;
1690 e
->wc_list_contiguous
++;
1691 if (unlikely(e
->wc_list_contiguous
== BIO_MAX_PAGES
)) {
1692 if (unlikely(wc
->writeback_all
)) {
1693 next_node
= rb_next(&f
->rb_node
);
1694 if (likely(next_node
))
1695 g
= container_of(next_node
, struct wc_entry
, rb_node
);
1703 if (!list_empty(&skipped
)) {
1704 list_splice_tail(&skipped
, &wc
->lru
);
1706 * If we didn't do any progress, we must wait until some
1707 * writeback finishes to avoid burning CPU in a loop
1709 if (unlikely(!wbl
.size
))
1710 writecache_wait_for_writeback(wc
);
1715 blk_start_plug(&plug
);
1717 if (WC_MODE_PMEM(wc
))
1718 __writecache_writeback_pmem(wc
, &wbl
);
1720 __writecache_writeback_ssd(wc
, &wbl
);
1722 blk_finish_plug(&plug
);
1724 if (unlikely(wc
->writeback_all
)) {
1726 while (writecache_wait_for_writeback(wc
));
1731 static int calculate_memory_size(uint64_t device_size
, unsigned block_size
,
1732 size_t *n_blocks_p
, size_t *n_metadata_blocks_p
)
1734 uint64_t n_blocks
, offset
;
1737 n_blocks
= device_size
;
1738 do_div(n_blocks
, block_size
+ sizeof(struct wc_memory_entry
));
1743 /* Verify the following entries[n_blocks] won't overflow */
1744 if (n_blocks
>= ((size_t)-sizeof(struct wc_memory_superblock
) /
1745 sizeof(struct wc_memory_entry
)))
1747 offset
= offsetof(struct wc_memory_superblock
, entries
[n_blocks
]);
1748 offset
= (offset
+ block_size
- 1) & ~(uint64_t)(block_size
- 1);
1749 if (offset
+ n_blocks
* block_size
<= device_size
)
1754 /* check if the bit field overflows */
1756 if (e
.index
!= n_blocks
)
1760 *n_blocks_p
= n_blocks
;
1761 if (n_metadata_blocks_p
)
1762 *n_metadata_blocks_p
= offset
>> __ffs(block_size
);
1766 static int init_memory(struct dm_writecache
*wc
)
1771 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
, &wc
->n_blocks
, NULL
);
1775 r
= writecache_alloc_entries(wc
);
1779 for (b
= 0; b
< ARRAY_SIZE(sb(wc
)->padding
); b
++)
1780 pmem_assign(sb(wc
)->padding
[b
], cpu_to_le64(0));
1781 pmem_assign(sb(wc
)->version
, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION
));
1782 pmem_assign(sb(wc
)->block_size
, cpu_to_le32(wc
->block_size
));
1783 pmem_assign(sb(wc
)->n_blocks
, cpu_to_le64(wc
->n_blocks
));
1784 pmem_assign(sb(wc
)->seq_count
, cpu_to_le64(0));
1786 for (b
= 0; b
< wc
->n_blocks
; b
++)
1787 write_original_sector_seq_count(wc
, &wc
->entries
[b
], -1, -1);
1789 writecache_flush_all_metadata(wc
);
1790 writecache_commit_flushed(wc
, false);
1791 pmem_assign(sb(wc
)->magic
, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC
));
1792 writecache_flush_region(wc
, &sb(wc
)->magic
, sizeof sb(wc
)->magic
);
1793 writecache_commit_flushed(wc
, false);
1798 static void writecache_dtr(struct dm_target
*ti
)
1800 struct dm_writecache
*wc
= ti
->private;
1805 if (wc
->endio_thread
)
1806 kthread_stop(wc
->endio_thread
);
1808 if (wc
->flush_thread
)
1809 kthread_stop(wc
->flush_thread
);
1811 bioset_exit(&wc
->bio_set
);
1813 mempool_exit(&wc
->copy_pool
);
1815 if (wc
->writeback_wq
)
1816 destroy_workqueue(wc
->writeback_wq
);
1819 dm_put_device(ti
, wc
->dev
);
1822 dm_put_device(ti
, wc
->ssd_dev
);
1827 if (wc
->memory_map
) {
1828 if (WC_MODE_PMEM(wc
))
1829 persistent_memory_release(wc
);
1831 vfree(wc
->memory_map
);
1835 dm_kcopyd_client_destroy(wc
->dm_kcopyd
);
1838 dm_io_client_destroy(wc
->dm_io
);
1840 if (wc
->dirty_bitmap
)
1841 vfree(wc
->dirty_bitmap
);
1846 static int writecache_ctr(struct dm_target
*ti
, unsigned argc
, char **argv
)
1848 struct dm_writecache
*wc
;
1849 struct dm_arg_set as
;
1851 unsigned opt_params
;
1852 size_t offset
, data_size
;
1855 int high_wm_percent
= HIGH_WATERMARK
;
1856 int low_wm_percent
= LOW_WATERMARK
;
1858 struct wc_memory_superblock s
;
1860 static struct dm_arg _args
[] = {
1861 {0, 10, "Invalid number of feature args"},
1867 wc
= kzalloc(sizeof(struct dm_writecache
), GFP_KERNEL
);
1869 ti
->error
= "Cannot allocate writecache structure";
1876 mutex_init(&wc
->lock
);
1877 writecache_poison_lists(wc
);
1878 init_waitqueue_head(&wc
->freelist_wait
);
1879 timer_setup(&wc
->autocommit_timer
, writecache_autocommit_timer
, 0);
1881 for (i
= 0; i
< 2; i
++) {
1882 atomic_set(&wc
->bio_in_progress
[i
], 0);
1883 init_waitqueue_head(&wc
->bio_in_progress_wait
[i
]);
1886 wc
->dm_io
= dm_io_client_create();
1887 if (IS_ERR(wc
->dm_io
)) {
1888 r
= PTR_ERR(wc
->dm_io
);
1889 ti
->error
= "Unable to allocate dm-io client";
1894 wc
->writeback_wq
= alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM
, 1);
1895 if (!wc
->writeback_wq
) {
1897 ti
->error
= "Could not allocate writeback workqueue";
1900 INIT_WORK(&wc
->writeback_work
, writecache_writeback
);
1901 INIT_WORK(&wc
->flush_work
, writecache_flush_work
);
1903 raw_spin_lock_init(&wc
->endio_list_lock
);
1904 INIT_LIST_HEAD(&wc
->endio_list
);
1905 wc
->endio_thread
= kthread_create(writecache_endio_thread
, wc
, "writecache_endio");
1906 if (IS_ERR(wc
->endio_thread
)) {
1907 r
= PTR_ERR(wc
->endio_thread
);
1908 wc
->endio_thread
= NULL
;
1909 ti
->error
= "Couldn't spawn endio thread";
1912 wake_up_process(wc
->endio_thread
);
1915 * Parse the mode (pmem or ssd)
1917 string
= dm_shift_arg(&as
);
1921 if (!strcasecmp(string
, "s")) {
1922 wc
->pmem_mode
= false;
1923 } else if (!strcasecmp(string
, "p")) {
1924 #ifdef DM_WRITECACHE_HAS_PMEM
1925 wc
->pmem_mode
= true;
1926 wc
->writeback_fua
= true;
1929 * If the architecture doesn't support persistent memory or
1930 * the kernel doesn't support any DAX drivers, this driver can
1931 * only be used in SSD-only mode.
1934 ti
->error
= "Persistent memory or DAX not supported on this system";
1941 if (WC_MODE_PMEM(wc
)) {
1942 r
= bioset_init(&wc
->bio_set
, BIO_POOL_SIZE
,
1943 offsetof(struct writeback_struct
, bio
),
1946 ti
->error
= "Could not allocate bio set";
1950 r
= mempool_init_kmalloc_pool(&wc
->copy_pool
, 1, sizeof(struct copy_struct
));
1952 ti
->error
= "Could not allocate mempool";
1958 * Parse the origin data device
1960 string
= dm_shift_arg(&as
);
1963 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->dev
);
1965 ti
->error
= "Origin data device lookup failed";
1970 * Parse cache data device (be it pmem or ssd)
1972 string
= dm_shift_arg(&as
);
1976 r
= dm_get_device(ti
, string
, dm_table_get_mode(ti
->table
), &wc
->ssd_dev
);
1978 ti
->error
= "Cache data device lookup failed";
1981 wc
->memory_map_size
= i_size_read(wc
->ssd_dev
->bdev
->bd_inode
);
1984 * Parse the cache block size
1986 string
= dm_shift_arg(&as
);
1989 if (sscanf(string
, "%u%c", &wc
->block_size
, &dummy
) != 1 ||
1990 wc
->block_size
< 512 || wc
->block_size
> PAGE_SIZE
||
1991 (wc
->block_size
& (wc
->block_size
- 1))) {
1993 ti
->error
= "Invalid block size";
1996 wc
->block_size_bits
= __ffs(wc
->block_size
);
1998 wc
->max_writeback_jobs
= MAX_WRITEBACK_JOBS
;
1999 wc
->autocommit_blocks
= !WC_MODE_PMEM(wc
) ? AUTOCOMMIT_BLOCKS_SSD
: AUTOCOMMIT_BLOCKS_PMEM
;
2000 wc
->autocommit_jiffies
= msecs_to_jiffies(AUTOCOMMIT_MSEC
);
2003 * Parse optional arguments
2005 r
= dm_read_arg_group(_args
, &as
, &opt_params
, &ti
->error
);
2009 while (opt_params
) {
2010 string
= dm_shift_arg(&as
), opt_params
--;
2011 if (!strcasecmp(string
, "start_sector") && opt_params
>= 1) {
2012 unsigned long long start_sector
;
2013 string
= dm_shift_arg(&as
), opt_params
--;
2014 if (sscanf(string
, "%llu%c", &start_sector
, &dummy
) != 1)
2015 goto invalid_optional
;
2016 wc
->start_sector
= start_sector
;
2017 if (wc
->start_sector
!= start_sector
||
2018 wc
->start_sector
>= wc
->memory_map_size
>> SECTOR_SHIFT
)
2019 goto invalid_optional
;
2020 } else if (!strcasecmp(string
, "high_watermark") && opt_params
>= 1) {
2021 string
= dm_shift_arg(&as
), opt_params
--;
2022 if (sscanf(string
, "%d%c", &high_wm_percent
, &dummy
) != 1)
2023 goto invalid_optional
;
2024 if (high_wm_percent
< 0 || high_wm_percent
> 100)
2025 goto invalid_optional
;
2026 wc
->high_wm_percent_set
= true;
2027 } else if (!strcasecmp(string
, "low_watermark") && opt_params
>= 1) {
2028 string
= dm_shift_arg(&as
), opt_params
--;
2029 if (sscanf(string
, "%d%c", &low_wm_percent
, &dummy
) != 1)
2030 goto invalid_optional
;
2031 if (low_wm_percent
< 0 || low_wm_percent
> 100)
2032 goto invalid_optional
;
2033 wc
->low_wm_percent_set
= true;
2034 } else if (!strcasecmp(string
, "writeback_jobs") && opt_params
>= 1) {
2035 string
= dm_shift_arg(&as
), opt_params
--;
2036 if (sscanf(string
, "%u%c", &wc
->max_writeback_jobs
, &dummy
) != 1)
2037 goto invalid_optional
;
2038 wc
->max_writeback_jobs_set
= true;
2039 } else if (!strcasecmp(string
, "autocommit_blocks") && opt_params
>= 1) {
2040 string
= dm_shift_arg(&as
), opt_params
--;
2041 if (sscanf(string
, "%u%c", &wc
->autocommit_blocks
, &dummy
) != 1)
2042 goto invalid_optional
;
2043 wc
->autocommit_blocks_set
= true;
2044 } else if (!strcasecmp(string
, "autocommit_time") && opt_params
>= 1) {
2045 unsigned autocommit_msecs
;
2046 string
= dm_shift_arg(&as
), opt_params
--;
2047 if (sscanf(string
, "%u%c", &autocommit_msecs
, &dummy
) != 1)
2048 goto invalid_optional
;
2049 if (autocommit_msecs
> 3600000)
2050 goto invalid_optional
;
2051 wc
->autocommit_jiffies
= msecs_to_jiffies(autocommit_msecs
);
2052 wc
->autocommit_time_set
= true;
2053 } else if (!strcasecmp(string
, "fua")) {
2054 if (WC_MODE_PMEM(wc
)) {
2055 wc
->writeback_fua
= true;
2056 wc
->writeback_fua_set
= true;
2057 } else goto invalid_optional
;
2058 } else if (!strcasecmp(string
, "nofua")) {
2059 if (WC_MODE_PMEM(wc
)) {
2060 wc
->writeback_fua
= false;
2061 wc
->writeback_fua_set
= true;
2062 } else goto invalid_optional
;
2066 ti
->error
= "Invalid optional argument";
2071 if (high_wm_percent
< low_wm_percent
) {
2073 ti
->error
= "High watermark must be greater than or equal to low watermark";
2077 if (WC_MODE_PMEM(wc
)) {
2078 r
= persistent_memory_claim(wc
);
2080 ti
->error
= "Unable to map persistent memory for cache";
2084 struct dm_io_region region
;
2085 struct dm_io_request req
;
2086 size_t n_blocks
, n_metadata_blocks
;
2087 uint64_t n_bitmap_bits
;
2089 wc
->memory_map_size
-= (uint64_t)wc
->start_sector
<< SECTOR_SHIFT
;
2091 bio_list_init(&wc
->flush_list
);
2092 wc
->flush_thread
= kthread_create(writecache_flush_thread
, wc
, "dm_writecache_flush");
2093 if (IS_ERR(wc
->flush_thread
)) {
2094 r
= PTR_ERR(wc
->flush_thread
);
2095 wc
->flush_thread
= NULL
;
2096 ti
->error
= "Couldn't spawn flush thread";
2099 wake_up_process(wc
->flush_thread
);
2101 r
= calculate_memory_size(wc
->memory_map_size
, wc
->block_size
,
2102 &n_blocks
, &n_metadata_blocks
);
2104 ti
->error
= "Invalid device size";
2108 n_bitmap_bits
= (((uint64_t)n_metadata_blocks
<< wc
->block_size_bits
) +
2109 BITMAP_GRANULARITY
- 1) / BITMAP_GRANULARITY
;
2110 /* this is limitation of test_bit functions */
2111 if (n_bitmap_bits
> 1U << 31) {
2113 ti
->error
= "Invalid device size";
2117 wc
->memory_map
= vmalloc(n_metadata_blocks
<< wc
->block_size_bits
);
2118 if (!wc
->memory_map
) {
2120 ti
->error
= "Unable to allocate memory for metadata";
2124 wc
->dm_kcopyd
= dm_kcopyd_client_create(&dm_kcopyd_throttle
);
2125 if (IS_ERR(wc
->dm_kcopyd
)) {
2126 r
= PTR_ERR(wc
->dm_kcopyd
);
2127 ti
->error
= "Unable to allocate dm-kcopyd client";
2128 wc
->dm_kcopyd
= NULL
;
2132 wc
->metadata_sectors
= n_metadata_blocks
<< (wc
->block_size_bits
- SECTOR_SHIFT
);
2133 wc
->dirty_bitmap_size
= (n_bitmap_bits
+ BITS_PER_LONG
- 1) /
2134 BITS_PER_LONG
* sizeof(unsigned long);
2135 wc
->dirty_bitmap
= vzalloc(wc
->dirty_bitmap_size
);
2136 if (!wc
->dirty_bitmap
) {
2138 ti
->error
= "Unable to allocate dirty bitmap";
2142 region
.bdev
= wc
->ssd_dev
->bdev
;
2143 region
.sector
= wc
->start_sector
;
2144 region
.count
= wc
->metadata_sectors
;
2145 req
.bi_op
= REQ_OP_READ
;
2146 req
.bi_op_flags
= REQ_SYNC
;
2147 req
.mem
.type
= DM_IO_VMA
;
2148 req
.mem
.ptr
.vma
= (char *)wc
->memory_map
;
2149 req
.client
= wc
->dm_io
;
2150 req
.notify
.fn
= NULL
;
2152 r
= dm_io(&req
, 1, ®ion
, NULL
);
2154 ti
->error
= "Unable to read metadata";
2159 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2161 ti
->error
= "Hardware memory error when reading superblock";
2164 if (!le32_to_cpu(s
.magic
) && !le32_to_cpu(s
.version
)) {
2165 r
= init_memory(wc
);
2167 ti
->error
= "Unable to initialize device";
2170 r
= memcpy_mcsafe(&s
, sb(wc
), sizeof(struct wc_memory_superblock
));
2172 ti
->error
= "Hardware memory error when reading superblock";
2177 if (le32_to_cpu(s
.magic
) != MEMORY_SUPERBLOCK_MAGIC
) {
2178 ti
->error
= "Invalid magic in the superblock";
2183 if (le32_to_cpu(s
.version
) != MEMORY_SUPERBLOCK_VERSION
) {
2184 ti
->error
= "Invalid version in the superblock";
2189 if (le32_to_cpu(s
.block_size
) != wc
->block_size
) {
2190 ti
->error
= "Block size does not match superblock";
2195 wc
->n_blocks
= le64_to_cpu(s
.n_blocks
);
2197 offset
= wc
->n_blocks
* sizeof(struct wc_memory_entry
);
2198 if (offset
/ sizeof(struct wc_memory_entry
) != le64_to_cpu(sb(wc
)->n_blocks
)) {
2200 ti
->error
= "Overflow in size calculation";
2204 offset
+= sizeof(struct wc_memory_superblock
);
2205 if (offset
< sizeof(struct wc_memory_superblock
))
2207 offset
= (offset
+ wc
->block_size
- 1) & ~(size_t)(wc
->block_size
- 1);
2208 data_size
= wc
->n_blocks
* (size_t)wc
->block_size
;
2209 if (!offset
|| (data_size
/ wc
->block_size
!= wc
->n_blocks
) ||
2210 (offset
+ data_size
< offset
))
2212 if (offset
+ data_size
> wc
->memory_map_size
) {
2213 ti
->error
= "Memory area is too small";
2218 wc
->metadata_sectors
= offset
>> SECTOR_SHIFT
;
2219 wc
->block_start
= (char *)sb(wc
) + offset
;
2221 x
= (uint64_t)wc
->n_blocks
* (100 - high_wm_percent
);
2224 wc
->freelist_high_watermark
= x
;
2225 x
= (uint64_t)wc
->n_blocks
* (100 - low_wm_percent
);
2228 wc
->freelist_low_watermark
= x
;
2230 r
= writecache_alloc_entries(wc
);
2232 ti
->error
= "Cannot allocate memory";
2236 ti
->num_flush_bios
= 1;
2237 ti
->flush_supported
= true;
2238 ti
->num_discard_bios
= 1;
2240 if (WC_MODE_PMEM(wc
))
2241 persistent_memory_flush_cache(wc
->memory_map
, wc
->memory_map_size
);
2247 ti
->error
= "Bad arguments";
2253 static void writecache_status(struct dm_target
*ti
, status_type_t type
,
2254 unsigned status_flags
, char *result
, unsigned maxlen
)
2256 struct dm_writecache
*wc
= ti
->private;
2257 unsigned extra_args
;
2262 case STATUSTYPE_INFO
:
2263 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc
),
2264 (unsigned long long)wc
->n_blocks
, (unsigned long long)wc
->freelist_size
,
2265 (unsigned long long)wc
->writeback_size
);
2267 case STATUSTYPE_TABLE
:
2268 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc
) ? 'p' : 's',
2269 wc
->dev
->name
, wc
->ssd_dev
->name
, wc
->block_size
);
2271 if (wc
->start_sector
)
2273 if (wc
->high_wm_percent_set
)
2275 if (wc
->low_wm_percent_set
)
2277 if (wc
->max_writeback_jobs_set
)
2279 if (wc
->autocommit_blocks_set
)
2281 if (wc
->autocommit_time_set
)
2283 if (wc
->writeback_fua_set
)
2286 DMEMIT("%u", extra_args
);
2287 if (wc
->start_sector
)
2288 DMEMIT(" start_sector %llu", (unsigned long long)wc
->start_sector
);
2289 if (wc
->high_wm_percent_set
) {
2290 x
= (uint64_t)wc
->freelist_high_watermark
* 100;
2291 x
+= wc
->n_blocks
/ 2;
2292 do_div(x
, (size_t)wc
->n_blocks
);
2293 DMEMIT(" high_watermark %u", 100 - (unsigned)x
);
2295 if (wc
->low_wm_percent_set
) {
2296 x
= (uint64_t)wc
->freelist_low_watermark
* 100;
2297 x
+= wc
->n_blocks
/ 2;
2298 do_div(x
, (size_t)wc
->n_blocks
);
2299 DMEMIT(" low_watermark %u", 100 - (unsigned)x
);
2301 if (wc
->max_writeback_jobs_set
)
2302 DMEMIT(" writeback_jobs %u", wc
->max_writeback_jobs
);
2303 if (wc
->autocommit_blocks_set
)
2304 DMEMIT(" autocommit_blocks %u", wc
->autocommit_blocks
);
2305 if (wc
->autocommit_time_set
)
2306 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc
->autocommit_jiffies
));
2307 if (wc
->writeback_fua_set
)
2308 DMEMIT(" %sfua", wc
->writeback_fua
? "" : "no");
2313 static struct target_type writecache_target
= {
2314 .name
= "writecache",
2315 .version
= {1, 1, 1},
2316 .module
= THIS_MODULE
,
2317 .ctr
= writecache_ctr
,
2318 .dtr
= writecache_dtr
,
2319 .status
= writecache_status
,
2320 .postsuspend
= writecache_suspend
,
2321 .resume
= writecache_resume
,
2322 .message
= writecache_message
,
2323 .map
= writecache_map
,
2324 .end_io
= writecache_end_io
,
2325 .iterate_devices
= writecache_iterate_devices
,
2326 .io_hints
= writecache_io_hints
,
2329 static int __init
dm_writecache_init(void)
2333 r
= dm_register_target(&writecache_target
);
2335 DMERR("register failed %d", r
);
2342 static void __exit
dm_writecache_exit(void)
2344 dm_unregister_target(&writecache_target
);
2347 module_init(dm_writecache_init
);
2348 module_exit(dm_writecache_exit
);
2350 MODULE_DESCRIPTION(DM_NAME
" writecache target");
2351 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2352 MODULE_LICENSE("GPL");