Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp3671166ima; Mon, 4 Feb 2019 03:10:46 -0800 (PST) X-Google-Smtp-Source: ALg8bN7s/F0GGjqinB/4OVJJ5938CDLtQyVagvWltGnEzuK6L9PbTXvl0ohXFgDhlA/zUvhMqTyw X-Received: by 2002:a62:1a91:: with SMTP id a139mr52343046pfa.64.1549278646792; Mon, 04 Feb 2019 03:10:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549278646; cv=none; d=google.com; s=arc-20160816; b=WkjVxCNxmrB4PgoardzOJVCJI4dwSvJVUqyIKkBGrYsB0WXATuTcWdLWgVBDU1fJvx pRuENphNON2ZQ7yFTojsqQFEtelxITAG27o6IKuvXFrcJun2TgiPwx/o+vu0CqVkcM6G yxCAcRqdZnt6ktTwpiuUzMhsQQbWeEYFhOYYQbbPOvKFOl2fvtb+b/a2uci52+Oeulfh C55Ctxfsi7zKpxJvW/n0GGRGAiaqkikNrNoRxJTz3xKq+xIeLH7IVS79RbX/UyepP6Kz BhonwBrliUs2RqnCzlzxSXidBUwxA60BhtHUfph8+MwIkpV6i3BdSRATAjfwA1BsMA4t dAcA== 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=DiwMuZNLNxb24Ol9+N5cNPmYCI2Z5jIadS1SCjqN20w=; b=iXRmhsIpsTXFj5bQnh2t26yQgvY94CJ5ZCdU0fENojpIQAKp6K4l78f1aws8uZ7FTL x77UYwetYeI2f/iIvPwR7gZWYsfKkM+tAeb1zzftAaaG7TugKBgTOtqRTZyWEBGlMfx8 3HBWnmdqj1R2jmsHeIqL063wbVhWx5hLxaGW4TdveXeHFKm/essKiPgDAOe7JIO+8xoD kQpo3CQvAEGx77DqPNr5NV8Z1HzYOS5JqzxhJ0rd5pxCBxAksinFMX/W9Sp25ES+kEqN dEw/Z+Qskmvjmf46oLjANO2EPGNLy4gr85YBF87xv7IqCsJjGoW2J+0KFNyufkszZnGh pIkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YOaKcXRP; 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 j195si278441pfd.165.2019.02.04.03.10.30; Mon, 04 Feb 2019 03:10:46 -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=YOaKcXRP; 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 S1730276AbfBDLIa (ORCPT + 99 others); Mon, 4 Feb 2019 06:08:30 -0500 Received: from mail.kernel.org ([198.145.29.99]:39654 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730346AbfBDKlm (ORCPT ); Mon, 4 Feb 2019 05:41:42 -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 6994B217D6; Mon, 4 Feb 2019 10:41:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549276901; bh=FJblxdGoqIjFaQ6PJO1hq+chfKtv6YBkKFWa2H4Hv4A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YOaKcXRPJcTWfhuQFbfZORUIPi5sz5SV7yrxhoTsSWIt2zmH+bEF5tHKF5nYSi/l6 F5PVEmFJhh14AVGo/g6OnI/u5zvGViLJUzS5yZFv6joMlm2bvWp4IIhzF4yvJ1mjZt It9Xrj0dolZfU6aE+aBXaKA5k/L9fezGB3j3F3uM= 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.4 53/65] drivers: core: Remove glue dirs from sysfs earlier Date: Mon, 4 Feb 2019 11:36:46 +0100 Message-Id: <20190204103619.370839354@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190204103610.583715954@linuxfoundation.org> References: <20190204103610.583715954@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.4-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;