1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 SUSE Linux. All Rights Reserved.
4 * Author: Jan Kara <jack@suse.cz>
5 * Chnaged to use fzsync library by Cyril Hrubis <chrubis@suse.cz>
6 *
7 * DESCRIPTION
8 * Test for inotify mark connector destruction race.
9 *
10 * Kernels prior to 4.17 have a race when the last fsnotify mark on the inode
11 * is being deleted while another process reports event happening on that
12 * inode. When the race is hit, the kernel crashes or loops.
13 *
14 * The problem has been fixed by commit:
15 * d90a10e2444b "fsnotify: Fix fsnotify_mark_connector race"
16 */
17
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <time.h>
25 #include <signal.h>
26 #include <sys/time.h>
27 #include <sys/wait.h>
28 #include <sys/syscall.h>
29
30 #include "tst_test.h"
31 #include "tst_safe_pthread.h"
32 #include "tst_fuzzy_sync.h"
33 #include "inotify.h"
34
35 #define FNAME "stress_fname"
36
37 #if defined(HAVE_SYS_INOTIFY_H)
38 #include <sys/inotify.h>
39
40 static struct tst_fzsync_pair fzsync_pair;
41 static int fd;
42
write_seek(void * unused)43 static void *write_seek(void *unused)
44 {
45 char buf[64];
46
47 while (tst_fzsync_run_b(&fzsync_pair)) {
48 tst_fzsync_start_race_b(&fzsync_pair);
49 SAFE_WRITE(0, fd, buf, sizeof(buf));
50 SAFE_LSEEK(fd, 0, SEEK_SET);
51 tst_fzsync_end_race_b(&fzsync_pair);
52 }
53 return unused;
54 }
55
setup(void)56 static void setup(void)
57 {
58 fd = SAFE_OPEN(FNAME, O_CREAT | O_RDWR, 0600);
59 tst_fzsync_pair_init(&fzsync_pair);
60 }
61
cleanup(void)62 static void cleanup(void)
63 {
64 if (fd > 0)
65 SAFE_CLOSE(fd);
66
67 tst_fzsync_pair_cleanup(&fzsync_pair);
68 }
69
verify_inotify(void)70 static void verify_inotify(void)
71 {
72 int inotify_fd;
73 int wd;
74
75 inotify_fd = SAFE_MYINOTIFY_INIT1(0);
76
77 tst_fzsync_pair_reset(&fzsync_pair, write_seek);
78 while (tst_fzsync_run_a(&fzsync_pair)) {
79 wd = SAFE_MYINOTIFY_ADD_WATCH(inotify_fd, FNAME, IN_MODIFY);
80
81 tst_fzsync_start_race_a(&fzsync_pair);
82 wd = myinotify_rm_watch(inotify_fd, wd);
83 tst_fzsync_end_race_a(&fzsync_pair);
84 if (wd < 0)
85 tst_brk(TBROK | TERRNO, "inotify_rm_watch() failed.");
86 }
87 SAFE_CLOSE(inotify_fd);
88 /* We survived for given time - test succeeded */
89 tst_res(TPASS, "kernel survived inotify beating");
90 }
91
92 static struct tst_test test = {
93 .needs_tmpdir = 1,
94 .setup = setup,
95 .cleanup = cleanup,
96 .test_all = verify_inotify,
97 };
98
99 #else
100 TST_TEST_TCONF("system doesn't have required inotify support");
101 #endif
102