1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/*-
* Copyright (c) 2010-2020 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This material is based upon work partially supported by The
* NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_worker.c,v 1.10 2020/08/27 18:49:36 riastradh Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mutex.h>
#include <sys/kmem.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/cprng.h>
#endif
#include "npf_impl.h"
typedef struct npf_worker {
kmutex_t lock;
kcondvar_t cv;
kcondvar_t exit_cv;
bool exit;
LIST_HEAD(, npf) instances;
unsigned worker_count;
lwp_t * worker[];
} npf_workerinfo_t;
#define NPF_GC_MINWAIT (10) // 10 ms
#define NPF_GC_MAXWAIT (10 * 1000) // 10 sec
/*
* Flags for the npf_t::worker_flags field.
*/
#define WFLAG_ACTIVE 0x01 // instance enqueued for workers
#define WFLAG_INITED 0x02 // worker setup the instance
#define WFLAG_REMOVE 0x04 // remove the instance
static void npf_worker(void *) __dead;
static npf_workerinfo_t * worker_info __read_mostly;
int
npf_worker_sysinit(unsigned nworkers)
{
const size_t len = offsetof(npf_workerinfo_t, worker[nworkers]);
npf_workerinfo_t *winfo;
KASSERT(worker_info == NULL);
if (!nworkers) {
return 0;
}
winfo = kmem_zalloc(len, KM_SLEEP);
winfo->worker_count = nworkers;
mutex_init(&winfo->lock, MUTEX_DEFAULT, IPL_SOFTNET);
cv_init(&winfo->exit_cv, "npfgcx");
cv_init(&winfo->cv, "npfgcw");
LIST_INIT(&winfo->instances);
worker_info = winfo;
for (unsigned i = 0; i < nworkers; i++) {
if (kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN,
NULL, npf_worker, winfo, &winfo->worker[i], "npfgc%u", i)) {
npf_worker_sysfini();
return ENOMEM;
}
}
return 0;
}
void
npf_worker_sysfini(void)
{
npf_workerinfo_t *winfo = worker_info;
unsigned nworkers;
if (!winfo) {
return;
}
/* Notify the workers to exit. */
mutex_enter(&winfo->lock);
winfo->exit = true;
cv_broadcast(&winfo->cv);
mutex_exit(&winfo->lock);
/* Wait for them to finish and then destroy. */
nworkers = winfo->worker_count;
for (unsigned i = 0; i < nworkers; i++) {
lwp_t *worker;
if ((worker = winfo->worker[i]) != NULL) {
kthread_join(worker);
}
}
cv_destroy(&winfo->cv);
cv_destroy(&winfo->exit_cv);
mutex_destroy(&winfo->lock);
kmem_free(winfo, offsetof(npf_workerinfo_t, worker[nworkers]));
worker_info = NULL;
}
int
npf_worker_addfunc(npf_t *npf, npf_workfunc_t work)
{
KASSERTMSG(npf->worker_flags == 0,
"the task must be added before the npf_worker_enlist() call");
for (unsigned i = 0; i < NPF_MAX_WORKS; i++) {
if (npf->worker_funcs[i] == NULL) {
npf->worker_funcs[i] = work;
return 0;
}
}
return -1;
}
void
npf_worker_signal(npf_t *npf)
{
npf_workerinfo_t *winfo = worker_info;
if ((npf->worker_flags & WFLAG_ACTIVE) == 0) {
return;
}
KASSERT(winfo != NULL);
mutex_enter(&winfo->lock);
cv_signal(&winfo->cv);
mutex_exit(&winfo->lock);
}
/*
* npf_worker_enlist: add the NPF instance for worker(s) to process.
*/
void
npf_worker_enlist(npf_t *npf)
{
npf_workerinfo_t *winfo = worker_info;
KASSERT(npf->worker_flags == 0);
if (!winfo) {
return;
}
mutex_enter(&winfo->lock);
LIST_INSERT_HEAD(&winfo->instances, npf, worker_entry);
npf->worker_flags |= WFLAG_ACTIVE;
mutex_exit(&winfo->lock);
}
/*
* npf_worker_discharge: remove the NPF instance the list for workers.
*
* => May block waiting for a worker to finish processing the instance.
*/
void
npf_worker_discharge(npf_t *npf)
{
npf_workerinfo_t *winfo = worker_info;
if ((npf->worker_flags & WFLAG_ACTIVE) == 0) {
return;
}
KASSERT(winfo != NULL);
/*
* Notify the worker(s) that we are removing this instance.
*/
mutex_enter(&winfo->lock);
KASSERT(npf->worker_flags & WFLAG_ACTIVE);
npf->worker_flags |= WFLAG_REMOVE;
cv_broadcast(&winfo->cv);
/* Wait for a worker to process this request. */
while (npf->worker_flags & WFLAG_ACTIVE) {
cv_wait(&winfo->exit_cv, &winfo->lock);
}
mutex_exit(&winfo->lock);
KASSERT(npf->worker_flags == 0);
}
static void
remove_npf_instance(npf_workerinfo_t *winfo, npf_t *npf)
{
KASSERT(mutex_owned(&winfo->lock));
KASSERT(npf->worker_flags & WFLAG_ACTIVE);
KASSERT(npf->worker_flags & WFLAG_REMOVE);
/*
* Remove the NPF instance:
* - Release any structures owned by the worker.
* - Remove the instance from the list.
* - Notify any thread waiting for removal to complete.
*/
if (npf->worker_flags & WFLAG_INITED) {
npfk_thread_unregister(npf);
}
LIST_REMOVE(npf, worker_entry);
npf->worker_flags = 0;
cv_broadcast(&winfo->exit_cv);
}
static unsigned
process_npf_instance(npf_workerinfo_t *winfo, npf_t *npf)
{
npf_workfunc_t work;
KASSERT(mutex_owned(&winfo->lock));
if (npf->worker_flags & WFLAG_REMOVE) {
remove_npf_instance(winfo, npf);
return NPF_GC_MAXWAIT;
}
if ((npf->worker_flags & WFLAG_INITED) == 0) {
npfk_thread_register(npf);
npf->worker_flags |= WFLAG_INITED;
}
/* Run the jobs. */
for (unsigned i = 0; i < NPF_MAX_WORKS; i++) {
if ((work = npf->worker_funcs[i]) == NULL) {
break;
}
work(npf);
}
return MAX(MIN(npf->worker_wait_time, NPF_GC_MAXWAIT), NPF_GC_MINWAIT);
}
/*
* npf_worker: the main worker loop, processing enlisted NPF instances.
*
* XXX: Currently, npf_workerinfo_t::lock would serialize all workers,
* so there is no point to have more than one worker; but there might
* not be much point anyway.
*/
static void
npf_worker(void *arg)
{
npf_workerinfo_t *winfo = arg;
npf_t *npf;
mutex_enter(&winfo->lock);
for (;;) {
unsigned wait_time = NPF_GC_MAXWAIT;
/*
* Iterate all instances. We do not use LIST_FOREACH here,
* since the instance can be removed.
*/
npf = LIST_FIRST(&winfo->instances);
while (npf) {
npf_t *next = LIST_NEXT(npf, worker_entry);
unsigned i_wait_time = process_npf_instance(winfo, npf);
wait_time = MIN(wait_time, i_wait_time);
npf = next;
}
/*
* Sleep and periodically wake up, unless we get notified.
*/
if (winfo->exit) {
break;
}
cv_timedwait(&winfo->cv, &winfo->lock, mstohz(wait_time));
}
mutex_exit(&winfo->lock);
KASSERTMSG(LIST_EMPTY(&winfo->instances),
"NPF instances must be discharged before the npfk_sysfini() call");
kthread_exit(0);
}
|