-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
stress-loop.c
424 lines (375 loc) · 10.2 KB
/
stress-loop.c
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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022-2024 Colin Ian King.
*
* This program 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-builtin.h"
#include "core-capabilities.h"
#include "core-mincore.h"
#include <sys/ioctl.h>
#if defined(HAVE_LINUX_LOOP_H)
#include <linux/loop.h>
#endif
static const stress_help_t help[] = {
{ NULL, "loop N", "start N workers exercising loopback devices" },
{ NULL, "loop-ops N", "stop after N bogo loopback operations" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_LINUX_LOOP_H) && \
defined(LOOP_CTL_GET_FREE) && \
defined(LOOP_SET_FD) && \
defined(LOOP_CLR_FD) && \
defined(LOOP_CTL_REMOVE)
#if !defined(LOOP_CONFIGURE)
#define LOOP_CONFIGURE (0x4C0A)
#endif
/*
* See include/uapi/linux/loop.h
*/
struct shim_loop_info64 {
uint64_t lo_device;
uint64_t lo_inode;
uint64_t lo_rdevice;
uint64_t lo_offset;
uint64_t lo_sizelimit;
uint32_t lo_number;
uint32_t lo_encrypt_type;
uint32_t lo_encrypt_key_size;
uint32_t lo_flags;
uint8_t lo_file_name[LO_NAME_SIZE];
uint8_t lo_crypt_name[LO_NAME_SIZE];
uint8_t lo_encrypt_key[LO_KEY_SIZE];
uint64_t lo_init[2];
};
struct shim_loop_config {
uint32_t fd;
uint32_t block_size;
struct loop_info64 info;
uint64_t reserved[8];
};
static const char * const loop_attr[] = {
"backing_file",
"offset",
"sizelimit",
"autoclear",
"partscan",
"dio"
};
/*
* stress_loot_supported()
* check if we can run this as root
*/
static int stress_loop_supported(const char *name)
{
if (!stress_check_capability(SHIM_CAP_SYS_ADMIN)) {
pr_inf_skip("%s stressor will be skipped, "
"need to be running with CAP_SYS_ADMIN "
"rights for this stressor\n", name);
return -1;
}
return 0;
}
/*
* stress_loop()
* stress loopback device
*/
static int stress_loop(stress_args_t *args)
{
int ret, backing_fd, rc = EXIT_FAILURE;
char backing_file[PATH_MAX];
size_t backing_size = 2 * MB;
const int bad_fd = stress_get_bad_fd();
ret = stress_temp_dir_mk_args(args);
if (ret < 0)
return stress_exit_status(-ret);
(void)stress_temp_filename_args(args,
backing_file, sizeof(backing_file), stress_mwc32());
if ((backing_fd = open(backing_file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
pr_fail("%s: open %s failed, errno=%d (%s)\n",
args->name, backing_file, errno, strerror(errno));
goto tidy;
}
(void)shim_unlink(backing_file);
if (ftruncate(backing_fd, (off_t)backing_size) < 0) {
pr_fail("%s: ftruncate failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)close(backing_fd);
goto tidy;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
int ctrl_dev, loop_dev;
void *ptr;
size_t i;
long dev_num;
#if defined(LOOP_SET_DIRECT_IO)
unsigned long dio;
#endif
char dev_name[PATH_MAX];
#if defined(LOOP_GET_STATUS)
struct loop_info info;
#endif
#if defined(LOOP_GET_STATUS64)
struct loop_info64 info64;
#endif
#if defined(LOOP_SET_BLOCK_SIZE)
unsigned long blk_size;
static const uint16_t blk_sizes[] = {
256, /* Invalid */
512, /* Valid */
1024, /* Valid */
2048, /* Valid */
4096, /* Valid */
8192, /* Invalid */
4000, /* Invalid */
0, /* Invalid */
65535 /* Invalid */
};
#endif
/*
* Open loop control device
*/
ctrl_dev = open("/dev/loop-control", O_RDWR);
if (ctrl_dev < 0) {
pr_fail("%s: cannot open /dev/loop-control: %d (%s)\n",
args->name, errno, strerror(errno));
break;
}
/*
* Try for a random loop number first...
*/
dev_num = stress_mwc1() ? -1 :
ioctl(ctrl_dev, LOOP_CTL_ADD, stress_mwc16() + 1024);
if (dev_num < 0) {
/*
* then attempt to get a free loop device
*/
dev_num = ioctl(ctrl_dev, LOOP_CTL_GET_FREE);
if (dev_num < 0)
goto next;
}
/*
* Open new loop device
*/
(void)snprintf(dev_name, sizeof(dev_name), "/dev/loop%ld", dev_num);
loop_dev = open(dev_name, O_RDWR);
if (loop_dev < 0)
goto destroy_loop;
/*
* Associate loop device with bad backing storage fd
*/
ret = ioctl(loop_dev, LOOP_SET_FD, bad_fd);
if (ret == 0)
ioctl(loop_dev, LOOP_CLR_FD, bad_fd);
/*
* Associate loop device with backing storage
*/
ret = ioctl(loop_dev, LOOP_SET_FD, backing_fd);
if (ret < 0)
goto close_loop;
for (i = 0; i < SIZEOF_ARRAY(loop_attr); i++) {
char attr_path[PATH_MAX];
char buf[4096];
(void)snprintf(attr_path, sizeof(attr_path),
"/sys/devices/virtual/block/loop%ld/loop/%s",
dev_num, loop_attr[i]);
VOID_RET(ssize_t, stress_system_read(attr_path, buf, sizeof(buf)));
}
#if defined(LOOP_GET_STATUS)
/*
* Fetch loop device status information
*/
ret = ioctl(loop_dev, LOOP_GET_STATUS, &info);
if (ret < 0)
goto clr_loop;
/*
* Try to set some flags
*/
info.lo_flags |= (LO_FLAGS_AUTOCLEAR | LO_FLAGS_READ_ONLY);
#if defined(LOOP_SET_STATUS)
VOID_RET(int, ioctl(loop_dev, LOOP_SET_STATUS, &info));
switch (stress_mwc1()) {
case 0:
info.lo_encrypt_type = LO_CRYPT_NONE;
info.lo_encrypt_key_size = 0;
break;
case 1:
info.lo_encrypt_type = LO_CRYPT_XOR;
stress_rndbuf(info.lo_encrypt_key, LO_KEY_SIZE);
info.lo_encrypt_key[LO_KEY_SIZE - 1] = '\0';
info.lo_encrypt_key_size = LO_KEY_SIZE - 1;
break;
}
VOID_RET(int, ioctl(loop_dev, LOOP_SET_STATUS, &info));
#endif
#endif
ptr = stress_mmap_populate(NULL, backing_size, PROT_READ | PROT_WRITE,
MAP_SHARED, loop_dev, 0);
if (ptr != MAP_FAILED) {
(void)stress_mincore_touch_pages_interruptible(ptr, backing_size);
#if defined(MS_ASYNC)
(void)shim_msync(ptr, backing_size, MS_ASYNC);
#endif
(void)stress_munmap_retry_enomem(ptr, backing_size);
(void)shim_fsync(loop_dev);
}
#if defined(LOOP_GET_STATUS64)
/*
* Fetch loop device status information
*/
ret = ioctl(loop_dev, LOOP_GET_STATUS64, &info64);
if (ret < 0)
goto clr_loop;
/*
* Try to set some flags
*/
info.lo_flags |= (LO_FLAGS_AUTOCLEAR | LO_FLAGS_READ_ONLY);
#if defined(LOOP_SET_STATUS64)
VOID_RET(int, ioctl(loop_dev, LOOP_SET_STATUS64, &info64));
#endif
#endif
#if defined(LOOP_SET_CAPACITY)
/*
* Resize command (even though we have not changed size)
*/
VOID_RET(int, ftruncate(backing_fd, (off_t)backing_size * 2));
VOID_RET(int, ioctl(loop_dev, LOOP_SET_CAPACITY));
#endif
/*
* Sync is required to avoid loop_set_block_size
* warning messages
*/
(void)sync();
#if defined(LOOP_SET_BLOCK_SIZE)
/*
* Set block size, ignore error return. This will
* produce kernel warnings but should not break the
* kernel.
*/
blk_size = (unsigned long)blk_sizes[stress_mwc8modn((uint8_t)SIZEOF_ARRAY(blk_sizes))];
VOID_RET(int, ioctl(loop_dev, LOOP_SET_BLOCK_SIZE, blk_size));
#endif
#if defined(LOOP_SET_DIRECT_IO)
dio = 1;
VOID_RET(int, ioctl(loop_dev, LOOP_SET_DIRECT_IO, dio));
dio = 0;
VOID_RET(int, ioctl(loop_dev, LOOP_SET_DIRECT_IO, dio));
#endif
#if defined(LOOP_CHANGE_FD)
/*
* Attempt to change fd using a known illegal
* fd to force a failure.
*/
VOID_RET(int, ioctl(loop_dev, LOOP_CHANGE_FD, bad_fd));
/*
* This should fail because backing store is
* not read-only.
*/
VOID_RET(int, ioctl(loop_dev, LOOP_CHANGE_FD, backing_fd));
#endif
#if defined(LOOP_CONFIGURE)
{
struct shim_loop_config config;
/*
* Attempt to configure with illegal fd
*/
(void)shim_memset(&config, 0, sizeof(config));
config.fd = (uint32_t)bad_fd;
VOID_RET(int, ioctl(loop_dev, LOOP_CONFIGURE, &config));
/*
* Attempt to configure with NULL config
*/
VOID_RET(int, ioctl(loop_dev, LOOP_CONFIGURE, NULL));
}
#endif
#if defined(LOOP_GET_STATUS)
clr_loop:
#endif
/*
* Disassociate backing store from loop device
*/
for (i = 0; i < 1000; i++) {
ret = ioctl(loop_dev, LOOP_CLR_FD, backing_fd);
if (ret < 0) {
if (errno == EBUSY) {
(void)shim_usleep(10);
} else {
pr_fail("%s: failed to disassociate %s from backing store, "
"errno=%d (%s)\n",
args->name, dev_name, errno, strerror(errno));
goto close_loop;
}
} else {
break;
}
}
close_loop:
(void)close(loop_dev);
/*
* Remove the loop device, may need several retries
* if we get EBUSY
*/
destroy_loop:
for (i = 0; i < 1000; i++) {
ret = ioctl(ctrl_dev, LOOP_CTL_REMOVE, dev_num);
if ((ret < 0) && (errno == EBUSY)) {
(void)shim_usleep(10);
} else {
break;
}
}
/* Remove invalid loop device */
VOID_RET(int, ioctl(ctrl_dev, LOOP_CTL_REMOVE, -1));
/* Exercise invalid ioctl */
VOID_RET(int, ioctl(ctrl_dev, (LOOP_SET_FD & 0xff00) | 0xff, -1));
next:
(void)close(ctrl_dev);
#if defined(LOOP_SET_CAPACITY)
VOID_RET(int, ftruncate(backing_fd, (off_t)backing_size));
#endif
stress_bogo_inc(args);
} while (stress_continue(args));
rc = EXIT_SUCCESS;
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
(void)close(backing_fd);
tidy:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
(void)stress_temp_dir_rm_args(args);
return rc;
}
stressor_info_t stress_loop_info = {
.stressor = stress_loop,
.supported = stress_loop_supported,
.class = CLASS_OS | CLASS_DEV,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
static int stress_loop_supported(const char *name)
{
pr_inf_skip("%s: stressor will be skipped, loop is not available\n", name);
return -1;
}
stressor_info_t stress_loop_info = {
.stressor = stress_unimplemented,
.supported = stress_loop_supported,
.class = CLASS_OS | CLASS_DEV,
.help = help,
.unimplemented_reason = "built without linux/loop.h or loop ioctl() commands"
};
#endif