xref: /linux-6.15/include/linux/iocontext.h (revision 283287a5)
1 #ifndef IOCONTEXT_H
2 #define IOCONTEXT_H
3 
4 #include <linux/radix-tree.h>
5 #include <linux/rcupdate.h>
6 
7 struct cfq_queue;
8 struct cfq_ttime {
9 	unsigned long last_end_request;
10 
11 	unsigned long ttime_total;
12 	unsigned long ttime_samples;
13 	unsigned long ttime_mean;
14 };
15 
16 struct cfq_io_context {
17 	void *key;
18 	struct request_queue *q;
19 
20 	struct cfq_queue *cfqq[2];
21 
22 	struct io_context *ioc;
23 
24 	struct cfq_ttime ttime;
25 
26 	struct list_head queue_list;
27 	struct hlist_node cic_list;
28 
29 	void (*dtor)(struct io_context *); /* destructor */
30 	void (*exit)(struct io_context *); /* called on task exit */
31 
32 	struct rcu_head rcu_head;
33 };
34 
35 /*
36  * I/O subsystem state of the associated processes.  It is refcounted
37  * and kmalloc'ed. These could be shared between processes.
38  */
39 struct io_context {
40 	atomic_long_t refcount;
41 	atomic_t nr_tasks;
42 
43 	/* all the fields below are protected by this lock */
44 	spinlock_t lock;
45 
46 	unsigned short ioprio;
47 	unsigned short ioprio_changed;
48 
49 #if defined(CONFIG_BLK_CGROUP) || defined(CONFIG_BLK_CGROUP_MODULE)
50 	unsigned short cgroup_changed;
51 #endif
52 
53 	/*
54 	 * For request batching
55 	 */
56 	int nr_batch_requests;     /* Number of requests left in the batch */
57 	unsigned long last_waited; /* Time last woken after wait for request */
58 
59 	struct radix_tree_root radix_root;
60 	struct hlist_head cic_list;
61 	void __rcu *ioc_data;
62 };
63 
64 static inline struct io_context *ioc_task_link(struct io_context *ioc)
65 {
66 	/*
67 	 * if ref count is zero, don't allow sharing (ioc is going away, it's
68 	 * a race).
69 	 */
70 	if (ioc && atomic_long_inc_not_zero(&ioc->refcount)) {
71 		atomic_inc(&ioc->nr_tasks);
72 		return ioc;
73 	}
74 
75 	return NULL;
76 }
77 
78 struct task_struct;
79 #ifdef CONFIG_BLOCK
80 void put_io_context(struct io_context *ioc);
81 void exit_io_context(struct task_struct *task);
82 struct io_context *get_task_io_context(struct task_struct *task,
83 				       gfp_t gfp_flags, int node);
84 #else
85 struct io_context;
86 static inline void put_io_context(struct io_context *ioc) { }
87 static inline void exit_io_context(struct task_struct *task) { }
88 #endif
89 
90 #endif
91