2008-01-29 16:51:59 +03:00
/*
* Functions related to tagged command queuing
*/
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/bio.h>
# include <linux/blkdev.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2008-01-29 16:51:59 +03:00
2008-03-04 13:23:44 +03:00
# include "blk.h"
2008-01-29 16:51:59 +03:00
/**
* blk_queue_find_tag - find a request by its tag and queue
* @ q : The request queue for the device
* @ tag : The tag of the request
*
* Notes :
* Should be used when a device returns a tag and you want to match
* it with a request .
*
* no locks need be held .
* */
struct request * blk_queue_find_tag ( struct request_queue * q , int tag )
{
return blk_map_queue_find_tag ( q - > queue_tags , tag ) ;
}
EXPORT_SYMBOL ( blk_queue_find_tag ) ;
/**
* __blk_free_tags - release a given set of tag maintenance info
* @ bqt : the tag map to free
*
2008-08-19 22:13:11 +04:00
* Tries to free the specified @ bqt . Returns true if it was
2008-01-29 16:51:59 +03:00
* actually freed and false if there are still references using it
*/
static int __blk_free_tags ( struct blk_queue_tag * bqt )
{
int retval ;
retval = atomic_dec_and_test ( & bqt - > refcnt ) ;
if ( retval ) {
2008-08-26 11:02:28 +04:00
BUG_ON ( find_first_bit ( bqt - > tag_map , bqt - > max_depth ) <
bqt - > max_depth ) ;
2008-01-29 16:51:59 +03:00
kfree ( bqt - > tag_index ) ;
bqt - > tag_index = NULL ;
kfree ( bqt - > tag_map ) ;
bqt - > tag_map = NULL ;
kfree ( bqt ) ;
}
return retval ;
}
/**
* __blk_queue_free_tags - release tag maintenance info
* @ q : the request queue for the device
*
* Notes :
* blk_cleanup_queue ( ) will take care of calling this function , if tagging
* has been used . So there ' s no need to call this directly .
* */
void __blk_queue_free_tags ( struct request_queue * q )
{
struct blk_queue_tag * bqt = q - > queue_tags ;
if ( ! bqt )
return ;
__blk_free_tags ( bqt ) ;
q - > queue_tags = NULL ;
2008-05-07 11:27:43 +04:00
queue_flag_clear_unlocked ( QUEUE_FLAG_QUEUED , q ) ;
2008-01-29 16:51:59 +03:00
}
/**
* blk_free_tags - release a given set of tag maintenance info
* @ bqt : the tag map to free
*
2008-08-19 22:13:11 +04:00
* For externally managed @ bqt frees the map . Callers of this
2008-01-29 16:51:59 +03:00
* function must guarantee to have released all the queues that
* might have been using this tag map .
*/
void blk_free_tags ( struct blk_queue_tag * bqt )
{
if ( unlikely ( ! __blk_free_tags ( bqt ) ) )
BUG ( ) ;
}
EXPORT_SYMBOL ( blk_free_tags ) ;
/**
* blk_queue_free_tags - release tag maintenance info
* @ q : the request queue for the device
*
* Notes :
2008-08-19 22:13:11 +04:00
* This is used to disable tagged queuing to a device , yet leave
2008-01-29 16:51:59 +03:00
* queue in function .
* */
void blk_queue_free_tags ( struct request_queue * q )
{
2008-05-07 11:27:43 +04:00
queue_flag_clear_unlocked ( QUEUE_FLAG_QUEUED , q ) ;
2008-01-29 16:51:59 +03:00
}
EXPORT_SYMBOL ( blk_queue_free_tags ) ;
static int
init_tag_map ( struct request_queue * q , struct blk_queue_tag * tags , int depth )
{
struct request * * tag_index ;
unsigned long * tag_map ;
int nr_ulongs ;
if ( q & & depth > q - > nr_requests * 2 ) {
depth = q - > nr_requests * 2 ;
printk ( KERN_ERR " %s: adjusted depth to %d \n " ,
2008-05-01 15:35:17 +04:00
__func__ , depth ) ;
2008-01-29 16:51:59 +03:00
}
tag_index = kzalloc ( depth * sizeof ( struct request * ) , GFP_ATOMIC ) ;
if ( ! tag_index )
goto fail ;
nr_ulongs = ALIGN ( depth , BITS_PER_LONG ) / BITS_PER_LONG ;
tag_map = kzalloc ( nr_ulongs * sizeof ( unsigned long ) , GFP_ATOMIC ) ;
if ( ! tag_map )
goto fail ;
tags - > real_max_depth = depth ;
tags - > max_depth = depth ;
tags - > tag_index = tag_index ;
tags - > tag_map = tag_map ;
return 0 ;
fail :
kfree ( tag_index ) ;
return - ENOMEM ;
}
static struct blk_queue_tag * __blk_queue_init_tags ( struct request_queue * q ,
int depth )
{
struct blk_queue_tag * tags ;
tags = kmalloc ( sizeof ( struct blk_queue_tag ) , GFP_ATOMIC ) ;
if ( ! tags )
goto fail ;
if ( init_tag_map ( q , tags , depth ) )
goto fail ;
atomic_set ( & tags - > refcnt , 1 ) ;
return tags ;
fail :
kfree ( tags ) ;
return NULL ;
}
/**
* blk_init_tags - initialize the tag info for an external tag map
* @ depth : the maximum queue depth supported
* */
struct blk_queue_tag * blk_init_tags ( int depth )
{
return __blk_queue_init_tags ( NULL , depth ) ;
}
EXPORT_SYMBOL ( blk_init_tags ) ;
/**
* blk_queue_init_tags - initialize the queue tag info
* @ q : the request queue for the device
* @ depth : the maximum queue depth supported
* @ tags : the tag to use
2008-05-07 11:27:43 +04:00
*
* Queue lock must be held here if the function is called to resize an
* existing map .
2008-01-29 16:51:59 +03:00
* */
int blk_queue_init_tags ( struct request_queue * q , int depth ,
struct blk_queue_tag * tags )
{
int rc ;
BUG_ON ( tags & & q - > queue_tags & & tags ! = q - > queue_tags ) ;
if ( ! tags & & ! q - > queue_tags ) {
tags = __blk_queue_init_tags ( q , depth ) ;
if ( ! tags )
goto fail ;
} else if ( q - > queue_tags ) {
2008-01-31 15:03:55 +03:00
rc = blk_queue_resize_tags ( q , depth ) ;
if ( rc )
2008-01-29 16:51:59 +03:00
return rc ;
2008-04-29 16:48:33 +04:00
queue_flag_set ( QUEUE_FLAG_QUEUED , q ) ;
2008-01-29 16:51:59 +03:00
return 0 ;
} else
atomic_inc ( & tags - > refcnt ) ;
/*
* assign it , all done
*/
q - > queue_tags = tags ;
2008-05-07 11:27:43 +04:00
queue_flag_set_unlocked ( QUEUE_FLAG_QUEUED , q ) ;
2008-01-29 16:51:59 +03:00
INIT_LIST_HEAD ( & q - > tag_busy_list ) ;
return 0 ;
fail :
kfree ( tags ) ;
return - ENOMEM ;
}
EXPORT_SYMBOL ( blk_queue_init_tags ) ;
/**
* blk_queue_resize_tags - change the queueing depth
* @ q : the request queue for the device
* @ new_depth : the new max command queueing depth
*
* Notes :
* Must be called with the queue lock held .
* */
int blk_queue_resize_tags ( struct request_queue * q , int new_depth )
{
struct blk_queue_tag * bqt = q - > queue_tags ;
struct request * * tag_index ;
unsigned long * tag_map ;
int max_depth , nr_ulongs ;
if ( ! bqt )
return - ENXIO ;
/*
* if we already have large enough real_max_depth . just
* adjust max_depth . * NOTE * as requests with tag value
* between new_depth and real_max_depth can be in - flight , tag
* map can not be shrunk blindly here .
*/
if ( new_depth < = bqt - > real_max_depth ) {
bqt - > max_depth = new_depth ;
return 0 ;
}
/*
* Currently cannot replace a shared tag map with a new
* one , so error out if this is the case
*/
if ( atomic_read ( & bqt - > refcnt ) ! = 1 )
return - EBUSY ;
/*
* save the old state info , so we can copy it back
*/
tag_index = bqt - > tag_index ;
tag_map = bqt - > tag_map ;
max_depth = bqt - > real_max_depth ;
if ( init_tag_map ( q , bqt , new_depth ) )
return - ENOMEM ;
memcpy ( bqt - > tag_index , tag_index , max_depth * sizeof ( struct request * ) ) ;
nr_ulongs = ALIGN ( max_depth , BITS_PER_LONG ) / BITS_PER_LONG ;
memcpy ( bqt - > tag_map , tag_map , nr_ulongs * sizeof ( unsigned long ) ) ;
kfree ( tag_index ) ;
kfree ( tag_map ) ;
return 0 ;
}
EXPORT_SYMBOL ( blk_queue_resize_tags ) ;
/**
* blk_queue_end_tag - end tag operations for a request
* @ q : the request queue for the device
* @ rq : the request that has completed
*
* Description :
2008-08-19 22:13:11 +04:00
* Typically called when end_that_request_first ( ) returns % 0 , meaning
2008-01-29 16:51:59 +03:00
* all transfers have been done for a request . It ' s important to call
* this function before end_that_request_last ( ) , as that will put the
* request back on the free list thus corrupting the internal tag list .
*
* Notes :
* queue lock must be held .
* */
void blk_queue_end_tag ( struct request_queue * q , struct request * rq )
{
struct blk_queue_tag * bqt = q - > queue_tags ;
int tag = rq - > tag ;
BUG_ON ( tag = = - 1 ) ;
2011-10-25 12:20:05 +04:00
if ( unlikely ( tag > = bqt - > max_depth ) ) {
2008-01-29 16:51:59 +03:00
/*
* This can happen after tag depth has been reduced .
2011-10-25 12:20:05 +04:00
* But tag shouldn ' t be larger than real_max_depth .
2008-01-29 16:51:59 +03:00
*/
2011-10-25 12:20:05 +04:00
WARN_ON ( tag > = bqt - > real_max_depth ) ;
2008-01-29 16:51:59 +03:00
return ;
2011-10-25 12:20:05 +04:00
}
2008-01-29 16:51:59 +03:00
list_del_init ( & rq - > queuelist ) ;
rq - > cmd_flags & = ~ REQ_QUEUED ;
rq - > tag = - 1 ;
if ( unlikely ( bqt - > tag_index [ tag ] = = NULL ) )
printk ( KERN_ERR " %s: tag %d is missing \n " ,
2008-05-01 15:35:17 +04:00
__func__ , tag ) ;
2008-01-29 16:51:59 +03:00
bqt - > tag_index [ tag ] = NULL ;
if ( unlikely ( ! test_bit ( tag , bqt - > tag_map ) ) ) {
printk ( KERN_ERR " %s: attempt to clear non-busy tag (%d) \n " ,
2008-05-01 15:35:17 +04:00
__func__ , tag ) ;
2008-01-29 16:51:59 +03:00
return ;
}
/*
* The tag_map bit acts as a lock for tag_index [ bit ] , so we need
* unlock memory barrier semantics .
*/
clear_bit_unlock ( tag , bqt - > tag_map ) ;
}
EXPORT_SYMBOL ( blk_queue_end_tag ) ;
/**
* blk_queue_start_tag - find a free tag and assign it
* @ q : the request queue for the device
* @ rq : the block request that needs tagging
*
* Description :
* This can either be used as a stand - alone helper , or possibly be
* assigned as the queue & prep_rq_fn ( in which case & struct request
* automagically gets a tag assigned ) . Note that this function
* assumes that any type of request can be queued ! if this is not
* true for your device , you must check the request type before
* calling this function . The request will also be removed from
* the request queue , so it ' s the drivers responsibility to readd
* it if it should need to be restarted for some reason .
*
* Notes :
* queue lock must be held .
* */
int blk_queue_start_tag ( struct request_queue * q , struct request * rq )
{
struct blk_queue_tag * bqt = q - > queue_tags ;
2009-05-20 10:54:31 +04:00
unsigned max_depth ;
2008-01-29 16:51:59 +03:00
int tag ;
if ( unlikely ( ( rq - > cmd_flags & REQ_QUEUED ) ) ) {
2008-01-31 15:03:55 +03:00
printk ( KERN_ERR
2008-01-29 16:51:59 +03:00
" %s: request %p for device [%s] already tagged %d " ,
2008-05-01 15:35:17 +04:00
__func__ , rq ,
2008-01-29 16:51:59 +03:00
rq - > rq_disk ? rq - > rq_disk - > disk_name : " ? " , rq - > tag ) ;
BUG ( ) ;
}
/*
* Protect against shared tag maps , as we may not have exclusive
* access to the tag map .
2008-09-25 13:42:41 +04:00
*
* We reserve a few tags just for sync IO , since we don ' t want
* to starve sync IO on behalf of flooding async IO .
2008-01-29 16:51:59 +03:00
*/
2008-09-25 13:42:41 +04:00
max_depth = bqt - > max_depth ;
2009-05-20 10:54:31 +04:00
if ( ! rq_is_sync ( rq ) & & max_depth > 1 ) {
max_depth - = 2 ;
if ( ! max_depth )
max_depth = 1 ;
2009-10-06 22:19:02 +04:00
if ( q - > in_flight [ BLK_RW_ASYNC ] > max_depth )
2009-05-20 10:54:31 +04:00
return 1 ;
}
2008-09-25 13:42:41 +04:00
2008-01-29 16:51:59 +03:00
do {
2009-05-20 10:54:31 +04:00
tag = find_first_zero_bit ( bqt - > tag_map , max_depth ) ;
2008-09-25 13:42:41 +04:00
if ( tag > = max_depth )
2008-01-29 16:51:59 +03:00
return 1 ;
} while ( test_and_set_bit_lock ( tag , bqt - > tag_map ) ) ;
/*
* We need lock ordering semantics given by test_and_set_bit_lock .
* See blk_queue_end_tag for details .
*/
rq - > cmd_flags | = REQ_QUEUED ;
rq - > tag = tag ;
bqt - > tag_index [ tag ] = rq ;
2009-05-08 06:54:16 +04:00
blk_start_request ( rq ) ;
2008-01-29 16:51:59 +03:00
list_add ( & rq - > queuelist , & q - > tag_busy_list ) ;
return 0 ;
}
EXPORT_SYMBOL ( blk_queue_start_tag ) ;
/**
* blk_queue_invalidate_tags - invalidate all pending tags
* @ q : the request queue for the device
*
* Description :
* Hardware conditions may dictate a need to stop all pending requests .
* In this case , we will safely clear the block side of the tag queue and
* readd all requests to the request queue in the right order .
*
* Notes :
* queue lock must be held .
* */
void blk_queue_invalidate_tags ( struct request_queue * q )
{
struct list_head * tmp , * n ;
list_for_each_safe ( tmp , n , & q - > tag_busy_list )
blk_requeue_request ( q , list_entry_rq ( tmp ) ) ;
}
EXPORT_SYMBOL ( blk_queue_invalidate_tags ) ;