1 From: ilbsmart <wgqimut@gmail.com>
2 Date: Wed, 17 Oct 2018 02:11:24 +0800
3 Subject: deadlock between mm_sem and tx assign in zfs_write() and page fault
6 1. thread #1, `zfs_write` assign a txg "n".
7 2. In a same process, thread #2, mmap page fault (which means the
8 `mm_sem` is hold) occurred, `zfs_dirty_inode` open a txg failed,
9 and wait previous txg "n" completed.
10 3. thread #1 call `uiomove` to write, however page fault is occurred
11 in `uiomove`, which means it need `mm_sem`, but `mm_sem` is hold by
12 thread #2, so it stuck and can't complete, then txg "n" will
15 So thread #1 and thread #2 are deadlocked.
17 Reviewed-by: Chunwei Chen <tuxoko@gmail.com>
18 Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
19 Reviewed-by: Matthew Ahrens <mahrens@delphix.com>
20 Signed-off-by: Grady Wong <grady.w@xtaotech.com>
23 (backported from: zfs-upstream 779a6c0bf6df76e0dd92c1ccf81f48512b835bb0)
24 Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
26 include/sys/uio_impl.h | 2 +-
27 module/zcommon/zfs_uio.c | 31 ++++-
28 module/zfs/zfs_vnops.c | 24 +++-
29 tests/zfs-tests/cmd/mmapwrite/mmapwrite.c | 140 +++++++++++++++------
30 .../tests/functional/mmap/mmap_write_001_pos.ksh | 8 +-
31 5 files changed, 151 insertions(+), 54 deletions(-)
33 diff --git a/include/sys/uio_impl.h b/include/sys/uio_impl.h
34 index 37e283d..cfef0b9 100644
35 --- a/include/sys/uio_impl.h
36 +++ b/include/sys/uio_impl.h
40 extern int uiomove(void *, size_t, enum uio_rw, uio_t *);
41 -extern void uio_prefaultpages(ssize_t, uio_t *);
42 +extern int uio_prefaultpages(ssize_t, uio_t *);
43 extern int uiocopy(void *, size_t, enum uio_rw, uio_t *, size_t *);
44 extern void uioskip(uio_t *, size_t);
46 diff --git a/module/zcommon/zfs_uio.c b/module/zcommon/zfs_uio.c
47 index 7b4175b..8e969bb 100644
48 --- a/module/zcommon/zfs_uio.c
49 +++ b/module/zcommon/zfs_uio.c
51 #include <sys/types.h>
52 #include <sys/uio_impl.h>
53 #include <linux/kmap_compat.h>
54 +#include <linux/uaccess.h>
57 * Move "n" bytes at byte address "p"; "rw" indicates the direction
58 @@ -77,8 +78,24 @@ uiomove_iov(void *p, size_t n, enum uio_rw rw, struct uio *uio)
59 if (copy_to_user(iov->iov_base+skip, p, cnt))
62 - if (copy_from_user(p, iov->iov_base+skip, cnt))
64 + if (uio->uio_fault_disable) {
65 + if (!access_ok(VERIFY_READ,
66 + (iov->iov_base + skip), cnt)) {
70 + pagefault_disable();
71 + if (__copy_from_user_inatomic(p,
72 + (iov->iov_base + skip), cnt)) {
78 + if (copy_from_user(p,
79 + (iov->iov_base + skip), cnt))
85 @@ -156,7 +173,7 @@ EXPORT_SYMBOL(uiomove);
86 * error will terminate the process as this is only a best attempt to get
91 uio_prefaultpages(ssize_t n, struct uio *uio)
93 const struct iovec *iov;
94 @@ -170,7 +187,7 @@ uio_prefaultpages(ssize_t n, struct uio *uio)
95 switch (uio->uio_segflg) {
103 @@ -194,7 +211,7 @@ uio_prefaultpages(ssize_t n, struct uio *uio)
104 p = iov->iov_base + skip;
106 if (fuword8((uint8_t *)p, &tmp))
109 incr = MIN(cnt, PAGESIZE);
112 @@ -204,8 +221,10 @@ uio_prefaultpages(ssize_t n, struct uio *uio)
115 if (fuword8((uint8_t *)p, &tmp))
122 EXPORT_SYMBOL(uio_prefaultpages);
124 diff --git a/module/zfs/zfs_vnops.c b/module/zfs/zfs_vnops.c
125 index 5a2e55e..c866352 100644
126 --- a/module/zfs/zfs_vnops.c
127 +++ b/module/zfs/zfs_vnops.c
128 @@ -675,7 +675,10 @@ zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
129 xuio = (xuio_t *)uio;
132 - uio_prefaultpages(MIN(n, max_blksz), uio);
133 + if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
135 + return (SET_ERROR(EFAULT));
139 * If in append mode, set the io offset pointer to eof.
140 @@ -820,8 +823,19 @@ zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
143 tx_bytes = uio->uio_resid;
144 + uio->uio_fault_disable = B_TRUE;
145 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
147 + if (error == EFAULT) {
149 + if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
153 + } else if (error != 0) {
157 tx_bytes -= uio->uio_resid;
160 @@ -921,8 +935,12 @@ zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
161 ASSERT(tx_bytes == nbytes);
164 - if (!xuio && n > 0)
165 - uio_prefaultpages(MIN(n, max_blksz), uio);
166 + if (!xuio && n > 0) {
167 + if (uio_prefaultpages(MIN(n, max_blksz), uio)) {
174 zfs_inode_update(zp);
175 diff --git a/tests/zfs-tests/cmd/mmapwrite/mmapwrite.c b/tests/zfs-tests/cmd/mmapwrite/mmapwrite.c
176 index 190d31a..b9915d5 100644
177 --- a/tests/zfs-tests/cmd/mmapwrite/mmapwrite.c
178 +++ b/tests/zfs-tests/cmd/mmapwrite/mmapwrite.c
181 #include <sys/mman.h>
187 * --------------------------------------------------------------------
189 + * Bug Issue Id: #7512
190 + * The bug time sequence:
191 + * 1. context #1, zfs_write assign a txg "n".
192 + * 2. In the same process, context #2, mmap page fault (which means the mm_sem
193 + * is hold) occurred, zfs_dirty_inode open a txg failed, and wait previous
194 + * txg "n" completed.
195 + * 3. context #1 call uiomove to write, however page fault is occurred in
196 + * uiomove, which means it need mm_sem, but mm_sem is hold by
197 + * context #2, so it stuck and can't complete, then txg "n" will not
200 - * Simply writing to a file and mmaping that file at the same time can
201 - * result in deadlock. Nothing perverse like writing from the file's
202 - * own mapping is required.
203 + * So context #1 and context #2 trap into the "dead lock".
204 * --------------------------------------------------------------------
207 +#define NORMAL_WRITE_TH_NUM 2
211 +normal_writer(void *filename)
214 - int fd = *(int *)fdp;
215 + char *file_path = filename;
217 + ssize_t write_num = 0;
218 + int page_size = getpagesize();
221 - mmap(0, 8192, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
224 + fd = open(file_path, O_RDWR | O_CREAT, 0777);
226 + err(1, "failed to open %s", file_path);
229 - if (mmap(addr, 8192, PROT_READ,
230 - MAP_SHARED|MAP_FIXED, fd, 0) == MAP_FAILED) {
234 + char *buf = malloc(1);
236 + write_num = write(fd, buf, 1);
237 + if (write_num == 0) {
238 + err(1, "write failed!");
241 + lseek(fd, page_size, SEEK_CUR);
248 - return ((void *)1);
252 -main(int argc, char **argv)
254 +map_writer(void *filename)
262 + int page_size = getpagesize();
264 + char *file_path = filename;
266 - memset(buf, 'a', sizeof (buf));
268 + ret = access(file_path, F_OK);
271 + if (op_errno == ENOENT) {
272 + fd = open(file_path, O_RDWR | O_CREAT, 0777);
274 + err(1, "open file failed");
278 - (void) printf("usage: %s <file name>\n", argv[0]);
281 + ret = ftruncate(fd, page_size);
283 + err(1, "truncate file failed");
286 + err(1, "access file failed!");
289 + fd = open(file_path, O_RDWR, 0777);
291 + err(1, "open file failed");
295 - if ((fd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1) {
298 + if ((buf = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
299 + MAP_SHARED, fd, 0)) == MAP_FAILED) {
300 + err(1, "map file failed");
306 + char s[10] = {0, };
307 + memcpy(buf, s, 10);
308 + ret = munmap(buf, page_size);
310 + err(1, "unmap file failed");
315 - (void) pthread_setconcurrency(2);
316 - if (pthread_create(&tid, NULL, mapper, &fd) != 0) {
317 - perror("pthread_create");
320 +main(int argc, char **argv)
322 + pthread_t map_write_tid;
323 + pthread_t normal_write_tid[NORMAL_WRITE_TH_NUM];
327 + (void) printf("usage: %s <normal write file name>"
328 + "<map write file name>\n", argv[0]);
332 - if (write(fd, buf, sizeof (buf)) == -1) {
337 + for (i = 0; i < NORMAL_WRITE_TH_NUM; i++) {
338 + if (pthread_create(&normal_write_tid[i], NULL, normal_writer,
340 + err(1, "pthread_create normal_writer failed.");
345 + if (pthread_create(&map_write_tid, NULL, map_writer, argv[2])) {
346 + err(1, "pthread_create map_writer failed.");
350 + pthread_join(map_write_tid, NULL);
353 diff --git a/tests/zfs-tests/tests/functional/mmap/mmap_write_001_pos.ksh b/tests/zfs-tests/tests/functional/mmap/mmap_write_001_pos.ksh
354 index 1eda971..24150b8 100755
355 --- a/tests/zfs-tests/tests/functional/mmap/mmap_write_001_pos.ksh
356 +++ b/tests/zfs-tests/tests/functional/mmap/mmap_write_001_pos.ksh
357 @@ -53,12 +53,14 @@ if ! is_mp; then
360 log_must chmod 777 $TESTDIR
361 -mmapwrite $TESTDIR/test-write-file &
362 +mmapwrite $TESTDIR/normal_write_file $TESTDIR/map_write_file &
364 -log_note "mmapwrite $TESTDIR/test-write-file pid: $PID_MMAPWRITE"
365 +log_note "mmapwrite $TESTDIR/normal_write_file $TESTDIR/map_write_file"\
366 + "pid: $PID_MMAPWRITE"
369 log_must kill -9 $PID_MMAPWRITE
370 -log_must ls -l $TESTDIR/test-write-file
371 +log_must ls -l $TESTDIR/normal_write_file
372 +log_must ls -l $TESTDIR/map_write_file
374 log_pass "write(2) a mmap(2)'ing file succeeded."