From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6B3EC43331 for ; Thu, 7 Jan 2021 12:42:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7CDCD2336D for ; Thu, 7 Jan 2021 12:42:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728645AbhAGMl7 (ORCPT ); Thu, 7 Jan 2021 07:41:59 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39078 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728126AbhAGMlz (ORCPT ); Thu, 7 Jan 2021 07:41:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1610023228; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xdr1bNi/8x8nhjwZ1YkQeFiw/2vRifC0b4Sbse7y77w=; b=gqaMBcYtUmGSyjTweoNkeNrW+268oDoytWJbX8kNf/6dro1fDHkbqdyrAEutMpsFcj/aBP 3V1GpKCLZvKRLzx+I7Hj2xKHMnVMoL363jbRqATtilXCOFjneFbXCKqBO8bnBn+b7nEr89 L1webUmFcEjXKMv8rrQZ60RESYYbbw0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-269-gEfkk03cPxeM1nhMd-DFLg-1; Thu, 07 Jan 2021 07:40:27 -0500 X-MC-Unique: gEfkk03cPxeM1nhMd-DFLg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9F2B9800D53; Thu, 7 Jan 2021 12:40:25 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C4F460C5C; Thu, 7 Jan 2021 12:40:23 +0000 (UTC) From: Maxim Levitsky To: linux-kernel@vger.kernel.org Cc: "Darrick J . Wong" , Alexander Viro , linux-fsdevel@vger.kernel.org (open list:FILESYSTEMS (VFS and infrastructure)), Jan Kara , Maxim Levitsky Subject: [PATCH] block: fallocate: avoid false positive on collision detection Date: Thu, 7 Jan 2021 14:40:22 +0200 Message-Id: <20210107124022.900172-1-mlevitsk@redhat.com> In-Reply-To: <45420b24124b5b91bc0a80a4abad2e06acb8c2b3.camel@redhat.com> References: <45420b24124b5b91bc0a80a4abad2e06acb8c2b3.camel@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Align start and end on page boundaries before calling invalidate_inode_pages2_range. This might allow us to miss a collision if the write and the discard were done to the same page and do overlap but it is still better than returning -EBUSY if those writes didn't overlap. Signed-off-by: Maxim Levitsky --- fs/block_dev.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/fs/block_dev.c b/fs/block_dev.c index 9e84b1928b94..97f0d16661b5 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -1970,6 +1970,7 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start, loff_t end = start + len - 1; loff_t isize; int error; + pgoff_t invalidate_first_page, invalidate_last_page; /* Fail if we don't recognize the flags. */ if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) @@ -2020,12 +2021,23 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start, /* * Invalidate again; if someone wandered in and dirtied a page, - * the caller will be given -EBUSY. The third argument is - * inclusive, so the rounding here is safe. + * the caller will be given -EBUSY. + * + * If the start/end of the range is not page aligned, exclude the + * non aligned regions to avoid false positives. */ + invalidate_first_page = DIV_ROUND_UP(start, PAGE_SIZE); + invalidate_last_page = end >> PAGE_SHIFT; + + if ((end + 1) & PAGE_MASK) + invalidate_last_page--; + + if (invalidate_last_page < invalidate_first_page) + return 0; + return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping, - start >> PAGE_SHIFT, - end >> PAGE_SHIFT); + invalidate_first_page, + invalidate_last_page); } const struct file_operations def_blk_fops = { -- 2.26.2