Return-Path: Received: from mail5.windriver.com ([192.103.53.11]:39916 "EHLO mail5.wrs.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750794AbdAWKCs (ORCPT ); Mon, 23 Jan 2017 05:02:48 -0500 Subject: Re: [PATCH 3/3] i2c: add missing of_node_put in i2c_mux_del_adapters To: Peter Rosin , , References: <1485138118-255836-1-git-send-email-qi.hou@windriver.com> <1485138118-255836-4-git-send-email-qi.hou@windriver.com> <456443d3-1ccd-0be4-ac54-5a83b892a161@axentia.se> CC: , , , From: qhou Message-ID: <50929f05-b3dc-c92c-27a7-e8139eeb7fa4@windriver.com> Date: Mon, 23 Jan 2017 18:02:27 +0800 MIME-Version: 1.0 In-Reply-To: <456443d3-1ccd-0be4-ac54-5a83b892a161@axentia.se> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org List-ID: On 2017年01月23日 16:30, Peter Rosin wrote: > On 2017-01-23 03:21, Qi Hou wrote: >> Refcount of of_node is increased with of_node_get() in i2c_mux_add_adapter(). >> It must be decreased with of_node_put() in i2c_mux_del_adapters(). >> >> Signed-off-by: Qi Hou >> Reviewed-by: Zhang Xiao >> Acked-by: Bruce Ashfield >> --- >> drivers/i2c/i2c-mux.c | 1 + >> 1 file changed, 1 insertion(+) >> >> diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c >> index 83768e8..d7ebbfb 100644 >> --- a/drivers/i2c/i2c-mux.c >> +++ b/drivers/i2c/i2c-mux.c >> @@ -437,6 +437,7 @@ void i2c_mux_del_adapters(struct i2c_mux_core *muxc) >> sysfs_remove_link(&muxc->dev->kobj, symlink_name); >> >> sysfs_remove_link(&priv->adap.dev.kobj, "mux_device"); >> + of_node_put(adap->dev.of_node); > I'd feel more comfortable if the node was put after the i2c_del_adapter > call. The current patch invites use-after-free. Unfortunately (for us) > i2c_del_adapter zeros out the .dev member (isn't that a bit dubious?) of > the adapter which means that the of_node has to be kept in a temporary. Yes, it is ridiculous to invite use-after-free, I considered this before. It is good to put of_node in a temporary. What about ? diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c index d7ebbfb..cd5c2f2 100644 --- a/drivers/i2c/i2c-mux.c +++ b/drivers/i2c/i2c-mux.c @@ -429,6 +429,7 @@ void i2c_mux_del_adapters(struct i2c_mux_core *muxc) while (muxc->num_adapters) { struct i2c_adapter *adap = muxc->adapter[--muxc->num_adapters]; struct i2c_mux_priv *priv = adap->algo_data; + struct device_node *of = adap->dev.of_node; muxc->adapter[muxc->num_adapters] = NULL; @@ -437,8 +438,8 @@ void i2c_mux_del_adapters(struct i2c_mux_core *muxc) sysfs_remove_link(&muxc->dev->kobj, symlink_name); sysfs_remove_link(&priv->adap.dev.kobj, "mux_device"); - of_node_put(adap->dev.of_node); i2c_del_adapter(adap); + of_node_put(of); kfree(priv); } } -- Best regards, Qi Hou > > Cheers, > peda > >> i2c_del_adapter(adap); >> kfree(priv); >> } >>