Skip to content

Commit 5801169

Browse files
frowandrobherring
authored andcommitted
of: __of_detach_node() - remove node from phandle cache
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: 0b3ce78 ("of: cache phandle nodes to reduce cost of of_find_node_by_phandle()") Reported-by: Michael Bringmann <mwb@linux.vnet.ibm.com> Cc: stable@vger.kernel.org # v4.17+ Signed-off-by: Frank Rowand <frank.rowand@sony.com> Signed-off-by: Rob Herring <robh@kernel.org>
1 parent b8a9ac1 commit 5801169

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎drivers/of/base.c‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ int of_free_phandle_cache(void)
169169
late_initcall_sync(of_free_phandle_cache);
170170
#endif
171171

172+
/*
173+
* Caller must hold devtree_lock.
174+
*/
175+
void __of_free_phandle_cache_entry(phandle handle)
176+
{
177+
phandle masked_handle;
178+
struct device_node *np;
179+
180+
if (!handle)
181+
return;
182+
183+
masked_handle = handle & phandle_cache_mask;
184+
185+
if (phandle_cache) {
186+
np = phandle_cache[masked_handle];
187+
if (np && handle == np->phandle) {
188+
of_node_put(np);
189+
phandle_cache[masked_handle] = NULL;
190+
}
191+
}
192+
}
193+
172194
void of_populate_phandle_cache(void)
173195
{
174196
unsigned long flags;
@@ -1214,11 +1236,18 @@ struct device_node *of_find_node_by_phandle(phandle handle)
12141236
if (phandle_cache[masked_handle] &&
12151237
handle == phandle_cache[masked_handle]->phandle)
12161238
np = phandle_cache[masked_handle];
1239+
if (np && of_node_check_flag(np, OF_DETACHED)) {
1240+
WARN_ON(1); /* did not uncache np on node removal */
1241+
of_node_put(np);
1242+
phandle_cache[masked_handle] = NULL;
1243+
np = NULL;
1244+
}
12171245
}
12181246

12191247
if (!np) {
12201248
for_each_of_allnodes(np)
1221-
if (np->phandle == handle) {
1249+
if (np->phandle == handle &&
1250+
!of_node_check_flag(np, OF_DETACHED)) {
12221251
if (phandle_cache) {
12231252
/* will put when removed from cache */
12241253
of_node_get(np);

‎drivers/of/dynamic.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ void __of_detach_node(struct device_node *np)
277277
}
278278

279279
of_node_set_flag(np, OF_DETACHED);
280+
281+
/* race with of_find_node_by_phandle() prevented by devtree_lock */
282+
__of_free_phandle_cache_entry(np->phandle);
280283
}
281284

282285
/**

‎drivers/of/of_private.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ static inline void __of_detach_node_sysfs(struct device_node *np) {}
8484
int of_resolve_phandles(struct device_node *tree);
8585
#endif
8686

87+
#if defined(CONFIG_OF_DYNAMIC)
88+
void __of_free_phandle_cache_entry(phandle handle);
89+
#endif
90+
8791
#if defined(CONFIG_OF_OVERLAY)
8892
void of_overlay_mutex_lock(void);
8993
void of_overlay_mutex_unlock(void);

0 commit comments

Comments
 (0)