From patchwork Sat Apr 23 10:07:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Marinas X-Patchwork-Id: 12824455 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 803CEC433EF for ; Sat, 23 Apr 2022 10:10:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Mh3JCHcj/deE9hwMJ8+9J8aF/NXBaDpwWSu9eLc1qsg=; b=CsIN+VTNc1W/Zd 7AejNuMimwtUsKElHH5NXlIQ+slu+4fXhyzh+JwiONb6UOvl8fO9ZBBgeCYskLgFbURugwdc7NOH+ Ml2WosU5gjFFy65sivGDd1GKEMO5Hs8yLut2jIzi5kKmmW+mztuq39j3SGK4276zyOYeUO/3iee8W dQraopnNs4cFnJ3X2LDCgHJLtHZK0C33uU8c2ouM+w6G+JHng6JEY+AE5fZ3QGBD21Un2yw5FcG8v FUATUBvcZUvEOUAneKAiY8mdWtYmOPhOMOPrsS9bH/J1ZLJMxkhtCkFR3NlTl+UcNrbZLxcCy8Iaw JYp8ktGIOTW6TJ+4JzHg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1niChW-0042rS-Cp; Sat, 23 Apr 2022 10:09:14 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1niCgQ-0042MK-NK for linux-arm-kernel@lists.infradead.org; Sat, 23 Apr 2022 10:08:08 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 2E879B80AD4; Sat, 23 Apr 2022 10:08:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDB8AC385A5; Sat, 23 Apr 2022 10:08:01 +0000 (UTC) From: Catalin Marinas To: Andrew Morton Cc: Linus Torvalds , Andreas Gruenbacher , Josef Bacik , Al Viro , Chris Mason , David Sterba , Will Deacon , linux-fsdevel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/3] btrfs: Avoid live-lock in search_ioctl() on hardware with sub-page faults Date: Sat, 23 Apr 2022 11:07:51 +0100 Message-Id: <20220423100751.1870771-4-catalin.marinas@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220423100751.1870771-1-catalin.marinas@arm.com> References: <20220423100751.1870771-1-catalin.marinas@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220423_030806_980660_74D4ECE0 X-CRM114-Status: GOOD ( 14.15 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Commit a48b73eca4ce ("btrfs: fix potential deadlock in the search ioctl") addressed a lockdep warning by pre-faulting the user pages and attempting the copy_to_user_nofault() in an infinite loop. On architectures like arm64 with MTE, an access may fault within a page at a location different from what fault_in_writeable() probed. Since the sk_offset is rewound to the previous struct btrfs_ioctl_search_header boundary, there is no guaranteed forward progress and search_ioctl() may live-lock. Use fault_in_subpage_writeable() instead of fault_in_writeable() to ensure the permission is checked at the right granularity (smaller than PAGE_SIZE). Signed-off-by: Catalin Marinas Fixes: a48b73eca4ce ("btrfs: fix potential deadlock in the search ioctl") Reported-by: Al Viro Acked-by: David Sterba Cc: Chris Mason Cc: Josef Bacik --- fs/btrfs/ioctl.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index be6c24577dbe..9bf0616a3069 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -2565,7 +2565,12 @@ static noinline int search_ioctl(struct inode *inode, while (1) { ret = -EFAULT; - if (fault_in_writeable(ubuf + sk_offset, *buf_size - sk_offset)) + /* + * Ensure that the whole user buffer is faulted in at sub-page + * granularity, otherwise the loop may live-lock. + */ + if (fault_in_subpage_writeable(ubuf + sk_offset, + *buf_size - sk_offset)) break; ret = btrfs_search_forward(root, &key, path, sk->min_transid);