Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp10428191ybi; Wed, 24 Jul 2019 23:02:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUQZKI+FsCUCRiZL/C0dWNQSdtslr4v21pxBkGxCQ192i5dYEPbxXM0Tnhwqeq/1c9PIFs X-Received: by 2002:a62:e716:: with SMTP id s22mr14903195pfh.250.1564034576283; Wed, 24 Jul 2019 23:02:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564034576; cv=none; d=google.com; s=arc-20160816; b=R9blNvw2YfGe9hzITZ3YDzijXsGS8OPbPEo5Ox6Z9aN28d+ge0u8QjXVUzXtdRCLN0 Er1kVyNWEMkmGFHpaBFoi1Y0HT3PTSGo85P2qjXAJ8gqzqbddC6GNPNLyskCLeKZsVtf eDwRGZp1qJ27MJPZVFLAdf5s6ICLfZrERlHw3R9RpD3YeMr6StgmGi65KUs5WnmbfffF 0SlUnZErWFiAGkh/koe/k5uXjpM2MHnvipD4GOofMDxRTnLwxwt6aHWF+Yf66JhiuOQH vxZ5q5XAWxwbR2YhnoWjhfk/4jRl3GIMJ0hkJXW44tWi3cKLyvq5tBgNHJR26b399xjW 7ZwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1eAWJXK3cn43kbZgUOE9h6IE3ut2DLsGhA17MWcLzXA=; b=cRl+VMFnQzL6HDQWvUpvNa/cqnHRp+BL/+nWE7+AFfBdUgF5noY9iaKk+fk/JV6irv HmKZsGxUUvz4QlHg26J8rLkF2jj4vv9fnjP9d32OcnaLGyHRHZajPGq0w66qBdUJrZ0v yMefXqZubtqJlLCQSRI3D8YBQe1neO8Wuioy5OFVDaxrMpAHpB5R/h/qXEtTfhuY0xTu q7Os/2VC4c4X4XEfyfR70B50bZlSdjcBOw0LwEnTTseIwxkffsnqp401isJXS47sQ7zx kTq/sRF31GNAL8zL5qbVb6PtVhLIxct0NRYKiv/1LuTiVjaMbZGeZ0yOjAseu7iRNxwm 4/qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Ihn/Yhyb"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e1si7849841pge.227.2019.07.24.23.02.38; Wed, 24 Jul 2019 23:02:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="Ihn/Yhyb"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727986AbfGYFqG (ORCPT + 99 others); Thu, 25 Jul 2019 01:46:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:33550 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726681AbfGYFqF (ORCPT ); Thu, 25 Jul 2019 01:46:05 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id B91FE21850; Thu, 25 Jul 2019 05:46:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564033564; bh=8vP176BE1QHHUg47JaiFC0gZi7QLD3qCYVXke8Y2W60=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ihn/YhybidxNgU10x2wsfBxJhufpuD2dGi6057icI2qrrbYjHyMu1CJ0b5KSb/CV0 aEEGkQBnil63k4Z1KetuIdc4AZROagBbJ6/1VXF6vsBIuWQVJeCttVrc7xF4JtH7Xf Ql+/dWxA2dXE2CqHJlQR8weSripwaw3+el9f96/Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Brian Foster , Allison Henderson , Dave Chinner , "Darrick J. Wong" , Luis Chamberlain , Sasha Levin Subject: [PATCH 4.19 249/271] xfs: serialize unaligned dio writes against all other dio writes Date: Wed, 24 Jul 2019 21:21:58 +0200 Message-Id: <20190724191716.531152633@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190724191655.268628197@linuxfoundation.org> References: <20190724191655.268628197@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org commit 2032a8a27b5cc0f578d37fa16fa2494b80a0d00a upstream. XFS applies more strict serialization constraints to unaligned direct writes to accommodate things like direct I/O layer zeroing, unwritten extent conversion, etc. Unaligned submissions acquire the exclusive iolock and wait for in-flight dio to complete to ensure multiple submissions do not race on the same block and cause data corruption. This generally works in the case of an aligned dio followed by an unaligned dio, but the serialization is lost if I/Os occur in the opposite order. If an unaligned write is submitted first and immediately followed by an overlapping, aligned write, the latter submits without the typical unaligned serialization barriers because there is no indication of an unaligned dio still in-flight. This can lead to unpredictable results. To provide proper unaligned dio serialization, require that such direct writes are always the only dio allowed in-flight at one time for a particular inode. We already acquire the exclusive iolock and drain pending dio before submitting the unaligned dio. Wait once more after the dio submission to hold the iolock across the I/O and prevent further submissions until the unaligned I/O completes. This is heavy handed, but consistent with the current pre-submission serialization for unaligned direct writes. Signed-off-by: Brian Foster Reviewed-by: Allison Henderson Reviewed-by: Dave Chinner Reviewed-by: Darrick J. Wong Signed-off-by: Darrick J. Wong Signed-off-by: Luis Chamberlain Signed-off-by: Sasha Levin --- fs/xfs/xfs_file.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c index 61a5ad2600e8..10f75965243c 100644 --- a/fs/xfs/xfs_file.c +++ b/fs/xfs/xfs_file.c @@ -529,18 +529,17 @@ xfs_file_dio_aio_write( count = iov_iter_count(from); /* - * If we are doing unaligned IO, wait for all other IO to drain, - * otherwise demote the lock if we had to take the exclusive lock - * for other reasons in xfs_file_aio_write_checks. + * If we are doing unaligned IO, we can't allow any other overlapping IO + * in-flight at the same time or we risk data corruption. Wait for all + * other IO to drain before we submit. If the IO is aligned, demote the + * iolock if we had to take the exclusive lock in + * xfs_file_aio_write_checks() for other reasons. */ if (unaligned_io) { - /* If we are going to wait for other DIO to finish, bail */ - if (iocb->ki_flags & IOCB_NOWAIT) { - if (atomic_read(&inode->i_dio_count)) - return -EAGAIN; - } else { - inode_dio_wait(inode); - } + /* unaligned dio always waits, bail */ + if (iocb->ki_flags & IOCB_NOWAIT) + return -EAGAIN; + inode_dio_wait(inode); } else if (iolock == XFS_IOLOCK_EXCL) { xfs_ilock_demote(ip, XFS_IOLOCK_EXCL); iolock = XFS_IOLOCK_SHARED; @@ -548,6 +547,14 @@ xfs_file_dio_aio_write( trace_xfs_file_direct_write(ip, count, iocb->ki_pos); ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io); + + /* + * If unaligned, this is the only IO in-flight. If it has not yet + * completed, wait on it before we release the iolock to prevent + * subsequent overlapping IO. + */ + if (ret == -EIOCBQUEUED && unaligned_io) + inode_dio_wait(inode); out: xfs_iunlock(ip, iolock); -- 2.20.1