Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp3666858ima; Mon, 4 Feb 2019 03:06:58 -0800 (PST) X-Google-Smtp-Source: ALg8bN4olKhtzMZ54a+Q0I9FrBPM7ueRPBP1+DHIpZddDW7TWHxMoMsGUxMflJ5fiPf/DbbnYgcz X-Received: by 2002:a17:902:a6:: with SMTP id a35mr51092476pla.201.1549278418310; Mon, 04 Feb 2019 03:06:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549278418; cv=none; d=google.com; s=arc-20160816; b=fcjTwD11LLhcaw2iRm3e/76ubCNXXj8QRGlIj6QFUjEx2yjOzp5XHZ2KbUR0qAtTAD 1bF9tAcIZmt9pzR4EuEVEU+iAm9Kr2G7FZ8WLt1mW8mUE9I1UL8eX3ECeZlULHiVeGzg 28U5jsCjYQWi8Lht761lxq1yMOWvyc/QggoFCsVL1Ho2AHNNuSs7aVMfwB/H6aJVv/i+ UZ24QlbCMoBcSykAeVnocCxE+XeP+gFCLCNYhCzuHJLutAG+axvFVJq/OaWExRVSPKAt w2k7IRx/7j6Bub2RDzI509RQZjvGEKINiqO9knWESqgPaQvTAUHxzmj+SDjwW1xmOBv7 R3wg== 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=/PJkMZCB3wtXUeImGnqurNX9Z9Nyb4z+0rEQrlB52NA=; b=sPRCY+dkKcvA0hHe9WBH64l4sIqhyhpGLWzoxeffiCRbiGFOWEyZK25Iq1bXsEl5oo B0AZsnQ8quCU6t90S7tVhXPx/v4hmYLxQ2SCaxgt1bImVEgWdWVJd2ajo86zoaAFQAEt b80m6JlSqtPthF0JQRf/OYobvF/U2SC1MVJlepW33FCnGNgSf48zf+zL7SXvUNyWeFRp 2TnVyxHksGKHH79pQfX3nX4siHxtXz8U1YV1iKCPs8UDzjpGIDy4OuhPGjfMKSOJkJsn 5j/LEUU/45woxbeiKarWMQypfp5YTkHqAkB5RRL8bnU+MwkwfbNRS+/goqPp1gV5Gwch s/mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=j+PeDbuO; 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 r1si15964946plb.330.2019.02.04.03.06.42; Mon, 04 Feb 2019 03:06:58 -0800 (PST) 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=j+PeDbuO; 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 S1730927AbfBDKoM (ORCPT + 99 others); Mon, 4 Feb 2019 05:44:12 -0500 Received: from mail.kernel.org ([198.145.29.99]:41954 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730914AbfBDKoK (ORCPT ); Mon, 4 Feb 2019 05:44:10 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 6E2E7217F9; Mon, 4 Feb 2019 10:44:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549277048; bh=Lcnf4sb7+YDkW16nk+f31Ag5RSWgeUL4n9bfZS+kMms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j+PeDbuO5zeVhaTUDXrqpIVh/YyYIdlltxPn1SUS4vAZKUtt5fjpXM5BO+0eqQiz8 FOLPFuWkUDcJBELUikaDLzhZUpbZa40HlwCxGTwUq8Ufd78RKXALandTkg93PuEYnd 0F/DQS3FwIGOTFOlVdcKrqYp2gAXMl8QqdtkT5wU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Benjamin Herrenschmidt , Linus Torvalds , Zubin Mithra , Guenter Roeck Subject: [PATCH 4.9 28/30] drivers: core: Remove glue dirs from sysfs earlier Date: Mon, 4 Feb 2019 11:37:06 +0100 Message-Id: <20190204103610.559380598@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190204103605.271746870@linuxfoundation.org> References: <20190204103605.271746870@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Benjamin Herrenschmidt commit 726e41097920a73e4c7c33385dcc0debb1281e18 upstream. For devices with a class, we create a "glue" directory between the parent device and the new device with the class name. This directory is never "explicitely" removed when empty however, this is left to the implicit sysfs removal done by kobject_release() when the object loses its last reference via kobject_put(). This is problematic because as long as it's not been removed from sysfs, it is still present in the class kset and in sysfs directory structure. The presence in the class kset exposes a use after free bug fixed by the previous patch, but the presence in sysfs means that until the kobject is released, which can take a while (especially with kobject debugging), any attempt at re-creating such as binding a new device for that class/parent pair, will result in a sysfs duplicate file name error. This fixes it by instead doing an explicit kobject_del() when the glue dir is empty, by keeping track of the number of child devices of the gluedir. This is made easy by the fact that all glue dir operations are done with a global mutex, and there's already a function (cleanup_glue_dir) called in all the right places taking that mutex that can be enhanced for this. It appears that this was in fact the intent of the function, but the implementation was wrong. Signed-off-by: Benjamin Herrenschmidt Acked-by: Linus Torvalds Signed-off-by: Zubin Mithra Cc: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- drivers/base/core.c | 2 ++ include/linux/kobject.h | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -862,6 +862,8 @@ static void cleanup_glue_dir(struct devi return; mutex_lock(&gdp_mutex); + if (!kobject_has_children(glue_dir)) + kobject_del(glue_dir); kobject_put(glue_dir); mutex_unlock(&gdp_mutex); } --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -113,6 +113,23 @@ extern void kobject_put(struct kobject * extern const void *kobject_namespace(struct kobject *kobj); extern char *kobject_get_path(struct kobject *kobj, gfp_t flag); +/** + * kobject_has_children - Returns whether a kobject has children. + * @kobj: the object to test + * + * This will return whether a kobject has other kobjects as children. + * + * It does NOT account for the presence of attribute files, only sub + * directories. It also assumes there is no concurrent addition or + * removal of such children, and thus relies on external locking. + */ +static inline bool kobject_has_children(struct kobject *kobj) +{ + WARN_ON_ONCE(atomic_read(&kobj->kref.refcount) == 0); + + return kobj->sd && kobj->sd->dir.subdirs; +} + struct kobj_type { void (*release)(struct kobject *kobj); const struct sysfs_ops *sysfs_ops;