1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/radix-tree.h>
6 #include <linux/rcupdate.h>
7 #include <linux/workqueue.h>
11 ICQ_DESTROYED
= 1 << 3,
15 * An io_cq (icq) is association between an io_context (ioc) and a
16 * request_queue (q). This is used by elevators which need to track
17 * information per ioc - q pair.
19 * Elevator can request use of icq by setting elevator_type->icq_size and
20 * ->icq_align. Both size and align must be larger than that of struct
21 * io_cq and elevator can use the tail area for private information. The
22 * recommended way to do this is defining a struct which contains io_cq as
23 * the first member followed by private members and using its size and
26 * struct snail_io_cq {
32 * struct elevator_type snail_elv_type {
34 * .icq_size = sizeof(struct snail_io_cq),
35 * .icq_align = __alignof__(struct snail_io_cq),
39 * If icq_size is set, block core will manage icq's. All requests will
40 * have its ->elv.icq field set before elevator_ops->elevator_set_req_fn()
41 * is called and be holding a reference to the associated io_context.
43 * Whenever a new icq is created, elevator_ops->elevator_init_icq_fn() is
44 * called and, on destruction, ->elevator_exit_icq_fn(). Both functions
45 * are called with both the associated io_context and queue locks held.
47 * Elevator is allowed to lookup icq using ioc_lookup_icq() while holding
48 * queue lock but the returned icq is valid only until the queue lock is
49 * released. Elevators can not and should not try to create or destroy
52 * As icq's are linked from both ioc and q, the locking rules are a bit
55 * - ioc lock nests inside q lock.
57 * - ioc->icq_list and icq->ioc_node are protected by ioc lock.
58 * q->icq_list and icq->q_node by q lock.
60 * - ioc->icq_tree and ioc->icq_hint are protected by ioc lock, while icq
61 * itself is protected by q lock. However, both the indexes and icq
62 * itself are also RCU managed and lookup can be performed holding only
65 * - icq's are not reference counted. They are destroyed when either the
66 * ioc or q goes away. Each request with icq set holds an extra
67 * reference to ioc to ensure it stays until the request is completed.
69 * - Linking and unlinking icq's are performed while holding both ioc and q
70 * locks. Due to the lock ordering, q exit is simple but ioc exit
71 * requires reverse-order double lock dance.
74 struct request_queue
*q
;
75 struct io_context
*ioc
;
78 * q_node and ioc_node link io_cq through icq_list of q and ioc
79 * respectively. Both fields are unused once ioc_exit_icq() is
80 * called and shared with __rcu_icq_cache and __rcu_head which are
81 * used for RCU free of io_cq.
84 struct list_head q_node
;
85 struct kmem_cache
*__rcu_icq_cache
;
88 struct hlist_node ioc_node
;
89 struct rcu_head __rcu_head
;
96 * I/O subsystem state of the associated processes. It is refcounted
97 * and kmalloc'ed. These could be shared between processes.
100 atomic_long_t refcount
;
103 unsigned short ioprio
;
105 #ifdef CONFIG_BLK_ICQ
106 /* all the fields below are protected by this lock */
109 struct radix_tree_root icq_tree
;
110 struct io_cq __rcu
*icq_hint
;
111 struct hlist_head icq_list
;
113 struct work_struct release_work
;
114 #endif /* CONFIG_BLK_ICQ */
119 void put_io_context(struct io_context
*ioc
);
120 void exit_io_context(struct task_struct
*task
);
121 int __copy_io(unsigned long clone_flags
, struct task_struct
*tsk
);
122 static inline int copy_io(unsigned long clone_flags
, struct task_struct
*tsk
)
124 if (!current
->io_context
)
126 return __copy_io(clone_flags
, tsk
);
130 static inline void put_io_context(struct io_context
*ioc
) { }
131 static inline void exit_io_context(struct task_struct
*task
) { }
132 static inline int copy_io(unsigned long clone_flags
, struct task_struct
*tsk
)
136 #endif /* CONFIG_BLOCK */
138 #endif /* IOCONTEXT_H */