forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-mmap.c
881 lines (789 loc) · 23.2 KB
/
stress-mmap.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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 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"
#if defined(__NR_mmap2)
#define HAVE_MMAP2
#endif
#define MIN_MMAP_BYTES (4 * KB)
#define MAX_MMAP_BYTES (MAX_MEM_LIMIT)
#define DEFAULT_MMAP_BYTES (256 * MB)
static const stress_help_t help[] = {
{ NULL, "mmap N", "start N workers stressing mmap and munmap" },
{ NULL, "mmap-ops N", "stop after N mmap bogo operations" },
{ NULL, "mmap-async", "using asynchronous msyncs for file based mmap" },
{ NULL, "mmap-bytes N", "mmap and munmap N bytes for each stress iteration" },
{ NULL, "mmap-file", "mmap onto a file using synchronous msyncs" },
{ NULL, "mmap-mprotect", "enable mmap mprotect stressing" },
{ NULL, "mmap-osync", "enable O_SYNC on file" },
{ NULL, "mmap-odirect", "enable O_DIRECT on file" },
{ NULL, NULL, NULL }
};
typedef void * (*mmap_func_t)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
typedef struct {
int fd;
int flags;
size_t sz;
size_t mmap_bytes;
bool mmap_mprotect;
bool mmap_file;
bool mmap_async;
mmap_func_t mmap;
size_t mmap_prot_count;
int *mmap_prot_perms;
size_t mmap_flag_count;
int *mmap_flag_perms;
} stress_mmap_context_t;
#define NO_MEM_RETRIES_MAX (65536)
static sigjmp_buf jmp_env;
static bool jmp_env_set;
static const int mmap_prot[] = {
#if defined(PROT_NONE)
PROT_NONE,
#endif
#if defined(PROT_EXEC)
PROT_EXEC,
#endif
#if defined(PROT_READ)
PROT_READ,
#endif
#if defined(PROT_WRITE)
PROT_WRITE,
#endif
};
static const int mmap_std_flags[] = {
#if defined(MAP_ANONYMOUS)
MAP_ANONYMOUS,
#endif
#if defined(MAP_SHARED)
MAP_SHARED,
#endif
#if defined(MAP_SHARED_VALIDATE)
MAP_SHARED_VALIDATE,
#endif
#if defined(MAP_PRIVATE)
MAP_PRIVATE,
#endif
};
/* Misc randomly chosen mmap flags */
static const int mmap_flags[] = {
#if defined(MAP_HUGE_2MB) && \
defined(MAP_HUGETLB)
MAP_HUGE_2MB | MAP_HUGETLB,
#endif
#if defined(MAP_HUGE_1GB) && \
defined(MAP_HUGETLB)
MAP_HUGE_1GB | MAP_HUGETLB,
#endif
#if defined(MAP_HUGETLB)
MAP_HUGETLB,
#endif
#if defined(MAP_NONBLOCK)
MAP_NONBLOCK,
#endif
#if defined(MAP_GROWSDOWN)
MAP_GROWSDOWN,
#endif
#if defined(MAP_LOCKED)
MAP_LOCKED,
#endif
#if defined(MAP_32BIT) && (defined(__x86_64__) || defined(__x86_64))
MAP_32BIT,
#endif
#if defined(MAP_NOCACHE) /* Mac OS X */
MAP_NOCACHE,
#endif
#if defined(MAP_HASSEMAPHORE) /* Mac OS X */
MAP_HASSEMAPHORE,
#endif
#if defined(MAP_NORESERVE)
MAP_NORESERVE,
#endif
#if defined(MAP_STACK)
MAP_STACK,
#endif
#if defined(MAP_EXECUTABLE)
MAP_EXECUTABLE,
#endif
#if defined(MAP_UNINITIALIZED)
MAP_UNINITIALIZED,
#endif
#if defined(MAP_DENYWRITE)
MAP_DENYWRITE,
#endif
/* OpenBSD */
#if defined(MAP_HASSEMAPHORE)
MAP_HASSEMAPHORE,
#endif
/* OpenBSD */
#if defined(MAP_INHERIT)
MAP_INHERIT,
#endif
/* FreeBSD */
#if defined(MAP_NOCORE)
MAP_NOCORE,
#endif
/* FreeBSD */
#if defined(MAP_NOSYNC)
MAP_NOSYNC,
#endif
/* FreeBSD */
#if defined(MAP_PREFAULT_READ)
MAP_PREFAULT_READ,
#endif
/* NetBSD */
#if defined(MAP_WIRED)
MAP_WIRED,
#endif
/* NetBSD */
#if defined(MAP_ALIGNMENT_64KB)
MAP_ALIGNMENT_64KB,
#endif
/* NetBSD */
#if defined(MAP_ALIGNMENT_16MB)
MAP_ALIGNMENT_16MB,
#endif
/* OpenBSD */
#if defined(MAP_CONCEAL)
MAP_CONCEAL,
#endif
/* OpenBSD */
#if defined(MAP_COPY)
MAP_COPY,
#endif
/* Solaris */
#if defined(MAP_LOW32)
MAP_LOW32,
#endif
0,
};
static void MLOCKED_TEXT stress_mmap_sighandler(int signum)
{
(void)signum;
if (jmp_env_set)
siglongjmp(jmp_env, 1);
}
/*
* mmap2_try()
* If mmap2 is requested then try to use it for 4K page aligned
* offsets. Fall back to mmap() if not possible.
*/
#if defined(HAVE_MMAP2)
static void *mmap2_try(void *addr, size_t length, int prot, int flags,
int fd, off_t offset)
{
void *ptr;
off_t pgoffset;
/* Non 4K-page aligned offsets need to use mmap() */
if (offset & 4095)
return mmap(addr, length, prot, flags, fd, offset);
pgoffset = offset >> 12;
ptr = (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, pgoffset);
if (ptr == MAP_FAILED) {
/* For specific failure cases retry with mmap() */
if ((errno == ENOSYS) || (errno == EINVAL))
ptr = mmap(addr, length, prot, flags, fd, offset);
}
return ptr;
}
#endif
static int stress_set_mmap_bytes(const char *opt)
{
size_t mmap_bytes;
mmap_bytes = (size_t)stress_get_uint64_byte_memory(opt, 1);
stress_check_range_bytes("mmap-bytes", mmap_bytes,
MIN_MMAP_BYTES, MAX_MMAP_BYTES);
return stress_set_setting("mmap-bytes", TYPE_ID_SIZE_T, &mmap_bytes);
}
static int stress_set_mmap_mprotect(const char *opt)
{
bool mmap_mprotect = true;
(void)opt;
return stress_set_setting("mmap-mprotect", TYPE_ID_BOOL, &mmap_mprotect);
}
static int stress_set_mmap_file(const char *opt)
{
bool mmap_file = true;
(void)opt;
return stress_set_setting("mmap-file", TYPE_ID_BOOL, &mmap_file);
}
static int stress_set_mmap_async(const char *opt)
{
bool mmap_async = true;
(void)opt;
return stress_set_setting("mmap-async", TYPE_ID_BOOL, &mmap_async);
}
static int stress_set_mmap_osync(const char *opt)
{
bool mmap_osync = true;
(void)opt;
return stress_set_setting("mmap-osync", TYPE_ID_BOOL, &mmap_osync);
}
static int stress_set_mmap_odirect(const char *opt)
{
bool mmap_odirect = true;
(void)opt;
return stress_set_setting("mmap-odirect", TYPE_ID_BOOL, &mmap_odirect);
}
static int stress_set_mmap_mmap2(const char *opt)
{
bool mmap_mmap2 = true;
(void)opt;
return stress_set_setting("mmap-mmap2", TYPE_ID_BOOL, &mmap_mmap2);
}
/*
* stress_mmap_mprotect()
* cycle through page settings on a region of mmap'd memory
*/
static void stress_mmap_mprotect(
const char *name,
void *addr,
const size_t len,
const size_t page_size,
const bool mmap_mprotect)
{
#if defined(HAVE_MPROTECT)
if (mmap_mprotect) {
void *last_page = (void *)(~(uintptr_t)0 & ~(page_size - 1));
/* Invalid mix of PROT_GROWSDOWN | PROT_GROWSUP */
#if defined(PROT_GROWSDOWN) && \
defined(PROT_GROWSUP)
VOID_RET(int, mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_GROWSDOWN | PROT_GROWSUP));
#endif
/* Invalid non-page aligned start address */
VOID_RET(int, mprotect((void *)(((uint8_t *)addr) + 7), len, PROT_READ | PROT_WRITE));
/* Exercise zero len (should succeed) */
VOID_RET(int, mprotect(addr, 0, PROT_READ | PROT_WRITE));
/* Exercise flags all set */
VOID_RET(int, mprotect(addr, len, ~0));
/* Exercise invalid unmapped addressed, should return ENOMEM */
VOID_RET(int, mprotect(last_page, page_size, PROT_READ | PROT_WRITE));
/* Exercise invalid wrapped range, should return EINVAL */
VOID_RET(int, mprotect(last_page, page_size << 1, PROT_READ | PROT_WRITE));
/* Cycle through potection */
if (mprotect(addr, len, PROT_NONE) < 0)
pr_fail("%s: mprotect set to PROT_NONE failed\n", name);
if (mprotect(addr, len, PROT_READ) < 0)
pr_fail("%s: mprotect set to PROT_READ failed\n", name);
if (mprotect(addr, len, PROT_WRITE) < 0)
pr_fail("%s: mprotect set to PROT_WRITE failed\n", name);
if (mprotect(addr, len, PROT_EXEC) < 0)
pr_fail("%s: mprotect set to PROT_EXEC failed\n", name);
if (mprotect(addr, len, PROT_READ | PROT_WRITE) < 0)
pr_fail("%s: mprotect set to PROT_READ | PROT_WRITE failed\n", name);
}
#else
(void)name;
(void)addr;
(void)len;
(void)page_size;
(void)mmap_mprotect;
#endif
}
/*
* stress_mmap_invalid()
* exercise invalid mmap mapping, munmap allocation if
* it succeeds (which it is not expected to do).
*/
static void stress_mmap_invalid(
void *addr,
size_t length,
int prot,
int flags,
int fd,
off_t offset)
{
void *ptr;
ptr = mmap(addr, length, prot, flags, fd, offset);
if (ptr != MAP_FAILED)
(void)munmap(ptr, length);
#if defined(__NR_mmap)
/*
* libc may detect offset is invalid and not do the syscall so
* do direct syscall if possible
*/
ptr = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset + 1);
if (ptr != MAP_FAILED)
(void)munmap(ptr, length);
#endif
/* Do the above via libc */
ptr = mmap(addr, length, prot, flags, fd, offset + 1);
if (ptr != MAP_FAILED)
(void)munmap(ptr, length);
}
static int stress_mmap_child(const stress_args_t *args, void *ctxt)
{
stress_mmap_context_t *context = (stress_mmap_context_t *)ctxt;
const size_t page_size = args->page_size;
const size_t sz = context->sz;
const size_t pages4k = sz / page_size;
const bool mmap_file = context->mmap_file;
const int fd = context->fd;
NOCLOBBER int no_mem_retries = 0;
const int bad_fd = stress_get_bad_fd();
const int ms_flags = context->mmap_async ? MS_ASYNC : MS_SYNC;
uint8_t *mapped, **mappings;
void *hint;
int ret, mask = ~0;
VOID_RET(int, stress_sighandler(args->name, SIGBUS, stress_mmap_sighandler, NULL));
mapped = calloc(pages4k, sizeof(*mapped));
if (!mapped) {
pr_dbg("%s: cannot allocate mapped buffer: %d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
}
mappings = calloc(pages4k, sizeof(*mappings));
if (!mappings) {
pr_dbg("%s: cannot allocate mappings buffer: %d (%s)\n",
args->name, errno, strerror(errno));
free(mapped);
return EXIT_NO_RESOURCE;
}
do {
size_t n;
int rnd, rnd_flag;
uint8_t *buf = NULL;
#if defined(MAP_HUGETLB) || \
defined(MAP_UNINITIALIZED) || \
defined(MAP_DENYWRITE)
retry:
#endif
if (no_mem_retries >= NO_MEM_RETRIES_MAX) {
pr_inf("%s: gave up trying to mmap, no available memory\n",
args->name);
break;
}
if (!keep_stressing_flag())
break;
/*
* ret is 1 if SIGBUS occurs, so re-try mmap. This
* can occur when Hugepages are allocated but not
* available to containers or pods that don't have
* access to the hugepages mount points. It's a useful
* corner case worth exercising to see if the kernel
* generates a SIGBUS, so we need to handle it.
*/
ret = sigsetjmp(jmp_env, 1);
if (ret == 1)
continue;
jmp_env_set = true;
rnd = stress_mwc32() % SIZEOF_ARRAY(mmap_flags); /* cppcheck-suppress moduloofone */
rnd_flag = mmap_flags[rnd];
/*
* ARM64, one can opt-int to getting VAs from 52 bit
* space by hinting with an address that is > 48 bits.
* Since this is a hint, we can try this for all
* architectures.
*/
hint = stress_mwc1() ? NULL : (void *)~(uintptr_t)0;
buf = (uint8_t *)context->mmap(hint, sz,
PROT_READ | PROT_WRITE, (context->flags | rnd_flag) & mask, fd, 0);
if (buf == MAP_FAILED) {
#if defined(MAP_POPULATE)
/* Force MAP_POPULATE off, just in case */
if (context->flags & MAP_POPULATE) {
context->flags &= ~MAP_POPULATE;
no_mem_retries++;
continue;
}
#endif
#if defined(MAP_HUGETLB)
/* Force MAP_HUGETLB off, just in case */
if (rnd_flag & MAP_HUGETLB) {
mask &= ~MAP_HUGETLB;
no_mem_retries++;
goto retry;
}
#endif
#if defined(MAP_UNINITIALIZED)
/* Force MAP_UNINITIALIZED off, just in case */
if (rnd_flag & MAP_UNINITIALIZED) {
mask &= ~MAP_UNINITIALIZED;
no_mem_retries++;
goto retry;
}
#endif
#if defined(MAP_DENYWRITE)
/* Force MAP_DENYWRITE off, just in case */
if (rnd_flag & MAP_DENYWRITE) {
mask &= ~MAP_DENYWRITE;
no_mem_retries++;
goto retry;
}
#endif
no_mem_retries++;
if (no_mem_retries > 1)
(void)shim_usleep(100000);
continue; /* Try again */
}
no_mem_retries = 0;
if (mmap_file) {
(void)memset(buf, 0xff, sz);
(void)shim_msync((void *)buf, sz, ms_flags);
}
(void)stress_madvise_random(buf, sz);
(void)stress_mincore_touch_pages(buf, context->mmap_bytes);
stress_mmap_mprotect(args->name, buf, sz, page_size, context->mmap_mprotect);
for (n = 0; n < pages4k; n++) {
mapped[n] = PAGE_MAPPED;
mappings[n] = buf + (n * page_size);
}
/* Ensure we can write to the mapped pages */
stress_mmap_set(buf, sz, page_size);
if (g_opt_flags & OPT_FLAGS_VERIFY) {
if (stress_mmap_check(buf, sz, page_size) < 0)
pr_fail("%s: mmap'd region of %zu bytes does "
"not contain expected data\n", args->name, sz);
}
/*
* Step #0, write + read the mmap'd data from the file back into
* the mappings.
*/
if ((fd >= 0) && (mmap_file)) {
off_t offset = 0;
for (n = 0; n < pages4k; n++, offset += page_size) {
if (lseek(fd, offset, SEEK_SET) < 0)
continue;
VOID_RET(ssize_t, write(fd, mappings[n], page_size));
VOID_RET(ssize_t, read(fd, mappings[n], page_size));
}
}
/*
* Step #1, unmap all pages in random order
*/
(void)stress_mincore_touch_pages(buf, context->mmap_bytes);
for (n = pages4k; n; ) {
uint64_t j, i = stress_mwc64() % pages4k;
for (j = 0; j < n; j++) {
uint64_t page = (i + j) % pages4k;
if (mapped[page] == PAGE_MAPPED) {
mapped[page] = 0;
#if defined(HAVE_MQUERY) && \
defined(MAP_FIXED)
{
/* Exercise OpenBSD mquery */
VOID_RET(void *, mquery(mappings[page], page_size,
PROT_READ, MAP_FIXED, -1, 0));
}
#endif
(void)stress_madvise_random(mappings[page], page_size);
stress_mmap_mprotect(args->name, mappings[page],
page_size, page_size, context->mmap_mprotect);
(void)munmap((void *)mappings[page], page_size);
n--;
break;
}
if (!keep_stressing_flag())
goto cleanup;
}
}
(void)munmap((void *)buf, sz);
#if defined(MAP_FIXED)
/*
* Step #2, map them back in random order
*/
for (n = pages4k; n; ) {
uint64_t j, i = stress_mwc64() % pages4k;
for (j = 0; j < n; j++) {
uint64_t page = (i + j) % pages4k;
if (!mapped[page]) {
off_t offset = mmap_file ? (off_t)(page * page_size) : 0;
int fixed_flags = MAP_FIXED;
/*
* Attempt to map them back into the original address, this
* may fail (it's not the most portable operation), so keep
* track of failed mappings too
*/
#if defined(MAP_FIXED_NOREPLACE)
if (stress_mwc1())
fixed_flags = MAP_FIXED_NOREPLACE;
#endif
mappings[page] = (uint8_t *)context->mmap((void *)mappings[page],
page_size, PROT_READ | PROT_WRITE, fixed_flags | context->flags, fd, offset);
if (mappings[page] == MAP_FAILED) {
mapped[page] = PAGE_MAPPED_FAIL;
mappings[page] = NULL;
} else {
(void)stress_mincore_touch_pages(mappings[page], page_size);
(void)stress_madvise_random(mappings[page], page_size);
stress_mmap_mprotect(args->name, mappings[page],
page_size, page_size, context->mmap_mprotect);
mapped[page] = PAGE_MAPPED;
/* Ensure we can write to the mapped page */
stress_mmap_set(mappings[page], page_size, page_size);
if (stress_mmap_check(mappings[page], page_size, page_size) < 0)
pr_fail("%s: mmap'd region of %zu bytes does "
"not contain expected data\n", args->name, page_size);
if (mmap_file) {
(void)memset(mappings[page], (int)n, page_size);
(void)shim_msync((void *)mappings[page], page_size, ms_flags);
#if defined(FALLOC_FL_KEEP_SIZE) && \
defined(FALLOC_FL_PUNCH_HOLE)
(void)shim_fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
offset, (off_t)page_size);
#endif
}
}
n--;
break;
}
if (!keep_stressing_flag())
goto cleanup;
}
}
#endif
cleanup:
/*
* Step #3, unmap them all
*/
for (n = 0; n < pages4k; n++) {
if (mapped[n] & PAGE_MAPPED) {
(void)stress_madvise_random(mappings[n], page_size);
stress_mmap_mprotect(args->name, mappings[n],
page_size, page_size, context->mmap_mprotect);
(void)munmap((void *)mappings[n], page_size);
}
}
/*
* Step #4, invalid unmapping on the first found page that
* was successfully mapped earlier. This page should be now
* unmapped so unmap it again in various ways
*/
for (n = 0; n < pages4k; n++) {
if (mapped[n] & PAGE_MAPPED) {
(void)munmap((void *)mappings[n], 0);
(void)munmap((void *)mappings[n], page_size);
break;
}
}
/*
* Step #5, invalid mappings
*/
stress_mmap_invalid(NULL, 0, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
stress_mmap_invalid((void *)(~(uintptr_t)0), 0, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
stress_mmap_invalid(NULL, ~(size_t)0, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
stress_mmap_invalid((void *)(~(uintptr_t)0), ~(size_t)0, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
stress_mmap_invalid(NULL, args->page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, bad_fd, 0);
stress_mmap_invalid(NULL, args->page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, bad_fd, 0);
if (fd >= 0)
stress_mmap_invalid(NULL, args->page_size << 2, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd,
(off_t)(((~(size_t)0) & ~(args->page_size - 1)) - args->page_size));
/*
* Step #6, invalid unmappings
*/
(void)munmap(NULL, 0);
(void)munmap(NULL, ~(size_t)0);
/*
* Step #7, random choice from any of the valid/invalid
* mmap flag permutations
*/
if ((context->mmap_prot_perms) && (context->mmap_prot_count > 0)) {
const size_t rnd_sz = stress_mwc16() % context->mmap_prot_count;
const int rnd_prot = context->mmap_prot_perms[rnd_sz];
buf = (uint8_t *)mmap(NULL, rnd_sz, rnd_prot, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (buf != MAP_FAILED)
(void)munmap((void *)buf, rnd_sz);
}
/*
* Step #8, work through all flag permutations
*/
if ((context->mmap_flag_perms) && (context->mmap_flag_count > 0)) {
static int index;
const int flag = context->mmap_flag_perms[index];
int tmpfd;
if (flag & MAP_ANONYMOUS)
tmpfd = -1;
else
tmpfd = open("/dev/zero", O_RDONLY);
buf = (uint8_t *)mmap(NULL, page_size, PROT_READ, flag, tmpfd, 0);
if (buf != MAP_FAILED)
(void)munmap((void *)buf, page_size);
if (tmpfd >= 0)
(void)close(tmpfd);
index++;
index %= context->mmap_flag_count;
}
inc_counter(args);
} while (keep_stressing(args));
jmp_env_set = false;
free(mappings);
free(mapped);
return EXIT_SUCCESS;
}
/*
* stress_mmap()
* stress mmap
*/
static int stress_mmap(const stress_args_t *args)
{
const size_t page_size = args->page_size;
char filename[PATH_MAX];
bool mmap_osync = false;
bool mmap_odirect = false;
bool mmap_mmap2 = false;
int ret, all_flags;
stress_mmap_context_t context;
size_t i;
jmp_env_set = false;
context.fd = -1;
context.mmap = (mmap_func_t)mmap;
context.mmap_bytes = DEFAULT_MMAP_BYTES;
context.mmap_async = false;
context.mmap_file = false;
context.mmap_mprotect = false;
context.flags = MAP_PRIVATE | MAP_ANONYMOUS;
#if defined(MAP_POPULATE)
context.flags |= MAP_POPULATE;
#endif
(void)stress_get_setting("mmap-async", &context.mmap_async);
(void)stress_get_setting("mmap-file", &context.mmap_file);
(void)stress_get_setting("mmap-mprotect", &context.mmap_mprotect);
(void)stress_get_setting("mmap-osync", &mmap_osync);
(void)stress_get_setting("mmap-odirect", &mmap_odirect);
(void)stress_get_setting("mmap-mmap2", &mmap_mmap2);
for (all_flags = 0, i = 0; i < SIZEOF_ARRAY(mmap_prot); i++)
all_flags |= mmap_prot[i];
context.mmap_prot_count = stress_flag_permutation(all_flags, &context.mmap_prot_perms);
for (all_flags = 0, i = 0; i < SIZEOF_ARRAY(mmap_std_flags); i++)
all_flags |= mmap_std_flags[i];
for (i = 0; i < SIZEOF_ARRAY(mmap_flags); i++)
all_flags |= mmap_flags[i];
context.mmap_flag_count = stress_flag_permutation(all_flags, &context.mmap_flag_perms);
if (mmap_osync || mmap_odirect)
context.mmap_file = true;
if (mmap_mmap2) {
#if defined(HAVE_MMAP2)
context.mmap = (mmap_func_t)mmap2_try;
#else
pr_inf("%s: using mmap instead of mmap2 as it is not available\n",
args->name);
#endif
}
if (!stress_get_setting("mmap-bytes", &context.mmap_bytes)) {
if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
context.mmap_bytes = MAX_32;
if (g_opt_flags & OPT_FLAGS_MINIMIZE)
context.mmap_bytes = MIN_MMAP_BYTES;
}
context.mmap_bytes /= args->num_instances;
if (context.mmap_bytes < MIN_MMAP_BYTES)
context.mmap_bytes = MIN_MMAP_BYTES;
if (context.mmap_bytes < page_size)
context.mmap_bytes = page_size;
context.sz = context.mmap_bytes & ~(page_size - 1);
if (context.mmap_file) {
int file_flags = O_CREAT | O_RDWR;
ssize_t wr_ret, rc;
rc = stress_temp_dir_mk_args(args);
if (rc < 0)
return exit_status((int)-rc);
(void)stress_temp_filename_args(args,
filename, sizeof(filename), stress_mwc32());
if (mmap_odirect) {
#if defined(O_DIRECT)
file_flags |= O_DIRECT;
#else
pr_inf("%s: --mmap-odirect selected by not supported by this system\n",
args->name);
UNEXPECTED
#endif
}
if (mmap_osync) {
#if defined(O_SYNC)
file_flags |= O_SYNC;
#else
pr_inf("%s: --mmap-osync selected by not supported by this system\n",
args->name);
UNEXPECTED
#endif
}
context.fd = open(filename, file_flags, S_IRUSR | S_IWUSR);
if (context.fd < 0) {
rc = exit_status(errno);
pr_fail("%s: open %s failed, errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
(void)shim_unlink(filename);
(void)stress_temp_dir_rm_args(args);
return (int)rc;
}
(void)shim_unlink(filename);
if (lseek(context.fd, (off_t)(context.sz - args->page_size), SEEK_SET) < 0) {
pr_fail("%s: lseek failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)close(context.fd);
(void)stress_temp_dir_rm_args(args);
return EXIT_FAILURE;
}
redo:
/*
* Write a page aligned chunk of data, we can
* use g_shared as this is mmap'd and hence
* page algned and always available for reading
*/
wr_ret = write(context.fd, g_shared, args->page_size);
if (wr_ret != (ssize_t)args->page_size) {
if ((errno == EAGAIN) || (errno == EINTR))
goto redo;
rc = exit_status(errno);
pr_fail("%s: write failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)close(context.fd);
(void)stress_temp_dir_rm_args(args);
return (int)rc;
}
context.flags &= ~(MAP_ANONYMOUS | MAP_PRIVATE);
context.flags |= MAP_SHARED;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
ret = stress_oomable_child(args, &context, stress_mmap_child, STRESS_OOMABLE_NORMAL);
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
if (context.mmap_file) {
(void)close(context.fd);
(void)stress_temp_dir_rm_args(args);
}
if (context.mmap_prot_perms)
free(context.mmap_prot_perms);
if (context.mmap_flag_perms)
free(context.mmap_flag_perms);
return ret;
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_mmap_async, stress_set_mmap_async },
{ OPT_mmap_bytes, stress_set_mmap_bytes },
{ OPT_mmap_file, stress_set_mmap_file },
{ OPT_mmap_mprotect, stress_set_mmap_mprotect },
{ OPT_mmap_osync, stress_set_mmap_osync },
{ OPT_mmap_odirect, stress_set_mmap_odirect },
{ OPT_mmap_mmap2, stress_set_mmap_mmap2 },
{ 0, NULL }
};
stressor_info_t stress_mmap_info = {
.stressor = stress_mmap,
.class = CLASS_VM | CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.verify = VERIFY_OPTIONAL,
.help = help
};