Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2498911imu; Mon, 17 Dec 2018 03:04:46 -0800 (PST) X-Google-Smtp-Source: AFSGD/VPdBvtfI4GcIfric46w2Y40lmuBuU999It+Ilnftk9gkJVAvp20ngDSWlAFZ/fXyky/Rh/ X-Received: by 2002:a63:3858:: with SMTP id h24mr11373100pgn.300.1545044686660; Mon, 17 Dec 2018 03:04:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545044686; cv=none; d=google.com; s=arc-20160816; b=DZbmEcCTg+AGywnpfs8xHpkNIj8jYpwYR3c+vQk0KZmbMgb/sTFXaPjoKZaXqAi5G/ Jo3P2wBY+OG82LABbkiNba0sY6oBDJlAo/CPv9AqipvtcHBcwtu6skWEu0f2IGZMaYuc GsLiicV5dFnJip9G0M4Ge9j5EAnFM3biFm/i2Bucl1aEHCwync29JGqnr471WaVP+n9r 64ydYCE4zfGrhN0wzVjIlSe4ZQ3AZ9RAZ3A7eBgMubo0TWhf0IxHNutD+lHQ3GRud3P9 Vy2xptWbniZMMoS+DO3VtLRLbeASJ57AZQazc7pQGtfs/ufpx3C68GNsBJAuvMDcdOCo pu1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from; bh=jtaE+hiV2lfWefWsJWUdtYJ7PLoqM/JXSda+cW5hXFw=; b=VPAvMUwJhgGaDS41zOCxiRv+UWdERe6aC6DXcjO+lccD7qr46ZPzCNM5zykZ8QMnKl 7fckvXdMm5UUvFdYvFAL4FNGRD+HC+0UzCBw7tww+ugqdSCvE7pPK7XFy6tHxebBSHb7 anaiGTQu5/uykqFX0dNbakLZ7qfjm9NAbTqrCL1Pr4Hmd7eSt0nIt8IgHeDU5FiNJuDU BgpYRBOT1OmmPfZIUgTHEpD9pv7Jus32WpQlHjB9hIDADrDvJdOsKqQP0tJ21j1bci+z YOMeg129Kz/YePILJS+w5Mo9TxT6OkFhsmgkpk+Dhk0p7t2c/RjTrs0FCT+mMVouBomX z+XA== ARC-Authentication-Results: i=1; mx.google.com; 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 e7si9326096pfh.147.2018.12.17.03.04.31; Mon, 17 Dec 2018 03:04: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; 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 S1731614AbeLQKwg (ORCPT + 99 others); Mon, 17 Dec 2018 05:52:36 -0500 Received: from ozlabs.org ([203.11.71.1]:53115 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726566AbeLQKwf (ORCPT ); Mon, 17 Dec 2018 05:52:35 -0500 Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 43JHzj23Kwz9sBh; Mon, 17 Dec 2018 21:52:33 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ellerman.id.au From: Michael Ellerman To: frowand.list@gmail.com, robh+dt@kernel.org, Michael Bringmann , linuxppc-dev@lists.ozlabs.org Cc: Tyrel Datwyler , Thomas Falcon , Juliet Kim , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] of: __of_detach_node() - remove node from phandle cache In-Reply-To: <1545033396-24485-3-git-send-email-frowand.list@gmail.com> References: <1545033396-24485-1-git-send-email-frowand.list@gmail.com> <1545033396-24485-3-git-send-email-frowand.list@gmail.com> Date: Mon, 17 Dec 2018 21:52:28 +1100 Message-ID: <871s6gv30z.fsf@concordia.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Frank, frowand.list@gmail.com writes: > From: Frank Rowand > > Non-overlay dynamic devicetree node removal may leave the node in > the phandle cache. Subsequent calls to of_find_node_by_phandle() > will incorrectly find the stale entry. Remove the node from the > cache. > > Add paranoia checks in of_find_node_by_phandle() as a second level > of defense (do not return cached node if detached, do not add node > to cache if detached). > > Reported-by: Michael Bringmann > Signed-off-by: Frank Rowand > --- Similarly here can we add: Fixes: 0b3ce78e90fc ("of: cache phandle nodes to reduce cost of of_find_node_by_phandle()") Cc: stable@vger.kernel.org # v4.17+ Thanks for doing this series. Some minor comments below. > diff --git a/drivers/of/base.c b/drivers/of/base.c > index 6c33d63361b8..ad71864cecf5 100644 > --- a/drivers/of/base.c > +++ b/drivers/of/base.c > @@ -162,6 +162,27 @@ int of_free_phandle_cache(void) > late_initcall_sync(of_free_phandle_cache); > #endif > > +/* > + * Caller must hold devtree_lock. > + */ > +void __of_free_phandle_cache_entry(phandle handle) > +{ > + phandle masked_handle; > + > + if (!handle) > + return; We could fold the phandle_cache check into that if and return early for both cases couldn't we? > + masked_handle = handle & phandle_cache_mask; > + > + if (phandle_cache) { Meaning this wouldn't be necessary. > + if (phandle_cache[masked_handle] && > + handle == phandle_cache[masked_handle]->phandle) { > + of_node_put(phandle_cache[masked_handle]); > + phandle_cache[masked_handle] = NULL; > + } A temporary would help the readability here I think, eg: struct device_node *np; np = phandle_cache[masked_handle]; if (np && handle == np->phandle) { of_node_put(np); phandle_cache[masked_handle] = NULL; } > @@ -1209,11 +1230,18 @@ struct device_node *of_find_node_by_phandle(phandle handle) > if (phandle_cache[masked_handle] && > handle == phandle_cache[masked_handle]->phandle) > np = phandle_cache[masked_handle]; > + if (np && of_node_check_flag(np, OF_DETACHED)) { > + WARN_ON(1); > + of_node_put(np); Do we really want to do the put here? We're here because something has gone wrong, possibly even memory corruption such that np is not even pointing at a device node anymore. So it seems like it would be safer to just leave the ref count alone, possibly leak a small amount of memory, and NULL out the reference. cheers