Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp3960581img; Mon, 25 Mar 2019 23:34:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqxoSZkXfFCvma3U9t71123+D0XQNW7m7cEkARAoPF1rnZa/W19EiAJreSDLuderKILDKJgV X-Received: by 2002:a17:902:2bc9:: with SMTP id l67mr29201159plb.102.1553582056686; Mon, 25 Mar 2019 23:34:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553582056; cv=none; d=google.com; s=arc-20160816; b=juayUDEm43XZnazYkenmMQ3BpQIxEEBWpslVWgZEec4HtW+rsEpGhoIZFR5N7rl9gd /qGtVLYQT+oWs5f/OsVWjKwy/PQgaaB4ECGFqCKeH+8O9rPMYF2xGpnD8STUZzincGE9 vNKwwquv+AsAuY83MmQgZf/Y6zdYi1JBAXCrC3r/OGl17/PwruwmScc2IIXS8j7Cfdqw ii/skoYqAcPIS1c3Ov2gcmiKI6QgWTzkyVDxBSr0FzHgaoAUdzeUIFHnLJc+7FK4gz3c okVQSlNRoZK9f6LsgUY+tA3Ba08DqnZqdG+wPlEIwhf+Ig9EwtwHM8k3TAkRJL2gzqq4 J4nw== 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=zChQqJd70R7jVR87haqKQdhBttYKLKdk882WmZndBcM=; b=SIwvxShVeQFWWbBSOQmFJX/4e66s5wGI4eMv6IJtf8ELOxyoEja8Uzr9gS86kCCJeW q3if1A3QJ3RxY/D3nN1osi2xCyjZ7qJwmwbTrsas75U6BgH7gj/pMw78JZ5mJN1KwKjA 3q4cqRSSmSfQxQU8nYRNLBCfATDDJw7FpxB9JscyXGcSYsozgBkEK7FVRVX+Vh0Sx2qP 6YeIh4qyN2mC76ugBgih12MuokoQr6Niffey/nEDaxWygMbgB8bcjGn2nFjA265Wdgbx oDSXBkS2rlZwR5XH/C1dYCQFleWXotUO9yQ3BIwPxoAfgsOxYev1H1/1ice7+WN9fECB yYZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iSmM6I7T; 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 j16si14931161pfa.197.2019.03.25.23.34.01; Mon, 25 Mar 2019 23:34:16 -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=iSmM6I7T; 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 S1731530AbfCZGcy (ORCPT + 99 others); Tue, 26 Mar 2019 02:32:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:42326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731497AbfCZGcw (ORCPT ); Tue, 26 Mar 2019 02:32:52 -0400 Received: from localhost (unknown [104.132.152.111]) (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 8BD8B20857; Tue, 26 Mar 2019 06:32:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553581972; bh=sBwvjyicwVeAiQTiK1lqJkRVfjphBUdDaAxmtOcHxRo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iSmM6I7TczVVcOc1ZcNgn5vikE5wuR+6woEvwQb2sDCfGeiqg6gWbEZCcBmaOR3w/ agHegtwEOFoz3DraUiv5LPY0gfuMlroL8wuN84zdeo2KVzjo67emTFCNB4nna62lA3 EJzWL3BCwHH1VlRmP/4epZFoussKCan1xzpYE6Qs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Frank Sorenson , Lukas Czerner , Theodore Tso Subject: [PATCH 4.9 12/30] ext4: fix data corruption caused by unaligned direct AIO Date: Tue, 26 Mar 2019 15:29:51 +0900 Message-Id: <20190326042607.991805470@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190326042607.558087893@linuxfoundation.org> References: <20190326042607.558087893@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lukas Czerner commit 372a03e01853f860560eade508794dd274e9b390 upstream. Ext4 needs to serialize unaligned direct AIO because the zeroing of partial blocks of two competing unaligned AIOs can result in data corruption. However it decides not to serialize if the potentially unaligned aio is past i_size with the rationale that no pending writes are possible past i_size. Unfortunately if the i_size is not block aligned and the second unaligned write lands past i_size, but still into the same block, it has the potential of corrupting the previous unaligned write to the same block. This is (very simplified) reproducer from Frank // 41472 = (10 * 4096) + 512 // 37376 = 41472 - 4096 ftruncate(fd, 41472); io_prep_pwrite(iocbs[0], fd, buf[0], 4096, 37376); io_prep_pwrite(iocbs[1], fd, buf[1], 4096, 41472); io_submit(io_ctx, 1, &iocbs[1]); io_submit(io_ctx, 1, &iocbs[2]); io_getevents(io_ctx, 2, 2, events, NULL); Without this patch the 512B range from 40960 up to the start of the second unaligned write (41472) is going to be zeroed overwriting the data written by the first write. This is a data corruption. 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 * 00009200 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 * 0000a000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 * 0000a200 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 With this patch the data corruption is avoided because we will recognize the unaligned_aio and wait for the unwritten extent conversion. 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 * 00009200 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 * 0000a200 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 * 0000b200 Reported-by: Frank Sorenson Signed-off-by: Lukas Czerner Signed-off-by: Theodore Ts'o Fixes: e9e3bcecf44c ("ext4: serialize unaligned asynchronous DIO") Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- fs/ext4/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -79,7 +79,7 @@ ext4_unaligned_aio(struct inode *inode, struct super_block *sb = inode->i_sb; int blockmask = sb->s_blocksize - 1; - if (pos >= i_size_read(inode)) + if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize)) return 0; if ((pos | iov_iter_alignment(from)) & blockmask)