Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp986883imu; Fri, 11 Jan 2019 12:46:09 -0800 (PST) X-Google-Smtp-Source: ALg8bN5zovyns4xeqiRnxcsP/o/7jHSQKCJGjHj/UsJDBWf+u25XSM41C+V+Kq2EQlY7i2/pu31e X-Received: by 2002:a63:d904:: with SMTP id r4mr14756991pgg.207.1547239569297; Fri, 11 Jan 2019 12:46:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547239569; cv=none; d=google.com; s=arc-20160816; b=DvM8kch5DyUsldnFvmlF3UlZMFdyl4Ciwc2wjXxCmzHVATTbpYSH0zJCzNXv95O46L u4JIli4a0H5XY9eIeOl7HBG4VGXU8FZVzwsvxo8O0JkOgsDuscgIJqX98x5M+jjlYjy/ x+evUjE2wcdMt/BfZC2sAFsZqGVSuLt99C97H/95S4pJKkU/HCaab2mNnT/BOTcAJjUo G6bS68A03xWdh97BRXEhtVBQoQWsRNLEcgMns523+J6Mepe4MQ7YWc6BBYKJLQWuq4MG XNa1U1UIIE/47/wnmQUh7tC2unx6X+2dddPzK0q4maGXRUDKDXyZsGSjhG2Vs7hs+Af9 Gy0A== 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=/FPJvsFRaA5emD8tWu7jNECLw8cncw2rZiNBENexnM4=; b=SBBecqrtgiHLV/3BF57WGhZYTdXQyMoPlGygXxTfUWwCE88XyV3iSt4wFgTtY7Flis lKmPmxuDEjCcqkbAi4lzb/xYy2rEBkI5Wr35c5j7K2Ik7oTTOYsR4vMZ4HDl0Uuw8jS7 +XkL8au7j94WcPixk15mum+EGlGXaeO2f+WH/hJDv0hHp7Jk5ZCdD4v4rUO20dHAsEKj Uqllyk15VduWOpUTES29Mm/ypO2lx8cZwj+kvPZxuqlIAwG9lgpqCkOi7f+/EXwhDGcU HC0Y0002tXxPT+PiDzqpZJEFZWa5VvS5QcJ4/mWSBcuuH1A0fC/5kgO1qCvta6JEyqbA 0Buw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Vq90Wau3; 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 c6si73536250plr.414.2019.01.11.12.45.54; Fri, 11 Jan 2019 12:46:09 -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=Vq90Wau3; 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 S2391422AbfAKOpT (ORCPT + 99 others); Fri, 11 Jan 2019 09:45:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:37048 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404511AbfAKOoG (ORCPT ); Fri, 11 Jan 2019 09:44:06 -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 E6AA52063F; Fri, 11 Jan 2019 14:44:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1547217845; bh=Ca0baCC0+GHqC8VTZUBvJrm3nviRqvbp3N/3nfBpon8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Vq90Wau3ZZNr2zX0EnEPfS8NUysSYgLJLdYkGhAKfX3K0SGQY8vA1tWGr+vJe39+k y3MVmBsQdlrEBUGK70akqp/XCCvxvCR/mWmRdaAv0VkAJ7JLOzNN04IR1AQ5tJmHRl eU2/ZqoWE8vTNpauqvIPqPVNAIgK03mUiQ1Kl2Sw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Michael Bringmann , Frank Rowand , Rob Herring Subject: [PATCH 4.20 61/65] of: __of_detach_node() - remove node from phandle cache Date: Fri, 11 Jan 2019 15:15:47 +0100 Message-Id: <20190111131104.202310590@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190111131055.331350141@linuxfoundation.org> References: <20190111131055.331350141@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.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Frank Rowand commit 5801169a2ed20003f771acecf3ac00574cf10a38 upstream. 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). Fixes: 0b3ce78e90fc ("of: cache phandle nodes to reduce cost of of_find_node_by_phandle()") Reported-by: Michael Bringmann Cc: stable@vger.kernel.org # v4.17+ Signed-off-by: Frank Rowand Signed-off-by: Rob Herring Signed-off-by: Greg Kroah-Hartman --- drivers/of/base.c | 31 ++++++++++++++++++++++++++++++- drivers/of/dynamic.c | 3 +++ drivers/of/of_private.h | 4 ++++ 3 files changed, 37 insertions(+), 1 deletion(-) --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -162,6 +162,28 @@ 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; + struct device_node *np; + + if (!handle) + return; + + masked_handle = handle & phandle_cache_mask; + + if (phandle_cache) { + np = phandle_cache[masked_handle]; + if (np && handle == np->phandle) { + of_node_put(np); + phandle_cache[masked_handle] = NULL; + } + } +} + void of_populate_phandle_cache(void) { unsigned long flags; @@ -1209,11 +1231,18 @@ struct device_node *of_find_node_by_phan 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); /* did not uncache np on node removal */ + of_node_put(np); + phandle_cache[masked_handle] = NULL; + np = NULL; + } } if (!np) { for_each_of_allnodes(np) - if (np->phandle == handle) { + if (np->phandle == handle && + !of_node_check_flag(np, OF_DETACHED)) { if (phandle_cache) { /* will put when removed from cache */ of_node_get(np); --- a/drivers/of/dynamic.c +++ b/drivers/of/dynamic.c @@ -268,6 +268,9 @@ void __of_detach_node(struct device_node } of_node_set_flag(np, OF_DETACHED); + + /* race with of_find_node_by_phandle() prevented by devtree_lock */ + __of_free_phandle_cache_entry(np->phandle); } /** --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -84,6 +84,10 @@ static inline void __of_detach_node_sysf int of_resolve_phandles(struct device_node *tree); #endif +#if defined(CONFIG_OF_DYNAMIC) +void __of_free_phandle_cache_entry(phandle handle); +#endif + #if defined(CONFIG_OF_OVERLAY) void of_overlay_mutex_lock(void); void of_overlay_mutex_unlock(void);