1167 lines · 26.6 KB · C
RawDownload
/* *  Block device elevator/IO-scheduler. * *  Copyright (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE * * 30042000 Jens Axboe <axboe@kernel.dk> : * * Split the elevator a bit so that it is possible to choose a different * one or even write a new "plug in". There are three pieces: * - elevator_fn, inserts a new request in the queue list * - elevator_merge_fn, decides whether a new buffer can be merged with *   an existing request * - elevator_dequeue_fn, called when a request is taken off the active list * * 20082000 Dave Jones <davej@suse.de> : * Removed tests for max-bomb-segments, which was breaking elvtune *  when run without -bN * * Jens: * - Rework again to work with bio instead of buffer_heads * - loose bi_dev comparisons, partition handling is right now * - completely modularize elevator setup and teardown * */#include <linux/kernel.h>#include <linux/fs.h>#include <linux/blkdev.h>#include <linux/elevator.h>#include <linux/bio.h>#include <linux/module.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/compiler.h>#include <linux/blktrace_api.h>#include <linux/hash.h>#include <linux/uaccess.h>#include <linux/pm_runtime.h>#include <linux/blk-cgroup.h> #include <trace/events/block.h> #include "blk.h"#include "blk-mq-sched.h"#include "blk-wbt.h" static DEFINE_SPINLOCK(elv_list_lock);static LIST_HEAD(elv_list); /* * Merge hash stuff. */#define rq_hash_key(rq)		(blk_rq_pos(rq) + blk_rq_sectors(rq)) /* * Query io scheduler to see if the current process issuing bio may be * merged with rq. */static int elv_iosched_allow_bio_merge(struct request *rq, struct bio *bio){	struct request_queue *q = rq->q;	struct elevator_queue *e = q->elevator; 	if (e->uses_mq && e->type->ops.mq.allow_merge)		return e->type->ops.mq.allow_merge(q, rq, bio);	else if (!e->uses_mq && e->type->ops.sq.elevator_allow_bio_merge_fn)		return e->type->ops.sq.elevator_allow_bio_merge_fn(q, rq, bio); 	return 1;} /* * can we safely merge with this request? */bool elv_bio_merge_ok(struct request *rq, struct bio *bio){	if (!blk_rq_merge_ok(rq, bio))		return false; 	if (!elv_iosched_allow_bio_merge(rq, bio))		return false; 	return true;}EXPORT_SYMBOL(elv_bio_merge_ok); static struct elevator_type *elevator_find(const char *name){	struct elevator_type *e; 	list_for_each_entry(e, &elv_list, list) {		if (!strcmp(e->elevator_name, name))			return e;	} 	return NULL;} static void elevator_put(struct elevator_type *e){	module_put(e->elevator_owner);} static struct elevator_type *elevator_get(const char *name, bool try_loading){	struct elevator_type *e; 	spin_lock(&elv_list_lock); 	e = elevator_find(name);	if (!e && try_loading) {		spin_unlock(&elv_list_lock);		request_module("%s-iosched", name);		spin_lock(&elv_list_lock);		e = elevator_find(name);	} 	if (e && !try_module_get(e->elevator_owner))		e = NULL; 	spin_unlock(&elv_list_lock); 	return e;} static char chosen_elevator[ELV_NAME_MAX]; static int __init elevator_setup(char *str){	/*	 * Be backwards-compatible with previous kernels, so users	 * won't get the wrong elevator.	 */	strncpy(chosen_elevator, str, sizeof(chosen_elevator) - 1);	return 1;} __setup("elevator=", elevator_setup); /* called during boot to load the elevator chosen by the elevator param */void __init load_default_elevator_module(void){	struct elevator_type *e; 	if (!chosen_elevator[0])		return; 	spin_lock(&elv_list_lock);	e = elevator_find(chosen_elevator);	spin_unlock(&elv_list_lock); 	if (!e)		request_module("%s-iosched", chosen_elevator);} static struct kobj_type elv_ktype; struct elevator_queue *elevator_alloc(struct request_queue *q,				  struct elevator_type *e){	struct elevator_queue *eq; 	eq = kzalloc_node(sizeof(*eq), GFP_KERNEL, q->node);	if (unlikely(!eq))		return NULL; 	eq->type = e;	kobject_init(&eq->kobj, &elv_ktype);	mutex_init(&eq->sysfs_lock);	hash_init(eq->hash);	eq->uses_mq = e->uses_mq; 	return eq;}EXPORT_SYMBOL(elevator_alloc); static void elevator_release(struct kobject *kobj){	struct elevator_queue *e; 	e = container_of(kobj, struct elevator_queue, kobj);	elevator_put(e->type);	kfree(e);} int elevator_init(struct request_queue *q, char *name){	struct elevator_type *e = NULL;	int err; 	/*	 * q->sysfs_lock must be held to provide mutual exclusion between	 * elevator_switch() and here.	 */	lockdep_assert_held(&q->sysfs_lock); 	if (unlikely(q->elevator))		return 0; 	INIT_LIST_HEAD(&q->queue_head);	q->last_merge = NULL;	q->end_sector = 0;	q->boundary_rq = NULL; 	if (name) {		e = elevator_get(name, true);		if (!e)			return -EINVAL;	} 	/*	 * Use the default elevator specified by config boot param for	 * non-mq devices, or by config option. Don't try to load modules	 * as we could be running off async and request_module() isn't	 * allowed from async.	 */	if (!e && !q->mq_ops && *chosen_elevator) {		e = elevator_get(chosen_elevator, false);		if (!e)			printk(KERN_ERR "I/O scheduler %s not found\n",							chosen_elevator);	} 	if (!e) {		/*		 * For blk-mq devices, we default to using mq-deadline,		 * if available, for single queue devices. If deadline		 * isn't available OR we have multiple queues, default		 * to "none".		 */		if (q->mq_ops) {			if (q->nr_hw_queues == 1)				e = elevator_get("mq-deadline", false);			if (!e)				return 0;		} else			e = elevator_get(CONFIG_DEFAULT_IOSCHED, false); 		if (!e) {			printk(KERN_ERR				"Default I/O scheduler not found. " \				"Using noop.\n");			e = elevator_get("noop", false);		}	} 	if (e->uses_mq)		err = blk_mq_init_sched(q, e);	else		err = e->ops.sq.elevator_init_fn(q, e);	if (err)		elevator_put(e);	return err;}EXPORT_SYMBOL(elevator_init); void elevator_exit(struct request_queue *q, struct elevator_queue *e){	mutex_lock(&e->sysfs_lock);	if (e->uses_mq && e->type->ops.mq.exit_sched)		blk_mq_exit_sched(q, e);	else if (!e->uses_mq && e->type->ops.sq.elevator_exit_fn)		e->type->ops.sq.elevator_exit_fn(e);	mutex_unlock(&e->sysfs_lock); 	kobject_put(&e->kobj);}EXPORT_SYMBOL(elevator_exit); static inline void __elv_rqhash_del(struct request *rq){	hash_del(&rq->hash);	rq->rq_flags &= ~RQF_HASHED;} void elv_rqhash_del(struct request_queue *q, struct request *rq){	if (ELV_ON_HASH(rq))		__elv_rqhash_del(rq);}EXPORT_SYMBOL_GPL(elv_rqhash_del); void elv_rqhash_add(struct request_queue *q, struct request *rq){	struct elevator_queue *e = q->elevator; 	BUG_ON(ELV_ON_HASH(rq));	hash_add(e->hash, &rq->hash, rq_hash_key(rq));	rq->rq_flags |= RQF_HASHED;}EXPORT_SYMBOL_GPL(elv_rqhash_add); void elv_rqhash_reposition(struct request_queue *q, struct request *rq){	__elv_rqhash_del(rq);	elv_rqhash_add(q, rq);} struct request *elv_rqhash_find(struct request_queue *q, sector_t offset){	struct elevator_queue *e = q->elevator;	struct hlist_node *next;	struct request *rq; 	hash_for_each_possible_safe(e->hash, rq, next, hash, offset) {		BUG_ON(!ELV_ON_HASH(rq)); 		if (unlikely(!rq_mergeable(rq))) {			__elv_rqhash_del(rq);			continue;		} 		if (rq_hash_key(rq) == offset)			return rq;	} 	return NULL;} /* * RB-tree support functions for inserting/lookup/removal of requests * in a sorted RB tree. */void elv_rb_add(struct rb_root *root, struct request *rq){	struct rb_node **p = &root->rb_node;	struct rb_node *parent = NULL;	struct request *__rq; 	while (*p) {		parent = *p;		__rq = rb_entry(parent, struct request, rb_node); 		if (blk_rq_pos(rq) < blk_rq_pos(__rq))			p = &(*p)->rb_left;		else if (blk_rq_pos(rq) >= blk_rq_pos(__rq))			p = &(*p)->rb_right;	} 	rb_link_node(&rq->rb_node, parent, p);	rb_insert_color(&rq->rb_node, root);}EXPORT_SYMBOL(elv_rb_add); void elv_rb_del(struct rb_root *root, struct request *rq){	BUG_ON(RB_EMPTY_NODE(&rq->rb_node));	rb_erase(&rq->rb_node, root);	RB_CLEAR_NODE(&rq->rb_node);}EXPORT_SYMBOL(elv_rb_del); struct request *elv_rb_find(struct rb_root *root, sector_t sector){	struct rb_node *n = root->rb_node;	struct request *rq; 	while (n) {		rq = rb_entry(n, struct request, rb_node); 		if (sector < blk_rq_pos(rq))			n = n->rb_left;		else if (sector > blk_rq_pos(rq))			n = n->rb_right;		else			return rq;	} 	return NULL;}EXPORT_SYMBOL(elv_rb_find); /* * Insert rq into dispatch queue of q.  Queue lock must be held on * entry.  rq is sort instead into the dispatch queue. To be used by * specific elevators. */void elv_dispatch_sort(struct request_queue *q, struct request *rq){	sector_t boundary;	struct list_head *entry; 	if (q->last_merge == rq)		q->last_merge = NULL; 	elv_rqhash_del(q, rq); 	q->nr_sorted--; 	boundary = q->end_sector;	list_for_each_prev(entry, &q->queue_head) {		struct request *pos = list_entry_rq(entry); 		if (req_op(rq) != req_op(pos))			break;		if (rq_data_dir(rq) != rq_data_dir(pos))			break;		if (pos->rq_flags & (RQF_STARTED | RQF_SOFTBARRIER))			break;		if (blk_rq_pos(rq) >= boundary) {			if (blk_rq_pos(pos) < boundary)				continue;		} else {			if (blk_rq_pos(pos) >= boundary)				break;		}		if (blk_rq_pos(rq) >= blk_rq_pos(pos))			break;	} 	list_add(&rq->queuelist, entry);}EXPORT_SYMBOL(elv_dispatch_sort); /* * Insert rq into dispatch queue of q.  Queue lock must be held on * entry.  rq is added to the back of the dispatch queue. To be used by * specific elevators. */void elv_dispatch_add_tail(struct request_queue *q, struct request *rq){	if (q->last_merge == rq)		q->last_merge = NULL; 	elv_rqhash_del(q, rq); 	q->nr_sorted--; 	q->end_sector = rq_end_sector(rq);	q->boundary_rq = rq;	list_add_tail(&rq->queuelist, &q->queue_head);}EXPORT_SYMBOL(elv_dispatch_add_tail); enum elv_merge elv_merge(struct request_queue *q, struct request **req,		struct bio *bio){	struct elevator_queue *e = q->elevator;	struct request *__rq; 	/*	 * Levels of merges:	 * 	nomerges:  No merges at all attempted	 * 	noxmerges: Only simple one-hit cache try	 * 	merges:	   All merge tries attempted	 */	if (blk_queue_nomerges(q) || !bio_mergeable(bio))		return ELEVATOR_NO_MERGE; 	/*	 * First try one-hit cache.	 */	if (q->last_merge && elv_bio_merge_ok(q->last_merge, bio)) {		enum elv_merge ret = blk_try_merge(q->last_merge, bio); 		if (ret != ELEVATOR_NO_MERGE) {			*req = q->last_merge;			return ret;		}	} 	if (blk_queue_noxmerges(q))		return ELEVATOR_NO_MERGE; 	/*	 * See if our hash lookup can find a potential backmerge.	 */	__rq = elv_rqhash_find(q, bio->bi_iter.bi_sector);	if (__rq && elv_bio_merge_ok(__rq, bio)) {		*req = __rq;		return ELEVATOR_BACK_MERGE;	} 	if (e->uses_mq && e->type->ops.mq.request_merge)		return e->type->ops.mq.request_merge(q, req, bio);	else if (!e->uses_mq && e->type->ops.sq.elevator_merge_fn)		return e->type->ops.sq.elevator_merge_fn(q, req, bio); 	return ELEVATOR_NO_MERGE;} /* * Attempt to do an insertion back merge. Only check for the case where * we can append 'rq' to an existing request, so we can throw 'rq' away * afterwards. * * Returns true if we merged, false otherwise */bool elv_attempt_insert_merge(struct request_queue *q, struct request *rq){	struct request *__rq;	bool ret; 	if (blk_queue_nomerges(q))		return false; 	/*	 * First try one-hit cache.	 */	if (q->last_merge && blk_attempt_req_merge(q, q->last_merge, rq))		return true;

Lines 1–500 of 1167.