wkq.c 8.79 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
3
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This program, aufs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
J. R. Okajima's avatar
J. R. Okajima committed
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
 */

/*
 * workqueue for asynchronous/super-io operations
 * todo: try new credential scheme
 */

#include <linux/module.h>
#include "aufs.h"

/* internal workqueue named AUFS_WKQ_NAME */

static struct workqueue_struct *au_wkq;

struct au_wkinfo {
	struct work_struct wk;
	struct kobject *kobj;

	unsigned int flags; /* see wkq.h */

	au_wkq_func_t func;
	void *args;

#ifdef CONFIG_LOCKDEP
	int dont_check;
	struct held_lock **hlock;
#endif

	struct completion *comp;
};

/* ---------------------------------------------------------------------- */
/*
 * Aufs passes some operations to the workqueue such as the internal copyup.
 * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
 * job run by workqueue depends upon the locks acquired in the other task.
 * Delegating a small operation to the workqueue, aufs passes its lockdep
 * information too. And the job in the workqueue restores the info in order to
 * pretend as if it acquired those locks. This is just to make LOCKDEP work
 * correctly and expectedly.
 */

#ifndef CONFIG_LOCKDEP
AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
#else
static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
{
	wkinfo->hlock = NULL;
	wkinfo->dont_check = 0;
}

/*
 * 1: matched
 * 0: unmatched
 */
static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
{
	static DEFINE_SPINLOCK(spin);
	static struct {
		char *name;
		struct lock_class_key *key;
	} a[] = {
		{ .name = "&sbinfo->si_rwsem" },
		{ .name = "&finfo->fi_rwsem" },
		{ .name = "&dinfo->di_rwsem" },
		{ .name = "&iinfo->ii_rwsem" }
	};
	static int set;
	int i;

	/* lockless read from 'set.' see below */
	if (set == ARRAY_SIZE(a)) {
		for (i = 0; i < ARRAY_SIZE(a); i++)
			if (a[i].key == key)
				goto match;
		goto unmatch;
	}

	spin_lock(&spin);
	if (set)
		for (i = 0; i < ARRAY_SIZE(a); i++)
			if (a[i].key == key) {
				spin_unlock(&spin);
				goto match;
			}
	for (i = 0; i < ARRAY_SIZE(a); i++) {
		if (a[i].key) {
			if (unlikely(a[i].key == key)) { /* rare but possible */
				spin_unlock(&spin);
				goto match;
			} else
				continue;
		}
		if (strstr(a[i].name, name)) {
			/*
			 * the order of these three lines is important for the
			 * lockless read above.
			 */
			a[i].key = key;
			spin_unlock(&spin);
			set++;
			/* AuDbg("%d, %s\n", set, name); */
			goto match;
		}
	}
	spin_unlock(&spin);
	goto unmatch;

match:
	return 1;
unmatch:
	return 0;
}

static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
{
	int err, n;
	struct task_struct *curr;
	struct held_lock **hl, *held_locks, *p;

	err = 0;
	curr = current;
	wkinfo->dont_check = lockdep_recursing(curr);
	if (wkinfo->dont_check)
		goto out;
	n = curr->lockdep_depth;
	if (!n)
		goto out;

	err = -ENOMEM;
	wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
	if (unlikely(!wkinfo->hlock))
		goto out;

	err = 0;
#if 0
	if (0 && au_debug_test()) /* left for debugging */
		lockdep_print_held_locks(curr);
#endif
	held_locks = curr->held_locks;
	hl = wkinfo->hlock;
	while (n--) {
		p = held_locks++;
		if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
			*hl++ = p;
	}
	*hl = NULL;

out:
	return err;
}

static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
{
	au_kfree_try_rcu(wkinfo->hlock);
}

static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
{
	struct held_lock *p, **hl = wkinfo->hlock;
	int subclass;

	if (wkinfo->dont_check)
		lockdep_off();
	if (!hl)
		return;
	while ((p = *hl++)) { /* assignment */
		subclass = lockdep_hlock_class(p)->subclass;
		/* AuDbg("%s, %d\n", p->instance->name, subclass); */
		if (p->read)
			rwsem_acquire_read(p->instance, subclass, 0,
					   /*p->acquire_ip*/_RET_IP_);
		else
			rwsem_acquire(p->instance, subclass, 0,
				      /*p->acquire_ip*/_RET_IP_);
	}
}

static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
{
	struct held_lock *p, **hl = wkinfo->hlock;

	if (wkinfo->dont_check)
		lockdep_on();
	if (!hl)
		return;
	while ((p = *hl++)) /* assignment */
		rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
}
#endif

static void wkq_func(struct work_struct *wk)
{
	struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);

	AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
	AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);

	au_wkq_lockdep_pre(wkinfo);
	wkinfo->func(wkinfo->args);
	au_wkq_lockdep_post(wkinfo);
	if (au_ftest_wkq(wkinfo->flags, WAIT))
		complete(wkinfo->comp);
	else {
		kobject_put(wkinfo->kobj);
		module_put(THIS_MODULE); /* todo: ?? */
		au_kfree_rcu(wkinfo);
	}
}

/*
 * Since struct completion is large, try allocating it dynamically.
 */
#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
#define AuWkqCompDeclare(name)	struct completion *comp = NULL

static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
{
	*comp = kmalloc(sizeof(**comp), GFP_NOFS);
	if (*comp) {
		init_completion(*comp);
		wkinfo->comp = *comp;
		return 0;
	}
	return -ENOMEM;
}

static void au_wkq_comp_free(struct completion *comp)
{
	au_kfree_rcu(comp);
}

#else

/* no braces */
#define AuWkqCompDeclare(name) \
	DECLARE_COMPLETION_ONSTACK(_ ## name); \
	struct completion *comp = &_ ## name

static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
{
	wkinfo->comp = *comp;
	return 0;
}

static void au_wkq_comp_free(struct completion *comp __maybe_unused)
{
	/* empty */
}
#endif /* 4KSTACKS */

static void au_wkq_run(struct au_wkinfo *wkinfo)
{
J. R. Okajima's avatar
J. R. Okajima committed
274
275
276
277
278
279
280
281
282
	if (au_ftest_wkq(wkinfo->flags, NEST)) {
		if (au_wkq_test()) {
			AuWarn1("wkq from wkq, unless silly-rename on NFS,"
				" due to a dead dir by UDBA,"
				" or async xino write?\n");
			AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
		}
	} else
		au_dbg_verify_kthread();
J. R. Okajima's avatar
J. R. Okajima committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

	if (au_ftest_wkq(wkinfo->flags, WAIT)) {
		INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
		queue_work(au_wkq, &wkinfo->wk);
	} else {
		INIT_WORK(&wkinfo->wk, wkq_func);
		schedule_work(&wkinfo->wk);
	}
}

/*
 * Be careful. It is easy to make deadlock happen.
 * processA: lock, wkq and wait
 * processB: wkq and wait, lock in wkq
 * --> deadlock
 */
int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
{
	int err;
	AuWkqCompDeclare(comp);
	struct au_wkinfo wkinfo = {
		.flags	= flags,
		.func	= func,
		.args	= args
	};

	err = au_wkq_comp_alloc(&wkinfo, &comp);
	if (unlikely(err))
		goto out;
	err = au_wkq_lockdep_alloc(&wkinfo);
	if (unlikely(err))
		goto out_comp;
	if (!err) {
		au_wkq_run(&wkinfo);
		/* no timeout, no interrupt */
		wait_for_completion(wkinfo.comp);
	}
	au_wkq_lockdep_free(&wkinfo);

out_comp:
	au_wkq_comp_free(comp);
out:
	destroy_work_on_stack(&wkinfo.wk);
	return err;
}

/*
 * Note: dget/dput() in func for aufs dentries are not supported. It will be a
 * problem in a concurrent umounting.
 */
int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
		  unsigned int flags)
{
	int err;
	struct au_wkinfo *wkinfo;

	atomic_inc(&au_sbi(sb)->si_nowait.nw_len);

	/*
	 * wkq_func() must free this wkinfo.
	 * it highly depends upon the implementation of workqueue.
	 */
	err = 0;
	wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
	if (wkinfo) {
		wkinfo->kobj = &au_sbi(sb)->si_kobj;
		wkinfo->flags = flags & ~AuWkq_WAIT;
		wkinfo->func = func;
		wkinfo->args = args;
		wkinfo->comp = NULL;
		au_wkq_lockdep_init(wkinfo);
		kobject_get(wkinfo->kobj);
		__module_get(THIS_MODULE); /* todo: ?? */

		au_wkq_run(wkinfo);
	} else {
		err = -ENOMEM;
		au_nwt_done(&au_sbi(sb)->si_nowait);
	}

	return err;
}

/* ---------------------------------------------------------------------- */

void au_nwt_init(struct au_nowait_tasks *nwt)
{
	atomic_set(&nwt->nw_len, 0);
	/* smp_mb(); */ /* atomic_set */
	init_waitqueue_head(&nwt->nw_wq);
}

void au_wkq_fin(void)
{
	destroy_workqueue(au_wkq);
}

int __init au_wkq_init(void)
{
	int err;

	err = 0;
	au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
	if (IS_ERR(au_wkq))
		err = PTR_ERR(au_wkq);
	else if (!au_wkq)
		err = -ENOMEM;

	return err;
}