xref: /freebsd/contrib/netbsd-tests/rump/kernspace/alloc.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*	$NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #if !defined(lint)
32 __RCSID("$NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $");
33 #endif /* !lint */
34 
35 #include <sys/param.h>
36 #include <sys/condvar.h>
37 #include <sys/kmem.h>
38 #include <sys/kthread.h>
39 #include <sys/mutex.h>
40 #include <sys/pool.h>
41 #include <sys/proc.h>
42 
43 #include <uvm/uvm.h>
44 
45 #include <rump/rumpuser.h>
46 #include "kernspace.h"
47 
48 static void *store[32];
49 static struct pool pp1, pp2;
50 
51 static kmutex_t mtx;
52 static kcondvar_t kcv;
53 static int curstat;
54 
55 static void
56 hthr(void *arg)
57 {
58 	int i;
59 
60 	mutex_enter(&mtx);
61 	curstat++;
62 	cv_signal(&kcv);
63 
64 	while (curstat < 2)
65 		cv_wait(&kcv, &mtx);
66 	mutex_exit(&mtx);
67 
68 	/* try to guarantee that the sleep is triggered in PR_WAITOK */
69 	while ((kernel_map->flags & VM_MAP_WANTVA) == 0)
70 		kpause("take5", false, 1, NULL);
71 
72 	for (i = 0; i < __arraycount(store); i++) {
73 		pool_put(&pp1, store[i]);
74 	}
75 
76 	kthread_exit(0);
77 }
78 
79 void
80 rumptest_alloc(size_t thelimit)
81 {
82 	char *c;
83 	int succ, i;
84 
85 	mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
86 	cv_init(&kcv, "venailu");
87 
88 	kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, hthr, NULL, NULL, "h");
89 
90 	pool_init(&pp1, 1024, 0, 0, 0, "vara-allas",
91 	    &pool_allocator_nointr, IPL_NONE);
92 	pool_init(&pp2, 1024, 0, 0, 0, "allas",
93 	    &pool_allocator_nointr, IPL_NONE);
94 
95 	for (i = 0; i < __arraycount(store); i++) {
96 		store[i] = pool_get(&pp1, PR_NOWAIT);
97 		if (store[i] == NULL) {
98 			panic("pool_get store failed");
99 		}
100 	}
101 
102 	/* wait until other thread runs */
103 	mutex_enter(&mtx);
104 	while (curstat == 0)
105 		cv_wait(&kcv, &mtx);
106 	mutex_exit(&mtx);
107 
108 	for (succ = 0;; succ++) {
109 		if (succ * 1024 > thelimit)
110 			panic("managed to allocate over limit");
111 		if ((c = pool_get(&pp2, PR_NOWAIT)) == NULL) {
112 			mutex_enter(&mtx);
113 			curstat++;
114 			cv_signal(&kcv);
115 			mutex_exit(&mtx);
116 			if (pool_get(&pp2, PR_WAITOK) == NULL)
117 				panic("pool get PR_WAITOK failed");
118 			break;
119 		}
120 		*c = 'a';
121 	}
122 }
123