From patchwork Thu Dec 13 22:25:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 10730017 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D54A2112E for ; Thu, 13 Dec 2018 22:25:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C5F812C95D for ; Thu, 13 Dec 2018 22:25:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B81B32C961; Thu, 13 Dec 2018 22:25:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6E24E2C95D for ; Thu, 13 Dec 2018 22:25:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728020AbeLMWZn (ORCPT ); Thu, 13 Dec 2018 17:25:43 -0500 Received: from sandeen.net ([63.231.237.45]:44424 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727901AbeLMWZn (ORCPT ); Thu, 13 Dec 2018 17:25:43 -0500 Received: by sandeen.net (Postfix, from userid 500) id ABDA633D1; Thu, 13 Dec 2018 16:25:32 -0600 (CST) From: Eric Sandeen To: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Christoph@sandeen.net, Hellwig@sandeen.net, Subject: [PATCH 1/3] iomap: use SECTOR_SIZE instead of 512 in iomap_page Date: Thu, 13 Dec 2018 16:25:27 -0600 Message-Id: <1544739929-21651-2-git-send-email-sandeen@sandeen.net> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> References: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Sandeen because iomap_page_create initializes the uptodate bitmap using the macro, not the open-coded value. Signed-off-by: Eric Sandeen Signed-off-by: Eric Sandeen --- include/linux/iomap.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 9a42581..edcdb3a 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -104,12 +105,13 @@ struct iomap_ops { /* * Structure allocate for each page when block size < PAGE_SIZE to track - * sub-page uptodate status and I/O completions. + * sub-page uptodate status and I/O completions. Allocate bitmap to + * accomodate blocks as small as SECTOR_SIZE */ struct iomap_page { atomic_t read_count; atomic_t write_count; - DECLARE_BITMAP(uptodate, PAGE_SIZE / 512); + DECLARE_BITMAP(uptodate, PAGE_SIZE / SECTOR_SIZE); }; static inline struct iomap_page *to_iomap_page(struct page *page) From patchwork Thu Dec 13 22:25:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 10730013 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BEC62174F for ; Thu, 13 Dec 2018 22:25:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B04DC2C95D for ; Thu, 13 Dec 2018 22:25:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A425E2C961; Thu, 13 Dec 2018 22:25:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41D832C960 for ; Thu, 13 Dec 2018 22:25:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728077AbeLMWZn (ORCPT ); Thu, 13 Dec 2018 17:25:43 -0500 Received: from sandeen.net ([63.231.237.45]:44426 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727904AbeLMWZn (ORCPT ); Thu, 13 Dec 2018 17:25:43 -0500 Received: by sandeen.net (Postfix, from userid 500) id C839433D3; Thu, 13 Dec 2018 16:25:32 -0600 (CST) From: Eric Sandeen To: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Christoph@sandeen.net, Hellwig@sandeen.net, Subject: [PATCH 2/3] iomap: don't search past page end in iomap_is_partially_uptodate Date: Thu, 13 Dec 2018 16:25:28 -0600 Message-Id: <1544739929-21651-3-git-send-email-sandeen@sandeen.net> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> References: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Sandeen iomap_is_partially_uptodate() is intended to check wither blocks within the selected range of a not-uptodate page are uptodate; if the range we care about is up to date, it's an optimization. However, the iomap implementation continues to check all blocks up to from+count, which is beyond the page, and can even be well beyond the iop->uptodate bitmap. I think the worst that will happen is that we may eventually find a zero bit and return "not partially uptodate" when it would have otherwise returned true, and skip the optimization. Still, it's clearly an invalid memory access that must be fixed. So: fix this by limiting the search to within the page as is done in the non-iomap variant, block_is_partially_uptodate(). Zorro noticed thiswhen KASAN went off for 512 byte blocks on a 64k page system: BUG: KASAN: slab-out-of-bounds in iomap_is_partially_uptodate+0x1a0/0x1e0 Read of size 8 at addr ffff800120c3a318 by task fsstress/22337 Reported-by: Zorro Lang Signed-off-by: Eric Sandeen Signed-off-by: Eric Sandeen Reviewed-by: Dave Chinner --- fs/iomap.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fs/iomap.c b/fs/iomap.c index d6bc98a..ce837d9 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -492,16 +492,29 @@ struct iomap_readpage_ctx { } EXPORT_SYMBOL_GPL(iomap_readpages); +/* + * iomap_is_partially_uptodate checks whether blocks within a page are + * uptodate or not. + * + * Returns true if all blocks which correspond to a file portion + * we want to read within the page are uptodate. + */ int iomap_is_partially_uptodate(struct page *page, unsigned long from, unsigned long count) { struct iomap_page *iop = to_iomap_page(page); struct inode *inode = page->mapping->host; - unsigned first = from >> inode->i_blkbits; - unsigned last = (from + count - 1) >> inode->i_blkbits; + unsigned len, first, last; unsigned i; + /* Limit range to one page */ + len = min_t(unsigned, PAGE_SIZE - from, count); + + /* First and last blocks in range within page */ + first = from >> inode->i_blkbits; + last = (from + len - 1) >> inode->i_blkbits; + if (iop) { for (i = first; i <= last; i++) if (!test_bit(i, iop->uptodate)) From patchwork Thu Dec 13 22:25:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 10730025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A8389112E for ; Thu, 13 Dec 2018 22:25:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9993F2C95D for ; Thu, 13 Dec 2018 22:25:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8E58E2C961; Thu, 13 Dec 2018 22:25:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E6B72C960 for ; Thu, 13 Dec 2018 22:25:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728168AbeLMWZq (ORCPT ); Thu, 13 Dec 2018 17:25:46 -0500 Received: from sandeen.net ([63.231.237.45]:44428 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727911AbeLMWZn (ORCPT ); Thu, 13 Dec 2018 17:25:43 -0500 Received: by sandeen.net (Postfix, from userid 500) id D713333D5; Thu, 13 Dec 2018 16:25:32 -0600 (CST) From: Eric Sandeen To: linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Christoph@sandeen.net, Hellwig@sandeen.net, Subject: [PATCH 3/3] iomap: optimize iomap_is_partially_uptodate for full page range Date: Thu, 13 Dec 2018 16:25:29 -0600 Message-Id: <1544739929-21651-4-git-send-email-sandeen@sandeen.net> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> References: <1544739929-21651-1-git-send-email-sandeen@sandeen.net> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Sandeen We only call ->is_partially_uptodate to look for an uptodate range within a not-uptodate page. If the range covers all blocks in the page, there is no point to checking each block individually - if the whole range (i.e. the whole page) were uptodate, the page would be uptodate as well. Hence in this case, we can return early and skip the loop. This is similar to what is done in block_is_partially_uptodate(). Signed-off-by: Eric Sandeen Signed-off-by: Eric Sandeen --- fs/iomap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/iomap.c b/fs/iomap.c index ce837d9..7d7d985 100644 --- a/fs/iomap.c +++ b/fs/iomap.c @@ -515,6 +515,10 @@ struct iomap_readpage_ctx { first = from >> inode->i_blkbits; last = (from + len - 1) >> inode->i_blkbits; + /* If page wasn't uptodate and range covers all blocks: no partial */ + if (first == 0 && last == (PAGE_SIZE - 1) >> inode->i_blkbits) + return 0; + if (iop) { for (i = first; i <= last; i++) if (!test_bit(i, iop->uptodate))