2009-08-05 11:07:21 +04:00
|
|
|
/*
|
|
|
|
* Functions related to interrupt-poll handling in the block layer. This
|
|
|
|
* is similar to NAPI for network devices.
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/bio.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/cpu.h>
|
2015-11-10 16:56:14 +03:00
|
|
|
#include <linux/irq_poll.h>
|
2009-08-05 11:07:21 +04:00
|
|
|
#include <linux/delay.h>
|
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
static unsigned int irq_poll_budget __read_mostly = 256;
|
2009-08-06 22:50:48 +04:00
|
|
|
|
2009-08-05 11:07:21 +04:00
|
|
|
static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
|
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* irq_poll_sched - Schedule a run of the iopoll handler
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-10 16:56:14 +03:00
|
|
|
* Add this irq_poll structure to the pending poll list and trigger the
|
2015-12-07 17:41:11 +03:00
|
|
|
* raise of the blk iopoll softirq.
|
2009-08-05 11:07:21 +04:00
|
|
|
**/
|
2015-11-10 16:56:14 +03:00
|
|
|
void irq_poll_sched(struct irq_poll *iop)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2015-12-07 17:41:11 +03:00
|
|
|
if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
|
|
|
|
return;
|
|
|
|
if (!test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
|
|
|
|
return;
|
|
|
|
|
2009-08-05 11:07:21 +04:00
|
|
|
local_irq_save(flags);
|
block: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
this_cpu_inc(y)
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-15 22:22:29 +04:00
|
|
|
list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
|
2015-11-10 16:56:14 +03:00
|
|
|
__raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
|
2009-08-05 11:07:21 +04:00
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
EXPORT_SYMBOL(irq_poll_sched);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* __irq_poll_complete - Mark this @iop as un-polled again
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-10 16:56:14 +03:00
|
|
|
* See irq_poll_complete(). This function must be called with interrupts
|
2009-08-06 22:49:14 +04:00
|
|
|
* disabled.
|
2009-08-05 11:07:21 +04:00
|
|
|
**/
|
2015-12-07 17:57:25 +03:00
|
|
|
static void __irq_poll_complete(struct irq_poll *iop)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
list_del(&iop->list);
|
2014-03-17 21:06:10 +04:00
|
|
|
smp_mb__before_atomic();
|
2015-11-10 16:56:14 +03:00
|
|
|
clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
|
2009-08-05 11:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* irq_poll_complete - Mark this @iop as un-polled again
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
*
|
|
|
|
* Description:
|
2009-08-06 22:49:14 +04:00
|
|
|
* If a driver consumes less than the assigned budget in its run of the
|
|
|
|
* iopoll handler, it'll end the polled mode by calling this function. The
|
2015-12-07 17:41:11 +03:00
|
|
|
* iopoll handler will not be invoked again before irq_poll_sched()
|
2009-08-06 22:49:14 +04:00
|
|
|
* is called.
|
2009-08-05 11:07:21 +04:00
|
|
|
**/
|
2015-11-10 16:56:14 +03:00
|
|
|
void irq_poll_complete(struct irq_poll *iop)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
2015-11-10 16:56:14 +03:00
|
|
|
__irq_poll_complete(iop);
|
2009-08-05 11:07:21 +04:00
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
EXPORT_SYMBOL(irq_poll_complete);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
static void irq_poll_softirq(struct softirq_action *h)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
block: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
this_cpu_inc(y)
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-15 22:22:29 +04:00
|
|
|
struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
|
2015-11-10 16:56:14 +03:00
|
|
|
int rearm = 0, budget = irq_poll_budget;
|
2009-08-05 11:07:21 +04:00
|
|
|
unsigned long start_time = jiffies;
|
|
|
|
|
|
|
|
local_irq_disable();
|
|
|
|
|
|
|
|
while (!list_empty(list)) {
|
2015-11-10 16:56:14 +03:00
|
|
|
struct irq_poll *iop;
|
2009-08-05 11:07:21 +04:00
|
|
|
int work, weight;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If softirq window is exhausted then punt.
|
|
|
|
*/
|
|
|
|
if (budget <= 0 || time_after(jiffies, start_time)) {
|
|
|
|
rearm = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
|
|
|
|
/* Even though interrupts have been re-enabled, this
|
|
|
|
* access is safe because interrupts can only add new
|
|
|
|
* entries to the tail of this list, and only ->poll()
|
|
|
|
* calls can remove this head entry from the list.
|
|
|
|
*/
|
2015-11-10 16:56:14 +03:00
|
|
|
iop = list_entry(list->next, struct irq_poll, list);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
|
|
|
weight = iop->weight;
|
|
|
|
work = 0;
|
2015-11-10 16:56:14 +03:00
|
|
|
if (test_bit(IRQ_POLL_F_SCHED, &iop->state))
|
2009-08-05 11:07:21 +04:00
|
|
|
work = iop->poll(iop, weight);
|
|
|
|
|
|
|
|
budget -= work;
|
|
|
|
|
|
|
|
local_irq_disable();
|
|
|
|
|
2009-08-06 22:53:23 +04:00
|
|
|
/*
|
|
|
|
* Drivers must not modify the iopoll state, if they
|
|
|
|
* consume their assigned weight (or more, some drivers can't
|
|
|
|
* easily just stop processing, they have to complete an
|
|
|
|
* entire mask of commands).In such cases this code
|
|
|
|
* still "owns" the iopoll instance and therefore can
|
2009-08-05 11:07:21 +04:00
|
|
|
* move the instance around on the list at-will.
|
|
|
|
*/
|
|
|
|
if (work >= weight) {
|
2015-12-07 17:56:36 +03:00
|
|
|
if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
|
2015-11-10 16:56:14 +03:00
|
|
|
__irq_poll_complete(iop);
|
2009-08-05 11:07:21 +04:00
|
|
|
else
|
|
|
|
list_move_tail(&iop->list, list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rearm)
|
2015-11-10 16:56:14 +03:00
|
|
|
__raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* irq_poll_disable - Disable iopoll on this @iop
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Disable io polling and wait for any pending callbacks to have completed.
|
|
|
|
**/
|
2015-11-10 16:56:14 +03:00
|
|
|
void irq_poll_disable(struct irq_poll *iop)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
2015-11-10 16:56:14 +03:00
|
|
|
set_bit(IRQ_POLL_F_DISABLE, &iop->state);
|
|
|
|
while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
|
2009-08-05 11:07:21 +04:00
|
|
|
msleep(1);
|
2015-11-10 16:56:14 +03:00
|
|
|
clear_bit(IRQ_POLL_F_DISABLE, &iop->state);
|
2009-08-05 11:07:21 +04:00
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
EXPORT_SYMBOL(irq_poll_disable);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* irq_poll_enable - Enable iopoll on this @iop
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
*
|
|
|
|
* Description:
|
2009-08-06 22:49:14 +04:00
|
|
|
* Enable iopoll on this @iop. Note that the handler run will not be
|
|
|
|
* scheduled, it will only mark it as active.
|
2009-08-05 11:07:21 +04:00
|
|
|
**/
|
2015-11-10 16:56:14 +03:00
|
|
|
void irq_poll_enable(struct irq_poll *iop)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
2015-11-10 16:56:14 +03:00
|
|
|
BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state));
|
2014-03-17 21:06:10 +04:00
|
|
|
smp_mb__before_atomic();
|
2015-11-10 16:56:14 +03:00
|
|
|
clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
|
2009-08-05 11:07:21 +04:00
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
EXPORT_SYMBOL(irq_poll_enable);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
|
|
|
/**
|
2015-11-10 16:56:14 +03:00
|
|
|
* irq_poll_init - Initialize this @iop
|
2009-08-05 11:07:21 +04:00
|
|
|
* @iop: The parent iopoll structure
|
|
|
|
* @weight: The default weight (or command completion budget)
|
|
|
|
* @poll_fn: The handler to invoke
|
|
|
|
*
|
|
|
|
* Description:
|
2015-12-07 17:38:28 +03:00
|
|
|
* Initialize and enable this irq_poll structure.
|
2009-08-05 11:07:21 +04:00
|
|
|
**/
|
2015-11-10 16:56:14 +03:00
|
|
|
void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
memset(iop, 0, sizeof(*iop));
|
|
|
|
INIT_LIST_HEAD(&iop->list);
|
|
|
|
iop->weight = weight;
|
|
|
|
iop->poll = poll_fn;
|
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
EXPORT_SYMBOL(irq_poll_init);
|
2009-08-05 11:07:21 +04:00
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
static int irq_poll_cpu_notify(struct notifier_block *self,
|
2013-06-19 23:26:23 +04:00
|
|
|
unsigned long action, void *hcpu)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If a CPU goes away, splice its entries to the current CPU
|
|
|
|
* and trigger a run of the softirq
|
|
|
|
*/
|
|
|
|
if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
|
|
|
|
int cpu = (unsigned long) hcpu;
|
|
|
|
|
|
|
|
local_irq_disable();
|
|
|
|
list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
|
block: Replace __get_cpu_var uses
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
#define __get_cpu_var(var) (*this_cpu_ptr(&(var)))
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
this_cpu_inc(y)
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2013-10-15 22:22:29 +04:00
|
|
|
this_cpu_ptr(&blk_cpu_iopoll));
|
2015-11-10 16:56:14 +03:00
|
|
|
__raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
|
2009-08-05 11:07:21 +04:00
|
|
|
local_irq_enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
static struct notifier_block irq_poll_cpu_notifier = {
|
|
|
|
.notifier_call = irq_poll_cpu_notify,
|
2009-08-05 11:07:21 +04:00
|
|
|
};
|
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
static __init int irq_poll_setup(void)
|
2009-08-05 11:07:21 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for_each_possible_cpu(i)
|
|
|
|
INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
|
|
|
|
|
2015-11-10 16:56:14 +03:00
|
|
|
open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq);
|
|
|
|
register_hotcpu_notifier(&irq_poll_cpu_notifier);
|
2009-08-05 11:07:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2015-11-10 16:56:14 +03:00
|
|
|
subsys_initcall(irq_poll_setup);
|