Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp461389yba; Wed, 15 May 2019 04:31:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqxO98w4I5husvbgVasOnL8vYEiHUw+zoi9FOYVe76Y8PyRFNANLca49Lr4OvguOErb3eQTE X-Received: by 2002:a63:6a42:: with SMTP id f63mr44130468pgc.377.1557919893597; Wed, 15 May 2019 04:31:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557919893; cv=none; d=google.com; s=arc-20160816; b=ePgFv/WWVbqTsVoz3aG/LBTdAKKK3XOZEHQcl2Wlj06qRZe1m4YDwNYVdhPUO5+hM3 VxKJY1lwz27OTgLcwIJHVSCFadilZcEFgQ6VGZJHx7WvIvOyG2HepO45KG8tOlg+oO/g qy/arIgU6M+SNKeSyWaW7LcUh1+NQo+3QE0LWt8eUVy+9kVvxnIHDRlsNfDzEdP/IsNk yYc0MP/jmi0B1SEbC1aVhYA2SvMgkivol7TsViLtuMkasc7dKQ4QIMMmhI6fG5ftO9ZL IWbcAkdI4RGLV+tMcDtJbYu/F+4/krG9ujcLDlibuDeXlBakPdcwDoz2SqcmtkSYQYTp waWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Br1unnTEoa5uNmHyAxzExojgmPTIrFVvEOJiW9js/kU=; b=pYu1d2XkSSma0QuaQjXOb4yt++G/pTQTJK5AMWMrB9XzMKzuiK42g0pBfJY2I8Lp3w E9sy8plehAuZd8XodDb/HZU62e8MvMSUHmAPXwe1+dkzjBcFrS90oHqlTnuPPaF6XYpF 0zu4KxOgafG/9N1WCeTCfAX6B94m4hqDSW3jcgY7070ldXSFzq831tZ1CFpZ3vtD3bgS 7uMK+M9qDlHBnZHDp/RMPStJCB1m/+xtaRU4+PjqjGrtVI5FQNcg6C+4D4wG3fCBcUQF LmVCsnJlaQK8fSndUq5BhdsE4gSfbU9KIgdzabHLOSPqbrKbnT9QsKbGvxmxKHF1lKXS 6+RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wraTOP3g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f14si1726488pfd.228.2019.05.15.04.31.18; Wed, 15 May 2019 04:31:33 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wraTOP3g; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732307AbfEOL3q (ORCPT + 99 others); Wed, 15 May 2019 07:29:46 -0400 Received: from mail.kernel.org ([198.145.29.99]:41086 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729916AbfEOL3o (ORCPT ); Wed, 15 May 2019 07:29:44 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 41BD621473; Wed, 15 May 2019 11:29:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557919783; bh=j0IGcgF6+P9TRifH3fJfkrkLZujfAjY6qjAoiIJAT+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wraTOP3gT4i4nA7F3KX2CQZ3Nlu7KYQAFT9vY9re/cPJEe7Kn/5Pte9j/tjs7+PwR Nr0/PueTUq3v+TpZrk+F3pTf2veCF+ZTALBm0365fT04NPfx3eqgwPs/6VuxpyEUsm jraiQSHRILHid42glwL+u5CxzRzqW9dbCbeTvDjE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Geert Uytterhoeven , Mukesh Ojha , Linus Walleij , Sasha Levin Subject: [PATCH 5.0 093/137] gpio: Fix gpiochip_add_data_with_key() error path Date: Wed, 15 May 2019 12:56:14 +0200 Message-Id: <20190515090700.291749384@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190515090651.633556783@linuxfoundation.org> References: <20190515090651.633556783@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 357798909164bf423eac6a78ff7da7e98d2d7f7f ] The err_remove_chip block is too coarse, and may perform cleanup that must not be done. E.g. if of_gpiochip_add() fails, of_gpiochip_remove() is still called, causing: OF: ERROR: Bad of_node_put() on /soc/gpio@e6050000 CPU: 1 PID: 20 Comm: kworker/1:1 Not tainted 5.1.0-rc2-koelsch+ #407 Hardware name: Generic R-Car Gen2 (Flattened Device Tree) Workqueue: events deferred_probe_work_func [] (unwind_backtrace) from [] (show_stack+0x10/0x14) [] (show_stack) from [] (dump_stack+0x7c/0x9c) [] (dump_stack) from [] (kobject_put+0x94/0xbc) [] (kobject_put) from [] (gpiochip_add_data_with_key+0x8d8/0xa3c) [] (gpiochip_add_data_with_key) from [] (gpio_rcar_probe+0x1d4/0x314) [] (gpio_rcar_probe) from [] (platform_drv_probe+0x48/0x94) and later, if a GPIO consumer tries to use a GPIO from a failed controller: WARNING: CPU: 0 PID: 1 at lib/refcount.c:156 kobject_get+0x38/0x4c refcount_t: increment on 0; use-after-free. Modules linked in: CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.1.0-rc2-koelsch+ #407 Hardware name: Generic R-Car Gen2 (Flattened Device Tree) [] (unwind_backtrace) from [] (show_stack+0x10/0x14) [] (show_stack) from [] (dump_stack+0x7c/0x9c) [] (dump_stack) from [] (__warn+0xd0/0xec) [] (__warn) from [] (warn_slowpath_fmt+0x44/0x6c) [] (warn_slowpath_fmt) from [] (kobject_get+0x38/0x4c) [] (kobject_get) from [] (of_node_get+0x14/0x1c) [] (of_node_get) from [] (of_find_node_by_phandle+0xc0/0xf0) [] (of_find_node_by_phandle) from [] (of_phandle_iterator_next+0x68/0x154) [] (of_phandle_iterator_next) from [] (__of_parse_phandle_with_args+0x40/0xd0) [] (__of_parse_phandle_with_args) from [] (of_parse_phandle_with_args_map+0x100/0x3ac) [] (of_parse_phandle_with_args_map) from [] (of_get_named_gpiod_flags+0x38/0x380) [] (of_get_named_gpiod_flags) from [] (gpiod_get_from_of_node+0x24/0xd8) [] (gpiod_get_from_of_node) from [] (devm_fwnode_get_index_gpiod_from_child+0xa0/0x144) [] (devm_fwnode_get_index_gpiod_from_child) from [] (gpio_keys_probe+0x418/0x7bc) [] (gpio_keys_probe) from [] (platform_drv_probe+0x48/0x94) Fix this by splitting the cleanup block, and adding a missing call to gpiochip_irqchip_remove(). Fixes: 28355f81969962cf ("gpio: defer probe if pinctrl cannot be found") Signed-off-by: Geert Uytterhoeven Reviewed-by: Mukesh Ojha Signed-off-by: Linus Walleij Signed-off-by: Sasha Levin --- drivers/gpio/gpiolib.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index d1adfdf50fb30..34fbf879411f6 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -1379,7 +1379,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, status = gpiochip_add_irqchip(chip, lock_key, request_key); if (status) - goto err_remove_chip; + goto err_free_gpiochip_mask; status = of_gpiochip_add(chip); if (status) @@ -1387,7 +1387,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, status = gpiochip_init_valid_mask(chip); if (status) - goto err_remove_chip; + goto err_remove_of_chip; for (i = 0; i < chip->ngpio; i++) { struct gpio_desc *desc = &gdev->descs[i]; @@ -1415,14 +1415,18 @@ int gpiochip_add_data_with_key(struct gpio_chip *chip, void *data, if (gpiolib_initialized) { status = gpiochip_setup_dev(gdev); if (status) - goto err_remove_chip; + goto err_remove_acpi_chip; } return 0; -err_remove_chip: +err_remove_acpi_chip: acpi_gpiochip_remove(chip); +err_remove_of_chip: gpiochip_free_hogs(chip); of_gpiochip_remove(chip); +err_remove_chip: + gpiochip_irqchip_remove(chip); +err_free_gpiochip_mask: gpiochip_free_valid_mask(chip); err_remove_irqchip_mask: gpiochip_irqchip_free_valid_mask(chip); -- 2.20.1