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