bool btrfs_workqueue_normal_congested(conststruct btrfs_workqueue *wq)
{ /* * We could compare wq->pending with num_online_cpus() * to support "thresh == NO_THRESHOLD" case, but it requires * moving up atomic_inc/dec in thresh_queue/exec_hook. Let's * postpone it until someone needs the support of that case.
*/ if (wq->thresh == NO_THRESHOLD) returnfalse;
/* * Hook for threshold which will be called in btrfs_queue_work. * This hook WILL be called in IRQ handler context, * so workqueue_set_max_active MUST NOT be called in this hook
*/ staticinlinevoid thresh_queue_hook(struct btrfs_workqueue *wq)
{ if (wq->thresh == NO_THRESHOLD) return;
atomic_inc(&wq->pending);
}
/* * Hook for threshold which will be called before executing the work, * This hook is called in kthread content. * So workqueue_set_max_active is called here.
*/ staticinlinevoid thresh_exec_hook(struct btrfs_workqueue *wq)
{ int new_current_active; long pending; bool need_change = false;
if (wq->thresh == NO_THRESHOLD) return;
atomic_dec(&wq->pending);
spin_lock(&wq->thres_lock); /* * Use wq->count to limit the calling frequency of * workqueue_set_max_active.
*/
wq->count++;
wq->count %= (wq->thresh / 4); if (!wq->count) goto out;
new_current_active = wq->current_active;
/* * pending may be changed later, but it's OK since we really * don't need it so accurate to calculate new_max_active.
*/
pending = atomic_read(&wq->pending); if (pending > wq->thresh)
new_current_active++; if (pending < wq->thresh / 2)
new_current_active--;
new_current_active = clamp_val(new_current_active, 1, wq->limit_active); if (new_current_active != wq->current_active) {
need_change = true;
wq->current_active = new_current_active;
}
out:
spin_unlock(&wq->thres_lock);
if (need_change)
workqueue_set_max_active(wq->normal_wq, wq->current_active);
}
while (1) {
spin_lock_irqsave(lock, flags); if (list_empty(list)) break;
work = list_first_entry(list, struct btrfs_work, ordered_list); if (!test_bit(WORK_DONE_BIT, &work->flags)) break; /* * Orders all subsequent loads after reading WORK_DONE_BIT, * paired with the smp_mb__before_atomic in btrfs_work_helper * this guarantees that the ordered function will see all * updates from ordinary work function.
*/
smp_rmb();
/* * we are going to call the ordered done function, but * we leave the work item on the list as a barrier so * that later work items that are done don't have their * functions called before this one returns
*/ if (test_and_set_bit(WORK_ORDER_DONE_BIT, &work->flags)) break;
trace_btrfs_ordered_sched(work);
spin_unlock_irqrestore(lock, flags);
work->ordered_func(work, false);
/* now take the lock again and drop our item from the list */
spin_lock_irqsave(lock, flags);
list_del(&work->ordered_list);
spin_unlock_irqrestore(lock, flags);
if (work == self) { /* * This is the work item that the worker is currently * executing. * * The kernel workqueue code guarantees non-reentrancy * of work items. I.e., if a work item with the same * address and work function is queued twice, the second * execution is blocked until the first one finishes. A * work item may be freed and recycled with the same * work function; the workqueue code assumes that the * original work item cannot depend on the recycled work * item in that case (see find_worker_executing_work()). * * Note that different types of Btrfs work can depend on * each other, and one type of work on one Btrfs * filesystem may even depend on the same type of work * on another Btrfs filesystem via, e.g., a loop device. * Therefore, we must not allow the current work item to * be recycled until we are really done, otherwise we * break the above assumption and can deadlock.
*/
free_self = true;
} else { /* * We don't want to call the ordered free functions with * the lock held.
*/
work->ordered_func(work, true); /* NB: work must not be dereferenced past this point. */
trace_btrfs_all_work_done(wq->fs_info, work);
}
}
spin_unlock_irqrestore(lock, flags);
if (free_self) {
self->ordered_func(self, true); /* NB: self must not be dereferenced past this point. */
trace_btrfs_all_work_done(wq->fs_info, self);
}
}
/* * We should not touch things inside work in the following cases: * 1) after work->func() if it has no ordered_func(..., true) to free * Since the struct is freed in work->func(). * 2) after setting WORK_DONE_BIT * The work may be freed in other threads almost instantly. * So we save the needed things here.
*/ if (work->ordered_func)
need_order = true;
trace_btrfs_work_sched(work);
thresh_exec_hook(wq);
work->func(work); if (need_order) { /* * Ensures all memory accesses done in the work function are * ordered before setting the WORK_DONE_BIT. Ensuring the thread * which is going to executed the ordered work sees them. * Pairs with the smp_rmb in run_ordered_work.
*/
smp_mb__before_atomic();
set_bit(WORK_DONE_BIT, &work->flags);
run_ordered_work(wq, work);
} else { /* NB: work must not be dereferenced past this point. */
trace_btrfs_all_work_done(wq->fs_info, work);
}
}
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.