| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | #include <linux/kdebug.h> |
|---|
| 2 | 3 | #include <linux/kprobes.h> |
|---|
| 3 | 4 | #include <linux/export.h> |
|---|
| .. | .. |
|---|
| 22 | 23 | struct notifier_block *n) |
|---|
| 23 | 24 | { |
|---|
| 24 | 25 | while ((*nl) != NULL) { |
|---|
| 25 | | - if (n->priority > (*nl)->priority) |
|---|
| 26 | | - break; |
|---|
| 27 | | - nl = &((*nl)->next); |
|---|
| 28 | | - } |
|---|
| 29 | | - n->next = *nl; |
|---|
| 30 | | - rcu_assign_pointer(*nl, n); |
|---|
| 31 | | - return 0; |
|---|
| 32 | | -} |
|---|
| 33 | | - |
|---|
| 34 | | -static int notifier_chain_cond_register(struct notifier_block **nl, |
|---|
| 35 | | - struct notifier_block *n) |
|---|
| 36 | | -{ |
|---|
| 37 | | - while ((*nl) != NULL) { |
|---|
| 38 | | - if ((*nl) == n) |
|---|
| 26 | + if (unlikely((*nl) == n)) { |
|---|
| 27 | + WARN(1, "double register detected"); |
|---|
| 39 | 28 | return 0; |
|---|
| 29 | + } |
|---|
| 40 | 30 | if (n->priority > (*nl)->priority) |
|---|
| 41 | 31 | break; |
|---|
| 42 | 32 | nl = &((*nl)->next); |
|---|
| .. | .. |
|---|
| 104 | 94 | } |
|---|
| 105 | 95 | NOKPROBE_SYMBOL(notifier_call_chain); |
|---|
| 106 | 96 | |
|---|
| 97 | +/** |
|---|
| 98 | + * notifier_call_chain_robust - Inform the registered notifiers about an event |
|---|
| 99 | + * and rollback on error. |
|---|
| 100 | + * @nl: Pointer to head of the blocking notifier chain |
|---|
| 101 | + * @val_up: Value passed unmodified to the notifier function |
|---|
| 102 | + * @val_down: Value passed unmodified to the notifier function when recovering |
|---|
| 103 | + * from an error on @val_up |
|---|
| 104 | + * @v Pointer passed unmodified to the notifier function |
|---|
| 105 | + * |
|---|
| 106 | + * NOTE: It is important the @nl chain doesn't change between the two |
|---|
| 107 | + * invocations of notifier_call_chain() such that we visit the |
|---|
| 108 | + * exact same notifier callbacks; this rules out any RCU usage. |
|---|
| 109 | + * |
|---|
| 110 | + * Returns: the return value of the @val_up call. |
|---|
| 111 | + */ |
|---|
| 112 | +static int notifier_call_chain_robust(struct notifier_block **nl, |
|---|
| 113 | + unsigned long val_up, unsigned long val_down, |
|---|
| 114 | + void *v) |
|---|
| 115 | +{ |
|---|
| 116 | + int ret, nr = 0; |
|---|
| 117 | + |
|---|
| 118 | + ret = notifier_call_chain(nl, val_up, v, -1, &nr); |
|---|
| 119 | + if (ret & NOTIFY_STOP_MASK) |
|---|
| 120 | + notifier_call_chain(nl, val_down, v, nr-1, NULL); |
|---|
| 121 | + |
|---|
| 122 | + return ret; |
|---|
| 123 | +} |
|---|
| 124 | + |
|---|
| 107 | 125 | /* |
|---|
| 108 | 126 | * Atomic notifier chain routines. Registration and unregistration |
|---|
| 109 | 127 | * use a spinlock, and call_chain is synchronized by RCU (no locks). |
|---|
| .. | .. |
|---|
| 154 | 172 | } |
|---|
| 155 | 173 | EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister); |
|---|
| 156 | 174 | |
|---|
| 175 | +int atomic_notifier_call_chain_robust(struct atomic_notifier_head *nh, |
|---|
| 176 | + unsigned long val_up, unsigned long val_down, void *v) |
|---|
| 177 | +{ |
|---|
| 178 | + unsigned long flags; |
|---|
| 179 | + int ret; |
|---|
| 180 | + |
|---|
| 181 | + /* |
|---|
| 182 | + * Musn't use RCU; because then the notifier list can |
|---|
| 183 | + * change between the up and down traversal. |
|---|
| 184 | + */ |
|---|
| 185 | + spin_lock_irqsave(&nh->lock, flags); |
|---|
| 186 | + ret = notifier_call_chain_robust(&nh->head, val_up, val_down, v); |
|---|
| 187 | + spin_unlock_irqrestore(&nh->lock, flags); |
|---|
| 188 | + |
|---|
| 189 | + return ret; |
|---|
| 190 | +} |
|---|
| 191 | +EXPORT_SYMBOL_GPL(atomic_notifier_call_chain_robust); |
|---|
| 192 | +NOKPROBE_SYMBOL(atomic_notifier_call_chain_robust); |
|---|
| 193 | + |
|---|
| 157 | 194 | /** |
|---|
| 158 | | - * __atomic_notifier_call_chain - Call functions in an atomic notifier chain |
|---|
| 195 | + * atomic_notifier_call_chain - Call functions in an atomic notifier chain |
|---|
| 159 | 196 | * @nh: Pointer to head of the atomic notifier chain |
|---|
| 160 | 197 | * @val: Value passed unmodified to notifier function |
|---|
| 161 | 198 | * @v: Pointer passed unmodified to notifier function |
|---|
| 162 | | - * @nr_to_call: See the comment for notifier_call_chain. |
|---|
| 163 | | - * @nr_calls: See the comment for notifier_call_chain. |
|---|
| 164 | 199 | * |
|---|
| 165 | 200 | * Calls each function in a notifier chain in turn. The functions |
|---|
| 166 | 201 | * run in an atomic context, so they must not block. |
|---|
| .. | .. |
|---|
| 173 | 208 | * Otherwise the return value is the return value |
|---|
| 174 | 209 | * of the last notifier function called. |
|---|
| 175 | 210 | */ |
|---|
| 176 | | -int __atomic_notifier_call_chain(struct atomic_notifier_head *nh, |
|---|
| 177 | | - unsigned long val, void *v, |
|---|
| 178 | | - int nr_to_call, int *nr_calls) |
|---|
| 211 | +int atomic_notifier_call_chain(struct atomic_notifier_head *nh, |
|---|
| 212 | + unsigned long val, void *v) |
|---|
| 179 | 213 | { |
|---|
| 180 | 214 | int ret; |
|---|
| 181 | 215 | |
|---|
| 182 | 216 | rcu_read_lock(); |
|---|
| 183 | | - ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); |
|---|
| 217 | + ret = notifier_call_chain(&nh->head, val, v, -1, NULL); |
|---|
| 184 | 218 | rcu_read_unlock(); |
|---|
| 185 | | - return ret; |
|---|
| 186 | | -} |
|---|
| 187 | | -EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain); |
|---|
| 188 | | -NOKPROBE_SYMBOL(__atomic_notifier_call_chain); |
|---|
| 189 | 219 | |
|---|
| 190 | | -int atomic_notifier_call_chain(struct atomic_notifier_head *nh, |
|---|
| 191 | | - unsigned long val, void *v) |
|---|
| 192 | | -{ |
|---|
| 193 | | - return __atomic_notifier_call_chain(nh, val, v, -1, NULL); |
|---|
| 220 | + return ret; |
|---|
| 194 | 221 | } |
|---|
| 195 | 222 | EXPORT_SYMBOL_GPL(atomic_notifier_call_chain); |
|---|
| 196 | 223 | NOKPROBE_SYMBOL(atomic_notifier_call_chain); |
|---|
| .. | .. |
|---|
| 231 | 258 | EXPORT_SYMBOL_GPL(blocking_notifier_chain_register); |
|---|
| 232 | 259 | |
|---|
| 233 | 260 | /** |
|---|
| 234 | | - * blocking_notifier_chain_cond_register - Cond add notifier to a blocking notifier chain |
|---|
| 235 | | - * @nh: Pointer to head of the blocking notifier chain |
|---|
| 236 | | - * @n: New entry in notifier chain |
|---|
| 237 | | - * |
|---|
| 238 | | - * Adds a notifier to a blocking notifier chain, only if not already |
|---|
| 239 | | - * present in the chain. |
|---|
| 240 | | - * Must be called in process context. |
|---|
| 241 | | - * |
|---|
| 242 | | - * Currently always returns zero. |
|---|
| 243 | | - */ |
|---|
| 244 | | -int blocking_notifier_chain_cond_register(struct blocking_notifier_head *nh, |
|---|
| 245 | | - struct notifier_block *n) |
|---|
| 246 | | -{ |
|---|
| 247 | | - int ret; |
|---|
| 248 | | - |
|---|
| 249 | | - down_write(&nh->rwsem); |
|---|
| 250 | | - ret = notifier_chain_cond_register(&nh->head, n); |
|---|
| 251 | | - up_write(&nh->rwsem); |
|---|
| 252 | | - return ret; |
|---|
| 253 | | -} |
|---|
| 254 | | -EXPORT_SYMBOL_GPL(blocking_notifier_chain_cond_register); |
|---|
| 255 | | - |
|---|
| 256 | | -/** |
|---|
| 257 | 261 | * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain |
|---|
| 258 | 262 | * @nh: Pointer to head of the blocking notifier chain |
|---|
| 259 | 263 | * @n: Entry to remove from notifier chain |
|---|
| .. | .. |
|---|
| 283 | 287 | } |
|---|
| 284 | 288 | EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister); |
|---|
| 285 | 289 | |
|---|
| 290 | +int blocking_notifier_call_chain_robust(struct blocking_notifier_head *nh, |
|---|
| 291 | + unsigned long val_up, unsigned long val_down, void *v) |
|---|
| 292 | +{ |
|---|
| 293 | + int ret = NOTIFY_DONE; |
|---|
| 294 | + |
|---|
| 295 | + /* |
|---|
| 296 | + * We check the head outside the lock, but if this access is |
|---|
| 297 | + * racy then it does not matter what the result of the test |
|---|
| 298 | + * is, we re-check the list after having taken the lock anyway: |
|---|
| 299 | + */ |
|---|
| 300 | + if (rcu_access_pointer(nh->head)) { |
|---|
| 301 | + down_read(&nh->rwsem); |
|---|
| 302 | + ret = notifier_call_chain_robust(&nh->head, val_up, val_down, v); |
|---|
| 303 | + up_read(&nh->rwsem); |
|---|
| 304 | + } |
|---|
| 305 | + return ret; |
|---|
| 306 | +} |
|---|
| 307 | +EXPORT_SYMBOL_GPL(blocking_notifier_call_chain_robust); |
|---|
| 308 | + |
|---|
| 286 | 309 | /** |
|---|
| 287 | | - * __blocking_notifier_call_chain - Call functions in a blocking notifier chain |
|---|
| 310 | + * blocking_notifier_call_chain - Call functions in a blocking notifier chain |
|---|
| 288 | 311 | * @nh: Pointer to head of the blocking notifier chain |
|---|
| 289 | 312 | * @val: Value passed unmodified to notifier function |
|---|
| 290 | 313 | * @v: Pointer passed unmodified to notifier function |
|---|
| 291 | | - * @nr_to_call: See comment for notifier_call_chain. |
|---|
| 292 | | - * @nr_calls: See comment for notifier_call_chain. |
|---|
| 293 | 314 | * |
|---|
| 294 | 315 | * Calls each function in a notifier chain in turn. The functions |
|---|
| 295 | 316 | * run in a process context, so they are allowed to block. |
|---|
| .. | .. |
|---|
| 301 | 322 | * Otherwise the return value is the return value |
|---|
| 302 | 323 | * of the last notifier function called. |
|---|
| 303 | 324 | */ |
|---|
| 304 | | -int __blocking_notifier_call_chain(struct blocking_notifier_head *nh, |
|---|
| 305 | | - unsigned long val, void *v, |
|---|
| 306 | | - int nr_to_call, int *nr_calls) |
|---|
| 325 | +int blocking_notifier_call_chain(struct blocking_notifier_head *nh, |
|---|
| 326 | + unsigned long val, void *v) |
|---|
| 307 | 327 | { |
|---|
| 308 | 328 | int ret = NOTIFY_DONE; |
|---|
| 309 | 329 | |
|---|
| .. | .. |
|---|
| 314 | 334 | */ |
|---|
| 315 | 335 | if (rcu_access_pointer(nh->head)) { |
|---|
| 316 | 336 | down_read(&nh->rwsem); |
|---|
| 317 | | - ret = notifier_call_chain(&nh->head, val, v, nr_to_call, |
|---|
| 318 | | - nr_calls); |
|---|
| 337 | + ret = notifier_call_chain(&nh->head, val, v, -1, NULL); |
|---|
| 319 | 338 | up_read(&nh->rwsem); |
|---|
| 320 | 339 | } |
|---|
| 321 | 340 | return ret; |
|---|
| 322 | | -} |
|---|
| 323 | | -EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain); |
|---|
| 324 | | - |
|---|
| 325 | | -int blocking_notifier_call_chain(struct blocking_notifier_head *nh, |
|---|
| 326 | | - unsigned long val, void *v) |
|---|
| 327 | | -{ |
|---|
| 328 | | - return __blocking_notifier_call_chain(nh, val, v, -1, NULL); |
|---|
| 329 | 341 | } |
|---|
| 330 | 342 | EXPORT_SYMBOL_GPL(blocking_notifier_call_chain); |
|---|
| 331 | 343 | |
|---|
| .. | .. |
|---|
| 368 | 380 | } |
|---|
| 369 | 381 | EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister); |
|---|
| 370 | 382 | |
|---|
| 383 | +int raw_notifier_call_chain_robust(struct raw_notifier_head *nh, |
|---|
| 384 | + unsigned long val_up, unsigned long val_down, void *v) |
|---|
| 385 | +{ |
|---|
| 386 | + return notifier_call_chain_robust(&nh->head, val_up, val_down, v); |
|---|
| 387 | +} |
|---|
| 388 | +EXPORT_SYMBOL_GPL(raw_notifier_call_chain_robust); |
|---|
| 389 | + |
|---|
| 371 | 390 | /** |
|---|
| 372 | | - * __raw_notifier_call_chain - Call functions in a raw notifier chain |
|---|
| 391 | + * raw_notifier_call_chain - Call functions in a raw notifier chain |
|---|
| 373 | 392 | * @nh: Pointer to head of the raw notifier chain |
|---|
| 374 | 393 | * @val: Value passed unmodified to notifier function |
|---|
| 375 | 394 | * @v: Pointer passed unmodified to notifier function |
|---|
| 376 | | - * @nr_to_call: See comment for notifier_call_chain. |
|---|
| 377 | | - * @nr_calls: See comment for notifier_call_chain |
|---|
| 378 | 395 | * |
|---|
| 379 | 396 | * Calls each function in a notifier chain in turn. The functions |
|---|
| 380 | 397 | * run in an undefined context. |
|---|
| .. | .. |
|---|
| 387 | 404 | * Otherwise the return value is the return value |
|---|
| 388 | 405 | * of the last notifier function called. |
|---|
| 389 | 406 | */ |
|---|
| 390 | | -int __raw_notifier_call_chain(struct raw_notifier_head *nh, |
|---|
| 391 | | - unsigned long val, void *v, |
|---|
| 392 | | - int nr_to_call, int *nr_calls) |
|---|
| 393 | | -{ |
|---|
| 394 | | - return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); |
|---|
| 395 | | -} |
|---|
| 396 | | -EXPORT_SYMBOL_GPL(__raw_notifier_call_chain); |
|---|
| 397 | | - |
|---|
| 398 | 407 | int raw_notifier_call_chain(struct raw_notifier_head *nh, |
|---|
| 399 | 408 | unsigned long val, void *v) |
|---|
| 400 | 409 | { |
|---|
| 401 | | - return __raw_notifier_call_chain(nh, val, v, -1, NULL); |
|---|
| 410 | + return notifier_call_chain(&nh->head, val, v, -1, NULL); |
|---|
| 402 | 411 | } |
|---|
| 403 | 412 | EXPORT_SYMBOL_GPL(raw_notifier_call_chain); |
|---|
| 404 | 413 | |
|---|
| .. | .. |
|---|
| 470 | 479 | EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister); |
|---|
| 471 | 480 | |
|---|
| 472 | 481 | /** |
|---|
| 473 | | - * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain |
|---|
| 482 | + * srcu_notifier_call_chain - Call functions in an SRCU notifier chain |
|---|
| 474 | 483 | * @nh: Pointer to head of the SRCU notifier chain |
|---|
| 475 | 484 | * @val: Value passed unmodified to notifier function |
|---|
| 476 | 485 | * @v: Pointer passed unmodified to notifier function |
|---|
| 477 | | - * @nr_to_call: See comment for notifier_call_chain. |
|---|
| 478 | | - * @nr_calls: See comment for notifier_call_chain |
|---|
| 479 | 486 | * |
|---|
| 480 | 487 | * Calls each function in a notifier chain in turn. The functions |
|---|
| 481 | 488 | * run in a process context, so they are allowed to block. |
|---|
| .. | .. |
|---|
| 487 | 494 | * Otherwise the return value is the return value |
|---|
| 488 | 495 | * of the last notifier function called. |
|---|
| 489 | 496 | */ |
|---|
| 490 | | -int __srcu_notifier_call_chain(struct srcu_notifier_head *nh, |
|---|
| 491 | | - unsigned long val, void *v, |
|---|
| 492 | | - int nr_to_call, int *nr_calls) |
|---|
| 497 | +int srcu_notifier_call_chain(struct srcu_notifier_head *nh, |
|---|
| 498 | + unsigned long val, void *v) |
|---|
| 493 | 499 | { |
|---|
| 494 | 500 | int ret; |
|---|
| 495 | 501 | int idx; |
|---|
| 496 | 502 | |
|---|
| 497 | 503 | idx = srcu_read_lock(&nh->srcu); |
|---|
| 498 | | - ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); |
|---|
| 504 | + ret = notifier_call_chain(&nh->head, val, v, -1, NULL); |
|---|
| 499 | 505 | srcu_read_unlock(&nh->srcu, idx); |
|---|
| 500 | 506 | return ret; |
|---|
| 501 | | -} |
|---|
| 502 | | -EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain); |
|---|
| 503 | | - |
|---|
| 504 | | -int srcu_notifier_call_chain(struct srcu_notifier_head *nh, |
|---|
| 505 | | - unsigned long val, void *v) |
|---|
| 506 | | -{ |
|---|
| 507 | | - return __srcu_notifier_call_chain(nh, val, v, -1, NULL); |
|---|
| 508 | 507 | } |
|---|
| 509 | 508 | EXPORT_SYMBOL_GPL(srcu_notifier_call_chain); |
|---|
| 510 | 509 | |
|---|
| .. | .. |
|---|
| 552 | 551 | |
|---|
| 553 | 552 | int register_die_notifier(struct notifier_block *nb) |
|---|
| 554 | 553 | { |
|---|
| 555 | | - vmalloc_sync_mappings(); |
|---|
| 556 | 554 | return atomic_notifier_chain_register(&die_chain, nb); |
|---|
| 557 | 555 | } |
|---|
| 558 | 556 | EXPORT_SYMBOL_GPL(register_die_notifier); |
|---|