]> git.proxmox.com Git - mirror_zfs.git/blame - module/splat/splat-linux.c
splat linux:shrinker: Fix race condition
[mirror_zfs.git] / module / splat / splat-linux.c
CommitLineData
bf0c60c0
BB
1/*****************************************************************************\
2 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
3 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
4 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
5 * UCRL-CODE-235197
6 *
7 * This file is part of the SPL, Solaris Porting Layer.
8 * For details, see <http://github.com/behlendorf/spl/>.
9 *
10 * The SPL is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 * The SPL is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
22 *****************************************************************************
23 * Solaris Porting LAyer Tests (SPLAT) Kernel Compatibility Tests.
24\*****************************************************************************/
25
df870a69 26#include <sys/kmem.h>
bf0c60c0
BB
27#include "splat-internal.h"
28
29#define SPLAT_LINUX_NAME "linux"
30#define SPLAT_LINUX_DESC "Kernel Compatibility Tests"
31
32#define SPLAT_LINUX_TEST1_ID 0x1001
33#define SPLAT_LINUX_TEST1_NAME "shrink_dcache"
34#define SPLAT_LINUX_TEST1_DESC "Shrink dcache test"
35
36#define SPLAT_LINUX_TEST2_ID 0x1002
37#define SPLAT_LINUX_TEST2_NAME "shrink_icache"
38#define SPLAT_LINUX_TEST2_DESC "Shrink icache test"
39
40#define SPLAT_LINUX_TEST3_ID 0x1003
41#define SPLAT_LINUX_TEST3_NAME "shrinker"
42#define SPLAT_LINUX_TEST3_DESC "Shrinker test"
43
44
45/*
46 * Attempt to shrink the dcache memory. This is simply a functional
47 * to ensure we can correctly call the shrinker. We don't check that
48 * the cache actually decreased because we have no control over what
49 * else may be running on the system. This avoid false positives.
50 */
51static int
52splat_linux_test1(struct file *file, void *arg)
53{
54 int remain_before;
55 int remain_after;
56
57 remain_before = shrink_dcache_memory(0, GFP_KERNEL);
58 remain_after = shrink_dcache_memory(KMC_REAP_CHUNK, GFP_KERNEL);
59
60 splat_vprint(file, SPLAT_LINUX_TEST1_NAME,
61 "Shrink dcache memory, remain %d -> %d\n",
62 remain_before, remain_after);
63
64 return 0;
65}
66
67/*
68 * Attempt to shrink the icache memory. This is simply a functional
69 * to ensure we can correctly call the shrinker. We don't check that
70 * the cache actually decreased because we have no control over what
71 * else may be running on the system. This avoid false positives.
72 */
73static int
74splat_linux_test2(struct file *file, void *arg)
75{
76 int remain_before;
77 int remain_after;
78
79 remain_before = shrink_icache_memory(0, GFP_KERNEL);
80 remain_after = shrink_icache_memory(KMC_REAP_CHUNK, GFP_KERNEL);
81
82 splat_vprint(file, SPLAT_LINUX_TEST2_NAME,
83 "Shrink icache memory, remain %d -> %d\n",
84 remain_before, remain_after);
85
86 return 0;
87}
88
ca072ee7
SJ
89/*
90 * Wait queue used to eliminate race between dropping of slab
91 * and execution of the shrinker callback
92 */
93DECLARE_WAIT_QUEUE_HEAD(shrinker_wait);
94
bf0c60c0
BB
95SPL_SHRINKER_CALLBACK_FWD_DECLARE(splat_linux_shrinker_fn);
96SPL_SHRINKER_DECLARE(splat_linux_shrinker, splat_linux_shrinker_fn, 1);
97static unsigned long splat_linux_shrinker_size = 0;
98static struct file *splat_linux_shrinker_file = NULL;
99
100static int
101__splat_linux_shrinker_fn(struct shrinker *shrink, struct shrink_control *sc)
102{
103 static int failsafe = 0;
104
105 if (sc->nr_to_scan) {
106 splat_linux_shrinker_size = splat_linux_shrinker_size -
107 MIN(sc->nr_to_scan, splat_linux_shrinker_size);
108
109 splat_vprint(splat_linux_shrinker_file, SPLAT_LINUX_TEST3_NAME,
110 "Reclaimed %lu objects, size now %lu\n",
111 sc->nr_to_scan, splat_linux_shrinker_size);
112 } else {
113 splat_vprint(splat_linux_shrinker_file, SPLAT_LINUX_TEST3_NAME,
114 "Cache size is %lu\n", splat_linux_shrinker_size);
115 }
116
117 /* Far more calls than expected abort drop_slab as a failsafe */
118 if ((++failsafe % 1000) == 0) {
119 splat_vprint(splat_linux_shrinker_file, SPLAT_LINUX_TEST3_NAME,
120 "Far more calls than expected (%d), size now %lu\n",
121 failsafe, splat_linux_shrinker_size);
122 return -1;
123 }
124
ca072ee7
SJ
125 /* Shrinker has run, so signal back to test. */
126 wake_up(&shrinker_wait);
127
bf0c60c0
BB
128 return (int)splat_linux_shrinker_size;
129}
130
131SPL_SHRINKER_CALLBACK_WRAPPER(splat_linux_shrinker_fn);
132
133#define DROP_SLAB_CMD \
134 "exec 0</dev/null " \
135 " 1>/proc/sys/vm/drop_caches " \
136 " 2>/dev/null; " \
137 "echo 2"
138
139static int
140splat_linux_drop_slab(struct file *file)
141{
142 char *argv[] = { "/bin/sh",
143 "-c",
144 DROP_SLAB_CMD,
145 NULL };
146 char *envp[] = { "HOME=/",
147 "TERM=linux",
148 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
149 NULL };
150 int rc;
151
152 rc = call_usermodehelper(argv[0], argv, envp, 1);
153 if (rc)
154 splat_vprint(file, SPLAT_LINUX_TEST3_NAME,
155 "Failed user helper '%s %s %s', rc = %d\n",
156 argv[0], argv[1], argv[2], rc);
157
158 return rc;
159}
160
161/*
162 * Verify correct shrinker functionality by registering a shrinker
163 * with the required compatibility macros. We then use a simulated
164 * cache and force the systems caches to be dropped. The shrinker
165 * should be repeatedly called until it reports that the cache is
166 * empty. It is then cleanly unregistered and correct behavior is
167 * verified. There are now four slightly different supported shrinker
168 * API and this test ensures the compatibility code is correct.
169 */
170static int
171splat_linux_test3(struct file *file, void *arg)
172{
173 int rc = -EINVAL;
174
175 /*
176 * Globals used by the shrinker, it is not safe to run this
177 * test concurrently this is a safe assumption for SPLAT tests.
178 * Regardless we do some minimal checking a bail if concurrent
179 * use is detected.
180 */
181 if (splat_linux_shrinker_size || splat_linux_shrinker_file) {
182 splat_vprint(file, SPLAT_LINUX_TEST3_NAME,
183 "Failed due to concurrent shrinker test, rc = %d\n", rc);
184 return (rc);
185 }
186
187 splat_linux_shrinker_size = 1024;
188 splat_linux_shrinker_file = file;
189
190 spl_register_shrinker(&splat_linux_shrinker);
191 rc = splat_linux_drop_slab(file);
192 if (rc)
193 goto out;
194
ca072ee7
SJ
195 /*
196 * By the time we get here, it is possible that the shrinker has not
197 * yet run. splat_linux_drop_slab sends a signal for it to run, but
198 * there is no guarantee of when it will actually run. We wait for it
199 * to run here, terminating when either the shrinker size is now 0 or
200 * we timeout after 1 second, which should be an eternity (error).
201 */
202 rc = wait_event_timeout(shrinker_wait, !splat_linux_shrinker_size, HZ);
203 if (!rc) {
204 splat_vprint(file, SPLAT_LINUX_TEST3_NAME,
205 "Failed cache shrinking timed out, size now %lu",
206 splat_linux_shrinker_size);
207 rc = -ETIMEDOUT;
208 } else {
209 rc = 0;
210 }
211
212 if (!rc && splat_linux_shrinker_size != 0) {
bf0c60c0
BB
213 splat_vprint(file, SPLAT_LINUX_TEST3_NAME,
214 "Failed cache was not shrunk to 0, size now %lu",
215 splat_linux_shrinker_size);
216 rc = -EDOM;
217 }
218out:
219 spl_unregister_shrinker(&splat_linux_shrinker);
220
221 splat_linux_shrinker_size = 0;
222 splat_linux_shrinker_file = NULL;
223
224 return rc;
225}
226
227splat_subsystem_t *
228splat_linux_init(void)
229{
230 splat_subsystem_t *sub;
231
232 sub = kmalloc(sizeof(*sub), GFP_KERNEL);
233 if (sub == NULL)
234 return NULL;
235
236 memset(sub, 0, sizeof(*sub));
237 strncpy(sub->desc.name, SPLAT_LINUX_NAME, SPLAT_NAME_SIZE);
238 strncpy(sub->desc.desc, SPLAT_LINUX_DESC, SPLAT_DESC_SIZE);
239 INIT_LIST_HEAD(&sub->subsystem_list);
240 INIT_LIST_HEAD(&sub->test_list);
241 spin_lock_init(&sub->test_lock);
242 sub->desc.id = SPLAT_SUBSYSTEM_LINUX;
243
244 SPLAT_TEST_INIT(sub, SPLAT_LINUX_TEST1_NAME, SPLAT_LINUX_TEST1_DESC,
245 SPLAT_LINUX_TEST1_ID, splat_linux_test1);
246 SPLAT_TEST_INIT(sub, SPLAT_LINUX_TEST2_NAME, SPLAT_LINUX_TEST2_DESC,
247 SPLAT_LINUX_TEST2_ID, splat_linux_test2);
248 SPLAT_TEST_INIT(sub, SPLAT_LINUX_TEST3_NAME, SPLAT_LINUX_TEST3_DESC,
249 SPLAT_LINUX_TEST3_ID, splat_linux_test3);
250
251 return sub;
252}
253
254void
255splat_linux_fini(splat_subsystem_t *sub)
256{
257 ASSERT(sub);
258 SPLAT_TEST_FINI(sub, SPLAT_LINUX_TEST3_ID);
259 SPLAT_TEST_FINI(sub, SPLAT_LINUX_TEST2_ID);
260 SPLAT_TEST_FINI(sub, SPLAT_LINUX_TEST1_ID);
261
262 kfree(sub);
263}
264
265int
266splat_linux_id(void) {
267 return SPLAT_SUBSYSTEM_LINUX;
268}