Skip to content

Commit

Permalink
of: Support hashtable lookups for phandles
Browse files Browse the repository at this point in the history
Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou authored and RobertCNelson committed May 11, 2017
1 parent 6c2f389 commit b13fae7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
35 changes: 32 additions & 3 deletions drivers/of/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/proc_fs.h>
#include <linux/rhashtable.h>

#include "of_private.h"

Expand All @@ -41,6 +42,16 @@ static const char *of_stdout_options;

struct kset *of_kset;

const struct rhashtable_params of_phandle_ht_params = {
.key_offset = offsetof(struct device_node, phandle), /* base offset */
.key_len = sizeof(phandle),
.head_offset = offsetof(struct device_node, ht_node),
.automatic_shrinking = true,
};

struct rhashtable of_phandle_ht;
bool of_phandle_ht_initialized;

/*
* Used to protect the of_aliases, to hold off addition of nodes to sysfs.
* This mutex must be held whenever modifications are being made to the
Expand Down Expand Up @@ -167,6 +178,12 @@ int __of_attach_node_post(struct device_node *np)
struct property *pp;
int rc;

if (of_phandle_ht_available()) {
rc = of_phandle_ht_insert(np);
WARN(rc, "insert to phandle hash fail @%s\n",
of_node_full_name(np));
}

if (!IS_ENABLED(CONFIG_SYSFS))
return 0;

Expand Down Expand Up @@ -200,6 +217,13 @@ void __init of_core_init(void)
struct device_node *np;
int ret;

ret = rhashtable_init(&of_phandle_ht, &of_phandle_ht_params);
if (ret) {
pr_warn("devicetree: Failed to initialize hashtable\n");
return;
}
of_phandle_ht_initialized = 1;

/* Create the kset, and register existing nodes */
mutex_lock(&of_mutex);
of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
Expand Down Expand Up @@ -1079,9 +1103,14 @@ struct device_node *of_find_node_by_phandle(phandle handle)
return NULL;

raw_spin_lock_irqsave(&devtree_lock, flags);
for_each_of_allnodes(np)
if (np->phandle == handle)
break;
/* when we're ready use the hash table */
if (of_phandle_ht_available() && !in_interrupt())
np = of_phandle_ht_lookup(handle);
else { /* fallback */
for_each_of_allnodes(np)
if (np->phandle == handle)
break;
}
of_node_get(np);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
return np;
Expand Down
8 changes: 8 additions & 0 deletions drivers/of/dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/proc_fs.h>
#include <linux/rhashtable.h>

#include "of_private.h"

Expand Down Expand Up @@ -44,6 +45,13 @@ EXPORT_SYMBOL(of_node_put);
void __of_detach_node_post(struct device_node *np)
{
struct property *pp;
int rc;

if (of_phandle_ht_available()) {
rc = of_phandle_ht_remove(np);
WARN(rc, "remove from phandle hash fail @%s\n",
of_node_full_name(np));
}

if (!IS_ENABLED(CONFIG_SYSFS))
return;
Expand Down
31 changes: 31 additions & 0 deletions drivers/of/of_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,35 @@ static inline int of_overlay_init(void)
}
#endif

extern const struct rhashtable_params of_phandle_ht_params;
extern struct rhashtable of_phandle_ht;
extern bool of_phandle_ht_initialized;

static inline bool of_phandle_ht_available(void)
{
return of_phandle_ht_initialized;
}

static inline int of_phandle_ht_insert(struct device_node *np)
{
if (!np || !np->phandle)
return 0;
return rhashtable_insert_fast(&of_phandle_ht,
&np->ht_node, of_phandle_ht_params);
}

static inline int of_phandle_ht_remove(struct device_node *np)
{
if (!np || !np->phandle)
return 0;
return rhashtable_remove_fast(&of_phandle_ht,
&np->ht_node, of_phandle_ht_params);
}

static inline struct device_node *of_phandle_ht_lookup(phandle handle)
{
return rhashtable_lookup_fast(&of_phandle_ht,
&handle, of_phandle_ht_params);
}

#endif /* _LINUX_OF_PRIVATE_H */
2 changes: 2 additions & 0 deletions include/linux/of.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <linux/notifier.h>
#include <linux/property.h>
#include <linux/list.h>
#include <linux/rhashtable.h>

#include <asm/byteorder.h>
#include <asm/errno.h>
Expand Down Expand Up @@ -52,6 +53,7 @@ struct device_node {
phandle phandle;
const char *full_name;
struct fwnode_handle fwnode;
struct rhash_head ht_node;

struct property *properties;
struct property *deadprops; /* removed properties */
Expand Down

0 comments on commit b13fae7

Please sign in to comment.