Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4546109pxj; Wed, 12 May 2021 07:56:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwU6PiNneuQ4+dYnlnu3hY69Bjakq5jjNT2abMIXtiTyRtl+BGsQOd5o4UJRU8ojEsEwQRj X-Received: by 2002:a54:4682:: with SMTP id k2mr20560681oic.107.1620831410365; Wed, 12 May 2021 07:56:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620831410; cv=none; d=google.com; s=arc-20160816; b=eCckVKLn/ayotfA+k2Scpy5HUDsDh5YEh5ydy4XB6CMW1HkMkQqbaaLMnUVzyJ/FI8 Agd/Yvgj/K09RTpgsmREwzmzJVHHRyX/7yXqXHhJzVd8+O8h9vrLUpFVzGMYuL1yZ6AW EfjDUhm081a5ywotDV7ZJqJdY/Rn/MXJ1Na7jo8eeJDTRuqNCeh3kxJtzEzHKmEYA1W8 cPnV8NAao4dXWZJXUqgDZuIqjPBbzU4K9jRKDKBAqWPGUErEl5WJC9mVRVgN4geiPkDN 88LGcgldvCdz0mntVBhmtZoHW+APZnRx3jDS0fXD1W5adBsYUrhG66QiGSQlGoNldLfI dvyA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BbT8D55rn6isck44EhgcTA92/IxQslhuECvo5J+iISg=; b=yCiuINjlwhvfvuBpa9LtAN53ETe5ZT93j/M/gZA2eWx8EUfJHUGyE8kYxz5a1z1uey 7Q/Amvr+fsSC5guS/r9yz2l5+bn5w1zxfLgDCTS9hjdw6AX2Dxh+6p3m5tu6gmO0iu5Y 0allIQH/+OD0/S99EOZGAE6TnZFy6SxvN8sy5mmcBCZnMVdCCQ4sy4+x6celdPwKGyhV T36UQ5PtE6YPdHOIwnkLw5vMCZH/HoHF6dCYL6ghkgwuttqEJviT4ksAklINAaLDCLtj 2Kn7br535cMzH3Kqc3Cg3LkJ6et98PT+WuertjvFDPqV82/RGRwgoyzYRySJBbGyUYj8 g+Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eq2149TG; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d7si238281oif.136.2021.05.12.07.56.36; Wed, 12 May 2021 07:56:50 -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=@linuxfoundation.org header.s=korg header.b=eq2149TG; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231907AbhELOzF (ORCPT + 99 others); Wed, 12 May 2021 10:55:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:43946 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231744AbhELOyk (ORCPT ); Wed, 12 May 2021 10:54:40 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 325E961433; Wed, 12 May 2021 14:53:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620831211; bh=FL9rwGVxNoAXLtIRyyPbdu6Ztu+DAGO57rH+wtguWsU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eq2149TGIxPYAnC2S6V5BBvibCsgV3lGlsK99DcQPAVhm/yXErJevT/zFUtdhQW1C 0h8j5V5Juer4Hjux6D0m8DVzpeu3zo5v6rs8tBNxnZ5YUaYwuxXbaMDbBMdy4Dqjhw Ti54/4UsqnoQdFpuAXoLx2KnYrvP4+jWKwgeIH5w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sudhakar Panneerselvam , Zhao Heming , Song Liu Subject: [PATCH 5.4 029/244] md/bitmap: wait for external bitmap writes to complete during tear down Date: Wed, 12 May 2021 16:46:40 +0200 Message-Id: <20210512144743.988336036@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144743.039977287@linuxfoundation.org> References: <20210512144743.039977287@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sudhakar Panneerselvam commit 404a8ef512587b2460107d3272c17a89aef75edf upstream. NULL pointer dereference was observed in super_written() when it tries to access the mddev structure. [The below stack trace is from an older kernel, but the problem described in this patch applies to the mainline kernel.] [ 1194.474861] task: ffff8fdd20858000 task.stack: ffffb99d40790000 [ 1194.488000] RIP: 0010:super_written+0x29/0xe1 [ 1194.499688] RSP: 0018:ffff8ffb7fcc3c78 EFLAGS: 00010046 [ 1194.512477] RAX: 0000000000000000 RBX: ffff8ffb7bf4a000 RCX: ffff8ffb78991048 [ 1194.527325] RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8ffb56b8a200 [ 1194.542576] RBP: ffff8ffb7fcc3c90 R08: 000000000000000b R09: 0000000000000000 [ 1194.558001] R10: ffff8ffb56b8a298 R11: 0000000000000000 R12: ffff8ffb56b8a200 [ 1194.573070] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 [ 1194.588117] FS: 0000000000000000(0000) GS:ffff8ffb7fcc0000(0000) knlGS:0000000000000000 [ 1194.604264] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1194.617375] CR2: 00000000000002b8 CR3: 00000021e040a002 CR4: 00000000007606e0 [ 1194.632327] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 1194.647865] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 1194.663316] PKRU: 55555554 [ 1194.674090] Call Trace: [ 1194.683735] [ 1194.692948] bio_endio+0xae/0x135 [ 1194.703580] blk_update_request+0xad/0x2fa [ 1194.714990] blk_update_bidi_request+0x20/0x72 [ 1194.726578] __blk_end_bidi_request+0x2c/0x4d [ 1194.738373] __blk_end_request_all+0x31/0x49 [ 1194.749344] blk_flush_complete_seq+0x377/0x383 [ 1194.761550] flush_end_io+0x1dd/0x2a7 [ 1194.772910] blk_finish_request+0x9f/0x13c [ 1194.784544] scsi_end_request+0x180/0x25c [ 1194.796149] scsi_io_completion+0xc8/0x610 [ 1194.807503] scsi_finish_command+0xdc/0x125 [ 1194.818897] scsi_softirq_done+0x81/0xde [ 1194.830062] blk_done_softirq+0xa4/0xcc [ 1194.841008] __do_softirq+0xd9/0x29f [ 1194.851257] irq_exit+0xe6/0xeb [ 1194.861290] do_IRQ+0x59/0xe3 [ 1194.871060] common_interrupt+0x1c6/0x382 [ 1194.881988] [ 1194.890646] RIP: 0010:cpuidle_enter_state+0xdd/0x2a5 [ 1194.902532] RSP: 0018:ffffb99d40793e68 EFLAGS: 00000246 ORIG_RAX: ffffffffffffff43 [ 1194.917317] RAX: ffff8ffb7fce27c0 RBX: ffff8ffb7fced800 RCX: 000000000000001f [ 1194.932056] RDX: 0000000000000000 RSI: 0000000000000004 RDI: 0000000000000000 [ 1194.946428] RBP: ffffb99d40793ea0 R08: 0000000000000004 R09: 0000000000002ed2 [ 1194.960508] R10: 0000000000002664 R11: 0000000000000018 R12: 0000000000000003 [ 1194.974454] R13: 000000000000000b R14: ffffffff925715a0 R15: 0000011610120d5a [ 1194.988607] ? cpuidle_enter_state+0xcc/0x2a5 [ 1194.999077] cpuidle_enter+0x17/0x19 [ 1195.008395] call_cpuidle+0x23/0x3a [ 1195.017718] do_idle+0x172/0x1d5 [ 1195.026358] cpu_startup_entry+0x73/0x75 [ 1195.035769] start_secondary+0x1b9/0x20b [ 1195.044894] secondary_startup_64+0xa5/0xa5 [ 1195.084921] RIP: super_written+0x29/0xe1 RSP: ffff8ffb7fcc3c78 [ 1195.096354] CR2: 00000000000002b8 bio in the above stack is a bitmap write whose completion is invoked after the tear down sequence sets the mddev structure to NULL in rdev. During tear down, there is an attempt to flush the bitmap writes, but for external bitmaps, there is no explicit wait for all the bitmap writes to complete. For instance, md_bitmap_flush() is called to flush the bitmap writes, but the last call to md_bitmap_daemon_work() in md_bitmap_flush() could generate new bitmap writes for which there is no explicit wait to complete those writes. The call to md_bitmap_update_sb() will return simply for external bitmaps and the follow-up call to md_update_sb() is conditional and may not get called for external bitmaps. This results in a kernel panic when the completion routine, super_written() is called which tries to reference mddev in the rdev that has been set to NULL(in unbind_rdev_from_array() by tear down sequence). The solution is to call md_super_wait() for external bitmaps after the last call to md_bitmap_daemon_work() in md_bitmap_flush() to ensure there are no pending bitmap writes before proceeding with the tear down. Cc: stable@vger.kernel.org Signed-off-by: Sudhakar Panneerselvam Reviewed-by: Zhao Heming Signed-off-by: Song Liu Signed-off-by: Greg Kroah-Hartman --- drivers/md/md-bitmap.c | 2 ++ 1 file changed, 2 insertions(+) --- a/drivers/md/md-bitmap.c +++ b/drivers/md/md-bitmap.c @@ -1726,6 +1726,8 @@ void md_bitmap_flush(struct mddev *mddev md_bitmap_daemon_work(mddev); bitmap->daemon_lastrun -= sleep; md_bitmap_daemon_work(mddev); + if (mddev->bitmap_info.external) + md_super_wait(mddev); md_bitmap_update_sb(bitmap); }