On Tue, Oct 10, 2017 at 06:56:30PM -0400, Steven Rostedt wrote:

> +/* Without jump labels we don't know how many instances, just assume one */
> +static inline int static_key_instances(struct static_key *key)
> +{
> +     return 1;
> +}

That's a tad dodgy.. it works for your current usage, but..


> +int static_key_instances(struct static_key *key)
> +{
> +     return jump_label_update(key, true);
>  }

That's a bit yuck too :/ But I'm not entirely sure something like the
below is actually better...

---
 kernel/jump_label.c | 50 ++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 10 deletions(-)

diff --git a/kernel/jump_label.c b/kernel/jump_label.c
index 0bf2e8f5244a..97286ed6c93d 100644
--- a/kernel/jump_label.c
+++ b/kernel/jump_label.c
@@ -354,9 +354,17 @@ static enum jump_label_type jump_label_type(struct 
jump_entry *entry)
        return enabled ^ branch;
 }
 
-static void __jump_label_update(struct static_key *key,
-                               struct jump_entry *entry,
-                               struct jump_entry *stop)
+typedef void (*jump_label_visit_f)(struct jump_entry *entry, void *data);
+
+void __jump_label_update(struct jump_entry *entry, void *data)
+{
+       arch_jump_label_transform(entry, jump_label_type(entry));
+}
+
+static void
+__jump_label_visit(struct static_key *key, struct jump_entry *entry,
+                  struct jump_entry *stop, jump_label_visit_f *visit,
+                  void *data)
 {
        for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
                /*
@@ -365,7 +373,7 @@ static void __jump_label_update(struct static_key *key,
                 * init code, see jump_label_invalidate_module_init().
                 */
                if (entry->code && kernel_text_address(entry->code))
-                       arch_jump_label_transform(entry, 
jump_label_type(entry));
+                       visit(entry, data);
        }
 }
 
@@ -470,7 +478,8 @@ static int __jump_label_mod_text_reserved(void *start, void 
*end)
                                start, end);
 }
 
-static void __jump_label_mod_update(struct static_key *key)
+static void
+__jump_label_mod_visit(struct static_key *key, jump_label_visit_f visit, void 
*data)
 {
        struct static_key_mod *mod;
 
@@ -490,7 +499,8 @@ static void __jump_label_mod_update(struct static_key *key)
                        stop = __stop___jump_table;
                else
                        stop = m->jump_entries + m->num_jump_entries;
-               __jump_label_update(key, mod->entries, stop);
+
+               __jump_label_visit(key, mod->entries, stop, visit, data);
        }
 }
 
@@ -571,7 +581,7 @@ static int jump_label_add_module(struct module *mod)
 
                /* Only update if we've changed from our initial state */
                if (jump_label_type(iter) != jump_label_init_type(iter))
-                       __jump_label_update(key, iter, iter_stop);
+                       __jump_label_visit(key, iter, iter_stop, 
__jump_label_update, NULL);
        }
 
        return 0;
@@ -711,7 +721,7 @@ int jump_label_text_reserved(void *start, void *end)
        return ret;
 }
 
-static void jump_label_update(struct static_key *key)
+static void jump_label_visit(struct static_key *key, jump_label_visit_f visit, 
void *data)
 {
        struct jump_entry *stop = __stop___jump_table;
        struct jump_entry *entry;
@@ -719,7 +729,7 @@ static void jump_label_update(struct static_key *key)
        struct module *mod;
 
        if (static_key_linked(key)) {
-               __jump_label_mod_update(key);
+               __jump_label_mod_visit(key, visit, data);
                return;
        }
 
@@ -732,7 +742,27 @@ static void jump_label_update(struct static_key *key)
        entry = static_key_entries(key);
        /* if there are no users, entry can be NULL */
        if (entry)
-               __jump_label_update(key, entry, stop);
+               __jump_label_visit(key, entry, stop, visit, data);
+}
+
+static void jump_label_update(struct static_key *key)
+{
+       jump_label_visit(key, __jump_label_update, NULL);
+}
+
+static void __jump_label_count(struct jump_entry *entry, void *data)
+{
+       unsigned int *count = data;
+       count++;
+}
+
+unsigned int static_key_instances(struct static_key *key)
+{
+       unsigned int count = 0;
+
+       jump_label_visit(key, __jump_label_count, &count);
+
+       return count;
 }
 
 #ifdef CONFIG_STATIC_KEYS_SELFTEST

Reply via email to