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