1 /*
2 * Copyright (c) 2018 SUSE Linux. All Rights Reserved.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Started by Jan Kara <jack@suse.cz>
18 * Chnaged to use fzsync library by Cyril Hrubis <chrubis@suse.cz>
19 *
20 * DESCRIPTION
21 * Test for inotify mark connector destruction race.
22 *
23 * Kernels prior to 4.17 have a race when the last fsnotify mark on the inode
24 * is being deleted while another process reports event happening on that
25 * inode. When the race is hit, the kernel crashes or loops.
26 *
27 * The problem has been fixed by commit:
28 * d90a10e2444b "fsnotify: Fix fsnotify_mark_connector race"
29 */
30
31 #include "config.h"
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <time.h>
38 #include <signal.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <sys/syscall.h>
42
43 #include "tst_test.h"
44 #include "tst_safe_pthread.h"
45 #include "tst_fuzzy_sync.h"
46 #include "inotify.h"
47
48 #define FNAME "stress_fname"
49
50 #if defined(HAVE_SYS_INOTIFY_H)
51 #include <sys/inotify.h>
52
53 static struct tst_fzsync_pair fzsync_pair;
54 static int fd;
55
write_seek(void * unused)56 static void *write_seek(void *unused)
57 {
58 char buf[64];
59
60 while (tst_fzsync_run_b(&fzsync_pair)) {
61 tst_fzsync_start_race_b(&fzsync_pair);
62 SAFE_WRITE(0, fd, buf, sizeof(buf));
63 SAFE_LSEEK(fd, 0, SEEK_SET);
64 tst_fzsync_end_race_b(&fzsync_pair);
65 }
66 return unused;
67 }
68
setup(void)69 static void setup(void)
70 {
71 fd = SAFE_OPEN(FNAME, O_CREAT | O_RDWR, 0600);
72 tst_fzsync_pair_init(&fzsync_pair);
73 }
74
cleanup(void)75 static void cleanup(void)
76 {
77 if (fd > 0)
78 SAFE_CLOSE(fd);
79
80 tst_fzsync_pair_cleanup(&fzsync_pair);
81 }
82
verify_inotify(void)83 static void verify_inotify(void)
84 {
85 int inotify_fd;
86 int wd;
87
88 inotify_fd = myinotify_init1(0);
89 if (inotify_fd < 0)
90 tst_brk(TBROK | TERRNO, "inotify_init failed");
91
92 tst_fzsync_pair_reset(&fzsync_pair, write_seek);
93 while (tst_fzsync_run_a(&fzsync_pair)) {
94 wd = myinotify_add_watch(inotify_fd, FNAME, IN_MODIFY);
95 if (wd < 0)
96 tst_brk(TBROK | TERRNO, "inotify_add_watch() failed.");
97
98 tst_fzsync_start_race_a(&fzsync_pair);
99 wd = myinotify_rm_watch(inotify_fd, wd);
100 tst_fzsync_end_race_a(&fzsync_pair);
101 if (wd < 0)
102 tst_brk(TBROK | TERRNO, "inotify_rm_watch() failed.");
103 }
104 SAFE_CLOSE(inotify_fd);
105 /* We survived for given time - test succeeded */
106 tst_res(TPASS, "kernel survived inotify beating");
107 }
108
109 static struct tst_test test = {
110 .needs_tmpdir = 1,
111 .setup = setup,
112 .cleanup = cleanup,
113 .test_all = verify_inotify,
114 };
115
116 #else
117 TST_TEST_TCONF("system doesn't have required inotify support");
118 #endif
119