1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/include/linux/sunrpc/sched.h
4  *
5  * Scheduling primitives for kernel Sun RPC.
6  *
7  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 #ifndef _LINUX_SUNRPC_SCHED_H_
11 #define _LINUX_SUNRPC_SCHED_H_
12 
13 #include <linux/timer.h>
14 #include <linux/ktime.h>
15 #include <linux/sunrpc/types.h>
16 #include <linux/spinlock.h>
17 #include <linux/wait_bit.h>
18 #include <linux/workqueue.h>
19 #include <linux/sunrpc/xdr.h>
20 
21 /*
22  * This is the actual RPC procedure call info.
23  */
24 struct rpc_procinfo;
25 struct rpc_message {
26 	const struct rpc_procinfo *rpc_proc;	/* Procedure information */
27 	void *			rpc_argp;	/* Arguments */
28 	void *			rpc_resp;	/* Result */
29 	struct rpc_cred *	rpc_cred;	/* Credentials */
30 };
31 
32 struct rpc_call_ops;
33 struct rpc_wait_queue;
34 struct rpc_wait {
35 	struct list_head	list;		/* wait queue links */
36 	struct list_head	links;		/* Links to related tasks */
37 	struct list_head	timer_list;	/* Timer list */
38 	unsigned long		expires;
39 };
40 
41 /*
42  * This is the RPC task struct
43  */
44 struct rpc_task {
45 	atomic_t		tk_count;	/* Reference count */
46 	int			tk_status;	/* result of last operation */
47 	struct list_head	tk_task;	/* global list of tasks */
48 
49 	/*
50 	 * callback	to be executed after waking up
51 	 * action	next procedure for async tasks
52 	 */
53 	void			(*tk_callback)(struct rpc_task *);
54 	void			(*tk_action)(struct rpc_task *);
55 
56 	unsigned long		tk_timeout;	/* timeout for rpc_sleep() */
57 	unsigned long		tk_runstate;	/* Task run status */
58 
59 	struct rpc_wait_queue 	*tk_waitqueue;	/* RPC wait queue we're on */
60 	union {
61 		struct work_struct	tk_work;	/* Async task work queue */
62 		struct rpc_wait		tk_wait;	/* RPC wait */
63 	} u;
64 
65 	/*
66 	 * RPC call state
67 	 */
68 	struct rpc_message	tk_msg;		/* RPC call info */
69 	void *			tk_calldata;	/* Caller private data */
70 	const struct rpc_call_ops *tk_ops;	/* Caller callbacks */
71 
72 	struct rpc_clnt *	tk_client;	/* RPC client */
73 	struct rpc_xprt *	tk_xprt;	/* Transport */
74 
75 	struct rpc_rqst *	tk_rqstp;	/* RPC request */
76 
77 	struct workqueue_struct	*tk_workqueue;	/* Normally rpciod, but could
78 						 * be any workqueue
79 						 */
80 	ktime_t			tk_start;	/* RPC task init timestamp */
81 
82 	pid_t			tk_owner;	/* Process id for batching tasks */
83 	unsigned short		tk_flags;	/* misc flags */
84 	unsigned short		tk_timeouts;	/* maj timeouts */
85 
86 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
87 	unsigned short		tk_pid;		/* debugging aid */
88 #endif
89 	unsigned char		tk_priority : 2,/* Task priority */
90 				tk_garb_retry : 2,
91 				tk_cred_retry : 2,
92 				tk_rebind_retry : 2;
93 };
94 
95 typedef void			(*rpc_action)(struct rpc_task *);
96 
97 struct rpc_call_ops {
98 	void (*rpc_call_prepare)(struct rpc_task *, void *);
99 	void (*rpc_call_done)(struct rpc_task *, void *);
100 	void (*rpc_count_stats)(struct rpc_task *, void *);
101 	void (*rpc_release)(void *);
102 };
103 
104 struct rpc_task_setup {
105 	struct rpc_task *task;
106 	struct rpc_clnt *rpc_client;
107 	struct rpc_xprt *rpc_xprt;
108 	const struct rpc_message *rpc_message;
109 	const struct rpc_call_ops *callback_ops;
110 	void *callback_data;
111 	struct workqueue_struct *workqueue;
112 	unsigned short flags;
113 	signed char priority;
114 };
115 
116 /*
117  * RPC task flags
118  */
119 #define RPC_TASK_ASYNC		0x0001		/* is an async task */
120 #define RPC_TASK_SWAPPER	0x0002		/* is swapping in/out */
121 #define RPC_CALL_MAJORSEEN	0x0020		/* major timeout seen */
122 #define RPC_TASK_ROOTCREDS	0x0040		/* force root creds */
123 #define RPC_TASK_DYNAMIC	0x0080		/* task was kmalloc'ed */
124 #define RPC_TASK_KILLED		0x0100		/* task was killed */
125 #define RPC_TASK_SOFT		0x0200		/* Use soft timeouts */
126 #define RPC_TASK_SOFTCONN	0x0400		/* Fail if can't connect */
127 #define RPC_TASK_SENT		0x0800		/* message was sent */
128 #define RPC_TASK_TIMEOUT	0x1000		/* fail with ETIMEDOUT on timeout */
129 #define RPC_TASK_NOCONNECT	0x2000		/* return ENOTCONN if not connected */
130 #define RPC_TASK_NO_RETRANS_TIMEOUT	0x4000		/* wait forever for a reply */
131 
132 #define RPC_IS_ASYNC(t)		((t)->tk_flags & RPC_TASK_ASYNC)
133 #define RPC_IS_SWAPPER(t)	((t)->tk_flags & RPC_TASK_SWAPPER)
134 #define RPC_DO_ROOTOVERRIDE(t)	((t)->tk_flags & RPC_TASK_ROOTCREDS)
135 #define RPC_ASSASSINATED(t)	((t)->tk_flags & RPC_TASK_KILLED)
136 #define RPC_IS_SOFT(t)		((t)->tk_flags & (RPC_TASK_SOFT|RPC_TASK_TIMEOUT))
137 #define RPC_IS_SOFTCONN(t)	((t)->tk_flags & RPC_TASK_SOFTCONN)
138 #define RPC_WAS_SENT(t)		((t)->tk_flags & RPC_TASK_SENT)
139 
140 #define RPC_TASK_RUNNING	0
141 #define RPC_TASK_QUEUED		1
142 #define RPC_TASK_ACTIVE		2
143 #define RPC_TASK_MSG_RECV	3
144 #define RPC_TASK_MSG_RECV_WAIT	4
145 
146 #define RPC_IS_RUNNING(t)	test_bit(RPC_TASK_RUNNING, &(t)->tk_runstate)
147 #define rpc_set_running(t)	set_bit(RPC_TASK_RUNNING, &(t)->tk_runstate)
148 #define rpc_test_and_set_running(t) \
149 				test_and_set_bit(RPC_TASK_RUNNING, &(t)->tk_runstate)
150 #define rpc_clear_running(t)	\
151 	do { \
152 		smp_mb__before_atomic(); \
153 		clear_bit(RPC_TASK_RUNNING, &(t)->tk_runstate); \
154 		smp_mb__after_atomic(); \
155 	} while (0)
156 
157 #define RPC_IS_QUEUED(t)	test_bit(RPC_TASK_QUEUED, &(t)->tk_runstate)
158 #define rpc_set_queued(t)	set_bit(RPC_TASK_QUEUED, &(t)->tk_runstate)
159 #define rpc_clear_queued(t)	\
160 	do { \
161 		smp_mb__before_atomic(); \
162 		clear_bit(RPC_TASK_QUEUED, &(t)->tk_runstate); \
163 		smp_mb__after_atomic(); \
164 	} while (0)
165 
166 #define RPC_IS_ACTIVATED(t)	test_bit(RPC_TASK_ACTIVE, &(t)->tk_runstate)
167 
168 /*
169  * Task priorities.
170  * Note: if you change these, you must also change
171  * the task initialization definitions below.
172  */
173 #define RPC_PRIORITY_LOW	(-1)
174 #define RPC_PRIORITY_NORMAL	(0)
175 #define RPC_PRIORITY_HIGH	(1)
176 #define RPC_PRIORITY_PRIVILEGED	(2)
177 #define RPC_NR_PRIORITY		(1 + RPC_PRIORITY_PRIVILEGED - RPC_PRIORITY_LOW)
178 
179 struct rpc_timer {
180 	struct timer_list timer;
181 	struct list_head list;
182 	unsigned long expires;
183 };
184 
185 /*
186  * RPC synchronization objects
187  */
188 struct rpc_wait_queue {
189 	spinlock_t		lock;
190 	struct list_head	tasks[RPC_NR_PRIORITY];	/* task queue for each priority level */
191 	pid_t			owner;			/* process id of last task serviced */
192 	unsigned char		maxpriority;		/* maximum priority (0 if queue is not a priority queue) */
193 	unsigned char		priority;		/* current priority */
194 	unsigned char		nr;			/* # tasks remaining for cookie */
195 	unsigned short		qlen;			/* total # tasks waiting in queue */
196 	struct rpc_timer	timer_list;
197 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
198 	const char *		name;
199 #endif
200 };
201 
202 /*
203  * This is the # requests to send consecutively
204  * from a single cookie.  The aim is to improve
205  * performance of NFS operations such as read/write.
206  */
207 #define RPC_BATCH_COUNT			16
208 #define RPC_IS_PRIORITY(q)		((q)->maxpriority > 0)
209 
210 /*
211  * Function prototypes
212  */
213 struct rpc_task *rpc_new_task(const struct rpc_task_setup *);
214 struct rpc_task *rpc_run_task(const struct rpc_task_setup *);
215 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req);
216 void		rpc_put_task(struct rpc_task *);
217 void		rpc_put_task_async(struct rpc_task *);
218 void		rpc_exit_task(struct rpc_task *);
219 void		rpc_exit(struct rpc_task *, int);
220 void		rpc_release_calldata(const struct rpc_call_ops *, void *);
221 void		rpc_killall_tasks(struct rpc_clnt *);
222 void		rpc_execute(struct rpc_task *);
223 void		rpc_init_priority_wait_queue(struct rpc_wait_queue *, const char *);
224 void		rpc_init_wait_queue(struct rpc_wait_queue *, const char *);
225 void		rpc_destroy_wait_queue(struct rpc_wait_queue *);
226 void		rpc_sleep_on(struct rpc_wait_queue *, struct rpc_task *,
227 					rpc_action action);
228 void		rpc_sleep_on_priority(struct rpc_wait_queue *,
229 					struct rpc_task *,
230 					rpc_action action,
231 					int priority);
232 void rpc_wake_up_queued_task_on_wq(struct workqueue_struct *wq,
233 		struct rpc_wait_queue *queue,
234 		struct rpc_task *task);
235 void		rpc_wake_up_queued_task(struct rpc_wait_queue *,
236 					struct rpc_task *);
237 void		rpc_wake_up(struct rpc_wait_queue *);
238 struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *);
239 struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq,
240 					struct rpc_wait_queue *,
241 					bool (*)(struct rpc_task *, void *),
242 					void *);
243 struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *,
244 					bool (*)(struct rpc_task *, void *),
245 					void *);
246 void		rpc_wake_up_status(struct rpc_wait_queue *, int);
247 void		rpc_delay(struct rpc_task *, unsigned long);
248 int		rpc_malloc(struct rpc_task *);
249 void		rpc_free(struct rpc_task *);
250 int		rpciod_up(void);
251 void		rpciod_down(void);
252 int		__rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *);
253 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
254 struct net;
255 void		rpc_show_tasks(struct net *);
256 #endif
257 int		rpc_init_mempool(void);
258 void		rpc_destroy_mempool(void);
259 extern struct workqueue_struct *rpciod_workqueue;
260 extern struct workqueue_struct *xprtiod_workqueue;
261 void		rpc_prepare_task(struct rpc_task *task);
262 
rpc_wait_for_completion_task(struct rpc_task * task)263 static inline int rpc_wait_for_completion_task(struct rpc_task *task)
264 {
265 	return __rpc_wait_for_completion_task(task, NULL);
266 }
267 
268 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
rpc_qname(const struct rpc_wait_queue * q)269 static inline const char * rpc_qname(const struct rpc_wait_queue *q)
270 {
271 	return ((q && q->name) ? q->name : "unknown");
272 }
273 
rpc_assign_waitqueue_name(struct rpc_wait_queue * q,const char * name)274 static inline void rpc_assign_waitqueue_name(struct rpc_wait_queue *q,
275 		const char *name)
276 {
277 	q->name = name;
278 }
279 #else
rpc_assign_waitqueue_name(struct rpc_wait_queue * q,const char * name)280 static inline void rpc_assign_waitqueue_name(struct rpc_wait_queue *q,
281 		const char *name)
282 {
283 }
284 #endif
285 
286 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
287 int rpc_clnt_swap_activate(struct rpc_clnt *clnt);
288 void rpc_clnt_swap_deactivate(struct rpc_clnt *clnt);
289 #else
290 static inline int
rpc_clnt_swap_activate(struct rpc_clnt * clnt)291 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
292 {
293 	return -EINVAL;
294 }
295 
296 static inline void
rpc_clnt_swap_deactivate(struct rpc_clnt * clnt)297 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
298 {
299 }
300 #endif /* CONFIG_SUNRPC_SWAP */
301 
302 #endif /* _LINUX_SUNRPC_SCHED_H_ */
303