block: simplify ioc_release_fn()
[deliverable/linux.git] / include / linux / iocontext.h
CommitLineData
fd0928df
JA
1#ifndef IOCONTEXT_H
2#define IOCONTEXT_H
3
4ac845a2 4#include <linux/radix-tree.h>
34e6bbf2 5#include <linux/rcupdate.h>
b2efa052 6#include <linux/workqueue.h>
4ac845a2 7
dc86900e 8enum {
d705ae6b
TH
9 ICQ_IOPRIO_CHANGED = 1 << 0,
10 ICQ_CGROUP_CHANGED = 1 << 1,
11
12 ICQ_CHANGED_MASK = ICQ_IOPRIO_CHANGED | ICQ_CGROUP_CHANGED,
dc86900e
TH
13};
14
f1f8cc94
TH
15/*
16 * An io_cq (icq) is association between an io_context (ioc) and a
17 * request_queue (q). This is used by elevators which need to track
18 * information per ioc - q pair.
19 *
20 * Elevator can request use of icq by setting elevator_type->icq_size and
21 * ->icq_align. Both size and align must be larger than that of struct
22 * io_cq and elevator can use the tail area for private information. The
23 * recommended way to do this is defining a struct which contains io_cq as
24 * the first member followed by private members and using its size and
25 * align. For example,
26 *
27 * struct snail_io_cq {
28 * struct io_cq icq;
29 * int poke_snail;
30 * int feed_snail;
31 * };
32 *
33 * struct elevator_type snail_elv_type {
34 * .ops = { ... },
35 * .icq_size = sizeof(struct snail_io_cq),
36 * .icq_align = __alignof__(struct snail_io_cq),
37 * ...
38 * };
39 *
40 * If icq_size is set, block core will manage icq's. All requests will
41 * have its ->elv.icq field set before elevator_ops->elevator_set_req_fn()
42 * is called and be holding a reference to the associated io_context.
43 *
44 * Whenever a new icq is created, elevator_ops->elevator_init_icq_fn() is
45 * called and, on destruction, ->elevator_exit_icq_fn(). Both functions
46 * are called with both the associated io_context and queue locks held.
47 *
48 * Elevator is allowed to lookup icq using ioc_lookup_icq() while holding
49 * queue lock but the returned icq is valid only until the queue lock is
50 * released. Elevators can not and should not try to create or destroy
51 * icq's.
52 *
53 * As icq's are linked from both ioc and q, the locking rules are a bit
54 * complex.
55 *
56 * - ioc lock nests inside q lock.
57 *
58 * - ioc->icq_list and icq->ioc_node are protected by ioc lock.
59 * q->icq_list and icq->q_node by q lock.
60 *
61 * - ioc->icq_tree and ioc->icq_hint are protected by ioc lock, while icq
62 * itself is protected by q lock. However, both the indexes and icq
63 * itself are also RCU managed and lookup can be performed holding only
64 * the q lock.
65 *
66 * - icq's are not reference counted. They are destroyed when either the
67 * ioc or q goes away. Each request with icq set holds an extra
68 * reference to ioc to ensure it stays until the request is completed.
69 *
70 * - Linking and unlinking icq's are performed while holding both ioc and q
71 * locks. Due to the lock ordering, q exit is simple but ioc exit
72 * requires reverse-order double lock dance.
73 */
c5869807
TH
74struct io_cq {
75 struct request_queue *q;
76 struct io_context *ioc;
fd0928df 77
7e5a8794
TH
78 /*
79 * q_node and ioc_node link io_cq through icq_list of q and ioc
80 * respectively. Both fields are unused once ioc_exit_icq() is
81 * called and shared with __rcu_icq_cache and __rcu_head which are
82 * used for RCU free of io_cq.
83 */
84 union {
85 struct list_head q_node;
86 struct kmem_cache *__rcu_icq_cache;
87 };
88 union {
89 struct hlist_node ioc_node;
90 struct rcu_head __rcu_head;
91 };
dc86900e 92
d705ae6b 93 unsigned int flags;
fd0928df
JA
94};
95
96/*
d38ecf93
JA
97 * I/O subsystem state of the associated processes. It is refcounted
98 * and kmalloc'ed. These could be shared between processes.
fd0928df
JA
99 */
100struct io_context {
d9c7d394 101 atomic_long_t refcount;
d38ecf93
JA
102 atomic_t nr_tasks;
103
104 /* all the fields below are protected by this lock */
105 spinlock_t lock;
fd0928df
JA
106
107 unsigned short ioprio;
31e4c28d 108
fd0928df
JA
109 /*
110 * For request batching
111 */
fd0928df 112 int nr_batch_requests; /* Number of requests left in the batch */
58c24a61 113 unsigned long last_waited; /* Time last woken after wait for request */
fd0928df 114
c5869807
TH
115 struct radix_tree_root icq_tree;
116 struct io_cq __rcu *icq_hint;
117 struct hlist_head icq_list;
b2efa052
TH
118
119 struct work_struct release_work;
fd0928df
JA
120};
121
d38ecf93
JA
122static inline struct io_context *ioc_task_link(struct io_context *ioc)
123{
124 /*
125 * if ref count is zero, don't allow sharing (ioc is going away, it's
126 * a race).
127 */
d9c7d394 128 if (ioc && atomic_long_inc_not_zero(&ioc->refcount)) {
cbb4f264 129 atomic_inc(&ioc->nr_tasks);
d38ecf93 130 return ioc;
d237e5c7 131 }
d38ecf93
JA
132
133 return NULL;
134}
135
b69f2292 136struct task_struct;
da9cbc87 137#ifdef CONFIG_BLOCK
11a3122f 138void put_io_context(struct io_context *ioc);
b69f2292 139void exit_io_context(struct task_struct *task);
6e736be7
TH
140struct io_context *get_task_io_context(struct task_struct *task,
141 gfp_t gfp_flags, int node);
dc86900e
TH
142void ioc_ioprio_changed(struct io_context *ioc, int ioprio);
143void ioc_cgroup_changed(struct io_context *ioc);
d705ae6b 144unsigned int icq_get_changed(struct io_cq *icq);
da9cbc87 145#else
da9cbc87 146struct io_context;
11a3122f 147static inline void put_io_context(struct io_context *ioc) { }
42ec57a8 148static inline void exit_io_context(struct task_struct *task) { }
da9cbc87
JA
149#endif
150
fd0928df 151#endif
This page took 1.75759 seconds and 5 git commands to generate.