Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp455013pxb; Thu, 9 Sep 2021 05:00:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1/jNo09vfgqQojipir5P+dXMvC0bmEV3q4M1lc6wyBgkvGSoVZgPmQiYW1LH1616/3v/4 X-Received: by 2002:a5d:8484:: with SMTP id t4mr2283510iom.126.1631188836717; Thu, 09 Sep 2021 05:00:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631188836; cv=none; d=google.com; s=arc-20160816; b=Rbi8TCIqyBjXNR5+Ob9lo+Kv5YouWtj+whHmjJFZ9qSjRk68iCGG6xnVyyQ9ya+UPq sBJbZbtyU4fXZ4sPeyULh/dFqAKBZ+NedLO5KOmDYVoojvumr5KfkO0e1DI/Kh5rFvxo nW3Ji0aYKpCXXhrUuiHtJgwKTFJeGKFFQZukATNQBzA/IF9L/pU80MdLNCWVnNMUvawV mE+4q4ZbQVTn5QLa4AL8CkpFMzkE9T+FRoF6nm65Mzyi6epdsPLdOFnDD5qAqmxnlFD6 RwPPBUXK3Nk56ZiXXMk12f7dqjJsFsjb4Oeg58FIlOraBIrB0qnmQoCgWjaA2GguraWq q4ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OOsYWH7ES3au2PFC8AxGTpPKKNn2vuFj7rwb8L6ONZI=; b=SfuqzxQudo/wyjUzBtcat65ZIasfZi1uWHPJ1GF6lSRoMAGvlfFVTjEF8HBvjHv/WE m2FW64bW5BKZvzWGxW31ZhVkD3tRH3XK5hMWu8A+m8PyMbtHHTb6U0H7Ol/Vc1jNDy3f JCZdpZF6SV7VLyCAWPX4+zHwDfxtUeh+HlSJwWTMylhFL1DLa6UpF2Y+ASZVI5+L4KOT Ieu26WjZcDZz9vp3tcLmSxvTOcVmfVFEJHtJd4BG7ieaoclUJC7ALnrYjmCdIPRsHhL1 H5ptihL2awQwlhjBOFxw+fw8fDbcsThlzNWArsrEHwyv2akIzSt62px5I7BXNjW2LdXf Ct7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OmV77zaA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u2si1541425ioc.87.2021.09.09.05.00.23; Thu, 09 Sep 2021 05:00:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OmV77zaA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240946AbhIIMAt (ORCPT + 99 others); Thu, 9 Sep 2021 08:00:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:34410 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242465AbhIIL4d (ORCPT ); Thu, 9 Sep 2021 07:56:33 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8A8CD600AA; Thu, 9 Sep 2021 11:45:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187917; bh=G80E6SOoKgwRQzLIPIMTCf16aVx9PPg6XET2sd6Jerk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OmV77zaArwykfhxwcXRZ5VA6zV6keqKaBOv1uDWNXeU+MnSTSkf7xlj1LYPiMPVTy 9UbAIq3DpFQk/6vUhm3G59YnaoiaYfWel2hK2o5nMeCNyyMPy/Nddr3GD+V470etcG bbyf3tTJTGvJV1S48VnqmpGW9j8L2JGHTf1Foosyyq81AnRM+Co3feS6ogHsCFE9Q1 yPX6irW3FdVT7/Y4dhKE3htV66RsIRdYPVH5F7v/TqBbL1cx+CklCOunwfpVzBY3ce 1eN2goovEyeB6BZhmOgsSRaqcIaA2vlZPZOpeB3STjoMDjXBV47K3ghb9W4W3ZkqNv czFx1HkFX6sSQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Qu Wenruo , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.14 192/252] btrfs: subpage: check if there are compressed extents inside one page Date: Thu, 9 Sep 2021 07:40:06 -0400 Message-Id: <20210909114106.141462-192-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit 3670e6451bc9c39ab3a46f1da19360219e4319f3 ] [BUG] When testing experimental subpage compressed write support, it hits a NULL pointer dereference inside read path: Unable to handle kernel NULL pointer dereference at virtual address 0000000000000018 pc : __pi_memcmp+0x28/0x1ec lr : check_data_csum+0xd0/0x274 [btrfs] Call trace: __pi_memcmp+0x28/0x1ec btrfs_verify_data_csum+0xf4/0x244 [btrfs] end_bio_extent_readpage+0x1d0/0x6b0 [btrfs] bio_endio+0x15c/0x1dc end_workqueue_fn+0x44/0x64 [btrfs] btrfs_work_helper+0x74/0x250 [btrfs] process_one_work+0x1d4/0x47c worker_thread+0x180/0x400 kthread+0x11c/0x120 ret_from_fork+0x10/0x30 Code: 54000261 d100044c d343fd8c f8408403 (f8408424) ---[ end trace 9e2c59f33ea40866 ]--- [CAUSE] When reading two compressed extents inside the same page, like the following layout, we trigger above crash: 0 32K 64K |-------|\\\\\\\| | \- Compressed extent (A) \--------- Compressed extent (B) For compressed read, we don't need to populate its io_bio->csum, as we rely on compressed_bio->csum to verify the compressed data, and then copy the decompressed to inode pages. Normally btrfs_verify_data_csum() skip such page by checking and clearing its PageChecked flag But since that flag is still for the full page, when endio for inode page range [0, 32K) gets executed, it clears PageChecked flag for the full page. Then when endio for inode page range [32K, 64K) gets executed, since the page no longer has PageChecked flag, it just continues checking, even though io_bio->csum is NULL. [FIX] Thankfully there are only two users of PageChecked bit: - Cow fixup Since subpage has its own way to trace page dirty (dirty_bitmap) and ordered bit (ordered_bitmap), it should never trigger cow fixup. - Compressed read We can distinguish such read by just checking io_bio->csum. So just check io_bio->csum before doing the verification to avoid such NULL pointer dereference. Signed-off-by: Qu Wenruo Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index ab04f9dd465e..ef6e1798fc2d 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -3257,6 +3257,20 @@ unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset, return 0; } + /* + * For subpage case, above PageChecked is not safe as it's not subpage + * compatible. + * But for now only cow fixup and compressed read utilize PageChecked + * flag, while in this context we can easily use io_bio->csum to + * determine if we really need to do csum verification. + * + * So for now, just exit if io_bio->csum is NULL, as it means it's + * compressed read, and its compressed data csum has already been + * verified. + */ + if (io_bio->csum == NULL) + return 0; + if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) return 0; -- 2.30.2