bpf: Remove in_atomic() from bpf_link_put().
bpf_free_inode() is invoked as a RCU callback. Usually RCU callbacks are invoked within softirq context. By setting rcutree.use_softirq=0 boot option the RCU callbacks will be invoked in a per-CPU kthread with bottom halves disabled which implies a RCU read section. On PREEMPT_RT the context remains fully preemptible. The RCU read section however does not allow schedule() invocation. The latter happens in mutex_lock() performed by bpf_trampoline_unlink_prog() originated from bpf_link_put(). It was pointed out that the bpf_link_put() invocation should not be delayed if originated from close(). It was also pointed out that other invocations from within a syscall should also avoid the workqueue. Everyone else should use workqueue by default to remain safe in the future (while auditing the code, every caller was preemptible except for the RCU case). Let bpf_link_put() use the worker unconditionally. Add bpf_link_put_direct() which will directly free the resources and is used by close() and from within __sys_bpf(). Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20230614083430.oENawF8f@linutronix.de
This commit is contained in:
parent
c03531e087
commit
ab5d47bd41
|
@ -2793,28 +2793,31 @@ static void bpf_link_put_deferred(struct work_struct *work)
|
||||||
bpf_link_free(link);
|
bpf_link_free(link);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bpf_link_put can be called from atomic context, but ensures that resources
|
/* bpf_link_put might be called from atomic context. It needs to be called
|
||||||
* are freed from process context
|
* from sleepable context in order to acquire sleeping locks during the process.
|
||||||
*/
|
*/
|
||||||
void bpf_link_put(struct bpf_link *link)
|
void bpf_link_put(struct bpf_link *link)
|
||||||
{
|
{
|
||||||
if (!atomic64_dec_and_test(&link->refcnt))
|
if (!atomic64_dec_and_test(&link->refcnt))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (in_atomic()) {
|
|
||||||
INIT_WORK(&link->work, bpf_link_put_deferred);
|
INIT_WORK(&link->work, bpf_link_put_deferred);
|
||||||
schedule_work(&link->work);
|
schedule_work(&link->work);
|
||||||
} else {
|
|
||||||
bpf_link_free(link);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(bpf_link_put);
|
EXPORT_SYMBOL(bpf_link_put);
|
||||||
|
|
||||||
|
static void bpf_link_put_direct(struct bpf_link *link)
|
||||||
|
{
|
||||||
|
if (!atomic64_dec_and_test(&link->refcnt))
|
||||||
|
return;
|
||||||
|
bpf_link_free(link);
|
||||||
|
}
|
||||||
|
|
||||||
static int bpf_link_release(struct inode *inode, struct file *filp)
|
static int bpf_link_release(struct inode *inode, struct file *filp)
|
||||||
{
|
{
|
||||||
struct bpf_link *link = filp->private_data;
|
struct bpf_link *link = filp->private_data;
|
||||||
|
|
||||||
bpf_link_put(link);
|
bpf_link_put_direct(link);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4787,7 +4790,7 @@ out_put_progs:
|
||||||
if (ret)
|
if (ret)
|
||||||
bpf_prog_put(new_prog);
|
bpf_prog_put(new_prog);
|
||||||
out_put_link:
|
out_put_link:
|
||||||
bpf_link_put(link);
|
bpf_link_put_direct(link);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4810,7 +4813,7 @@ static int link_detach(union bpf_attr *attr)
|
||||||
else
|
else
|
||||||
ret = -EOPNOTSUPP;
|
ret = -EOPNOTSUPP;
|
||||||
|
|
||||||
bpf_link_put(link);
|
bpf_link_put_direct(link);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4880,7 +4883,7 @@ static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
|
||||||
|
|
||||||
fd = bpf_link_new_fd(link);
|
fd = bpf_link_new_fd(link);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
bpf_link_put(link);
|
bpf_link_put_direct(link);
|
||||||
|
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
|
@ -4957,7 +4960,7 @@ static int bpf_iter_create(union bpf_attr *attr)
|
||||||
return PTR_ERR(link);
|
return PTR_ERR(link);
|
||||||
|
|
||||||
err = bpf_iter_new_fd(link);
|
err = bpf_iter_new_fd(link);
|
||||||
bpf_link_put(link);
|
bpf_link_put_direct(link);
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue