Whamcloud - gitweb
LU-9679 general: avoid bare return; at end of void function
[fs/lustre-release.git] / libcfs / libcfs / workitem.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/workitem.c
33  *
34  * Author: Isaac Huang <isaac@clusterfs.com>
35  *         Liang Zhen  <zhen.liang@sun.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LNET
39
40 #include <linux/kthread.h>
41 #include <libcfs/libcfs.h>
42
43 #define CFS_WS_NAME_LEN         16
44
45 struct cfs_wi_sched {
46         struct list_head                ws_list;        /* chain on global list */
47         /** serialised workitems */
48         spinlock_t                      ws_lock;
49         /** where schedulers sleep */
50         wait_queue_head_t               ws_waitq;
51         /** concurrent workitems */
52         struct list_head                ws_runq;
53         /** rescheduled running-workitems, a workitem can be rescheduled
54          * while running in wi_action(), but we don't to execute it again
55          * unless it returns from wi_action(), so we put it on ws_rerunq
56          * while rescheduling, and move it to runq after it returns
57          * from wi_action() */
58         struct list_head                ws_rerunq;
59         /** CPT-table for this scheduler */
60         struct cfs_cpt_table    *ws_cptab;
61         /** CPT id for affinity */
62         int                     ws_cpt;
63         /** number of scheduled workitems */
64         int                     ws_nscheduled;
65         /** started scheduler thread, protected by cfs_wi_data::wi_glock */
66         unsigned int            ws_nthreads:30;
67         /** shutting down, protected by cfs_wi_data::wi_glock */
68         unsigned int            ws_stopping:1;
69         /** serialize starting thread, protected by cfs_wi_data::wi_glock */
70         unsigned int            ws_starting:1;
71         /** scheduler name */
72         char                    ws_name[CFS_WS_NAME_LEN];
73 };
74
75 static struct cfs_workitem_data {
76         /** serialize */
77         spinlock_t              wi_glock;
78         /** list of all schedulers */
79         struct list_head        wi_scheds;
80         /** WI module is initialized */
81         int                     wi_init;
82         /** shutting down the whole WI module */
83         int                     wi_stopping;
84 } cfs_wi_data;
85
86 static inline int
87 cfs_wi_sched_cansleep(struct cfs_wi_sched *sched)
88 {
89         spin_lock(&sched->ws_lock);
90         if (sched->ws_stopping) {
91                 spin_unlock(&sched->ws_lock);
92                 return 0;
93         }
94
95         if (!list_empty(&sched->ws_runq)) {
96                 spin_unlock(&sched->ws_lock);
97                 return 0;
98         }
99         spin_unlock(&sched->ws_lock);
100         return 1;
101 }
102
103 /* XXX:
104  * 0. it only works when called from wi->wi_action.
105  * 1. when it returns no one shall try to schedule the workitem.
106  */
107 void
108 cfs_wi_exit(struct cfs_wi_sched *sched, struct cfs_workitem *wi)
109 {
110         LASSERT(!in_interrupt()); /* because we use plain spinlock */
111         LASSERT(!sched->ws_stopping);
112
113         spin_lock(&sched->ws_lock);
114
115         LASSERT(wi->wi_running);
116
117         if (wi->wi_scheduled) { /* cancel pending schedules */
118                 LASSERT(!list_empty(&wi->wi_list));
119                 list_del_init(&wi->wi_list);
120
121                 LASSERT(sched->ws_nscheduled > 0);
122                 sched->ws_nscheduled--;
123         }
124
125         LASSERT(list_empty(&wi->wi_list));
126
127         wi->wi_scheduled = 1; /* LBUG future schedule attempts */
128         spin_unlock(&sched->ws_lock);
129 }
130 EXPORT_SYMBOL(cfs_wi_exit);
131
132 /**
133  * cancel schedule request of workitem \a wi
134  */
135 int
136 cfs_wi_deschedule(struct cfs_wi_sched *sched, struct cfs_workitem *wi)
137 {
138         int     rc;
139
140         LASSERT(!in_interrupt()); /* because we use plain spinlock */
141         LASSERT(!sched->ws_stopping);
142
143         /*
144          * return 0 if it's running already, otherwise return 1, which
145          * means the workitem will not be scheduled and will not have
146          * any race with wi_action.
147          */
148         spin_lock(&sched->ws_lock);
149
150         rc = !(wi->wi_running);
151
152         if (wi->wi_scheduled) { /* cancel pending schedules */
153                 LASSERT(!list_empty(&wi->wi_list));
154                 list_del_init(&wi->wi_list);
155
156                 LASSERT(sched->ws_nscheduled > 0);
157                 sched->ws_nscheduled--;
158
159                 wi->wi_scheduled = 0;
160         }
161
162         LASSERT (list_empty(&wi->wi_list));
163
164         spin_unlock(&sched->ws_lock);
165         return rc;
166 }
167 EXPORT_SYMBOL(cfs_wi_deschedule);
168
169 /*
170  * Workitem scheduled with (serial == 1) is strictly serialised not only with
171  * itself, but also with others scheduled this way.
172  *
173  * Now there's only one static serialised queue, but in the future more might
174  * be added, and even dynamic creation of serialised queues might be supported.
175  */
176 void
177 cfs_wi_schedule(struct cfs_wi_sched *sched, struct cfs_workitem *wi)
178 {
179         LASSERT(!in_interrupt()); /* because we use plain spinlock */
180         LASSERT(!sched->ws_stopping);
181
182         spin_lock(&sched->ws_lock);
183
184         if (!wi->wi_scheduled) {
185                 LASSERT (list_empty(&wi->wi_list));
186
187                 wi->wi_scheduled = 1;
188                 sched->ws_nscheduled++;
189                 if (!wi->wi_running) {
190                         list_add_tail(&wi->wi_list, &sched->ws_runq);
191                         wake_up(&sched->ws_waitq);
192                 } else {
193                         list_add(&wi->wi_list, &sched->ws_rerunq);
194                 }
195         }
196
197         LASSERT (!list_empty(&wi->wi_list));
198         spin_unlock(&sched->ws_lock);
199 }
200 EXPORT_SYMBOL(cfs_wi_schedule);
201
202 static int
203 cfs_wi_scheduler(void *arg)
204 {
205         struct cfs_wi_sched *sched = (struct cfs_wi_sched *)arg;
206
207         cfs_block_allsigs();
208
209         /* CPT affinity scheduler? */
210         if (sched->ws_cptab != NULL)
211                 if (cfs_cpt_bind(sched->ws_cptab, sched->ws_cpt) != 0)
212                         CWARN("Unable to bind %s on CPU partition %d\n",
213                                 sched->ws_name, sched->ws_cpt);
214
215         spin_lock(&cfs_wi_data.wi_glock);
216
217         LASSERT(sched->ws_starting == 1);
218         sched->ws_starting--;
219         sched->ws_nthreads++;
220
221         spin_unlock(&cfs_wi_data.wi_glock);
222
223         spin_lock(&sched->ws_lock);
224
225         while (!sched->ws_stopping) {
226                 int             nloops = 0;
227                 int             rc;
228                 struct cfs_workitem *wi;
229
230                 while (!list_empty(&sched->ws_runq) &&
231                        nloops < CFS_WI_RESCHED) {
232                         wi = list_entry(sched->ws_runq.next,
233                                         struct cfs_workitem, wi_list);
234                         LASSERT(wi->wi_scheduled && !wi->wi_running);
235
236                         list_del_init(&wi->wi_list);
237
238                         LASSERT(sched->ws_nscheduled > 0);
239                         sched->ws_nscheduled--;
240
241                         wi->wi_running   = 1;
242                         wi->wi_scheduled = 0;
243
244                         spin_unlock(&sched->ws_lock);
245                         nloops++;
246
247                         rc = (*wi->wi_action) (wi);
248
249                         spin_lock(&sched->ws_lock);
250                         if (rc != 0) /* WI should be dead, even be freed! */
251                                 continue;
252
253                         wi->wi_running = 0;
254                         if (list_empty(&wi->wi_list))
255                                 continue;
256
257                         LASSERT(wi->wi_scheduled);
258                         /* wi is rescheduled, should be on rerunq now, we
259                          * move it to runq so it can run action now */
260                         list_move_tail(&wi->wi_list, &sched->ws_runq);
261                 }
262
263                 if (!list_empty(&sched->ws_runq)) {
264                         spin_unlock(&sched->ws_lock);
265                         /* don't sleep because some workitems still
266                          * expect me to come back soon */
267                         cond_resched();
268                         spin_lock(&sched->ws_lock);
269                         continue;
270                 }
271
272                 spin_unlock(&sched->ws_lock);
273                 rc = wait_event_interruptible_exclusive(sched->ws_waitq,
274                                 !cfs_wi_sched_cansleep(sched));
275                 spin_lock(&sched->ws_lock);
276         }
277
278         spin_unlock(&sched->ws_lock);
279
280         spin_lock(&cfs_wi_data.wi_glock);
281         sched->ws_nthreads--;
282         spin_unlock(&cfs_wi_data.wi_glock);
283
284         return 0;
285 }
286
287 void
288 cfs_wi_sched_destroy(struct cfs_wi_sched *sched)
289 {
290         LASSERT(cfs_wi_data.wi_init);
291         LASSERT(!cfs_wi_data.wi_stopping);
292
293         spin_lock(&cfs_wi_data.wi_glock);
294         if (sched->ws_stopping) {
295                 CDEBUG(D_INFO, "%s is in progress of stopping\n",
296                        sched->ws_name);
297                 spin_unlock(&cfs_wi_data.wi_glock);
298                 return;
299         }
300
301         LASSERT(!list_empty(&sched->ws_list));
302         sched->ws_stopping = 1;
303
304         spin_unlock(&cfs_wi_data.wi_glock);
305
306         wake_up_all(&sched->ws_waitq);
307
308         spin_lock(&cfs_wi_data.wi_glock);
309         {
310                 int i = 2;
311
312                 while (sched->ws_nthreads > 0) {
313                         CDEBUG(is_power_of_2(++i / 20) ? D_WARNING : D_NET,
314                                "waiting %us for %d %s worker threads to exit\n",
315                                i / 20, sched->ws_nthreads, sched->ws_name);
316
317                         spin_unlock(&cfs_wi_data.wi_glock);
318                         set_current_state(TASK_UNINTERRUPTIBLE);
319                         schedule_timeout(cfs_time_seconds(1) / 20);
320                         spin_lock(&cfs_wi_data.wi_glock);
321                 }
322         }
323
324         list_del(&sched->ws_list);
325
326         spin_unlock(&cfs_wi_data.wi_glock);
327
328         LASSERT(sched->ws_nscheduled == 0);
329
330         LIBCFS_FREE(sched, sizeof(*sched));
331 }
332 EXPORT_SYMBOL(cfs_wi_sched_destroy);
333
334 int
335 cfs_wi_sched_create(char *name, struct cfs_cpt_table *cptab,
336                     int cpt, int nthrs, struct cfs_wi_sched **sched_pp)
337 {
338         struct cfs_wi_sched     *sched;
339
340         LASSERT(cfs_wi_data.wi_init);
341         LASSERT(!cfs_wi_data.wi_stopping);
342         LASSERT(cptab == NULL || cpt == CFS_CPT_ANY ||
343                 (cpt >= 0 && cpt < cfs_cpt_number(cptab)));
344
345         LIBCFS_ALLOC(sched, sizeof(*sched));
346         if (sched == NULL)
347                 return -ENOMEM;
348
349         if (strlen(name) > sizeof(sched->ws_name)-1) {
350                 LIBCFS_FREE(sched, sizeof(*sched));
351                 return -E2BIG;
352         }
353         strlcpy(sched->ws_name, name, sizeof(sched->ws_name));
354
355         sched->ws_cptab = cptab;
356         sched->ws_cpt = cpt;
357
358         spin_lock_init(&sched->ws_lock);
359         init_waitqueue_head(&sched->ws_waitq);
360
361         INIT_LIST_HEAD(&sched->ws_runq);
362         INIT_LIST_HEAD(&sched->ws_rerunq);
363         INIT_LIST_HEAD(&sched->ws_list);
364
365         for (; nthrs > 0; nthrs--)  {
366                 char                    name[16];
367                 struct task_struct      *task;
368
369                 spin_lock(&cfs_wi_data.wi_glock);
370                 while (sched->ws_starting > 0) {
371                         spin_unlock(&cfs_wi_data.wi_glock);
372                         schedule();
373                         spin_lock(&cfs_wi_data.wi_glock);
374                 }
375
376                 sched->ws_starting++;
377                 spin_unlock(&cfs_wi_data.wi_glock);
378
379                 if (sched->ws_cptab != NULL && sched->ws_cpt >= 0) {
380                         snprintf(name, sizeof(name), "%s_%02d_%02d",
381                                  sched->ws_name, sched->ws_cpt,
382                                  sched->ws_nthreads);
383                 } else {
384                         snprintf(name, sizeof(name), "%s_%02d",
385                                  sched->ws_name, sched->ws_nthreads);
386                 }
387
388                 task = kthread_run(cfs_wi_scheduler, sched, name);
389                 if (IS_ERR(task)) {
390                         int rc = PTR_ERR(task);
391
392                         CERROR("Failed to create thread for "
393                                 "WI scheduler %s: %d\n", name, rc);
394
395                         spin_lock(&cfs_wi_data.wi_glock);
396
397                         /* make up for cfs_wi_sched_destroy */
398                         list_add(&sched->ws_list, &cfs_wi_data.wi_scheds);
399                         sched->ws_starting--;
400
401                         spin_unlock(&cfs_wi_data.wi_glock);
402
403                         cfs_wi_sched_destroy(sched);
404                         return rc;
405                 }
406         }
407
408         spin_lock(&cfs_wi_data.wi_glock);
409         list_add(&sched->ws_list, &cfs_wi_data.wi_scheds);
410         spin_unlock(&cfs_wi_data.wi_glock);
411
412         *sched_pp = sched;
413         return 0;
414 }
415 EXPORT_SYMBOL(cfs_wi_sched_create);
416
417 int
418 cfs_wi_startup(void)
419 {
420         memset(&cfs_wi_data, 0, sizeof(struct cfs_workitem_data));
421
422         spin_lock_init(&cfs_wi_data.wi_glock);
423         INIT_LIST_HEAD(&cfs_wi_data.wi_scheds);
424         cfs_wi_data.wi_init = 1;
425
426         return 0;
427 }
428
429 void
430 cfs_wi_shutdown (void)
431 {
432         struct cfs_wi_sched     *sched;
433
434         spin_lock(&cfs_wi_data.wi_glock);
435         cfs_wi_data.wi_stopping = 1;
436         spin_unlock(&cfs_wi_data.wi_glock);
437
438         /* nobody should contend on this list */
439         list_for_each_entry(sched, &cfs_wi_data.wi_scheds, ws_list) {
440                 sched->ws_stopping = 1;
441                 wake_up_all(&sched->ws_waitq);
442         }
443
444         list_for_each_entry(sched, &cfs_wi_data.wi_scheds, ws_list) {
445                 spin_lock(&cfs_wi_data.wi_glock);
446
447                 while (sched->ws_nthreads != 0) {
448                         spin_unlock(&cfs_wi_data.wi_glock);
449                         set_current_state(TASK_UNINTERRUPTIBLE);
450                         schedule_timeout(cfs_time_seconds(1) / 20);
451                         spin_lock(&cfs_wi_data.wi_glock);
452                 }
453                 spin_unlock(&cfs_wi_data.wi_glock);
454         }
455
456         while (!list_empty(&cfs_wi_data.wi_scheds)) {
457                 sched = list_entry(cfs_wi_data.wi_scheds.next,
458                                        struct cfs_wi_sched, ws_list);
459                 list_del(&sched->ws_list);
460                 LIBCFS_FREE(sched, sizeof(*sched));
461         }
462
463         cfs_wi_data.wi_stopping = 0;
464         cfs_wi_data.wi_init = 0;
465 }