Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp274765pxj; Thu, 17 Jun 2021 02:23:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwijJqDtKvlG4p2/ylPqsZ2MozMQOwZgjkQOvfg8asaU8NzFVgLPBVZELTdt1mGrKDGuJHQ X-Received: by 2002:a05:6402:310c:: with SMTP id dc12mr2846887edb.140.1623921812746; Thu, 17 Jun 2021 02:23:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623921812; cv=none; d=google.com; s=arc-20160816; b=vkqCO43NpPwcBSgse9GcD0SeiUeidh3J17jWXHjuAx3z7lCiHrpsnR3MJ1g3BzctVv 7mmi1VNraU3sffwpphuj6Be3g3Z4fJNo8d26hJqllJNn+nmuC9SouXR7TB5/ALUHL3+e EybnpycxX6mL1FRKP3gJcTtqCA1k8L7YWPBDhAeBsfE4i+O+MMft4fJeEVtNY0Dbl4Lr UdEVlt/gL3z/VwfDI43+BavGjjjt1/6oLB+1u0XKEPVvDWJJNCD0DYozU/5E4+y60F9P rtC44qP//eP7dhVdJ+nmqzjGamQUoxbnqyF9NE6SgfLjnkvn8hFUhWbWCs68iuzz/D2/ WX5Q== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=MjsgFk/OhGmlyJ9GkStbusQuxohzSr2Dr8cvabytXHY=; b=CRraW1Qh5TqruMJKxSZKvW+iBs7Qlw+ZC5n4a7EsJDUv++/ODllkznwW9Px4pCmCn+ pRzUDT3Mp484TjtLeLapdbHVzV4ChxV4EplHnpPrCcBtmnSQtm69SLfQKlq/W2FYt5CZ Pf6FmifoWSSVi5Ykhb01dD0sdE9UQnVBA11aMmtD+zgAGBPaW/ijQcJGql/gD8vkbW9f /GnqkZKFjyjTWjFJlPOTQRm9oc1WFrov5zKpaokeu8aSmbbf3gtrBWz3z+dehiDZlpkI tmRMMU/QI9+5pY911ONdcZIiP1EFC1uKz3TXg524WYd6p0pQFZhIKybO2eytyOyO1bs3 Fbgg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="RKBazyE/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bx28si4609523ejc.559.2021.06.17.02.23.09; Thu, 17 Jun 2021 02:23:32 -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=@gmail.com header.s=20161025 header.b="RKBazyE/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231513AbhFQJX5 (ORCPT + 99 others); Thu, 17 Jun 2021 05:23:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230162AbhFQJX5 (ORCPT ); Thu, 17 Jun 2021 05:23:57 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3034DC061574; Thu, 17 Jun 2021 02:21:50 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id m2so4457311pgk.7; Thu, 17 Jun 2021 02:21:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MjsgFk/OhGmlyJ9GkStbusQuxohzSr2Dr8cvabytXHY=; b=RKBazyE/iOSOGeAmvhw7ob6qBNoGdlYLkINn9y/wKri5cOtojSjkMkkzrbEDQkFmsw TG5olJiPpWKXPkGKaAHjSkPnJWaVzcGmWnh0f7Kn39y2rtkss5rS3tFcUudyHDTVUWFQ amg1ml5G8U0vdKR4Kolk4m6qO9gzk944/Uyngefd65QBCaonRbZG8gMyiuMeFwXx+awT sen/fAKLWgqzhYJIxxrl6fhbauI4/oBrO8Qwg6hwVwjqKNQ670xEXQCsTFfIZz0hDf79 Q+2jn7kCXWGXAFhpa2pqXZ/NCoOTG9plmXABtU3Nx+qKkJCC38g3Fxc0ZWPKwYfNb8la eFsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MjsgFk/OhGmlyJ9GkStbusQuxohzSr2Dr8cvabytXHY=; b=DE7/rgnH4LHZxgNDOy8XUaf2HH8tkCUzzMT6Kh/8ljZqijlT+rPR5YvOUSp2dCDRsH 0LsVB0r1u1THESizNcYgDup5xJLH81MQzzihrPJeDJT5cH7UELYzZs7LGShVVpuxiceY JRodk+PLU9gNtUraYxRq/UZ/dVx1/lb+rs/MfEKcxQySQq8vXwVguLEdh3gfMY/gTUqr chk3pzA9NxdLCeFVVV5R68TNkxKjR/K4S+1OUB9/TfdMUuJRB4Iu4BvNIYMnEO/5VcRd PPr4keeK8rRnHnlTtZrzFhtZpmq5+zZlI7SMl70/FCx2xnjTO2dzfh1UTy1na0OhBeCx OE0g== X-Gm-Message-State: AOAM533ctsW/w7/D5fvRez3JyrZMlBnG++nFq+Ar/7bb7U7ZsxcjKPJN AlVTlOFB9JKdWUbjpnH0Qcs= X-Received: by 2002:aa7:949c:0:b029:2fa:c881:dd0 with SMTP id z28-20020aa7949c0000b02902fac8810dd0mr4329426pfk.9.1623921709657; Thu, 17 Jun 2021 02:21:49 -0700 (PDT) Received: from localhost.localdomain ([118.200.190.93]) by smtp.gmail.com with ESMTPSA id v14sm5090542pgo.89.2021.06.17.02.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Jun 2021 02:21:49 -0700 (PDT) From: Desmond Cheong Zhi Xi To: axboe@kernel.dk Cc: Desmond Cheong Zhi Xi , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com Subject: [PATCH] block: break circular locks in blk_request_module Date: Thu, 17 Jun 2021 17:20:16 +0800 Message-Id: <20210617092016.522985-1-desmondcheongzx@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Syzbot reported a circular locking dependency: https://syzkaller.appspot.com/bug?id=7bd106c28e846d1023d4ca915718b1a0905444cb This happens because of the following lock dependencies: 1. loop_ctl_mutex -> bdev->bd_mutex (when loop_control_ioctl calls loop_remove, which then calls del_gendisk; this also happens in loop_exit which eventually calls loop_remove) 2. bdev->bd_mutex -> mtd_table_mutex (when blkdev_get_by_dev calls __blkdev_get, which then calls blktrans_open) 3. mtd_table_mutex -> major_names_lock (when register_mtd_blktrans calls __register_blkdev) 4. major_names_lock -> loop_ctl_mutex (when blk_request_module calls loop_probe) Hence there's an overall dependency of: loop_ctl_mutex ----------> bdev->bd_mutex ^ | | | | v major_names_lock <--------- mtd_table_mutex We can break this circular dependency by saving the reference to probe in blk_request_module, then calling it after releasing major_names_lock. This is safe because even if struct blk_major_name is freed, the reference to the probe function is still valid. Reported-and-tested-by: syzbot+6a8a0d93c91e8fbf2e80@syzkaller.appspotmail.com Signed-off-by: Desmond Cheong Zhi Xi --- block/genhd.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block/genhd.c b/block/genhd.c index 9f8cb7beaad1..ccaa5cf620f5 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -676,12 +676,14 @@ void blk_request_module(dev_t devt) { unsigned int major = MAJOR(devt); struct blk_major_name **n; + void (*probe)(dev_t devt); mutex_lock(&major_names_lock); for (n = &major_names[major_to_index(major)]; *n; n = &(*n)->next) { if ((*n)->major == major && (*n)->probe) { - (*n)->probe(devt); + probe = (*n)->probe; mutex_unlock(&major_names_lock); + probe(devt); return; } } -- 2.25.1