mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-21 18:14:48 +08:00
f5ad399149
The cleanup_srcu_struct_quiesced() function was added because NVME used WQ_MEM_RECLAIM workqueues and SRCU did not, which meant that NVME workqueues waiting on SRCU workqueues could result in deadlocks during low-memory conditions. However, SRCU now also has WQ_MEM_RECLAIM workqueues, so there is no longer a potential for deadlock. Furthermore, it turns out to be extremely hard to use cleanup_srcu_struct_quiesced() correctly due to the fact that SRCU callback invocation accesses the srcu_struct structure's per-CPU data area just after callbacks are invoked. Therefore, the usual practice of using srcu_barrier() to wait for callbacks to be invoked before invoking cleanup_srcu_struct_quiesced() fails because SRCU's callback-invocation workqueue handler might be delayed, which can result in cleanup_srcu_struct_quiesced() being invoked (and thus freeing the per-CPU data) before the SRCU's callback-invocation workqueue handler is finished using that per-CPU data. Nor is this a theoretical problem: KASAN emitted use-after-free warnings because of this problem on actual runs. In short, NVME can now safely invoke cleanup_srcu_struct(), which avoids the use-after-free scenario. And cleanup_srcu_struct_quiesced() is quite difficult to use safely. This commit therefore removes cleanup_srcu_struct_quiesced(), switching its sole user back to cleanup_srcu_struct(). This effectively reverts the following pair of commits:f7194ac32c
("srcu: Add cleanup_srcu_struct_quiesced()")4317228ad9
("nvme: Avoid flush dependency in delete controller flow") Reported-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Paul E. McKenney <paulmck@linux.ibm.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Tested-by: Bart Van Assche <bvanassche@acm.org>
216 lines
5.9 KiB
C
216 lines
5.9 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* Sleepable Read-Copy Update mechanism for mutual exclusion,
|
|
* tiny version for non-preemptible single-CPU use.
|
|
*
|
|
* Copyright (C) IBM Corporation, 2017
|
|
*
|
|
* Author: Paul McKenney <paulmck@linux.ibm.com>
|
|
*/
|
|
|
|
#include <linux/export.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/preempt.h>
|
|
#include <linux/rcupdate_wait.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/delay.h>
|
|
#include <linux/srcu.h>
|
|
|
|
#include <linux/rcu_node_tree.h>
|
|
#include "rcu_segcblist.h"
|
|
#include "rcu.h"
|
|
|
|
int rcu_scheduler_active __read_mostly;
|
|
static LIST_HEAD(srcu_boot_list);
|
|
static bool srcu_init_done;
|
|
|
|
static int init_srcu_struct_fields(struct srcu_struct *ssp)
|
|
{
|
|
ssp->srcu_lock_nesting[0] = 0;
|
|
ssp->srcu_lock_nesting[1] = 0;
|
|
init_swait_queue_head(&ssp->srcu_wq);
|
|
ssp->srcu_cb_head = NULL;
|
|
ssp->srcu_cb_tail = &ssp->srcu_cb_head;
|
|
ssp->srcu_gp_running = false;
|
|
ssp->srcu_gp_waiting = false;
|
|
ssp->srcu_idx = 0;
|
|
INIT_WORK(&ssp->srcu_work, srcu_drive_gp);
|
|
INIT_LIST_HEAD(&ssp->srcu_work.entry);
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_DEBUG_LOCK_ALLOC
|
|
|
|
int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
|
|
struct lock_class_key *key)
|
|
{
|
|
/* Don't re-initialize a lock while it is held. */
|
|
debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
|
|
lockdep_init_map(&ssp->dep_map, name, key, 0);
|
|
return init_srcu_struct_fields(ssp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__init_srcu_struct);
|
|
|
|
#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
|
|
|
|
/*
|
|
* init_srcu_struct - initialize a sleep-RCU structure
|
|
* @ssp: structure to initialize.
|
|
*
|
|
* Must invoke this on a given srcu_struct before passing that srcu_struct
|
|
* to any other function. Each srcu_struct represents a separate domain
|
|
* of SRCU protection.
|
|
*/
|
|
int init_srcu_struct(struct srcu_struct *ssp)
|
|
{
|
|
return init_srcu_struct_fields(ssp);
|
|
}
|
|
EXPORT_SYMBOL_GPL(init_srcu_struct);
|
|
|
|
#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
|
|
|
|
/*
|
|
* cleanup_srcu_struct - deconstruct a sleep-RCU structure
|
|
* @ssp: structure to clean up.
|
|
*
|
|
* Must invoke this after you are finished using a given srcu_struct that
|
|
* was initialized via init_srcu_struct(), else you leak memory.
|
|
*/
|
|
void cleanup_srcu_struct(struct srcu_struct *ssp)
|
|
{
|
|
WARN_ON(ssp->srcu_lock_nesting[0] || ssp->srcu_lock_nesting[1]);
|
|
flush_work(&ssp->srcu_work);
|
|
WARN_ON(ssp->srcu_gp_running);
|
|
WARN_ON(ssp->srcu_gp_waiting);
|
|
WARN_ON(ssp->srcu_cb_head);
|
|
WARN_ON(&ssp->srcu_cb_head != ssp->srcu_cb_tail);
|
|
}
|
|
EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
|
|
|
|
/*
|
|
* Removes the count for the old reader from the appropriate element of
|
|
* the srcu_struct.
|
|
*/
|
|
void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
|
|
{
|
|
int newval = ssp->srcu_lock_nesting[idx] - 1;
|
|
|
|
WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
|
|
if (!newval && READ_ONCE(ssp->srcu_gp_waiting))
|
|
swake_up_one(&ssp->srcu_wq);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__srcu_read_unlock);
|
|
|
|
/*
|
|
* Workqueue handler to drive one grace period and invoke any callbacks
|
|
* that become ready as a result. Single-CPU and !PREEMPT operation
|
|
* means that we get away with murder on synchronization. ;-)
|
|
*/
|
|
void srcu_drive_gp(struct work_struct *wp)
|
|
{
|
|
int idx;
|
|
struct rcu_head *lh;
|
|
struct rcu_head *rhp;
|
|
struct srcu_struct *ssp;
|
|
|
|
ssp = container_of(wp, struct srcu_struct, srcu_work);
|
|
if (ssp->srcu_gp_running || !READ_ONCE(ssp->srcu_cb_head))
|
|
return; /* Already running or nothing to do. */
|
|
|
|
/* Remove recently arrived callbacks and wait for readers. */
|
|
WRITE_ONCE(ssp->srcu_gp_running, true);
|
|
local_irq_disable();
|
|
lh = ssp->srcu_cb_head;
|
|
ssp->srcu_cb_head = NULL;
|
|
ssp->srcu_cb_tail = &ssp->srcu_cb_head;
|
|
local_irq_enable();
|
|
idx = ssp->srcu_idx;
|
|
WRITE_ONCE(ssp->srcu_idx, !ssp->srcu_idx);
|
|
WRITE_ONCE(ssp->srcu_gp_waiting, true); /* srcu_read_unlock() wakes! */
|
|
swait_event_exclusive(ssp->srcu_wq, !READ_ONCE(ssp->srcu_lock_nesting[idx]));
|
|
WRITE_ONCE(ssp->srcu_gp_waiting, false); /* srcu_read_unlock() cheap. */
|
|
|
|
/* Invoke the callbacks we removed above. */
|
|
while (lh) {
|
|
rhp = lh;
|
|
lh = lh->next;
|
|
local_bh_disable();
|
|
rhp->func(rhp);
|
|
local_bh_enable();
|
|
}
|
|
|
|
/*
|
|
* Enable rescheduling, and if there are more callbacks,
|
|
* reschedule ourselves. This can race with a call_srcu()
|
|
* at interrupt level, but the ->srcu_gp_running checks will
|
|
* straighten that out.
|
|
*/
|
|
WRITE_ONCE(ssp->srcu_gp_running, false);
|
|
if (READ_ONCE(ssp->srcu_cb_head))
|
|
schedule_work(&ssp->srcu_work);
|
|
}
|
|
EXPORT_SYMBOL_GPL(srcu_drive_gp);
|
|
|
|
/*
|
|
* Enqueue an SRCU callback on the specified srcu_struct structure,
|
|
* initiating grace-period processing if it is not already running.
|
|
*/
|
|
void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
|
|
rcu_callback_t func)
|
|
{
|
|
unsigned long flags;
|
|
|
|
rhp->func = func;
|
|
rhp->next = NULL;
|
|
local_irq_save(flags);
|
|
*ssp->srcu_cb_tail = rhp;
|
|
ssp->srcu_cb_tail = &rhp->next;
|
|
local_irq_restore(flags);
|
|
if (!READ_ONCE(ssp->srcu_gp_running)) {
|
|
if (likely(srcu_init_done))
|
|
schedule_work(&ssp->srcu_work);
|
|
else if (list_empty(&ssp->srcu_work.entry))
|
|
list_add(&ssp->srcu_work.entry, &srcu_boot_list);
|
|
}
|
|
}
|
|
EXPORT_SYMBOL_GPL(call_srcu);
|
|
|
|
/*
|
|
* synchronize_srcu - wait for prior SRCU read-side critical-section completion
|
|
*/
|
|
void synchronize_srcu(struct srcu_struct *ssp)
|
|
{
|
|
struct rcu_synchronize rs;
|
|
|
|
init_rcu_head_on_stack(&rs.head);
|
|
init_completion(&rs.completion);
|
|
call_srcu(ssp, &rs.head, wakeme_after_rcu);
|
|
wait_for_completion(&rs.completion);
|
|
destroy_rcu_head_on_stack(&rs.head);
|
|
}
|
|
EXPORT_SYMBOL_GPL(synchronize_srcu);
|
|
|
|
/* Lockdep diagnostics. */
|
|
void __init rcu_scheduler_starting(void)
|
|
{
|
|
rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
|
|
}
|
|
|
|
/*
|
|
* Queue work for srcu_struct structures with early boot callbacks.
|
|
* The work won't actually execute until the workqueue initialization
|
|
* phase that takes place after the scheduler starts.
|
|
*/
|
|
void __init srcu_init(void)
|
|
{
|
|
struct srcu_struct *ssp;
|
|
|
|
srcu_init_done = true;
|
|
while (!list_empty(&srcu_boot_list)) {
|
|
ssp = list_first_entry(&srcu_boot_list,
|
|
struct srcu_struct, srcu_work.entry);
|
|
list_del_init(&ssp->srcu_work.entry);
|
|
schedule_work(&ssp->srcu_work);
|
|
}
|
|
}
|