Skip to contentSkip to conversation

bpf: Implement prealloc for task_local_storage

RFC In review Tejun Heo 4 participants 8 messages Oct 20, 2021 – Oct 22, 2021

Subsystems (9)
VFS Kselftest BPF MM core +5 more subsystems
lore
Share & tools
lore

Tejun Heo

author maintainer
#0 lore raw

task_local_storage currently does not support pre-allocation and the memory is allocated on demand using the GFP_ATOMIC mask. While atomic allocations succeed most of the time and the occasional failures aren't a problem for many use-cases, there are some which can benefit from reliable allocations - e.g. tracking acquisitions and releases of specific resources to root cause long-term reference leaks.

This patchset implements prealloc support for task_local_storage so that once a map is created, it's guaranteed that the storage area is always available for all tasks unless explicitly deleted.

The only tricky part is syncronizing against the fork path. Fortunately, cgroup needs to do the same thing and is already using cgroup_threadgroup_rwsem and we can use the same mechanism without adding extra overhead. This patchset generalizes the rwsem and make cgroup and bpf select it.

This patchset is on top of bpf-next 223f903e9c83 ("bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG") and contains the following patches:

0001-cgroup-Drop-cgroup_-prefix-from-cgroup_threadgroup_r.patch 0002-sched-cgroup-Generalize-threadgroup_rwsem.patch 0003-bpf-Implement-prealloc-for-task_local_storage.patch

and also available in the following git branch:

git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git bpf-task-local-storage-prealloc

diffstat follows. Thanks.

 fs/exec.c                                                   |    7 +
 include/linux/bpf.h                                         |    6 +
 include/linux/bpf_local_storage.h                           |   12 +++
 include/linux/cgroup-defs.h                                 |   33 ---------
 include/linux/cgroup.h                                      |   11 +--
 include/linux/sched/threadgroup_rwsem.h                     |   46 ++++++++++++
 init/Kconfig                                                |    4 +
 kernel/bpf/Kconfig                                          |    1 
 kernel/bpf/bpf_local_storage.c                              |  112 ++++++++++++++++++++++--------
 kernel/bpf/bpf_task_storage.c                               |  138 +++++++++++++++++++++++++++++++++++++-
 kernel/cgroup/cgroup-internal.h                             |    4 -
 kernel/cgroup/cgroup-v1.c                                   |    9 +-
 kernel/cgroup/cgroup.c                                      |   74 ++++++++++++--------
 kernel/cgroup/pids.c                                        |    2 
 kernel/fork.c                                               |   16 ++++
 kernel/sched/core.c                                         |    4 +
 kernel/sched/sched.h                                        |    1 
 kernel/signal.c                                             |    7 +
 tools/testing/selftests/bpf/prog_tests/task_local_storage.c |  101 +++++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/task_ls_prealloc.c        |   15 ++++
 20 files changed, 489 insertions(+), 114 deletions(-)

--
tejun

author maintainer
#1 lore raw

PATCH 1/3 cgroup: Drop cgroup_ prefix from cgroup_threadgroup_rwsem and friends

From e9bad0a8967987edae58ad498b7ba5ba91923e1e Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Tue, 19 Oct 2021 09:50:17 -1000

threadgroup stabilization is being generalized so that it can be used outside cgroup. Let's drop the cgroup_ prefix in preparation. No functional changes.

---
 fs/exec.c                       |  6 ++---
 include/linux/cgroup-defs.h     | 20 +++++++-------
 kernel/cgroup/cgroup-internal.h |  4 +--
 kernel/cgroup/cgroup-v1.c       |  8 +++---
 kernel/cgroup/cgroup.c          | 48 ++++++++++++++++-----------------
 kernel/cgroup/pids.c            |  2 +-
 kernel/signal.c                 |  6 ++---
 7 files changed, 47 insertions(+), 47 deletions(-)
fs/exec.c +3 -3
index a098c133d8d7..caedd06a6d47 100644
@@ -1080,7 +1080,7 @@ static int de_thread(struct task_struct *tsk)
 		struct task_struct *leader = tsk->group_leader;
 
 		for (;;) {
-			cgroup_threadgroup_change_begin(tsk);
+			threadgroup_change_begin(tsk);
 			write_lock_irq(&tasklist_lock);
 			/*
 			 * Do this under tasklist_lock to ensure that
@@ -1091,7 +1091,7 @@ static int de_thread(struct task_struct *tsk)
 				break;
 			__set_current_state(TASK_KILLABLE);
 			write_unlock_irq(&tasklist_lock);
-			cgroup_threadgroup_change_end(tsk);
+			threadgroup_change_end(tsk);
 			schedule();
 			if (__fatal_signal_pending(tsk))
 				goto killed;
@@ -1146,7 +1146,7 @@ static int de_thread(struct task_struct *tsk)
 		if (unlikely(leader->ptrace))
 			__wake_up_parent(leader, leader->parent);
 		write_unlock_irq(&tasklist_lock);
-		cgroup_threadgroup_change_end(tsk);
+		threadgroup_change_end(tsk);
 
 		release_task(leader);
 	}
include/linux/cgroup-defs.h +10 -10
index db2e147e069f..1a77731e3309 100644
@@ -708,41 +708,41 @@ struct cgroup_subsys {
 	unsigned int depends_on;
 };
 
-extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
+extern struct percpu_rw_semaphore threadgroup_rwsem;
 
 /**
- * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
+ * threadgroup_change_begin - threadgroup exclusion for cgroups
  * @tsk: target task
  *
  * Allows cgroup operations to synchronize against threadgroup changes
  * using a percpu_rw_semaphore.
  */
-static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
+static inline void threadgroup_change_begin(struct task_struct *tsk)
 {
-	percpu_down_read(&cgroup_threadgroup_rwsem);
+	percpu_down_read(&threadgroup_rwsem);
 }
 
 /**
- * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
+ * threadgroup_change_end - threadgroup exclusion for cgroups
  * @tsk: target task
  *
- * Counterpart of cgroup_threadcgroup_change_begin().
+ * Counterpart of threadgroup_change_begin().
  */
-static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
+static inline void threadgroup_change_end(struct task_struct *tsk)
 {
-	percpu_up_read(&cgroup_threadgroup_rwsem);
+	percpu_up_read(&threadgroup_rwsem);
 }
 
 #else	/* CONFIG_CGROUPS */
 
 #define CGROUP_SUBSYS_COUNT 0
 
-static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
+static inline void threadgroup_change_begin(struct task_struct *tsk)
 {
 	might_sleep();
 }
 
-static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
+static inline void threadgroup_change_end(struct task_struct *tsk) {}
 
 #endif	/* CONFIG_CGROUPS */
 
kernel/cgroup/cgroup-internal.h +2 -2
index bfbeabc17a9d..9f76fc5aec8d 100644
@@ -233,9 +233,9 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
 		       bool threadgroup);
 struct task_struct *cgroup_procs_write_start(char *buf, bool threadgroup,
 					     bool *locked)
-	__acquires(&cgroup_threadgroup_rwsem);
+	__acquires(&threadgroup_rwsem);
 void cgroup_procs_write_finish(struct task_struct *task, bool locked)
-	__releases(&cgroup_threadgroup_rwsem);
+	__releases(&threadgroup_rwsem);
 
 void cgroup_lock_and_drain_offline(struct cgroup *cgrp);
 
kernel/cgroup/cgroup-v1.c +4 -4
index 35b920328344..03808e7deb2e 100644
@@ -59,7 +59,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
 	int retval = 0;
 
 	mutex_lock(&cgroup_mutex);
-	percpu_down_write(&cgroup_threadgroup_rwsem);
+	percpu_down_write(&threadgroup_rwsem);
 	for_each_root(root) {
 		struct cgroup *from_cgrp;
 
@@ -74,7 +74,7 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
 		if (retval)
 			break;
 	}
-	percpu_up_write(&cgroup_threadgroup_rwsem);
+	percpu_up_write(&threadgroup_rwsem);
 	mutex_unlock(&cgroup_mutex);
 
 	return retval;
@@ -111,7 +111,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
 
 	mutex_lock(&cgroup_mutex);
 
-	percpu_down_write(&cgroup_threadgroup_rwsem);
+	percpu_down_write(&threadgroup_rwsem);
 
 	/* all tasks in @from are being moved, all csets are source */
 	spin_lock_irq(&css_set_lock);
@@ -147,7 +147,7 @@ int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
 	} while (task && !ret);
 out_err:
 	cgroup_migrate_finish(&mgctx);
-	percpu_up_write(&cgroup_threadgroup_rwsem);
+	percpu_up_write(&threadgroup_rwsem);
 	mutex_unlock(&cgroup_mutex);
 	return ret;
 }
kernel/cgroup/cgroup.c +24 -24

167 lines not on this page · raw message

kernel/cgroup/pids.c +1 -1
index 511af87f685e..368bc3ea4dbb 100644
@@ -213,7 +213,7 @@ static void pids_cancel_attach(struct cgroup_taskset *tset)
 
 /*
  * task_css_check(true) in pids_can_fork() and pids_cancel_fork() relies
- * on cgroup_threadgroup_change_begin() held by the copy_process().
+ * on threadgroup_change_begin() held by the copy_process().
  */
 static int pids_can_fork(struct task_struct *task, struct css_set *cset)
 {
kernel/signal.c +3 -3
index 952741f6d0f9..f01b249369ce 100644
@@ -2956,11 +2956,11 @@ void exit_signals(struct task_struct *tsk)
 	 * @tsk is about to have PF_EXITING set - lock out users which
 	 * expect stable threadgroup.
 	 */
-	cgroup_threadgroup_change_begin(tsk);
+	threadgroup_change_begin(tsk);
 
 	if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
 		tsk->flags |= PF_EXITING;
-		cgroup_threadgroup_change_end(tsk);
+		threadgroup_change_end(tsk);
 		return;
 	}
 
@@ -2971,7 +2971,7 @@ void exit_signals(struct task_struct *tsk)
 	 */
 	tsk->flags |= PF_EXITING;
 
-	cgroup_threadgroup_change_end(tsk);
+	threadgroup_change_end(tsk);
 
 	if (!task_sigpending(tsk))
 		goto out;
Signature · 1 line
2.33.1
author maintainer
#2 lore raw

PATCH 2/3 sched, cgroup: Generalize threadgroup_rwsem

In reply to Tejun Heo

From 1b07d36b074acb8a97c8bb5c0f1604960763578e Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Tue, 19 Oct 2021 10:12:27 -1000

Generalize threadgroup stabilization through threadgroup_rwsem so that it can be used outside cgroup.

* A new config option CONFIG_THREADGROUP_RWSEM which is selected by CONFIG_CGROUPS enables threadgroup_rwsem.

* The declarations are moved to linux/sched/threadgroup_rwsem.h and the rwsem is now defined in kernel/sched/core.c.

* cgroup_mutex nests outside threadgroup_rwsem. During fork, cgroup_css_set_fork() which is called from cgroup_can_fork() was acquiring both. However, generalizing threadgroup_rwsem means that it needs to be acquired and released in the outer copy_process(). To maintain the locking order, break out cgroup_mutex acquisition into a separate function cgroup_prep_fork() which is called from copy_process() before acquiring threadgroup_rwsem.

No functional changes.

Cc Christian Brauner <[email protected]>
---
 fs/exec.c                               |  1 +
 include/linux/cgroup-defs.h             | 33 ------------------
 include/linux/cgroup.h                  | 11 +++---
 include/linux/sched/threadgroup_rwsem.h | 46 +++++++++++++++++++++++++
 init/Kconfig                            |  4 +++
 kernel/cgroup/cgroup-v1.c               |  1 +
 kernel/cgroup/cgroup.c                  | 38 +++++++++++++-------
 kernel/fork.c                           | 10 +++++-
 kernel/sched/core.c                     |  4 +++
 kernel/sched/sched.h                    |  1 +
 kernel/signal.c                         |  1 +
 11 files changed, 98 insertions(+), 52 deletions(-)
 create mode 100644 include/linux/sched/threadgroup_rwsem.h
fs/exec.c +1 -0
index caedd06a6d472..b18abc76e1ce0 100644
@@ -39,6 +39,7 @@
 #include <linux/sched/signal.h>
 #include <linux/sched/numa_balancing.h>
 #include <linux/sched/task.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/pagemap.h>
 #include <linux/perf_event.h>
 #include <linux/highmem.h>
include/linux/cgroup-defs.h +0 -33
index 1a77731e33096..b7e89b0c17057 100644
@@ -16,7 +16,6 @@
 #include <linux/rcupdate.h>
 #include <linux/refcount.h>
 #include <linux/percpu-refcount.h>
-#include <linux/percpu-rwsem.h>
 #include <linux/u64_stats_sync.h>
 #include <linux/workqueue.h>
 #include <linux/bpf-cgroup.h>
@@ -708,42 +707,10 @@ struct cgroup_subsys {
 	unsigned int depends_on;
 };
 
-extern struct percpu_rw_semaphore threadgroup_rwsem;
-
-/**
- * threadgroup_change_begin - threadgroup exclusion for cgroups
- * @tsk: target task
- *
- * Allows cgroup operations to synchronize against threadgroup changes
- * using a percpu_rw_semaphore.
- */
-static inline void threadgroup_change_begin(struct task_struct *tsk)
-{
-	percpu_down_read(&threadgroup_rwsem);
-}
-
-/**
- * threadgroup_change_end - threadgroup exclusion for cgroups
- * @tsk: target task
- *
- * Counterpart of threadgroup_change_begin().
- */
-static inline void threadgroup_change_end(struct task_struct *tsk)
-{
-	percpu_up_read(&threadgroup_rwsem);
-}
-
 #else	/* CONFIG_CGROUPS */
 
 #define CGROUP_SUBSYS_COUNT 0
 
-static inline void threadgroup_change_begin(struct task_struct *tsk)
-{
-	might_sleep();
-}
-
-static inline void threadgroup_change_end(struct task_struct *tsk) {}
-
 #endif	/* CONFIG_CGROUPS */
 
 #ifdef CONFIG_SOCK_CGROUP_DATA
include/linux/cgroup.h +5 -6
index 75c151413fda8..aa3df6361105f 100644
@@ -121,12 +121,10 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
 		     struct pid *pid, struct task_struct *tsk);
 
 void cgroup_fork(struct task_struct *p);
-extern int cgroup_can_fork(struct task_struct *p,
-			   struct kernel_clone_args *kargs);
-extern void cgroup_cancel_fork(struct task_struct *p,
-			       struct kernel_clone_args *kargs);
-extern void cgroup_post_fork(struct task_struct *p,
-			     struct kernel_clone_args *kargs);
+void cgroup_prep_fork(struct kernel_clone_args *kargs);
+int cgroup_can_fork(struct task_struct *p, struct kernel_clone_args *kargs);
+void cgroup_cancel_fork(struct task_struct *p, struct kernel_clone_args *kargs);
+void cgroup_post_fork(struct task_struct *p, struct kernel_clone_args *kargs);
 void cgroup_exit(struct task_struct *p);
 void cgroup_release(struct task_struct *p);
 void cgroup_free(struct task_struct *p);
@@ -713,6 +711,7 @@ static inline int cgroupstats_build(struct cgroupstats *stats,
 				    struct dentry *dentry) { return -EINVAL; }
 
 static inline void cgroup_fork(struct task_struct *p) {}
+static inline void cgroup_prep_fork(struct kernel_clone_args *kargs) { }
 static inline int cgroup_can_fork(struct task_struct *p,
 				  struct kernel_clone_args *kargs) { return 0; }
 static inline void cgroup_cancel_fork(struct task_struct *p,
include/linux/sched/threadgroup_rwsem.h new file +46 -0

46 lines not on this page · raw message

init/Kconfig +4 -0
index 11f8a845f259d..3a3699ccff3ce 100644
@@ -917,8 +917,12 @@ config NUMA_BALANCING_DEFAULT_ENABLED
 	  If set, automatic NUMA balancing will be enabled if running on a NUMA
 	  machine.
 
+config THREADGROUP_RWSEM
+        bool
+
 menuconfig CGROUPS
 	bool "Control Group support"
+	select THREADGROUP_RWSEM
 	select KERNFS
 	help
 	  This option adds support for grouping sets of processes together, for
kernel/cgroup/cgroup-v1.c +1 -0
index 03808e7deb2ea..9c747e258ae7c 100644
@@ -8,6 +8,7 @@
 #include <linux/mm.h>
 #include <linux/sched/signal.h>
 #include <linux/sched/task.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/magic.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
kernel/cgroup/cgroup.c +26 -12
index 2fd01c901b1ae..937888386210a 100644
@@ -42,6 +42,7 @@
 #include <linux/rcupdate.h>
 #include <linux/sched.h>
 #include <linux/sched/task.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/percpu-rwsem.h>
@@ -109,8 +110,6 @@ static DEFINE_SPINLOCK(cgroup_idr_lock);
  */
 static DEFINE_SPINLOCK(cgroup_file_kn_lock);
 
-DEFINE_PERCPU_RWSEM(threadgroup_rwsem);
-
 #define cgroup_assert_mutex_or_rcu_locked()				\
 	RCU_LOCKDEP_WARN(!rcu_read_lock_held() &&			\
 			   !lockdep_is_held(&cgroup_mutex),		\
@@ -6050,7 +6049,6 @@ static struct cgroup *cgroup_get_from_file(struct file *f)
  * to the target cgroup.
  */
 static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
-	__acquires(&cgroup_mutex) __acquires(&threadgroup_rwsem)
 {
 	int ret;
 	struct cgroup *dst_cgrp = NULL;
@@ -6058,11 +6056,6 @@ static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
 	struct super_block *sb;
 	struct file *f;
 
-	if (kargs->flags & CLONE_INTO_CGROUP)
-		mutex_lock(&cgroup_mutex);
-
-	threadgroup_change_begin(current);
-
 	spin_lock_irq(&css_set_lock);
 	cset = task_css_set(current);
 	get_css_set(cset);
@@ -6118,7 +6111,6 @@ static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
 	return ret;
 
 err:
-	threadgroup_change_end(current);
 	mutex_unlock(&cgroup_mutex);
 	if (f)
 		fput(f);
@@ -6138,10 +6130,8 @@ static int cgroup_css_set_fork(struct kernel_clone_args *kargs)
  * CLONE_INTO_CGROUP was requested.
  */
 static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
-	__releases(&threadgroup_rwsem) __releases(&cgroup_mutex)
+	__releases(&cgroup_mutex)
 {
-	threadgroup_change_end(current);
-
 	if (kargs->flags & CLONE_INTO_CGROUP) {
 		struct cgroup *cgrp = kargs->cgrp;
 		struct css_set *cset = kargs->cset;
@@ -6160,9 +6150,26 @@ static void cgroup_css_set_put_fork(struct kernel_clone_args *kargs)
 	}
 }
 
+/**
+ * cgroup_prep_fork - called during fork before threadgroup_rwsem is acquired
+ * @kargs: the arguments passed to create the child process
+ *
+ * CLONE_INTO_CGROUP requires cgroup_mutex as we're migrating while forking.
+ * However, cgroup_mutex must nest outside threadgroup_rwsem which is
+ * read-locked before cgroup_can_fork(). Break out cgroup_mutex locking to this
+ * function to follow the locking order.
+ */
+void cgroup_prep_fork(struct kernel_clone_args *kargs)
+	__acquires(&cgroup_mutex)
+{
+	if (kargs->flags & CLONE_INTO_CGROUP)
+		mutex_lock(&cgroup_mutex);
+}
+
 /**
  * cgroup_can_fork - called on a new task before the process is exposed
  * @child: the child process
+ * @kargs: the arguments passed to create the child process
  *
  * This prepares a new css_set for the child process which the child will
  * be attached to in cgroup_post_fork().
@@ -6175,6 +6182,13 @@ int cgroup_can_fork(struct task_struct *child, struct kernel_clone_args *kargs)
 	struct cgroup_subsys *ss;
 	int i, j, ret;
 
+	/*
+	 * cgroup_mutex should have been acquired by cgroup_prep_fork() if
+	 * CLONE_INTO_CGROUP
+	 */
+	if (kargs->flags & CLONE_INTO_CGROUP)
+		lockdep_assert_held(&cgroup_mutex);
+
 	ret = cgroup_css_set_fork(kargs);
 	if (ret)
 		return ret;
kernel/fork.c +9 -1
index 38681ad44c76b..34fb9db59148b 100644
@@ -23,6 +23,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/task_stack.h>
 #include <linux/sched/cputime.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/seq_file.h>
 #include <linux/rtmutex.h>
 #include <linux/init.h>
@@ -2285,6 +2286,10 @@ static __latent_entropy struct task_struct *copy_process(
 	p->kretprobe_instances.first = NULL;
 #endif
 
+	cgroup_prep_fork(args);
+
+	threadgroup_change_begin(current);
+
 	/*
 	 * Ensure that the cgroup subsystem policies allow the new process to be
 	 * forked. It should be noted that the new process's css_set can be changed
@@ -2293,7 +2298,7 @@ static __latent_entropy struct task_struct *copy_process(
 	 */
 	retval = cgroup_can_fork(p, args);
 	if (retval)
-		goto bad_fork_put_pidfd;
+		goto bad_fork_threadgroup_change_end;
 
 	/*
 	 * From this point on we must avoid any synchronous user-space
@@ -2407,6 +2412,7 @@ static __latent_entropy struct task_struct *copy_process(
 	proc_fork_connector(p);
 	sched_post_fork(p);
 	cgroup_post_fork(p, args);
+	threadgroup_change_end(current);
 	perf_event_fork(p);
 
 	trace_task_newtask(p, clone_flags);
@@ -2421,6 +2427,8 @@ static __latent_entropy struct task_struct *copy_process(
 	spin_unlock(&current->sighand->siglock);
 	write_unlock_irq(&tasklist_lock);
 	cgroup_cancel_fork(p, args);
+bad_fork_threadgroup_change_end:
+	threadgroup_change_end(current);
 bad_fork_put_pidfd:
 	if (clone_flags & CLONE_PIDFD) {
 		fput(pidfile);
kernel/sched/core.c +4 -0
index 1bba4128a3e68..bee6bf6d9659d 100644
@@ -84,6 +84,10 @@ unsigned int sysctl_sched_rt_period = 1000000;
 
 __read_mostly int scheduler_running;
 
+#ifdef CONFIG_THREADGROUP_RWSEM
+DEFINE_PERCPU_RWSEM(threadgroup_rwsem);
+#endif
+
 #ifdef CONFIG_SCHED_CORE
 
 DEFINE_STATIC_KEY_FALSE(__sched_core_enabled);
kernel/sched/sched.h +1 -0
index 3d3e5793e1172..135e4265fd259 100644
@@ -28,6 +28,7 @@
 #include <linux/sched/sysctl.h>
 #include <linux/sched/task.h>
 #include <linux/sched/task_stack.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/sched/topology.h>
 #include <linux/sched/user.h>
 #include <linux/sched/wake_q.h>
kernel/signal.c +1 -0
index f01b249369ce2..d46e63266faf4 100644
@@ -20,6 +20,7 @@
 #include <linux/sched/task.h>
 #include <linux/sched/task_stack.h>
 #include <linux/sched/cputime.h>
+#include <linux/sched/threadgroup_rwsem.h>
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/proc_fs.h>
Signature · 1 line
2.33.1
author maintainer
#3 lore raw

PATCH 3/3 bpf: Implement prealloc for task_local_storage

In reply to Tejun Heo

From 5e3ad0d4a0b0732e7ebe035582d282ab752397ed Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Wed, 20 Oct 2021 08:56:53 -1000

task_local_storage currently does not support pre-allocation and the memory is allocated on demand using the GFP_ATOMIC mask. While atomic allocations succeed most of the time and the occasional failures aren't a problem for many use-cases, there are some which can benefit from reliable allocations - e.g. tracking acquisitions and releases of specific resources to root cause long-term reference leaks.

Prealloc semantics for task_local_storage:

* When a prealloc map is created, the map's elements for all existing tasks are allocated.

* Afterwards, whenever a new task is forked, it automatically allocates the elements for the existing preallocated maps.

To synchronize against concurrent forks, CONFIG_BPF_SYSCALL now enables CONFIG_THREADGROUP_RWSEM and prealloc task_local_storage creation path write-locks threadgroup_rwsem, and the rest of the implementation is straight-forward.

---
 include/linux/bpf.h                           |   6 +
 include/linux/bpf_local_storage.h             |  12 ++
 kernel/bpf/Kconfig                            |   1 +
 kernel/bpf/bpf_local_storage.c                | 112 ++++++++++----
 kernel/bpf/bpf_task_storage.c                 | 138 +++++++++++++++++-
 kernel/fork.c                                 |   8 +-
 .../bpf/prog_tests/task_local_storage.c       | 101 +++++++++++++
 .../selftests/bpf/progs/task_ls_prealloc.c    |  15 ++
 8 files changed, 361 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/task_ls_prealloc.c
include/linux/bpf.h +6 -0
index d604c8251d88..7f9e5dea0660 100644
@@ -1673,6 +1673,7 @@ struct bpf_prog *bpf_prog_by_id(u32 id);
 struct bpf_link *bpf_link_by_id(u32 id);
 
 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id);
+int bpf_task_storage_fork(struct task_struct *task);
 void bpf_task_storage_free(struct task_struct *task);
 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
 const struct btf_func_model *
@@ -1882,6 +1883,11 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 	return NULL;
 }
 
+static inline int bpf_task_storage_fork(struct task_struct *p)
+{
+	return 0;
+}
+
 static inline void bpf_task_storage_free(struct task_struct *task)
 {
 }
include/linux/bpf_local_storage.h +12 -0
index 24496bc28e7b..bbb4cedbd2b2 100644
@@ -51,6 +51,12 @@ struct bpf_local_storage_map {
 	u32 bucket_log;
 	u16 elem_size;
 	u16 cache_idx;
+	/* Maps with prealloc need to be tracked and allocated when a new
+	 * containing object is created. The following node can be used to keep
+	 * track of the prealloc maps. Outside of initializing the field, the
+	 * shared local_storage code doesn't use it directly.
+	 */
+	struct list_head prealloc_node;
 };
 
 struct bpf_local_storage_data {
@@ -118,6 +124,7 @@ void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
 
 /* Helper functions for bpf_local_storage */
 int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
+int bpf_local_storage_prealloc_map_alloc_check(union bpf_attr *attr);
 
 struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr);
 
@@ -158,6 +165,11 @@ bpf_local_storage_alloc(void *owner,
 			struct bpf_local_storage_elem *first_selem);
 
 struct bpf_local_storage_data *
+__bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
+			   void *value, u64 map_flags,
+			   struct bpf_local_storage **local_storage_prealloc,
+			   struct bpf_local_storage_elem **selem_prealloc);
+struct bpf_local_storage_data *
 bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 			 void *value, u64 map_flags);
 
kernel/bpf/Kconfig +1 -0
index a82d6de86522..4d816664026f 100644
@@ -29,6 +29,7 @@ config BPF_SYSCALL
 	select IRQ_WORK
 	select TASKS_TRACE_RCU
 	select BINARY_PRINTF
+	select THREADGROUP_RWSEM
 	select NET_SOCK_MSG if NET
 	default n
 	help
kernel/bpf/bpf_local_storage.c +82 -30

183 lines not on this page · raw message

kernel/bpf/bpf_task_storage.c +137 -1

181 lines not on this page · raw message

kernel/fork.c +7 -1
index 34fb9db59148..845c49c6e89b 100644
@@ -2290,6 +2290,10 @@ static __latent_entropy struct task_struct *copy_process(
 
 	threadgroup_change_begin(current);
 
+	retval = bpf_task_storage_fork(p);
+	if (retval)
+		goto bad_fork_threadgroup_change_end;
+
 	/*
 	 * Ensure that the cgroup subsystem policies allow the new process to be
 	 * forked. It should be noted that the new process's css_set can be changed
@@ -2298,7 +2302,7 @@ static __latent_entropy struct task_struct *copy_process(
 	 */
 	retval = cgroup_can_fork(p, args);
 	if (retval)
-		goto bad_fork_threadgroup_change_end;
+		goto bad_fork_bpf_task_storage_free;
 
 	/*
 	 * From this point on we must avoid any synchronous user-space
@@ -2427,6 +2431,8 @@ static __latent_entropy struct task_struct *copy_process(
 	spin_unlock(&current->sighand->siglock);
 	write_unlock_irq(&tasklist_lock);
 	cgroup_cancel_fork(p, args);
+bad_fork_bpf_task_storage_free:
+	bpf_task_storage_free(p);
 bad_fork_threadgroup_change_end:
 	threadgroup_change_end(current);
 bad_fork_put_pidfd:
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +101 -0

121 lines not on this page · raw message

tools/testing/selftests/bpf/progs/task_ls_prealloc.c new file +15 -0
new file mode 100644
index 000000000000..8b252ee3511e
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+	__uint(map_flags, 0);
+	__type(key, int);
+	__type(value, int);
+} prealloc_map SEC(".maps");
Signature · 1 line
2.33.1
#4 lore raw
I love your patch! Yet something to improve:

Hi Tejun,

I love your patch! Yet something to improve:

[auto build test ERROR on tj-cgroup/for-next]
[also build test ERROR on bpf-next/master bpf/master v5.15-rc6 next-20211021] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Tejun-Heo/cgroup-Drop-cgroup_-prefix-from-cgroup_threadgroup_rwsem-and-friends/20211021-051806
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup.git for-next
config: arm-randconfig-c002-20211021 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project 3cea2505fd8d99a9ba0cb625aecfe28a47c4e3f8)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/0day-ci/linux/commit/fc6c3a4ef8dde2e304e1b4eee47b7195638c1224
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Tejun-Heo/cgroup-Drop-cgroup_-prefix-from-cgroup_threadgroup_rwsem-and-friends/20211021-051806 git checkout fc6c3a4ef8dde2e304e1b4eee47b7195638c1224
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> kernel/bpf/bpf_task_storage.c:371:20: error: use of undeclared identifier 'task_storage_map_ops'
                   smap->map.ops = &task_storage_map_ops;
                                    ^

1 error generated.

vim +/task_storage_map_ops +371 kernel/bpf/bpf_task_storage.c

355
356 static struct bpf_map *task_storage_map_alloc(union bpf_attr *attr)
357 {
358 struct bpf_local_storage_map *smap;
359 int err;
360
361 smap = bpf_local_storage_map_alloc(attr);
362 if (IS_ERR(smap))
363 return ERR_CAST(smap);
364
365 if (!(attr->map_flags & BPF_F_NO_PREALLOC)) {
366 /* We're going to exercise the regular update path to populate
367 * the map for the existing tasks, which will call into map ops
368 * which is normally initialized after this function returns.
369 * Initialize it early here.
370 */

 > 371			smap->map.ops = &task_storage_map_ops;

372
373 percpu_down_write(&threadgroup_rwsem);
374 list_add_tail(&smap->prealloc_node, &prealloc_smaps);
375 err = task_storage_map_populate(smap);
376 percpu_up_write(&threadgroup_rwsem);
377 if (err) {
378 bpf_local_storage_map_free(smap,
379 &bpf_task_storage_busy);
380 return ERR_PTR(err);
381 }
382 }
383
384 smap->cache_idx = bpf_local_storage_cache_idx_get(&task_cache);
385 return &smap->map;
386 }
387

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw
In reply to Tejun Heo
Quoted 23 lines · Tejun Heo

From 5e3ad0d4a0b0732e7ebe035582d282ab752397ed Mon Sep 17 00:00:00 2001
From: Tejun Heo <[email protected]>
Date: Wed, 20 Oct 2021 08:56:53 -1000

task_local_storage currently does not support pre-allocation and the memory is allocated on demand using the GFP_ATOMIC mask. While atomic allocations succeed most of the time and the occasional failures aren't a problem for many use-cases, there are some which can benefit from reliable allocations - e.g. tracking acquisitions and releases of specific resources to root cause long-term reference leaks.

Prealloc semantics for task_local_storage:

* When a prealloc map is created, the map's elements for all existing tasks are allocated.

* Afterwards, whenever a new task is forked, it automatically allocates the elements for the existing preallocated maps.

To synchronize against concurrent forks, CONFIG_BPF_SYSCALL now enables CONFIG_THREADGROUP_RWSEM and prealloc task_local_storage creation path write-locks threadgroup_rwsem, and the rest of the implementation is straight-forward.

[ ... ]

Quoted 13 lines
kernel/bpf/bpf_task_storage.c +13 -0
+static int task_storage_map_populate(struct bpf_local_storage_map *smap)
+{
+	struct bpf_local_storage *storage = NULL;
+	struct bpf_local_storage_elem *selem = NULL;
+	struct task_struct *p, *g;
+	int err = 0;
+
+	lockdep_assert_held(&threadgroup_rwsem);
+retry:
+	if (!storage)
+		storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
+					  GFP_USER);
+	if (!selem)

kernel/bpf/bpf_task_storage.c static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key) · line 309

Quoted 10 lines
kernel/bpf/bpf_task_storage.c +10 -0
+		selem = bpf_map_kzalloc(&smap->map, smap->elem_size, GFP_USER);
+	if (!storage || !selem) {
+		err = -ENOMEM;
+		goto out_free;
+	}
+
+	rcu_read_lock();
+	bpf_task_storage_lock();
+
+	for_each_process_thread(g, p) {

I am thinking if this loop can be done in bpf iter.

If the bpf_local_storage_map is sleepable safe (not yet done but there is an earlier attempt [0]), bpf_local_storage_update() should be able to alloc without GFP_ATOMIC by sleepable bpf prog and this potentially will be useful in general for other sleepable use cases.

For example, if a sleepable bpf iter prog can run in this loop (or the existing bpf task iter loop is as good?), the iter bpf prog can call bpf_task_storage_get(BPF_SK_STORAGE_GET_F_CREATE) on a sleepable bpf_local_storage_map.

This pre-alloc then can be done similarly on the tcp/udp socket side by running a bpf prog at the existing bpf tcp/udp iter.

[0]: https://lore.kernel.org/bpf/[email protected]/

Quoted 45 lines
kernel/bpf/bpf_task_storage.c +45 -0
+		struct bpf_local_storage_data *sdata;
+
+		/* Try inserting with atomic allocations. On failure, retry with
+		 * the preallocated ones.
+		 */
+		sdata = bpf_local_storage_update(p, smap, NULL, BPF_NOEXIST);
+
+		if (PTR_ERR(sdata) == -ENOMEM && storage && selem) {
+			sdata = __bpf_local_storage_update(p, smap, NULL,
+							   BPF_NOEXIST,
+							   &storage, &selem);
+		}
+
+		/* Check -EEXIST before need_resched() to guarantee forward
+		 * progress.
+		 */
+		if (PTR_ERR(sdata) == -EEXIST)
+			continue;
+
+		/* If requested or alloc failed, take a breather and loop back
+		 * to preallocate.
+		 */
+		if (need_resched() ||
+		    PTR_ERR(sdata) == -EAGAIN || PTR_ERR(sdata) == -ENOMEM) {
+			bpf_task_storage_unlock();
+			rcu_read_unlock();
+			cond_resched();
+			goto retry;
+		}
+
+		if (IS_ERR(sdata)) {
+			err = PTR_ERR(sdata);
+			goto out_unlock;
+		}
+	}
+out_unlock:
+	bpf_task_storage_unlock();
+	rcu_read_unlock();
+out_free:
+	if (storage)
+		kfree(storage);
+	if (selem)
+		kfree(selem);
+	return err;
+}

[ ... ]

kernel/bpf/bpf_task_storage.c const struct bpf_map_ops task_storage_map_ops = { · line 426

Quoted 7 lines
kernel/bpf/bpf_task_storage.c +7 -0
+int bpf_task_storage_fork(struct task_struct *task)
+{
+	struct bpf_local_storage_map *smap;
+
+	percpu_rwsem_assert_held(&threadgroup_rwsem);
+
+	list_for_each_entry(smap, &prealloc_smaps, prealloc_node) {

Mostly a comment here from the networking side, I suspect the common use case is going to be more selective based on different protocol (tcp or udp) and even port. There is some existing bpf hooks during inet_sock creation time, bind time ...etc. The bpf prog can be selective on what bpf_sk_storage it needs by inspecting different fields of a sk.

e.g. in inet_create(), there is BPF_CGROUP_RUN_PROG_INET_SOCK(sk). Would a similar hook be useful on the fork side?

Quoted 28 lines
kernel/bpf/bpf_task_storage.c +28 -0
+		struct bpf_local_storage *storage;
+		struct bpf_local_storage_elem *selem;
+		struct bpf_local_storage_data *sdata;
+
+		storage = bpf_map_kzalloc(&smap->map, sizeof(*storage),
+					  GFP_USER);
+		selem = bpf_map_kzalloc(&smap->map, smap->elem_size, GFP_USER);
+
+		rcu_read_lock();
+		bpf_task_storage_lock();
+		sdata = __bpf_local_storage_update(task, smap, NULL, BPF_NOEXIST,
+						   &storage, &selem);
+		bpf_task_storage_unlock();
+		rcu_read_unlock();
+
+		if (storage)
+			kfree(storage);
+		if (selem)
+			kfree(selem);
+
+		if (IS_ERR(sdata)) {
+			bpf_task_storage_free(task);
+			return PTR_ERR(sdata);
+		}
+	}
+
+	return 0;
+}
author maintainer
#6 lore raw

Hello,

On Fri, Oct 22, 2021 at 03:47:33PM -0700, Martin KaFai Lau wrote:
...

Quoted 8 lines

kernel/bpf/bpf_task_storage.c static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key) · line 309

Quoted 1 line
kernel/bpf/bpf_task_storage.c +1 -0
+	for_each_process_thread(g, p) {

I am thinking if this loop can be done in bpf iter.

If the bpf_local_storage_map is sleepable safe (not yet done but there is an earlier attempt [0]), bpf_local_storage_update() should be able to alloc without GFP_ATOMIC by sleepable bpf prog and this potentially will be useful in general for other sleepable use cases.

Quoted 4 lines

For example, if a sleepable bpf iter prog can run in this loop (or the existing bpf task iter loop is as good?), the iter bpf prog can call bpf_task_storage_get(BPF_SK_STORAGE_GET_F_CREATE) on a sleepable bpf_local_storage_map.

Yeah, whatever that can walk all the existing tasks should do, and I think the locked section can be shrunk too.

        percpu_down_write(&threadgroup_rwsem);
        list_add_tail(&smap->prealloc_node, &prealloc_smaps);
        percpu_up_write(&threadgroup_rwsem);

        // Here, it's guaranteed that all new tasks are guaranteed to
        // prealloc on fork.

        Iterate all tasks in whatever way and allocate if necessary;

Thanks.

Signature · 1 line
tejun
#7 lore raw
In reply to Tejun Heo
Quoted 11 lines · Tejun Heo

task_local_storage currently does not support pre-allocation and the memory is allocated on demand using the GFP_ATOMIC mask. While atomic allocations succeed most of the time and the occasional failures aren't a problem for many use-cases, there are some which can benefit from reliable allocations - e.g. tracking acquisitions and releases of specific resources to root cause long-term reference leaks.

This patchset implements prealloc support for task_local_storage so that once a map is created, it's guaranteed that the storage area is always available for all tasks unless explicitly deleted.

Song, Martin, can you please take a look at this? It might be worthwhile to consider having pre-allocated local storage for all supported types: socket, cgroup, task. Especially for cases where BPF app is going to touch all or almost all system entities (sockets, cgroups, tasks, respectively).

Song, in ced47e30ab8b ("bpf: runqslower: Use task local storage") you did some benchmarking of task-local storage vs hashmap and it was faster in all cases but the first allocation of task-local storage. It would be curious to see how numbers change if task-local storage is pre-allocated, if you get a chance to benchmark it with Tejun's changes. Thanks!

Quoted 45 lines

The only tricky part is syncronizing against the fork path. Fortunately, cgroup needs to do the same thing and is already using cgroup_threadgroup_rwsem and we can use the same mechanism without adding extra overhead. This patchset generalizes the rwsem and make cgroup and bpf select it.

This patchset is on top of bpf-next 223f903e9c83 ("bpf: Rename BTF_KIND_TAG to BTF_KIND_DECL_TAG") and contains the following patches:

0001-cgroup-Drop-cgroup_-prefix-from-cgroup_threadgroup_r.patch 0002-sched-cgroup-Generalize-threadgroup_rwsem.patch 0003-bpf-Implement-prealloc-for-task_local_storage.patch

and also available in the following git branch:

git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git bpf-task-local-storage-prealloc

diffstat follows. Thanks.

 fs/exec.c                                                   |    7 +
 include/linux/bpf.h                                         |    6 +
 include/linux/bpf_local_storage.h                           |   12 +++
 include/linux/cgroup-defs.h                                 |   33 ---------
 include/linux/cgroup.h                                      |   11 +--
 include/linux/sched/threadgroup_rwsem.h                     |   46 ++++++++++++
 init/Kconfig                                                |    4 +
 kernel/bpf/Kconfig                                          |    1
 kernel/bpf/bpf_local_storage.c                              |  112 ++++++++++++++++++++++--------
 kernel/bpf/bpf_task_storage.c                               |  138 +++++++++++++++++++++++++++++++++++++-
 kernel/cgroup/cgroup-internal.h                             |    4 -
 kernel/cgroup/cgroup-v1.c                                   |    9 +-
 kernel/cgroup/cgroup.c                                      |   74 ++++++++++++--------
 kernel/cgroup/pids.c                                        |    2
 kernel/fork.c                                               |   16 ++++
 kernel/sched/core.c                                         |    4 +
 kernel/sched/sched.h                                        |    1
 kernel/signal.c                                             |    7 +
 tools/testing/selftests/bpf/prog_tests/task_local_storage.c |  101 +++++++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/task_ls_prealloc.c        |   15 ++++
 20 files changed, 489 insertions(+), 114 deletions(-)

--
tejun