⬢ DragonFlyBSD Kernel Audit
sys/libkern/linux_idr.c
← back
  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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * Copyright (c) 2005-2012 The DragonFly Project.
 * Copyright (c) 2013 François Tigeot
 * Copyright (c) 2013 Matthew Dillon
 * All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Jeffrey Hsu.
 *
 * 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.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT HOLDERS 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 USERLAND_TEST
/*
 * Testing:
 *
 * cc -I. -DUSERLAND_TEST libkern/linux_idr.c -o /tmp/idr -g
 */

#define _KERNEL
#define _KERNEL_STRUCTURES
#define KLD_MODULE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include <sys/idr.h>
#include <sys/errno.h>

#undef MALLOC_DEFINE
#define MALLOC_DEFINE(a, b, c)
#define lwkt_gettoken(x)
#define lwkt_reltoken(x)
#undef kmalloc
#define kmalloc(bytes, zone, flags)	calloc(bytes, 1)
#define lwkt_token_init(a, b)
#define lwkt_token_uninit(a)
#define kfree(ptr, flags)		free(ptr)
#define KKASSERT(a)
#define panic(str, ...)			assert(0)
#define min(a, b)	(((a) < (b)) ? (a) : (b))
#define max(a, b)	(((a) > (b)) ? (a) : (b))

int
main(int ac, char **av)
{
	char buf[256];
	struct idr idr;
	intptr_t generation = 0x0;
	int error;
	int id;

	idr_init(&idr);

	printf("cmd> ");
	fflush(stdout);
	while (fgets(buf, sizeof(buf), stdin) != NULL) {
		if (sscanf(buf, "a %d", &id) == 1) {
			for (;;) {
				if (idr_pre_get(&idr, 0) == 0) {
					fprintf(stderr, "pre_get failed\n");
					exit(1);
				}
				error = idr_get_new_above(&idr,
							  (void *)generation,
							  id, &id);
				if (error == -EAGAIN)
					continue;
				if (error) {
					fprintf(stderr, "get_new err %d\n",
						error);
					exit(1);
				}
				printf("allocated %d value %08x\n",
					id, (int)generation);
				++generation;
				break;
			}
		} else if (sscanf(buf, "f %d", &id) == 1) {
			idr_remove(&idr, id);
		} else if (sscanf(buf, "g %d", &id) == 1) {
			void *res = idr_find(&idr, id);
			printf("find %d res %p\n", id, res);
		}
		printf("cmd> ");
		fflush(stdout);
	}
	return 0;
}

#else

#include <sys/idr.h>
#include <sys/kernel.h>
#include <sys/libkern.h>
#include <sys/malloc.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/spinlock2.h>
#include <sys/limits.h>

#endif

/* Must be 2^n - 1 */
#define IDR_DEFAULT_SIZE    255

MALLOC_DEFINE(M_IDR, "idr", "Integer ID management");

static void	idr_grow(struct idr *idp, int want);
static void	idr_reserve(struct idr *idp, int id, int incr);
static int	idr_find_free(struct idr *idp, int want, int lim);

/*
 * Number of nodes in right subtree, including the root.
 */
static __inline int
right_subtree_size(int n)
{
	return (n ^ (n | (n + 1)));
}

/*
 * Bigger ancestor.
 */
static __inline int
right_ancestor(int n)
{
	return (n | (n + 1));
}

/*
 * Smaller ancestor.
 */
static __inline int
left_ancestor(int n)
{
	return ((n & (n + 1)) - 1);
}

static __inline void
idrfixup(struct idr *idp, int id)
{
	if (id < idp->idr_freeindex) {
		idp->idr_freeindex = id;
	}
	while (idp->idr_lastindex >= 0 &&
		idp->idr_nodes[idp->idr_lastindex].data == NULL
	) {
		--idp->idr_lastindex;
	}
}

static __inline struct idr_node *
idr_get_node(struct idr *idp, int id)
{
	struct idr_node *idrnp;
	if (id < 0 || id >= idp->idr_count)
		return (NULL);
	idrnp = &idp->idr_nodes[id];
	if (idrnp->allocated == 0)
		return (NULL);
	return (idrnp);
}

static void
idr_reserve(struct idr *idp, int id, int incr)
{
	while (id >= 0) {
		idp->idr_nodes[id].allocated += incr;
		KKASSERT(idp->idr_nodes[id].allocated >= 0);
		id = left_ancestor(id);
	}
}

static int
idr_find_free(struct idr *idp, int want, int lim)
{
	int id, rsum, rsize, node;

	/*
	 * Search for a free descriptor starting at the higher
	 * of want or fd_freefile.  If that fails, consider
	 * expanding the ofile array.
	 *
	 * NOTE! the 'allocated' field is a cumulative recursive allocation
	 * count.  If we happen to see a value of 0 then we can shortcut
	 * our search.  Otherwise we run through through the tree going
	 * down branches we know have free descriptor(s) until we hit a
	 * leaf node.  The leaf node will be free but will not necessarily
	 * have an allocated field of 0.
	 */
	/* move up the tree looking for a subtree with a free node */
	for (id = max(want, idp->idr_freeindex); id < min(idp->idr_count, lim);
			id = right_ancestor(id)) {
		if (idp->idr_nodes[id].allocated == 0)
			return (id);

		rsize = right_subtree_size(id);
		if (idp->idr_nodes[id].allocated == rsize)
			continue;	/* right subtree full */

		/*
		 * Free fd is in the right subtree of the tree rooted at fd.
		 * Call that subtree R.  Look for the smallest (leftmost)
		 * subtree of R with an unallocated fd: continue moving
		 * down the left branch until encountering a full left
		 * subtree, then move to the right.
		 */
		for (rsum = 0, rsize /= 2; rsize > 0; rsize /= 2) {
			node = id + rsize;
			rsum += idp->idr_nodes[node].allocated;
			if (idp->idr_nodes[id].allocated == rsum + rsize) {
				id = node;	/* move to the right */
				if (idp->idr_nodes[node].allocated == 0)
					return (id);
				rsum = 0;
			}
		}
		return (id);
	}
	return (-1);
}

/*
 * Blocking pre-get support, allows callers to use idr_pre_get() in
 * combination with idr_get_new_above() such that idr_get_new_above()
 * can be called safely with a spinlock held.
 *
 * Returns 0 on failure, 1 on success.
 *
 * Caller must hold a blockable lock.
 */
int
idr_pre_get(struct idr *idp, unsigned gfp_mask __unused)
{
	int want = idp->idr_maxwant;
	int lim = INT_MAX;
	int result = 1;				/* success */
	int id;

	KKASSERT(mycpu->gd_spinlocks == 0);
	lwkt_gettoken(&idp->idr_token);
	for (;;) {
		/*
		 * Grow if necessary (or if forced by the loop)
		 */
		if (want >= idp->idr_count)
			idr_grow(idp, want);

		/*
		 * Check if a spot is available, break and return 0 if true,
		 * unless the available spot is beyond our limit.  It is
		 * possible to exceed the limit due to the way array growth
		 * works.
		 *
		 * XXX we assume that the caller uses a consistent <sid> such
		 *     that the idr_maxwant field is correct, otherwise we
		 *     may believe that a slot is available but the caller then
		 *     fails in idr_get_new_above() and loops.
		 */
		id = idr_find_free(idp, idp->idr_maxwant, lim);
		if (id != -1) {
			if (id >= lim)
				result = 0;	/* failure */
			break;
		}

		/*
		 * Return ENOSPC if our limit has been reached, otherwise
		 * loop and force growth.
		 */
		if (idp->idr_count >= lim) {
			result = 0;		/* failure */
			break;
		}
		want = idp->idr_count;
	}
	lwkt_reltoken(&idp->idr_token);
	return result;
}

/*
 * Allocate an integer.  If -EAGAIN is returned the caller should loop
 * and call idr_pre_get() with no locks held, and then retry the call
 * to idr_get_new_above().
 *
 * Can be safely called with spinlocks held.
 */
int
idr_get_new_above(struct idr *idp, void *ptr, int sid, int *id)
{
	int resid;

	/*
	 * NOTE! Because the idp is initialized with a non-zero count,
	 *	 sid might be < idp->idr_count but idr_maxwant might not
	 *	 yet be initialized.  So check both cases.
	 */
	lwkt_gettoken(&idp->idr_token);
	if (sid >= idp->idr_count || idp->idr_maxwant < sid) {
		idp->idr_maxwant = max(idp->idr_maxwant, sid);
		lwkt_reltoken(&idp->idr_token);
		return -EAGAIN;
	}

	resid = idr_find_free(idp, sid, INT_MAX);
	if (resid == -1) {
		lwkt_reltoken(&idp->idr_token);
		return -EAGAIN;
	}

	if (resid >= idp->idr_count)
		panic("idr_get_new_above(): illegal resid %d", resid);
	if (resid > idp->idr_lastindex)
		idp->idr_lastindex = resid;
	if (sid <= idp->idr_freeindex)
		idp->idr_freeindex = resid;
	*id = resid;
	idr_reserve(idp, resid, 1);
	idp->idr_nodes[resid].data = ptr;

	lwkt_reltoken(&idp->idr_token);
	return (0);
}

/*
 * start: minimum id (inclusive)
 * end:   maximum id (exclusive); or INT_MAX+1 if end <= 0
 */
int
idr_alloc(struct idr *idp, void *ptr, int start, int end,
	  unsigned gfp_mask __unused)
{
	int lim = end > 0 ? end - 1 : INT_MAX;
	int want = start;
	int result, id;

	if (start < 0)
		return -EINVAL;

	if (lim < start)
		return -ENOSPC;

	lwkt_gettoken(&idp->idr_token);

grow_again:
	if (want >= idp->idr_count)
		idr_grow(idp, want);

	/*
	 * Check if a spot is available, break and return 0 if true,
	 * unless the available spot is beyond our limit.  It is
	 * possible to exceed the limit due to the way array growth
	 * works.
	 */
	id = idr_find_free(idp, start, INT_MAX);
	if (id == -1) {
		want = idp->idr_count;
		goto grow_again;
	}

	if (id > lim) {
		result = -ENOSPC;
		goto done;
	}

	if (id >= idp->idr_count)
		panic("idr_alloc(): illegal resid %d", id);
	if (id > idp->idr_lastindex)
		idp->idr_lastindex = id;
	if (start <= idp->idr_freeindex)
		idp->idr_freeindex = id;
	result = id;
	idr_reserve(idp, id, 1);
	idp->idr_nodes[id].data = ptr;

done:
	lwkt_reltoken(&idp->idr_token);
	return result;
}

int
idr_get_new(struct idr *idp, void *ptr, int *id)
{
	return idr_get_new_above(idp, ptr, 0, id);
}

/*
 * Grow the file table so it can hold through descriptor (want).
 *
 * Caller must hold a blockable lock.
 */
static void
idr_grow(struct idr *idp, int want)
{
	struct idr_node *oldnodes, *newnodes;
	int nf;

	/* We want 2^n - 1 descriptors */
	nf = idp->idr_count;
	do {
		nf = 2 * nf + 1;
	} while (nf <= want);

#ifdef USERLAND_TEST
	printf("idr_grow: %d -> %d\n", idp->idr_count, nf);
#endif

	/* Allocate a new zero'ed node array */
	newnodes = kmalloc(nf * sizeof(struct idr_node),
			   M_IDR, M_ZERO | M_WAITOK);

	/* We might race another grow */
	if (nf <= idp->idr_count) {
		kfree(newnodes, M_IDR);
		return;
	}

	/*
	 * Copy existing nodes to the beginning of the new array
	 */
	oldnodes = idp->idr_nodes;
	if (oldnodes) {
		bcopy(oldnodes, newnodes,
		      idp->idr_count * sizeof(struct idr_node));
	}
	idp->idr_nodes = newnodes;
	idp->idr_count = nf;

	if (oldnodes) {
		kfree(oldnodes, M_IDR);
	}
	idp->idr_nexpands++;
}

void *
idr_remove(struct idr *idp, int id)
{
	void *ptr;

	lwkt_gettoken(&idp->idr_token);
	if (id < 0 || id >= idp->idr_count) {
		lwkt_reltoken(&idp->idr_token);
		return NULL;
	}
        if (idp->idr_nodes[id].allocated == 0) {
                lwkt_reltoken(&idp->idr_token);
                return NULL;
        }
	ptr = idp->idr_nodes[id].data;
	idp->idr_nodes[id].data = NULL;
	idr_reserve(idp, id, -1);
	idrfixup(idp, id);
	lwkt_reltoken(&idp->idr_token);

	return ptr;
}

/*
 * Remove all int allocations, leave array intact.
 *
 * Caller must hold a blockable lock (or be in a context where holding
 * the spinlock is not relevant).
 */
void
idr_remove_all(struct idr *idp)
{
	lwkt_gettoken(&idp->idr_token);
	bzero(idp->idr_nodes, idp->idr_count * sizeof(struct idr_node));
	idp->idr_lastindex = -1;
	idp->idr_freeindex = 0;
	idp->idr_nexpands = 0;
	idp->idr_maxwant = 0;
	lwkt_reltoken(&idp->idr_token);
}

void
idr_destroy(struct idr *idp)
{
	lwkt_token_uninit(&idp->idr_token);
	if (idp->idr_nodes) {
		kfree(idp->idr_nodes, M_IDR);
		idp->idr_nodes = NULL;
	}
	bzero(idp, sizeof(*idp));
}

void *
idr_find(struct idr *idp, int id)
{
	void *ret;

	if (id < 0 || id >= idp->idr_count) {
		ret = NULL;
	} else if (idp->idr_nodes[id].allocated == 0) {
		ret = NULL;
	} else {
		ret = idp->idr_nodes[id].data;
	}
	return ret;
}

int
idr_for_each(struct idr *idp, int (*fn)(int id, void *p, void *data),
	     void *data)
{
	int i, error = 0;
	struct idr_node *nodes;

	nodes = idp->idr_nodes;
	for (i = 0; i < idp->idr_count; i++) {
		if (nodes[i].data != NULL && nodes[i].allocated > 0) {
			error = fn(i, nodes[i].data, data);
			if (error != 0)
				break;
		}
	}
	return error;
}

void *
idr_replace(struct idr *idp, void *ptr, int id)
{
	struct idr_node *idrnp;
	void *ret;

	lwkt_gettoken(&idp->idr_token);
	idrnp = idr_get_node(idp, id);
	if (idrnp == NULL) {
		ret = (void *)(unsigned long)-ENOENT;
	} else {
		ret = idrnp->data;
		idrnp->data = ptr;
	}
	lwkt_reltoken(&idp->idr_token);
	return (ret);
}

void
idr_init(struct idr *idp)
{
	bzero(idp, sizeof(struct idr));
	idp->idr_nodes = kmalloc(IDR_DEFAULT_SIZE * sizeof(struct idr_node),
				 M_IDR, M_WAITOK | M_ZERO);
	idp->idr_count = IDR_DEFAULT_SIZE;
	idp->idr_lastindex = -1;
	idp->idr_maxwant = 0;
	lwkt_token_init(&idp->idr_token, "idr token");
}