// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2009-2011 Red Hat, Inc. * * Author: Mikulas Patocka <mpatocka@redhat.com> * * This file is released under the GPL.
*/
/* * Memory management policy: * Limit the number of buffers to DM_BUFIO_MEMORY_PERCENT of main memory * or DM_BUFIO_VMALLOC_PERCENT of vmalloc memory (whichever is lower). * Always allocate at least DM_BUFIO_MIN_BUFFERS buffers. * Start background writeback when there are DM_BUFIO_WRITEBACK_PERCENT * dirty buffers.
*/ #define DM_BUFIO_MIN_BUFFERS 8
/* * Rather than use an LRU list, we use a clock algorithm where entries * are held in a circular list. When an entry is 'hit' a reference bit * is set. The least recently used entry is approximated by running a * cursor around the list selecting unreferenced entries. Referenced * entries have their reference bit cleared as the cursor passes them.
*/ struct lru_entry { struct list_head list;
atomic_t referenced;
};
/* * Insert a new entry into the lru.
*/ staticvoid lru_insert(struct lru *lru, struct lru_entry *le)
{ /* * Don't be tempted to set to 1, makes the lru aspect * perform poorly.
*/
atomic_set(&le->referenced, 0);
/* * Convert a list_head pointer to an lru_entry pointer.
*/ staticinlinestruct lru_entry *to_le(struct list_head *l)
{ return container_of(l, struct lru_entry, list);
}
/* * Initialize an lru_iter and add it to the list of cursors in the lru.
*/ staticvoid lru_iter_begin(struct lru *lru, struct lru_iter *it)
{
it->lru = lru;
it->stop = lru->cursor ? to_le(lru->cursor->prev) : NULL;
it->e = lru->cursor ? to_le(lru->cursor) : NULL;
list_add(&it->list, &lru->iterators);
}
/* * Remove an lru_iter from the list of cursors in the lru.
*/ staticinlinevoid lru_iter_end(struct lru_iter *it)
{
list_del(&it->list);
}
/* Predicate function type to be used with lru_iter_next */ typedefbool (*iter_predicate)(struct lru_entry *le, void *context);
/* * Advance the cursor to the next entry that passes the * predicate, and return that entry. Returns NULL if the * iteration is complete.
*/ staticstruct lru_entry *lru_iter_next(struct lru_iter *it,
iter_predicate pred, void *context)
{ struct lru_entry *e;
while (it->e) {
e = it->e;
/* advance the cursor */ if (it->e == it->stop)
it->e = NULL; else
it->e = to_le(it->e->list.next);
if (pred(e, context)) return e;
}
return NULL;
}
/* * Invalidate a specific lru_entry and update all cursors in * the lru accordingly.
*/ staticvoid lru_iter_invalidate(struct lru *lru, struct lru_entry *e)
{ struct lru_iter *it;
list_for_each_entry(it, &lru->iterators, list) { /* Move c->e forwards if necc. */ if (it->e == e) {
it->e = to_le(it->e->list.next); if (it->e == e)
it->e = NULL;
}
/* Move it->stop backwards if necc. */ if (it->stop == e) {
it->stop = to_le(it->stop->list.prev); if (it->stop == e)
it->stop = NULL;
}
}
}
/*--------------*/
/* * Remove a specific entry from the lru.
*/ staticvoid lru_remove(struct lru *lru, struct lru_entry *le)
{
lru_iter_invalidate(lru, le); if (lru->count == 1) {
lru->cursor = NULL;
} else { if (lru->cursor == &le->list)
lru->cursor = lru->cursor->next;
list_del(&le->list);
}
lru->count--;
}
/* * Mark as referenced.
*/ staticinlinevoid lru_reference(struct lru_entry *le)
{
atomic_set(&le->referenced, 1);
}
/*--------------*/
/* * Remove the least recently used entry (approx), that passes the predicate. * Returns NULL on failure.
*/ enum evict_result {
ER_EVICT,
ER_DONT_EVICT,
ER_STOP, /* stop looking for something to evict */
};
if (!h) return NULL; /* * In the worst case we have to loop around twice. Once to clear * the reference flags, and then again to discover the predicate * fails for all entries.
*/ while (tested < lru->count) {
le = container_of(h, struct lru_entry, list);
if (atomic_read(&le->referenced)) {
atomic_set(&le->referenced, 0);
} else {
tested++; switch (pred(le, context)) { case ER_EVICT: /* * Adjust the cursor, so we start the next * search from here.
*/
lru->cursor = le->list.next;
lru_remove(lru, le); return le;
case ER_DONT_EVICT: break;
case ER_STOP:
lru->cursor = le->list.next; return NULL;
}
}
/* * The buffer cache manages buffers, particularly: * - inc/dec of holder count * - setting the last_accessed field * - maintains clean/dirty state along with lru * - selecting buffers that match predicates * * It does *not* handle: * - allocation/freeing of buffers. * - IO * - Eviction or cache sizing. * * cache_get() and cache_put() are threadsafe, you do not need to * protect these calls with a surrounding mutex. All the other * methods are not threadsafe; they do use locking primitives, but * only enough to ensure get/put are threadsafe.
*/
struct dm_buffer_cache { struct lru lru[LIST_SIZE]; /* * We spread entries across multiple trees to reduce contention * on the locks.
*/ unsignedint num_locks; bool no_sleep; struct buffer_tree trees[];
};
/* * Sometimes we want to repeatedly get and drop locks as part of an iteration. * This struct helps avoid redundant drop and gets of the same lock.
*/ struct lock_history { struct dm_buffer_cache *cache; bool write; unsignedint previous; unsignedint no_previous;
};
staticvoid __lh_lock(struct lock_history *lh, unsignedint index)
{ if (lh->write) { if (static_branch_unlikely(&no_sleep_enabled) && lh->cache->no_sleep)
write_lock_bh(&lh->cache->trees[index].u.spinlock); else
down_write(&lh->cache->trees[index].u.lock);
} else { if (static_branch_unlikely(&no_sleep_enabled) && lh->cache->no_sleep)
read_lock_bh(&lh->cache->trees[index].u.spinlock); else
down_read(&lh->cache->trees[index].u.lock);
}
}
staticvoid __lh_unlock(struct lock_history *lh, unsignedint index)
{ if (lh->write) { if (static_branch_unlikely(&no_sleep_enabled) && lh->cache->no_sleep)
write_unlock_bh(&lh->cache->trees[index].u.spinlock); else
up_write(&lh->cache->trees[index].u.lock);
} else { if (static_branch_unlikely(&no_sleep_enabled) && lh->cache->no_sleep)
read_unlock_bh(&lh->cache->trees[index].u.spinlock); else
up_read(&lh->cache->trees[index].u.lock);
}
}
/* * Make sure you call this since it will unlock the final lock.
*/ staticvoid lh_exit(struct lock_history *lh)
{ if (lh->previous != lh->no_previous) {
__lh_unlock(lh, lh->previous);
lh->previous = lh->no_previous;
}
}
/* * Named 'next' because there is no corresponding * 'up/unlock' call since it's done automatically.
*/ staticvoid lh_next(struct lock_history *lh, sector_t b)
{ unsignedint index = cache_index(b, lh->no_previous); /* no_previous is num_locks */
for (i = 0; i < bc->num_locks; i++) { if (no_sleep)
rwlock_init(&bc->trees[i].u.spinlock); else
init_rwsem(&bc->trees[i].u.lock);
bc->trees[i].root = RB_ROOT;
}
/* * not threadsafe, or racey depending how you look at it
*/ staticinlineunsignedlong cache_count(struct dm_buffer_cache *bc, int list_mode)
{ return bc->lru[list_mode].count;
}
/* * Gets a specific buffer, indexed by block. * If the buffer is found then its holder count will be incremented and * lru_reference will be called. * * threadsafe
*/ staticstruct dm_buffer *__cache_get(conststruct rb_root *root, sector_t block)
{ struct rb_node *n = root->rb_node; struct dm_buffer *b;
while (n) {
b = container_of(n, struct dm_buffer, node);
if (b->block == block) return b;
n = block < b->block ? n->rb_left : n->rb_right;
}
/* * Evicts a buffer based on a predicate. The oldest buffer that * matches the predicate will be selected. In addition to the * predicate the hold_count of the selected buffer will be zero.
*/ struct evict_wrapper { struct lock_history *lh;
b_predicate pred; void *context;
};
/* * Wraps the buffer predicate turning it into an lru predicate. Adds * extra test for hold_count.
*/ staticenum evict_result __evict_pred(struct lru_entry *le, void *context)
{ struct evict_wrapper *w = context; struct dm_buffer *b = le_to_buffer(le);
lh_next(w->lh, b->block);
if (atomic_read(&b->hold_count)) return ER_DONT_EVICT;
le = lru_evict(&bc->lru[list_mode], __evict_pred, &w, bc->no_sleep); if (!le) return NULL;
b = le_to_buffer(le); /* __evict_pred will have locked the appropriate tree. */
rb_erase(&b->node, &bc->trees[cache_index(b->block, bc->num_locks)].root);
/* * Mark a buffer as clean or dirty. Not threadsafe.
*/ staticvoid cache_mark(struct dm_buffer_cache *bc, struct dm_buffer *b, int list_mode)
{
cache_write_lock(bc, b->block); if (list_mode != b->list_mode) {
lru_remove(&bc->lru[b->list_mode], &b->lru);
b->list_mode = list_mode;
lru_insert(&bc->lru[b->list_mode], &b->lru);
}
cache_write_unlock(bc, b->block);
}
/*--------------*/
/* * Runs through the lru associated with 'old_mode', if the predicate matches then * it moves them to 'new_mode'. Not threadsafe.
*/ staticvoid __cache_mark_many(struct dm_buffer_cache *bc, int old_mode, int new_mode,
b_predicate pred, void *context, struct lock_history *lh)
{ struct lru_entry *le; struct dm_buffer *b; struct evict_wrapper w = {.lh = lh, .pred = pred, .context = context};
while (true) {
le = lru_evict(&bc->lru[old_mode], __evict_pred, &w, bc->no_sleep); if (!le) break;
b = le_to_buffer(le);
b->list_mode = new_mode;
lru_insert(&bc->lru[b->list_mode], &b->lru);
}
}
staticvoid cache_mark_many(struct dm_buffer_cache *bc, int old_mode, int new_mode,
b_predicate pred, void *context)
{ struct lock_history lh;
/* * Iterates through all clean or dirty entries calling a function for each * entry. The callback may terminate the iteration early. Not threadsafe.
*/
/* * Iterator functions should return one of these actions to indicate * how the iteration should proceed.
*/ enum it_action {
IT_NEXT,
IT_COMPLETE,
};
/* * Passes ownership of the buffer to the cache. Returns false if the * buffer was already present (in which case ownership does not pass). * eg, a race with another thread. * * Holder count should be 1 on insertion. * * Not threadsafe.
*/ staticbool __cache_insert(struct rb_root *root, struct dm_buffer *b)
{ struct rb_node **new = &root->rb_node, *parent = NULL; struct dm_buffer *found;
while (*new) {
found = container_of(*new, struct dm_buffer, node);
if (WARN_ON_ONCE(b->list_mode >= LIST_SIZE)) returnfalse;
cache_write_lock(bc, b->block);
BUG_ON(atomic_read(&b->hold_count) != 1);
r = __cache_insert(&bc->trees[cache_index(b->block, bc->num_locks)].root, b); if (r)
lru_insert(&bc->lru[b->list_mode], &b->lru);
cache_write_unlock(bc, b->block);
return r;
}
/*--------------*/
/* * Removes buffer from cache, ownership of the buffer passes back to the caller. * Fails if the hold_count is not one (ie. the caller holds the only reference). * * Not threadsafe.
*/ staticbool cache_remove(struct dm_buffer_cache *bc, struct dm_buffer *b)
{ bool r;
cache_write_lock(bc, b->block);
if (atomic_read(&b->hold_count) != 1) {
r = false;
} else {
r = true;
rb_erase(&b->node, &bc->trees[cache_index(b->block, bc->num_locks)].root);
lru_remove(&bc->lru[b->list_mode], &b->lru);
}
/* * Linking of buffers: * All buffers are linked to buffer_cache with their node field. * * Clean buffers that are not being written (B_WRITING not set) * are linked to lru[LIST_CLEAN] with their lru_list field. * * Dirty and clean buffers that are being written are linked to * lru[LIST_DIRTY] with their lru_list field. When the write * finishes, the buffer cannot be relinked immediately (because we * are in an interrupt context and relinking requires process * context), so some clean-not-writing buffers can be held on * dirty_lru too. They are later added to lru in the process * context.
*/ struct dm_bufio_client { struct block_device *bdev; unsignedint block_size;
s8 sectors_per_block_bits;
/* * Default cache size: available memory divided by the ratio.
*/ staticunsignedlong dm_bufio_default_cache_size;
/* * Total cache size set by the user.
*/ staticunsignedlong dm_bufio_cache_size;
/* * A copy of dm_bufio_cache_size because dm_bufio_cache_size can change * at any time. If it disagrees, the user has changed cache size.
*/ staticunsignedlong dm_bufio_cache_size_latch;
static DEFINE_SPINLOCK(global_spinlock);
staticunsignedint dm_bufio_max_age; /* No longer does anything */
if (dm_bufio_current_allocated > dm_bufio_peak_allocated)
dm_bufio_peak_allocated = dm_bufio_current_allocated;
if (!unlink) { if (dm_bufio_current_allocated > dm_bufio_cache_size)
queue_work(dm_bufio_wq, &dm_bufio_replacement_work);
}
spin_unlock(&global_spinlock);
}
/* * Change the number of clients and recalculate per-client limit.
*/ staticvoid __cache_size_refresh(void)
{ if (WARN_ON(!mutex_is_locked(&dm_bufio_clients_lock))) return; if (WARN_ON(dm_bufio_client_count < 0)) return;
/* * Use default if set to 0 and report the actual cache size used.
*/ if (!dm_bufio_cache_size_latch) {
(void)cmpxchg(&dm_bufio_cache_size, 0,
dm_bufio_default_cache_size);
dm_bufio_cache_size_latch = dm_bufio_default_cache_size;
}
}
/* * Allocating buffer data. * * Small buffers are allocated with kmem_cache, to use space optimally. * * For large buffers, we choose between get_free_pages and vmalloc. * Each has advantages and disadvantages. * * __get_free_pages can randomly fail if the memory is fragmented. * __vmalloc won't randomly fail, but vmalloc space is limited (it may be * as low as 128M) so using it for caching is not appropriate. * * If the allocation may fail we use __get_free_pages. Memory fragmentation * won't have a fatal effect here, but it just causes flushes of some other * buffers and more I/O will be performed. Don't use __get_free_pages if it * always fails (i.e. order > MAX_PAGE_ORDER). * * If the allocation shouldn't fail we use __vmalloc. This is only for the * initial reserve allocation, so there's no risk of wasting all vmalloc * space.
*/ staticvoid *alloc_buffer_data(struct dm_bufio_client *c, gfp_t gfp_mask, unsignedchar *data_mode)
{ if (unlikely(c->slab_cache != NULL)) {
*data_mode = DATA_MODE_SLAB; return kmem_cache_alloc(c->slab_cache, gfp_mask);
}
/* *-------------------------------------------------------------------------- * Submit I/O on the buffer. * * Bio interface is faster but it has some problems: * the vector list is limited (increasing this limit increases * memory-consumption per buffer, so it is not viable); * * the memory must be direct-mapped, not vmalloced; * * If the buffer is small enough (up to DM_BUFIO_INLINE_VECS pages) and * it is not vmalloced, try using the bio interface. * * If the buffer is big, if it is vmalloced or if the underlying device * rejects the bio because it is too large, use dm-io layer to do the I/O. * The dm-io layer splits the I/O into multiple requests, avoiding the above * shortcomings. *--------------------------------------------------------------------------
*/
/* * dm-io completion routine. It just calls b->bio.bi_end_io, pretending * that the request was handled directly with bio interface.
*/ staticvoid dmio_complete(unsignedlong error, void *context)
{ struct dm_buffer *b = context;
/* * The endio routine for write. * * Set the error, clear B_WRITING bit and wake anyone who was waiting on * it.
*/ staticvoid write_endio(struct dm_buffer *b, blk_status_t status)
{
b->write_error = status; if (unlikely(status)) { struct dm_bufio_client *c = b->c;
/* * Initiate a write on a dirty buffer, but don't wait for it. * * - If the buffer is not dirty, exit. * - If there some previous write going on, wait for it to finish (we can't * have two writes on the same buffer simultaneously). * - Submit our write and don't wait on it. We set B_WRITING indicating * that there is a write in progress.
*/ staticvoid __write_dirty_buffer(struct dm_buffer *b, struct list_head *write_list)
{ if (!test_bit(B_DIRTY, &b->state)) return;
/* * Wait until any activity on the buffer finishes. Possibly write the * buffer if it is dirty. When this function finishes, there is no I/O * running on the buffer and the buffer is not dirty.
*/ staticvoid __make_buffer_clean(struct dm_buffer *b)
{
BUG_ON(atomic_read(&b->hold_count));
/* smp_load_acquire() pairs with read_endio()'s smp_mb__before_atomic() */ if (!smp_load_acquire(&b->state)) /* fast case */ return;
/* These should never happen */ if (WARN_ON_ONCE(test_bit(B_WRITING, &b->state))) return ER_DONT_EVICT; if (WARN_ON_ONCE(test_bit(B_DIRTY, &b->state))) return ER_DONT_EVICT; if (WARN_ON_ONCE(b->list_mode != LIST_CLEAN)) return ER_DONT_EVICT;
if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep &&
unlikely(test_bit(B_READING, &b->state))) return ER_DONT_EVICT;
return ER_EVICT;
}
staticenum evict_result is_dirty(struct dm_buffer *b, void *context)
{ /* These should never happen */ if (WARN_ON_ONCE(test_bit(B_READING, &b->state))) return ER_DONT_EVICT; if (WARN_ON_ONCE(b->list_mode != LIST_DIRTY)) return ER_DONT_EVICT;
return ER_EVICT;
}
/* * Find some buffer that is not held by anybody, clean it, unlink it and * return it.
*/ staticstruct dm_buffer *__get_unclaimed_buffer(struct dm_bufio_client *c)
{ struct dm_buffer *b;
b = cache_evict(&c->cache, LIST_CLEAN, is_clean, c); if (b) { /* this also waits for pending reads */
__make_buffer_clean(b); return b;
}
if (static_branch_unlikely(&no_sleep_enabled) && c->no_sleep) return NULL;
b = cache_evict(&c->cache, LIST_DIRTY, is_dirty, NULL); if (b) {
__make_buffer_clean(b); return b;
}
return NULL;
}
/* * Wait until some other threads free some buffer or release hold count on * some buffer. * * This function is entered with c->lock held, drops it and regains it * before exiting.
*/ staticvoid __wait_for_free_buffer(struct dm_bufio_client *c)
{
DECLARE_WAITQUEUE(wait, current);
/* * It's possible to miss a wake up event since we don't always * hold c->lock when wake_up is called. So we have a timeout here, * just in case.
*/
io_schedule_timeout(5 * HZ);
/* * Allocate a new buffer. If the allocation is not possible, wait until * some other thread frees a buffer. * * May drop the lock and regain it.
*/ staticstruct dm_buffer *__alloc_buffer_wait_no_callback(struct dm_bufio_client *c, enum new_flag nf)
{ struct dm_buffer *b; bool tried_noio_alloc = false;
/* * dm-bufio is resistant to allocation failures (it just keeps * one buffer reserved in cases all the allocations fail). * So set flags to not try too hard: * GFP_NOWAIT: don't wait; if we need to sleep we'll release our * mutex and wait ourselves. * __GFP_NORETRY: don't retry and rather return failure * __GFP_NOMEMALLOC: don't use emergency reserves * __GFP_NOWARN: don't print a warning in case of failure * * For debugging, if we set the cache size to 1, no new buffers will * be allocated.
*/ while (1) { if (dm_bufio_cache_size_latch != 1) {
b = alloc_buffer(c, GFP_NOWAIT | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN); if (b) return b;
}
if (nf == NF_PREFETCH) return NULL;
if (dm_bufio_cache_size_latch != 1 && !tried_noio_alloc) {
dm_bufio_unlock(c);
b = alloc_buffer(c, GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
dm_bufio_lock(c); if (b) return b;
tried_noio_alloc = true;
}
if (!list_empty(&c->reserved_buffers)) {
b = list_to_buffer(c->reserved_buffers.next);
list_del(&b->lru.list);
c->need_reserved_buffers++;
/* * Free a buffer and wake other threads waiting for free buffers.
*/ staticvoid __free_buffer_wake(struct dm_buffer *b)
{ struct dm_bufio_client *c = b->c;
/* * We hold the bufio lock here, so no one can add entries to the * wait queue anyway.
*/ if (unlikely(waitqueue_active(&c->free_buffer_wait)))
wake_up(&c->free_buffer_wait);
}
staticenum evict_result cleaned(struct dm_buffer *b, void *context)
{ if (WARN_ON_ONCE(test_bit(B_READING, &b->state))) return ER_DONT_EVICT; /* should never happen */
/* * Check if we're over watermark. * If we are over threshold_buffers, start freeing buffers. * If we're over "limit_buffers", block until we get under the limit.
*/ staticvoid __check_watermark(struct dm_bufio_client *c, struct list_head *write_list)
{ if (cache_count(&c->cache, LIST_DIRTY) >
cache_count(&c->cache, LIST_CLEAN) * DM_BUFIO_WRITEBACK_RATIO)
__write_dirty_buffers_async(c, 1, write_list);
}
/* *-------------------------------------------------------------- * Getting a buffer *--------------------------------------------------------------
*/
staticvoid cache_put_and_wake(struct dm_bufio_client *c, struct dm_buffer *b)
{ /* * Relying on waitqueue_active() is racey, but we sleep * with schedule_timeout anyway.
*/ if (cache_put(&c->cache, b) &&
unlikely(waitqueue_active(&c->free_buffer_wait)))
wake_up(&c->free_buffer_wait);
}
/* * This assumes you have already checked the cache to see if the buffer * is already present (it will recheck after dropping the lock for allocation).
*/ staticstruct dm_buffer *__bufio_new(struct dm_bufio_client *c, sector_t block, enum new_flag nf, int *need_submit, struct list_head *write_list)
{ struct dm_buffer *b, *new_b = NULL;
*need_submit = 0;
/* This can't be called with NF_GET */ if (WARN_ON_ONCE(nf == NF_GET)) return NULL;
new_b = __alloc_buffer_wait(c, nf); if (!new_b) return NULL;
/* * We've had a period where the mutex was unlocked, so need to * recheck the buffer tree.
*/
b = cache_get(&c->cache, block); if (b) {
__free_buffer_wake(new_b); goto found_buffer;
}
/* * We mustn't insert into the cache until the B_READING state * is set. Otherwise another thread could get it and use * it before it had been read.
*/
cache_insert(&c->cache, b);
/* * Note: it is essential that we don't wait for the buffer to be * read if dm_bufio_get function is used. Both dm_bufio_get and * dm_bufio_prefetch can be used in the driver request routine. * If the user called both dm_bufio_prefetch and dm_bufio_get on * the same buffer, it would deadlock if we waited.
*/ if (nf == NF_GET && unlikely(test_bit_acquire(B_READING, &b->state))) {
cache_put_and_wake(c, b); return NULL;
}
return b;
}
/* * The endio routine for reading: set the error, clear the bit and wake up * anyone waiting on the buffer.
*/ staticvoid read_endio(struct dm_buffer *b, blk_status_t status)
{
b->read_error = status;
/* * A common routine for dm_bufio_new and dm_bufio_read. Operation of these * functions is similar except that dm_bufio_new doesn't read the * buffer from the disk (assuming that the caller overwrites all the data * and uses dm_bufio_mark_buffer_dirty to write new data back).
*/ staticvoid *new_read(struct dm_bufio_client *c, sector_t block, enum new_flag nf, struct dm_buffer **bp, unsignedshort ioprio)
{ int need_submit = 0; struct dm_buffer *b;
LIST_HEAD(write_list);
*bp = NULL;
/* * Fast path, hopefully the block is already in the cache. No need * to get the client lock for this.
*/
b = cache_get(&c->cache, block); if (b) { if (nf == NF_PREFETCH) {
cache_put_and_wake(c, b); return NULL;
}
/* * Note: it is essential that we don't wait for the buffer to be * read if dm_bufio_get function is used. Both dm_bufio_get and * dm_bufio_prefetch can be used in the driver request routine. * If the user called both dm_bufio_prefetch and dm_bufio_get on * the same buffer, it would deadlock if we waited.
*/ if (nf == NF_GET && unlikely(test_bit_acquire(B_READING, &b->state))) {
cache_put_and_wake(c, b); return NULL;
}
}
if (!b) { if (nf == NF_GET) return NULL;
dm_bufio_lock(c);
b = __bufio_new(c, block, nf, &need_submit, &write_list);
dm_bufio_unlock(c);
}
#ifdef CONFIG_DM_DEBUG_BLOCK_STACK_TRACING if (b && (atomic_read(&b->hold_count) == 1))
buffer_record_stack(b); #endif
__flush_write_list(&write_list);
if (!b) return NULL;
if (need_submit)
submit_io(b, REQ_OP_READ, ioprio, read_endio);
if (nf != NF_GET) /* we already tested this condition above */
wait_on_bit_io(&b->state, B_READING, TASK_UNINTERRUPTIBLE);
if (b->read_error) { int error = blk_status_to_errno(b->read_error);
/* * If there were errors on the buffer, and the buffer is not * to be written, free the buffer. There is no point in caching * invalid buffer.
*/ if ((b->read_error || b->write_error) &&
!test_bit_acquire(B_READING, &b->state) &&
!test_bit(B_WRITING, &b->state) &&
!test_bit(B_DIRTY, &b->state)) {
dm_bufio_lock(c);
/* cache remove can fail if there are other holders */ if (cache_remove(&c->cache, b)) {
__free_buffer_wake(b);
dm_bufio_unlock(c); return;
}
/* * For performance, it is essential that the buffers are written asynchronously * and simultaneously (so that the block layer can merge the writes) and then * waited upon. * * Finally, we flush hardware disk cache.
*/ staticbool is_writing(struct lru_entry *e, void *context)
{ struct dm_buffer *b = le_to_buffer(e);
return test_bit(B_WRITING, &b->state);
}
int dm_bufio_write_dirty_buffers(struct dm_bufio_client *c)
{ int a, f; unsignedlong nr_buffers; struct lru_entry *e; struct lru_iter it;
b = cache_get(&c->cache, block); if (b) { if (likely(!smp_load_acquire(&b->state))) { if (cache_remove(&c->cache, b))
__free_buffer_wake(b); else
cache_put_and_wake(c, b);
} else {
cache_put_and_wake(c, b);
}
}
}
/* * Free the given buffer. * * This is just a hint, if the buffer is in use or dirty, this function * does nothing.
*/ void dm_bufio_forget(struct dm_bufio_client *c, sector_t block)
{
dm_bufio_lock(c);
forget_buffer(c, block);
dm_bufio_unlock(c);
}
EXPORT_SYMBOL_GPL(dm_bufio_forget);
sector_t dm_bufio_get_device_size(struct dm_bufio_client *c)
{
sector_t s = bdev_nr_sectors(c->bdev);
if (s >= c->start)
s -= c->start; else
s = 0; if (likely(c->sectors_per_block_bits >= 0))
s >>= c->sectors_per_block_bits; else
sector_div(s, c->block_size >> SECTOR_SHIFT); return s;
}
EXPORT_SYMBOL_GPL(dm_bufio_get_device_size);
for (l = 0; l < LIST_SIZE; l++) { while (true) { if (count - freed <= retain_target)
atomic_long_set(&c->need_shrink, 0); if (!atomic_long_read(&c->need_shrink)) break;
b = cache_evict(&c->cache, l,
l == LIST_CLEAN ? is_clean : is_dirty, c); if (!b) break;
if (!block_size || block_size & ((1 << SECTOR_SHIFT) - 1)) {
DMERR("%s: block size not specified or is not multiple of 512b", __func__);
r = -EINVAL; goto bad_client;
}
num_locks = dm_num_hash_locks();
c = kzalloc(sizeof(*c) + (num_locks * sizeof(struct buffer_tree)), GFP_KERNEL); if (!c) {
r = -ENOMEM; goto bad_client;
}
cache_init(&c->cache, num_locks, (flags & DM_BUFIO_CLIENT_NO_SLEEP) != 0);
/* * Free the buffering interface. * It is required that there are no references on any buffers.
*/ void dm_bufio_client_destroy(struct dm_bufio_client *c)
{ unsignedint i;
/* * Global cleanup tries to evict the oldest buffers from across _all_ * the clients. It does this by repeatedly evicting a few buffers from * the client that holds the oldest buffer. It's approximate, but hopefully * good enough.
*/ staticstruct dm_bufio_client *__pop_client(void)
{ struct list_head *h;
if (list_empty(&dm_bufio_all_clients)) return NULL;
h = dm_bufio_all_clients.next;
list_del(h); return container_of(h, struct dm_bufio_client, client_list);
}
/* * Inserts the client in the global client list based on its * 'oldest_buffer' field.
*/ staticvoid __insert_client(struct dm_bufio_client *new_client)
{ struct dm_bufio_client *c; struct list_head *h = dm_bufio_all_clients.next;
while (h != &dm_bufio_all_clients) {
c = container_of(h, struct dm_bufio_client, client_list); if (time_after_eq(c->oldest_buffer, new_client->oldest_buffer)) break;
h = h->next;
}
list_add_tail(&new_client->client_list, h);
}
staticenum evict_result select_for_evict(struct dm_buffer *b, void *context)
{ /* In no-sleep mode, we cannot wait on IO. */ if (static_branch_unlikely(&no_sleep_enabled) && b->c->no_sleep) { if (test_bit_acquire(B_READING, &b->state) ||
test_bit(B_WRITING, &b->state) ||
test_bit(B_DIRTY, &b->state)) return ER_DONT_EVICT;
} return ER_EVICT;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.