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