From patchwork Thu Nov 26 17:10:20 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 7708031 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 9DA509F401 for ; Thu, 26 Nov 2015 17:10:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BA6092066C for ; Thu, 26 Nov 2015 17:10:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9766E205C2 for ; Thu, 26 Nov 2015 17:10:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752895AbbKZRKf (ORCPT ); Thu, 26 Nov 2015 12:10:35 -0500 Received: from mx2.suse.de ([195.135.220.15]:59711 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751474AbbKZRKe (ORCPT ); Thu, 26 Nov 2015 12:10:34 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id F3B2EAAC7; Thu, 26 Nov 2015 17:08:53 +0000 (UTC) Received: by quack.suse.cz (Postfix, from userid 1000) id 2488D82827; Thu, 26 Nov 2015 18:10:32 +0100 (CET) From: Jan Kara To: Al Viro Cc: Christoph Hellwig , linux-fsdevel@vger.kernel.org, Dmitry Monakhov , Jan Kara Subject: [PATCH] aio: Fix freeze protection of aio writes Date: Thu, 26 Nov 2015 18:10:20 +0100 Message-Id: <1448557820-26891-1-git-send-email-jack@suse.cz> X-Mailer: git-send-email 2.1.4 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-7.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently we dropped freeze protection of aio writes just after IO was submitted. Thus aio write could be in flight while the filesystem was frozen and that could result in unexpected situation like aio completion wanting to convert extent type on frozen filesystem. Testcase from Dmitry triggering this is like: for ((i=0;i<60;i++));do fsfreeze -f /mnt ;sleep 1;fsfreeze -u /mnt;done & fio --bs=4k --ioengine=libaio --iodepth=128 --size=1g --direct=1 \ --runtime=60 --filename=/mnt/file --name=rand-write --rw=randwrite Fix the problem by dropping freeze protection only once IO is completed in aio_complete(). Reported-by: Dmitry Monakhov Signed-off-by: Jan Kara --- fs/aio.c | 31 ++++++++++++++++++++++++++++--- include/linux/fs.h | 1 + 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index 155f84253f33..947481e0a691 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1065,6 +1065,19 @@ static void aio_complete(struct kiocb *kiocb, long res, long res2) unsigned tail, pos, head; unsigned long flags; + if (kiocb->ki_flags & IOCB_WRITE) { + struct file *f = kiocb->ki_filp; + + /* + * Tell lockdep we inherited freeze protection from submission + * thread. + */ + percpu_rwsem_acquire( + &f->f_inode->i_sb->s_writers.rw_sem[SB_FREEZE_WRITE-1], + 1, _THIS_IP_); + file_end_write(f); + } + /* * Special case handling for sync iocbs: * - events go directly into the iocb for fast handling @@ -1449,13 +1462,25 @@ rw_common: len = ret; - if (rw == WRITE) + if (rw == WRITE) { file_start_write(file); + req->ki_flags |= IOCB_WRITE; + } ret = iter_op(req, &iter); - if (rw == WRITE) - file_end_write(file); + if (rw == WRITE) { + /* + * We release freeze protection in aio_complete(). Fool + * lockdep by telling it the lock got released so that + * it doesn't complain about held lock when we return + * to userspace. + */ + percpu_rwsem_release( + &file->f_inode->i_sb->s_writers.rw_sem[SB_FREEZE_WRITE-1], + 1, _THIS_IP_); + } + kfree(iovec); break; diff --git a/include/linux/fs.h b/include/linux/fs.h index 3aa514254161..54af40ed6a26 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -319,6 +319,7 @@ struct writeback_control; #define IOCB_EVENTFD (1 << 0) #define IOCB_APPEND (1 << 1) #define IOCB_DIRECT (1 << 2) +#define IOCB_WRITE (1 << 3) struct kiocb { struct file *ki_filp;