Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * jump label support
0004  *
0005  * Copyright (C) 2009 Jason Baron <jbaron@redhat.com>
0006  * Copyright (C) 2011 Peter Zijlstra
0007  *
0008  */
0009 #include <linux/memory.h>
0010 #include <linux/uaccess.h>
0011 #include <linux/module.h>
0012 #include <linux/list.h>
0013 #include <linux/slab.h>
0014 #include <linux/sort.h>
0015 #include <linux/err.h>
0016 #include <linux/static_key.h>
0017 #include <linux/jump_label_ratelimit.h>
0018 #include <linux/bug.h>
0019 #include <linux/cpu.h>
0020 #include <asm/sections.h>
0021 
0022 /* mutex to protect coming/going of the jump_label table */
0023 static DEFINE_MUTEX(jump_label_mutex);
0024 
0025 void jump_label_lock(void)
0026 {
0027     mutex_lock(&jump_label_mutex);
0028 }
0029 
0030 void jump_label_unlock(void)
0031 {
0032     mutex_unlock(&jump_label_mutex);
0033 }
0034 
0035 static int jump_label_cmp(const void *a, const void *b)
0036 {
0037     const struct jump_entry *jea = a;
0038     const struct jump_entry *jeb = b;
0039 
0040     /*
0041      * Entrires are sorted by key.
0042      */
0043     if (jump_entry_key(jea) < jump_entry_key(jeb))
0044         return -1;
0045 
0046     if (jump_entry_key(jea) > jump_entry_key(jeb))
0047         return 1;
0048 
0049     /*
0050      * In the batching mode, entries should also be sorted by the code
0051      * inside the already sorted list of entries, enabling a bsearch in
0052      * the vector.
0053      */
0054     if (jump_entry_code(jea) < jump_entry_code(jeb))
0055         return -1;
0056 
0057     if (jump_entry_code(jea) > jump_entry_code(jeb))
0058         return 1;
0059 
0060     return 0;
0061 }
0062 
0063 static void jump_label_swap(void *a, void *b, int size)
0064 {
0065     long delta = (unsigned long)a - (unsigned long)b;
0066     struct jump_entry *jea = a;
0067     struct jump_entry *jeb = b;
0068     struct jump_entry tmp = *jea;
0069 
0070     jea->code   = jeb->code - delta;
0071     jea->target = jeb->target - delta;
0072     jea->key    = jeb->key - delta;
0073 
0074     jeb->code   = tmp.code + delta;
0075     jeb->target = tmp.target + delta;
0076     jeb->key    = tmp.key + delta;
0077 }
0078 
0079 static void
0080 jump_label_sort_entries(struct jump_entry *start, struct jump_entry *stop)
0081 {
0082     unsigned long size;
0083     void *swapfn = NULL;
0084 
0085     if (IS_ENABLED(CONFIG_HAVE_ARCH_JUMP_LABEL_RELATIVE))
0086         swapfn = jump_label_swap;
0087 
0088     size = (((unsigned long)stop - (unsigned long)start)
0089                     / sizeof(struct jump_entry));
0090     sort(start, size, sizeof(struct jump_entry), jump_label_cmp, swapfn);
0091 }
0092 
0093 static void jump_label_update(struct static_key *key);
0094 
0095 /*
0096  * There are similar definitions for the !CONFIG_JUMP_LABEL case in jump_label.h.
0097  * The use of 'atomic_read()' requires atomic.h and its problematic for some
0098  * kernel headers such as kernel.h and others. Since static_key_count() is not
0099  * used in the branch statements as it is for the !CONFIG_JUMP_LABEL case its ok
0100  * to have it be a function here. Similarly, for 'static_key_enable()' and
0101  * 'static_key_disable()', which require bug.h. This should allow jump_label.h
0102  * to be included from most/all places for CONFIG_JUMP_LABEL.
0103  */
0104 int static_key_count(struct static_key *key)
0105 {
0106     /*
0107      * -1 means the first static_key_slow_inc() is in progress.
0108      *  static_key_enabled() must return true, so return 1 here.
0109      */
0110     int n = atomic_read(&key->enabled);
0111 
0112     return n >= 0 ? n : 1;
0113 }
0114 EXPORT_SYMBOL_GPL(static_key_count);
0115 
0116 void static_key_slow_inc_cpuslocked(struct static_key *key)
0117 {
0118     int v, v1;
0119 
0120     STATIC_KEY_CHECK_USE(key);
0121     lockdep_assert_cpus_held();
0122 
0123     /*
0124      * Careful if we get concurrent static_key_slow_inc() calls;
0125      * later calls must wait for the first one to _finish_ the
0126      * jump_label_update() process.  At the same time, however,
0127      * the jump_label_update() call below wants to see
0128      * static_key_enabled(&key) for jumps to be updated properly.
0129      *
0130      * So give a special meaning to negative key->enabled: it sends
0131      * static_key_slow_inc() down the slow path, and it is non-zero
0132      * so it counts as "enabled" in jump_label_update().  Note that
0133      * atomic_inc_unless_negative() checks >= 0, so roll our own.
0134      */
0135     for (v = atomic_read(&key->enabled); v > 0; v = v1) {
0136         v1 = atomic_cmpxchg(&key->enabled, v, v + 1);
0137         if (likely(v1 == v))
0138             return;
0139     }
0140 
0141     jump_label_lock();
0142     if (atomic_read(&key->enabled) == 0) {
0143         atomic_set(&key->enabled, -1);
0144         jump_label_update(key);
0145         /*
0146          * Ensure that if the above cmpxchg loop observes our positive
0147          * value, it must also observe all the text changes.
0148          */
0149         atomic_set_release(&key->enabled, 1);
0150     } else {
0151         atomic_inc(&key->enabled);
0152     }
0153     jump_label_unlock();
0154 }
0155 
0156 void static_key_slow_inc(struct static_key *key)
0157 {
0158     cpus_read_lock();
0159     static_key_slow_inc_cpuslocked(key);
0160     cpus_read_unlock();
0161 }
0162 EXPORT_SYMBOL_GPL(static_key_slow_inc);
0163 
0164 void static_key_enable_cpuslocked(struct static_key *key)
0165 {
0166     STATIC_KEY_CHECK_USE(key);
0167     lockdep_assert_cpus_held();
0168 
0169     if (atomic_read(&key->enabled) > 0) {
0170         WARN_ON_ONCE(atomic_read(&key->enabled) != 1);
0171         return;
0172     }
0173 
0174     jump_label_lock();
0175     if (atomic_read(&key->enabled) == 0) {
0176         atomic_set(&key->enabled, -1);
0177         jump_label_update(key);
0178         /*
0179          * See static_key_slow_inc().
0180          */
0181         atomic_set_release(&key->enabled, 1);
0182     }
0183     jump_label_unlock();
0184 }
0185 EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked);
0186 
0187 void static_key_enable(struct static_key *key)
0188 {
0189     cpus_read_lock();
0190     static_key_enable_cpuslocked(key);
0191     cpus_read_unlock();
0192 }
0193 EXPORT_SYMBOL_GPL(static_key_enable);
0194 
0195 void static_key_disable_cpuslocked(struct static_key *key)
0196 {
0197     STATIC_KEY_CHECK_USE(key);
0198     lockdep_assert_cpus_held();
0199 
0200     if (atomic_read(&key->enabled) != 1) {
0201         WARN_ON_ONCE(atomic_read(&key->enabled) != 0);
0202         return;
0203     }
0204 
0205     jump_label_lock();
0206     if (atomic_cmpxchg(&key->enabled, 1, 0))
0207         jump_label_update(key);
0208     jump_label_unlock();
0209 }
0210 EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);
0211 
0212 void static_key_disable(struct static_key *key)
0213 {
0214     cpus_read_lock();
0215     static_key_disable_cpuslocked(key);
0216     cpus_read_unlock();
0217 }
0218 EXPORT_SYMBOL_GPL(static_key_disable);
0219 
0220 static bool static_key_slow_try_dec(struct static_key *key)
0221 {
0222     int val;
0223 
0224     val = atomic_fetch_add_unless(&key->enabled, -1, 1);
0225     if (val == 1)
0226         return false;
0227 
0228     /*
0229      * The negative count check is valid even when a negative
0230      * key->enabled is in use by static_key_slow_inc(); a
0231      * __static_key_slow_dec() before the first static_key_slow_inc()
0232      * returns is unbalanced, because all other static_key_slow_inc()
0233      * instances block while the update is in progress.
0234      */
0235     WARN(val < 0, "jump label: negative count!\n");
0236     return true;
0237 }
0238 
0239 static void __static_key_slow_dec_cpuslocked(struct static_key *key)
0240 {
0241     lockdep_assert_cpus_held();
0242 
0243     if (static_key_slow_try_dec(key))
0244         return;
0245 
0246     jump_label_lock();
0247     if (atomic_dec_and_test(&key->enabled))
0248         jump_label_update(key);
0249     jump_label_unlock();
0250 }
0251 
0252 static void __static_key_slow_dec(struct static_key *key)
0253 {
0254     cpus_read_lock();
0255     __static_key_slow_dec_cpuslocked(key);
0256     cpus_read_unlock();
0257 }
0258 
0259 void jump_label_update_timeout(struct work_struct *work)
0260 {
0261     struct static_key_deferred *key =
0262         container_of(work, struct static_key_deferred, work.work);
0263     __static_key_slow_dec(&key->key);
0264 }
0265 EXPORT_SYMBOL_GPL(jump_label_update_timeout);
0266 
0267 void static_key_slow_dec(struct static_key *key)
0268 {
0269     STATIC_KEY_CHECK_USE(key);
0270     __static_key_slow_dec(key);
0271 }
0272 EXPORT_SYMBOL_GPL(static_key_slow_dec);
0273 
0274 void static_key_slow_dec_cpuslocked(struct static_key *key)
0275 {
0276     STATIC_KEY_CHECK_USE(key);
0277     __static_key_slow_dec_cpuslocked(key);
0278 }
0279 
0280 void __static_key_slow_dec_deferred(struct static_key *key,
0281                     struct delayed_work *work,
0282                     unsigned long timeout)
0283 {
0284     STATIC_KEY_CHECK_USE(key);
0285 
0286     if (static_key_slow_try_dec(key))
0287         return;
0288 
0289     schedule_delayed_work(work, timeout);
0290 }
0291 EXPORT_SYMBOL_GPL(__static_key_slow_dec_deferred);
0292 
0293 void __static_key_deferred_flush(void *key, struct delayed_work *work)
0294 {
0295     STATIC_KEY_CHECK_USE(key);
0296     flush_delayed_work(work);
0297 }
0298 EXPORT_SYMBOL_GPL(__static_key_deferred_flush);
0299 
0300 void jump_label_rate_limit(struct static_key_deferred *key,
0301         unsigned long rl)
0302 {
0303     STATIC_KEY_CHECK_USE(key);
0304     key->timeout = rl;
0305     INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
0306 }
0307 EXPORT_SYMBOL_GPL(jump_label_rate_limit);
0308 
0309 static int addr_conflict(struct jump_entry *entry, void *start, void *end)
0310 {
0311     if (jump_entry_code(entry) <= (unsigned long)end &&
0312         jump_entry_code(entry) + jump_entry_size(entry) > (unsigned long)start)
0313         return 1;
0314 
0315     return 0;
0316 }
0317 
0318 static int __jump_label_text_reserved(struct jump_entry *iter_start,
0319         struct jump_entry *iter_stop, void *start, void *end, bool init)
0320 {
0321     struct jump_entry *iter;
0322 
0323     iter = iter_start;
0324     while (iter < iter_stop) {
0325         if (init || !jump_entry_is_init(iter)) {
0326             if (addr_conflict(iter, start, end))
0327                 return 1;
0328         }
0329         iter++;
0330     }
0331 
0332     return 0;
0333 }
0334 
0335 #ifndef arch_jump_label_transform_static
0336 static void arch_jump_label_transform_static(struct jump_entry *entry,
0337                          enum jump_label_type type)
0338 {
0339     /* nothing to do on most architectures */
0340 }
0341 #endif
0342 
0343 static inline struct jump_entry *static_key_entries(struct static_key *key)
0344 {
0345     WARN_ON_ONCE(key->type & JUMP_TYPE_LINKED);
0346     return (struct jump_entry *)(key->type & ~JUMP_TYPE_MASK);
0347 }
0348 
0349 static inline bool static_key_type(struct static_key *key)
0350 {
0351     return key->type & JUMP_TYPE_TRUE;
0352 }
0353 
0354 static inline bool static_key_linked(struct static_key *key)
0355 {
0356     return key->type & JUMP_TYPE_LINKED;
0357 }
0358 
0359 static inline void static_key_clear_linked(struct static_key *key)
0360 {
0361     key->type &= ~JUMP_TYPE_LINKED;
0362 }
0363 
0364 static inline void static_key_set_linked(struct static_key *key)
0365 {
0366     key->type |= JUMP_TYPE_LINKED;
0367 }
0368 
0369 /***
0370  * A 'struct static_key' uses a union such that it either points directly
0371  * to a table of 'struct jump_entry' or to a linked list of modules which in
0372  * turn point to 'struct jump_entry' tables.
0373  *
0374  * The two lower bits of the pointer are used to keep track of which pointer
0375  * type is in use and to store the initial branch direction, we use an access
0376  * function which preserves these bits.
0377  */
0378 static void static_key_set_entries(struct static_key *key,
0379                    struct jump_entry *entries)
0380 {
0381     unsigned long type;
0382 
0383     WARN_ON_ONCE((unsigned long)entries & JUMP_TYPE_MASK);
0384     type = key->type & JUMP_TYPE_MASK;
0385     key->entries = entries;
0386     key->type |= type;
0387 }
0388 
0389 static enum jump_label_type jump_label_type(struct jump_entry *entry)
0390 {
0391     struct static_key *key = jump_entry_key(entry);
0392     bool enabled = static_key_enabled(key);
0393     bool branch = jump_entry_is_branch(entry);
0394 
0395     /* See the comment in linux/jump_label.h */
0396     return enabled ^ branch;
0397 }
0398 
0399 static bool jump_label_can_update(struct jump_entry *entry, bool init)
0400 {
0401     /*
0402      * Cannot update code that was in an init text area.
0403      */
0404     if (!init && jump_entry_is_init(entry))
0405         return false;
0406 
0407     if (!kernel_text_address(jump_entry_code(entry))) {
0408         /*
0409          * This skips patching built-in __exit, which
0410          * is part of init_section_contains() but is
0411          * not part of kernel_text_address().
0412          *
0413          * Skipping built-in __exit is fine since it
0414          * will never be executed.
0415          */
0416         WARN_ONCE(!jump_entry_is_init(entry),
0417               "can't patch jump_label at %pS",
0418               (void *)jump_entry_code(entry));
0419         return false;
0420     }
0421 
0422     return true;
0423 }
0424 
0425 #ifndef HAVE_JUMP_LABEL_BATCH
0426 static void __jump_label_update(struct static_key *key,
0427                 struct jump_entry *entry,
0428                 struct jump_entry *stop,
0429                 bool init)
0430 {
0431     for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
0432         if (jump_label_can_update(entry, init))
0433             arch_jump_label_transform(entry, jump_label_type(entry));
0434     }
0435 }
0436 #else
0437 static void __jump_label_update(struct static_key *key,
0438                 struct jump_entry *entry,
0439                 struct jump_entry *stop,
0440                 bool init)
0441 {
0442     for (; (entry < stop) && (jump_entry_key(entry) == key); entry++) {
0443 
0444         if (!jump_label_can_update(entry, init))
0445             continue;
0446 
0447         if (!arch_jump_label_transform_queue(entry, jump_label_type(entry))) {
0448             /*
0449              * Queue is full: Apply the current queue and try again.
0450              */
0451             arch_jump_label_transform_apply();
0452             BUG_ON(!arch_jump_label_transform_queue(entry, jump_label_type(entry)));
0453         }
0454     }
0455     arch_jump_label_transform_apply();
0456 }
0457 #endif
0458 
0459 void __init jump_label_init(void)
0460 {
0461     struct jump_entry *iter_start = __start___jump_table;
0462     struct jump_entry *iter_stop = __stop___jump_table;
0463     struct static_key *key = NULL;
0464     struct jump_entry *iter;
0465 
0466     /*
0467      * Since we are initializing the static_key.enabled field with
0468      * with the 'raw' int values (to avoid pulling in atomic.h) in
0469      * jump_label.h, let's make sure that is safe. There are only two
0470      * cases to check since we initialize to 0 or 1.
0471      */
0472     BUILD_BUG_ON((int)ATOMIC_INIT(0) != 0);
0473     BUILD_BUG_ON((int)ATOMIC_INIT(1) != 1);
0474 
0475     if (static_key_initialized)
0476         return;
0477 
0478     cpus_read_lock();
0479     jump_label_lock();
0480     jump_label_sort_entries(iter_start, iter_stop);
0481 
0482     for (iter = iter_start; iter < iter_stop; iter++) {
0483         struct static_key *iterk;
0484         bool in_init;
0485 
0486         /* rewrite NOPs */
0487         if (jump_label_type(iter) == JUMP_LABEL_NOP)
0488             arch_jump_label_transform_static(iter, JUMP_LABEL_NOP);
0489 
0490         in_init = init_section_contains((void *)jump_entry_code(iter), 1);
0491         jump_entry_set_init(iter, in_init);
0492 
0493         iterk = jump_entry_key(iter);
0494         if (iterk == key)
0495             continue;
0496 
0497         key = iterk;
0498         static_key_set_entries(key, iter);
0499     }
0500     static_key_initialized = true;
0501     jump_label_unlock();
0502     cpus_read_unlock();
0503 }
0504 
0505 #ifdef CONFIG_MODULES
0506 
0507 enum jump_label_type jump_label_init_type(struct jump_entry *entry)
0508 {
0509     struct static_key *key = jump_entry_key(entry);
0510     bool type = static_key_type(key);
0511     bool branch = jump_entry_is_branch(entry);
0512 
0513     /* See the comment in linux/jump_label.h */
0514     return type ^ branch;
0515 }
0516 
0517 struct static_key_mod {
0518     struct static_key_mod *next;
0519     struct jump_entry *entries;
0520     struct module *mod;
0521 };
0522 
0523 static inline struct static_key_mod *static_key_mod(struct static_key *key)
0524 {
0525     WARN_ON_ONCE(!static_key_linked(key));
0526     return (struct static_key_mod *)(key->type & ~JUMP_TYPE_MASK);
0527 }
0528 
0529 /***
0530  * key->type and key->next are the same via union.
0531  * This sets key->next and preserves the type bits.
0532  *
0533  * See additional comments above static_key_set_entries().
0534  */
0535 static void static_key_set_mod(struct static_key *key,
0536                    struct static_key_mod *mod)
0537 {
0538     unsigned long type;
0539 
0540     WARN_ON_ONCE((unsigned long)mod & JUMP_TYPE_MASK);
0541     type = key->type & JUMP_TYPE_MASK;
0542     key->next = mod;
0543     key->type |= type;
0544 }
0545 
0546 static int __jump_label_mod_text_reserved(void *start, void *end)
0547 {
0548     struct module *mod;
0549     int ret;
0550 
0551     preempt_disable();
0552     mod = __module_text_address((unsigned long)start);
0553     WARN_ON_ONCE(__module_text_address((unsigned long)end) != mod);
0554     if (!try_module_get(mod))
0555         mod = NULL;
0556     preempt_enable();
0557 
0558     if (!mod)
0559         return 0;
0560 
0561     ret = __jump_label_text_reserved(mod->jump_entries,
0562                 mod->jump_entries + mod->num_jump_entries,
0563                 start, end, mod->state == MODULE_STATE_COMING);
0564 
0565     module_put(mod);
0566 
0567     return ret;
0568 }
0569 
0570 static void __jump_label_mod_update(struct static_key *key)
0571 {
0572     struct static_key_mod *mod;
0573 
0574     for (mod = static_key_mod(key); mod; mod = mod->next) {
0575         struct jump_entry *stop;
0576         struct module *m;
0577 
0578         /*
0579          * NULL if the static_key is defined in a module
0580          * that does not use it
0581          */
0582         if (!mod->entries)
0583             continue;
0584 
0585         m = mod->mod;
0586         if (!m)
0587             stop = __stop___jump_table;
0588         else
0589             stop = m->jump_entries + m->num_jump_entries;
0590         __jump_label_update(key, mod->entries, stop,
0591                     m && m->state == MODULE_STATE_COMING);
0592     }
0593 }
0594 
0595 static int jump_label_add_module(struct module *mod)
0596 {
0597     struct jump_entry *iter_start = mod->jump_entries;
0598     struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
0599     struct jump_entry *iter;
0600     struct static_key *key = NULL;
0601     struct static_key_mod *jlm, *jlm2;
0602 
0603     /* if the module doesn't have jump label entries, just return */
0604     if (iter_start == iter_stop)
0605         return 0;
0606 
0607     jump_label_sort_entries(iter_start, iter_stop);
0608 
0609     for (iter = iter_start; iter < iter_stop; iter++) {
0610         struct static_key *iterk;
0611         bool in_init;
0612 
0613         in_init = within_module_init(jump_entry_code(iter), mod);
0614         jump_entry_set_init(iter, in_init);
0615 
0616         iterk = jump_entry_key(iter);
0617         if (iterk == key)
0618             continue;
0619 
0620         key = iterk;
0621         if (within_module((unsigned long)key, mod)) {
0622             static_key_set_entries(key, iter);
0623             continue;
0624         }
0625         jlm = kzalloc(sizeof(struct static_key_mod), GFP_KERNEL);
0626         if (!jlm)
0627             return -ENOMEM;
0628         if (!static_key_linked(key)) {
0629             jlm2 = kzalloc(sizeof(struct static_key_mod),
0630                        GFP_KERNEL);
0631             if (!jlm2) {
0632                 kfree(jlm);
0633                 return -ENOMEM;
0634             }
0635             preempt_disable();
0636             jlm2->mod = __module_address((unsigned long)key);
0637             preempt_enable();
0638             jlm2->entries = static_key_entries(key);
0639             jlm2->next = NULL;
0640             static_key_set_mod(key, jlm2);
0641             static_key_set_linked(key);
0642         }
0643         jlm->mod = mod;
0644         jlm->entries = iter;
0645         jlm->next = static_key_mod(key);
0646         static_key_set_mod(key, jlm);
0647         static_key_set_linked(key);
0648 
0649         /* Only update if we've changed from our initial state */
0650         if (jump_label_type(iter) != jump_label_init_type(iter))
0651             __jump_label_update(key, iter, iter_stop, true);
0652     }
0653 
0654     return 0;
0655 }
0656 
0657 static void jump_label_del_module(struct module *mod)
0658 {
0659     struct jump_entry *iter_start = mod->jump_entries;
0660     struct jump_entry *iter_stop = iter_start + mod->num_jump_entries;
0661     struct jump_entry *iter;
0662     struct static_key *key = NULL;
0663     struct static_key_mod *jlm, **prev;
0664 
0665     for (iter = iter_start; iter < iter_stop; iter++) {
0666         if (jump_entry_key(iter) == key)
0667             continue;
0668 
0669         key = jump_entry_key(iter);
0670 
0671         if (within_module((unsigned long)key, mod))
0672             continue;
0673 
0674         /* No memory during module load */
0675         if (WARN_ON(!static_key_linked(key)))
0676             continue;
0677 
0678         prev = &key->next;
0679         jlm = static_key_mod(key);
0680 
0681         while (jlm && jlm->mod != mod) {
0682             prev = &jlm->next;
0683             jlm = jlm->next;
0684         }
0685 
0686         /* No memory during module load */
0687         if (WARN_ON(!jlm))
0688             continue;
0689 
0690         if (prev == &key->next)
0691             static_key_set_mod(key, jlm->next);
0692         else
0693             *prev = jlm->next;
0694 
0695         kfree(jlm);
0696 
0697         jlm = static_key_mod(key);
0698         /* if only one etry is left, fold it back into the static_key */
0699         if (jlm->next == NULL) {
0700             static_key_set_entries(key, jlm->entries);
0701             static_key_clear_linked(key);
0702             kfree(jlm);
0703         }
0704     }
0705 }
0706 
0707 static int
0708 jump_label_module_notify(struct notifier_block *self, unsigned long val,
0709              void *data)
0710 {
0711     struct module *mod = data;
0712     int ret = 0;
0713 
0714     cpus_read_lock();
0715     jump_label_lock();
0716 
0717     switch (val) {
0718     case MODULE_STATE_COMING:
0719         ret = jump_label_add_module(mod);
0720         if (ret) {
0721             WARN(1, "Failed to allocate memory: jump_label may not work properly.\n");
0722             jump_label_del_module(mod);
0723         }
0724         break;
0725     case MODULE_STATE_GOING:
0726         jump_label_del_module(mod);
0727         break;
0728     }
0729 
0730     jump_label_unlock();
0731     cpus_read_unlock();
0732 
0733     return notifier_from_errno(ret);
0734 }
0735 
0736 static struct notifier_block jump_label_module_nb = {
0737     .notifier_call = jump_label_module_notify,
0738     .priority = 1, /* higher than tracepoints */
0739 };
0740 
0741 static __init int jump_label_init_module(void)
0742 {
0743     return register_module_notifier(&jump_label_module_nb);
0744 }
0745 early_initcall(jump_label_init_module);
0746 
0747 #endif /* CONFIG_MODULES */
0748 
0749 /***
0750  * jump_label_text_reserved - check if addr range is reserved
0751  * @start: start text addr
0752  * @end: end text addr
0753  *
0754  * checks if the text addr located between @start and @end
0755  * overlaps with any of the jump label patch addresses. Code
0756  * that wants to modify kernel text should first verify that
0757  * it does not overlap with any of the jump label addresses.
0758  * Caller must hold jump_label_mutex.
0759  *
0760  * returns 1 if there is an overlap, 0 otherwise
0761  */
0762 int jump_label_text_reserved(void *start, void *end)
0763 {
0764     bool init = system_state < SYSTEM_RUNNING;
0765     int ret = __jump_label_text_reserved(__start___jump_table,
0766             __stop___jump_table, start, end, init);
0767 
0768     if (ret)
0769         return ret;
0770 
0771 #ifdef CONFIG_MODULES
0772     ret = __jump_label_mod_text_reserved(start, end);
0773 #endif
0774     return ret;
0775 }
0776 
0777 static void jump_label_update(struct static_key *key)
0778 {
0779     struct jump_entry *stop = __stop___jump_table;
0780     bool init = system_state < SYSTEM_RUNNING;
0781     struct jump_entry *entry;
0782 #ifdef CONFIG_MODULES
0783     struct module *mod;
0784 
0785     if (static_key_linked(key)) {
0786         __jump_label_mod_update(key);
0787         return;
0788     }
0789 
0790     preempt_disable();
0791     mod = __module_address((unsigned long)key);
0792     if (mod) {
0793         stop = mod->jump_entries + mod->num_jump_entries;
0794         init = mod->state == MODULE_STATE_COMING;
0795     }
0796     preempt_enable();
0797 #endif
0798     entry = static_key_entries(key);
0799     /* if there are no users, entry can be NULL */
0800     if (entry)
0801         __jump_label_update(key, entry, stop, init);
0802 }
0803 
0804 #ifdef CONFIG_STATIC_KEYS_SELFTEST
0805 static DEFINE_STATIC_KEY_TRUE(sk_true);
0806 static DEFINE_STATIC_KEY_FALSE(sk_false);
0807 
0808 static __init int jump_label_test(void)
0809 {
0810     int i;
0811 
0812     for (i = 0; i < 2; i++) {
0813         WARN_ON(static_key_enabled(&sk_true.key) != true);
0814         WARN_ON(static_key_enabled(&sk_false.key) != false);
0815 
0816         WARN_ON(!static_branch_likely(&sk_true));
0817         WARN_ON(!static_branch_unlikely(&sk_true));
0818         WARN_ON(static_branch_likely(&sk_false));
0819         WARN_ON(static_branch_unlikely(&sk_false));
0820 
0821         static_branch_disable(&sk_true);
0822         static_branch_enable(&sk_false);
0823 
0824         WARN_ON(static_key_enabled(&sk_true.key) == true);
0825         WARN_ON(static_key_enabled(&sk_false.key) == false);
0826 
0827         WARN_ON(static_branch_likely(&sk_true));
0828         WARN_ON(static_branch_unlikely(&sk_true));
0829         WARN_ON(!static_branch_likely(&sk_false));
0830         WARN_ON(!static_branch_unlikely(&sk_false));
0831 
0832         static_branch_enable(&sk_true);
0833         static_branch_disable(&sk_false);
0834     }
0835 
0836     return 0;
0837 }
0838 early_initcall(jump_label_test);
0839 #endif /* STATIC_KEYS_SELFTEST */