Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3629109pxb; Mon, 24 Jan 2022 13:55:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJwGRjU9HUgwvhAUkJkbs1SeMUfqZoHutZllQHQr2ozZIaY08w+YigMYm8oXJIie+1CdY0jr X-Received: by 2002:a17:902:9a49:b0:149:7b66:e85c with SMTP id x9-20020a1709029a4900b001497b66e85cmr16379537plv.66.1643061320059; Mon, 24 Jan 2022 13:55:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643061320; cv=none; d=google.com; s=arc-20160816; b=dhTMUgEPrkOE5PAY39rtiKRxCTAhbjMSxTJ8kXlkRleys6vXSYY01dkpfWqAhKKjvM uKAkUnqGOckTkrh5CzxtojDAnV56xcsEoliaZ9x+8RklAP1aoGx1juFYjfEaEiCrspga ulrWxqIPESRNPIH9kNgD6UlLMb8wV7ppmNRe3MSnLIiqRxs0nl25rjeX0HI2X/Uq5rqc jgnQGtQ3DetuD4apNSOeCGG9cgVYuosXjfvd06ryz0pRwYvIToKoV9lIbKpcfxEnNk8c 0sJO10Q4IeIzIH0KxjzyC5g8Tr7lourbVkW8+CcnY0DqTlaOZpnYpoUX/TUI3H5RjVMS XvOA== 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=5m+hhoxlMsvt33KDt1V3R/fOmdi6qeYMr25FBZZ0mjk=; b=LV2uSBfjHC+rXiLZXneHGFLFiPgkn6unsETlq4/LV3auQRMgtGWm+K7DnVwitFqcvK K4bgv/zQ5U1/90Kr/+otZ2lLCKVjbYJgq7M00JsLcdG/h+FtJbGh4V7wvEIZqyAeuVwb A+NaoJ1KsaSn9cWX0yqgqAA7VWFKo6JvGmjtz1LbXbfoioX8lIs8sKLwEa8CvZ81zMRo 5UehTzBlAQGQ0KSXusV16RFtYXIcra8FJm45mNpyQgZR2uJm+dEys+J3APxZWw2lHMMo 5dLVASlK2F5u0TfXQVMBXj5wTpxeTKwOxtp+8Nk+NVV6MRWQAWaVg5SrQFWSo3gk9nxZ qwBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=tdhOq8Fh; 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 b11si16979460pgi.186.2022.01.24.13.55.08; Mon, 24 Jan 2022 13:55:20 -0800 (PST) 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=tdhOq8Fh; 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 S1352923AbiAXVnr (ORCPT + 99 others); Mon, 24 Jan 2022 16:43:47 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:33854 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1447083AbiAXVKA (ORCPT ); Mon, 24 Jan 2022 16:10:00 -0500 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 dfw.source.kernel.org (Postfix) with ESMTPS id 6FB4461451; Mon, 24 Jan 2022 21:10:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50C8CC340E5; Mon, 24 Jan 2022 21:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058599; bh=xLe6OIxRf5HXXv5JzxhjAzembg+iCLkQRK2lINkvGpA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tdhOq8FhvLNKosgAgbk+IqRGc7ely3TkTwxfQ7sR6nfce2f6frId8naGvXnp0xYFe +oaVsfaXLEsHAnUVrC7+cMH+VwXZIQQLwUYjtVnU7/PRn4VDKBIfm5U/IbKWPl5h5P jKYiw1/AYUk95744hGzPX8N/XDGy0EazkZJBsz+0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot , Christoph Hellwig , Jens Axboe , Sasha Levin Subject: [PATCH 5.16 0334/1039] block: fix error unwinding in device_add_disk Date: Mon, 24 Jan 2022 19:35:23 +0100 Message-Id: <20220124184136.533605897@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@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: Christoph Hellwig [ Upstream commit 99d8690aae4b2f0d1d90075de355ac087f820a66 ] One device_add is called disk->ev will be freed by disk_release, so we should free it twice. Fix this by allocating disk->ev after device_add so that the extra local unwinding can be removed entirely. Based on an earlier patch from Tetsuo Handa. Reported-by: syzbot Tested-by: syzbot Fixes: 83cbce9574462c6b ("block: add error handling for device_add_disk / add_disk") Signed-off-by: Christoph Hellwig Link: https://lore.kernel.org/r/20211221161851.788424-1-hch@lst.de Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- block/genhd.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 30362aeacac4b..596e43764846b 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -437,10 +437,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, disk->flags |= GENHD_FL_EXT_DEVT; } - ret = disk_alloc_events(disk); - if (ret) - goto out_free_ext_minor; - /* delay uevents, until we scanned partition table */ dev_set_uevent_suppress(ddev, 1); @@ -451,7 +447,12 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk, ddev->devt = MKDEV(disk->major, disk->first_minor); ret = device_add(ddev); if (ret) - goto out_disk_release_events; + goto out_free_ext_minor; + + ret = disk_alloc_events(disk); + if (ret) + goto out_device_del; + if (!sysfs_deprecated) { ret = sysfs_create_link(block_depr, &ddev->kobj, kobject_name(&ddev->kobj)); @@ -539,8 +540,6 @@ out_del_block_link: sysfs_remove_link(block_depr, dev_name(ddev)); out_device_del: device_del(ddev); -out_disk_release_events: - disk_release_events(disk); out_free_ext_minor: if (disk->major == BLOCK_EXT_MAJOR) blk_free_ext_minor(disk->first_minor); -- 2.34.1