Skip to contentSkip to conversation

Provide fast access to thread-specific data

RFC PATCH New Prakash Sangappa 2 participants 7 messages Aug 27, 2021 – Aug 28, 2021

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

cover Provide fast access to thread-specific data

Sending this as RFC, looking for feedback.

Some applications, like a Databases require reading thread specific stats frequently from the kernel in latency sensitive codepath. The overhead of reading stats from kernel using system call affects performance. One use case is reading thread's scheduler stats from /proc schedstat file (/proc/pid/schedstat) to collect time spent by a thread executing on the cpu(sum_exec_runtime), time blocked waiting on runq(run_delay). These scheduler stats, read several times per transaction in latency-sensitive codepath, are used to measure time taken by DB operations.

This patch proposes to introduce a mechanism for kernel to share thread stats thru a per thread shared structure shared between userspace and kernel. The per thread shared structure is allocated on a page shared mapped between user space and kernel, which will provide a way for fast communication between user and kernel. Kernel publishes stats in this shared structure. Application thread can read from it in user space without requiring system calls.

Similarly, there can be other use cases for such shared structure mechanism.

Introduce 'off cpu' time:

The time spent executing on a cpu(sum_exec_runtime) by a thread, currently available thru thread's schedstat file, can be shared thru the shared structure mentioned above. However, when a thread is running on the cpu, this time gets updated periodically, can take upto 1ms or more as part of scheduler tick processing. If the application has to measure cpu time consumed across some DB operations, using 'sum_exec_runtime' will not be accurate. To address this the proposal is to introduce a thread's 'off cpu' time, which is measured at context switch, similar to time on runq(ie run_delay in schedstat file) is and should be more accurate. With that the application can determine cpu time consumed by taking the elapsed time and subtracting off cpu time. The off cpu time will be made available thru the shared structure along with the other schedstats from /proc/pid/schedstat file.

The elapsed time itself can be measured using clock_gettime, which is vdso optimized and would be fast. The schedstats(runq time & off cpu time) published in the shared structure will be accumulated time, same as what is available thru schedstat file, all in units of nanoseconds. The application would take the difference of the values from before and after the operation for measurement.

Preliminary results from a simple cached read Database workload shows performance benefit, when the database uses shared struct for reading stats vs reading from /proc directly.

Implementation:

A new system call is added to request use of shared structure by a user thread. Kernel will allocate page(s), shared mapped with user space in which per-thread shared structures will be allocated. These structures are padded to 128 bytes. This will contain struct members or nested structures corresponding to supported stats, like the thread's schedstats, published by the kernel for user space consumption. More struct members can be added as new feature support is implemented. Multiple such shared structures will be allocated from a page(upto 32 per 4k page) and avoid having to allocate one page per thread of a process. Although, will need optimizing for locality. Additional pages will be allocated as needed to accommodate more threads requesting use of shared structures. Aim is to not expose the layout of the shared structure itself to the application, which will allow future enhancements/changes without affecting the API.

The system call will return a pointer(user space mapped address) to the per thread shared structure members. Application would save this per thread pointer in a TLS variable and reference it.

The system call is of the form.
int task_getshared(int option, int flags, void __user *uaddr)

// Currently only TASK_SCHEDSTAT option is supported - returns pointer // to struct task_schedstat. The struct task_schedstat is nested within // the shared structure.

struct task_schedstat {
        volatile u64    sum_exec_runtime;
        volatile u64    run_delay;
        volatile u64    pcount;
        volatile u64    off_cpu;
};

Usage:

__thread struct task_schedstat *ts;
task_getshared(TASK_SCHEDSTAT, 0, &ts);

Subsequently the stats are accessed using the 'ts' pointer by the thread

Prakash Sangappa (3):
Introduce per thread user-kernel shared structure Publish tasks's scheduler stats thru the shared structure Introduce task's 'off cpu' time

 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 include/linux/mm_types.h               |   2 +
 include/linux/sched.h                  |   9 +
 include/linux/syscalls.h               |   2 +
 include/linux/task_shared.h            |  92 ++++++++++
 include/uapi/asm-generic/unistd.h      |   5 +-
 include/uapi/linux/task_shared.h       |  23 +++
 kernel/fork.c                          |   7 +
 kernel/sched/deadline.c                |   1 +
 kernel/sched/fair.c                    |   1 +
 kernel/sched/rt.c                      |   1 +
 kernel/sched/sched.h                   |   1 +
 kernel/sched/stats.h                   |  55 ++++--
 kernel/sched/stop_task.c               |   1 +
 kernel/sys_ni.c                        |   3 +
 mm/Makefile                            |   2 +-
 mm/task_shared.c                       | 314 +++++++++++++++++++++++++++++++++
 18 files changed, 501 insertions(+), 20 deletions(-)
 create mode 100644 include/linux/task_shared.h
 create mode 100644 include/uapi/linux/task_shared.h
 create mode 100644 mm/task_shared.c
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH 1/3 Introduce per thread user-kernel shared structure

A structure per thread is allocated from a page that is shared mapped between user space and kernel as means for faster communication. This will facilitate sharing information, Ex: per thread stats shared between kernel and user space, that can be read by applications without the need for making frequent system calls in latency sensitive code path.

A new system call is added, which will allocate the shared structure and return its mapped user address. Multiple such structures will be allocated on a page to accommodate requests from different threads of a multithreaded process. Available space on a page is managed using a bitmap. When a thread exits, the shared structure is freed and can get reused for another thread that requests the shared structure. More pages will be allocated and used as needed based on the number of threads requesting use of shared structures. These pages are all freed when the process exits.

Each of these shared structures are rounded to 128 bytes. Available space in this structure can be used to accommodate additional per thread stats, state etc as needed. In future, if more space beyond 128 bytes, is needed, multiple such shared structures per thread could be allocated and managed by the kernel. Although, space in shared structure for sharing any kind of stats or state should be sparingly used. Therefore shared structure layout is not exposed to user space. the system call will return the mapped user address of a specific member or nested structure within the shared structure corresponding to stats requested, This would allow future enhancements/changes without breaking the API.

---
 arch/x86/entry/syscalls/syscall_32.tbl |   1 +
 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 include/linux/mm_types.h               |   2 +
 include/linux/sched.h                  |   3 +
 include/linux/syscalls.h               |   2 +
 include/linux/task_shared.h            |  57 +++++++
 include/uapi/asm-generic/unistd.h      |   5 +-
 kernel/fork.c                          |   7 +
 kernel/sys_ni.c                        |   3 +
 mm/Makefile                            |   2 +-
 mm/task_shared.c                       | 301 +++++++++++++++++++++++++++++++++
 11 files changed, 382 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/task_shared.h
 create mode 100644 mm/task_shared.c
arch/x86/entry/syscalls/syscall_32.tbl +1 -0
index ce763a1..a194581 100644
@@ -452,3 +452,4 @@
 445	i386	landlock_add_rule	sys_landlock_add_rule
 446	i386	landlock_restrict_self	sys_landlock_restrict_self
 447	i386	memfd_secret		sys_memfd_secret
+448	i386	task_getshared		sys_task_getshared
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index f6b5779..9dda907 100644
@@ -369,6 +369,7 @@
 445	common	landlock_add_rule	sys_landlock_add_rule
 446	common	landlock_restrict_self	sys_landlock_restrict_self
 447	common	memfd_secret		sys_memfd_secret
+448	common	task_getshared		sys_task_getshared
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
include/linux/mm_types.h +2 -0
index 52bbd2b..5ec26ed 100644
@@ -572,6 +572,8 @@ struct mm_struct {
 #ifdef CONFIG_IOMMU_SUPPORT
 		u32 pasid;
 #endif
+		/* user shared pages */
+		void *usharedpg;
 	} __randomize_layout;
 
 	/*
include/linux/sched.h +3 -0
index ec8d07d..237aa21 100644
@@ -1400,6 +1400,9 @@ struct task_struct {
 	struct llist_head               kretprobe_instances;
 #endif
 
+	/* user shared struct */
+	void *task_ushrd;
+
 	/*
 	 * New fields for task_struct should be added above here, so that
 	 * they are included in the randomized portion of task_struct.
include/linux/syscalls.h +2 -0
index 69c9a70..09680b7 100644
@@ -1052,6 +1052,8 @@ asmlinkage long sys_landlock_add_rule(int ruleset_fd, enum landlock_rule_type ru
 asmlinkage long sys_landlock_restrict_self(int ruleset_fd, __u32 flags);
 asmlinkage long sys_memfd_secret(unsigned int flags);
 
+asmlinkage long sys_task_getshared(long opt, long flags, void __user *uaddr);
+
 /*
  * Architecture-specific system calls
  */
include/linux/task_shared.h new file +57 -0

57 lines not on this page · raw message

include/uapi/asm-generic/unistd.h +4 -1
index a9d6fcd..7c985b1 100644
@@ -878,8 +878,11 @@ __SYSCALL(__NR_landlock_restrict_self, sys_landlock_restrict_self)
 __SYSCALL(__NR_memfd_secret, sys_memfd_secret)
 #endif
 
+#define __NR_task_getshared 448
+__SYSCALL(__NR_task_getshared, sys_task_getshared)
+
 #undef __NR_syscalls
-#define __NR_syscalls 448
+#define __NR_syscalls 449
 
 /*
  * 32 bit systems traditionally used different
kernel/fork.c +7 -0
index bc94b2c..f84bac0 100644
@@ -97,6 +97,7 @@
 #include <linux/scs.h>
 #include <linux/io_uring.h>
 #include <linux/bpf.h>
+#include <linux/task_shared.h>
 
 #include <asm/pgalloc.h>
 #include <linux/uaccess.h>
@@ -903,6 +904,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node)
 	if (err)
 		goto free_stack;
 
+	/* task's ushared struct not inherited across fork */
+	tsk->task_ushrd =  NULL;
+
 #ifdef CONFIG_SECCOMP
 	/*
 	 * We must handle setting up seccomp filters once we're under
@@ -1049,6 +1053,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
 	mm->pmd_huge_pte = NULL;
 #endif
+	mm->usharedpg = NULL;
 	mm_init_uprobes_state(mm);
 
 	if (current->mm) {
@@ -1099,6 +1104,7 @@ static inline void __mmput(struct mm_struct *mm)
 	ksm_exit(mm);
 	khugepaged_exit(mm); /* must run before exit_mmap */
 	exit_mmap(mm);
+	mm_ushared_clear(mm);
 	mm_put_huge_zero_page(mm);
 	set_mm_exe_file(mm, NULL);
 	if (!list_empty(&mm->mmlist)) {
@@ -1308,6 +1314,7 @@ static int wait_for_vfork_done(struct task_struct *child,
 static void mm_release(struct task_struct *tsk, struct mm_struct *mm)
 {
 	uprobe_free_utask(tsk);
+	task_ushared_free(tsk);
 
 	/* Get rid of any cached register state */
 	deactivate_mm(tsk, mm);
kernel/sys_ni.c +3 -0
index 30971b1..8fbdc55 100644
@@ -481,3 +481,6 @@ COND_SYSCALL(setuid16);
 
 /* restartable sequence */
 COND_SYSCALL(rseq);
+
+/* task shared */
+COND_SYSCALL(task_getshared);
mm/Makefile +1 -1
index e343674..03f88fe 100644
@@ -52,7 +52,7 @@ obj-y			:= filemap.o mempool.o oom_kill.o fadvise.o \
 			   mm_init.o percpu.o slab_common.o \
 			   compaction.o vmacache.o \
 			   interval_tree.o list_lru.o workingset.o \
-			   debug.o gup.o mmap_lock.o $(mmu-y)
+			   debug.o gup.o mmap_lock.o task_shared.o $(mmu-y)
 
 # Give 'page_alloc' its own module-parameter namespace
 page-alloc-y := page_alloc.o
mm/task_shared.c new file +301 -0

301 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [RFC PATCH 1/3] Introduce per thread user-kernel shared structure

[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]

Hi Prakash,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on linus/master]
[also build test WARNING on v5.14-rc7]
[cannot apply to tip/sched/core hnaz-linux-mm/master tip/x86/asm next-20210827] [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/Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 8f9d0349841a2871624bb1e85309e03e9867c16e
config: arm-randconfig-r001-20210827 (attached as .config)
compiler: arm-linux-gnueabi-gcc (GCC) 11.2.0

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
# https://github.com/0day-ci/linux/commit/4afb2fb1653308287e0f2347dfff5c499acedee7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533 git checkout 4afb2fb1653308287e0f2347dfff5c499acedee7
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=arm

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

All warnings (new ones prefixed by >>):

>> <stdin>:1554:2: warning: #warning syscall task_getshared not implemented [-Wcpp]
--
>> <stdin>:1554:2: warning: #warning syscall task_getshared not implemented [-Wcpp]
--
>> <stdin>:1554:2: warning: #warning syscall task_getshared not implemented [-Wcpp]

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

#3 lore raw

Re: [RFC PATCH 1/3] Introduce per thread user-kernel shared structure

In reply to Prakash Sangappa
[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]

Hi Prakash,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on linus/master]
[also build test ERROR on v5.14-rc7]
[cannot apply to tip/sched/core hnaz-linux-mm/master tip/x86/asm next-20210827] [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/Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 8f9d0349841a2871624bb1e85309e03e9867c16e
config: nios2-defconfig (attached as .config)
compiler: nios2-linux-gcc (GCC) 11.2.0

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
# https://github.com/0day-ci/linux/commit/4afb2fb1653308287e0f2347dfff5c499acedee7
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533 git checkout 4afb2fb1653308287e0f2347dfff5c499acedee7
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=nios2

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

All errors (new ones prefixed by >>):

mm/task_shared.c: In function 'task_ushared_alloc':
mm/task_shared.c:264:1: warning: label 'out' defined but not used [-Wunused-label]

     264 | out:
         | ^~~
   In file included from mm/task_shared.c:9:
   mm/task_shared.c: At top level:
>> include/linux/syscalls.h:241:25: error: conflicting types for 'sys_task_getshared'; have 'long int(u64,  u64,  void *)' {aka 'long int(long long unsigned int,  long long unsigned int,  void *)'}
     241 |         asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))       \
         |                         ^~~
   include/linux/syscalls.h:227:9: note: in expansion of macro '__SYSCALL_DEFINEx'
     227 |         __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
         |         ^~~~~~~~~~~~~~~~~
   include/linux/syscalls.h:218:36: note: in expansion of macro 'SYSCALL_DEFINEx'
     218 | #define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)
         |                                    ^~~~~~~~~~~~~~~
   mm/task_shared.c:298:1: note: in expansion of macro 'SYSCALL_DEFINE3'
     298 | SYSCALL_DEFINE3(task_getshared, u64, opt, u64, flags, void __user *, uaddr)
         | ^~~~~~~~~~~~~~~
   In file included from mm/task_shared.c:9:
   include/linux/syscalls.h:1055:17: note: previous declaration of 'sys_task_getshared' with type 'long int(long int,  long int,  void *)'
    1055 | asmlinkage long sys_task_getshared(long opt, long flags, void __user *uaddr);
         |                 ^~~~~~~~~~~~~~~~~~

vim +241 include/linux/syscalls.h

1bd21c6c21e848 Dominik Brodowski   2018-04-05  230  
e145242ea0df6b Dominik Brodowski   2018-04-09  231  /*
e145242ea0df6b Dominik Brodowski   2018-04-09  232   * The asmlinkage stub is aliased to a function named __se_sys_*() which
e145242ea0df6b Dominik Brodowski   2018-04-09  233   * sign-extends 32-bit ints to longs whenever needed. The actual work is
e145242ea0df6b Dominik Brodowski   2018-04-09  234   * done within __do_sys_*().
e145242ea0df6b Dominik Brodowski   2018-04-09  235   */
1bd21c6c21e848 Dominik Brodowski   2018-04-05  236  #ifndef __SYSCALL_DEFINEx
bed1ffca022cc8 Frederic Weisbecker 2009-03-13  237  #define __SYSCALL_DEFINEx(x, name, ...)					\
bee20031772af3 Arnd Bergmann       2018-06-19  238  	__diag_push();							\
bee20031772af3 Arnd Bergmann       2018-06-19  239  	__diag_ignore(GCC, 8, "-Wattribute-alias",			\
bee20031772af3 Arnd Bergmann       2018-06-19  240  		      "Type aliasing is used to sanitize syscall arguments");\
83460ec8dcac14 Andi Kleen          2013-11-12 @241  	asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))	\
e145242ea0df6b Dominik Brodowski   2018-04-09  242  		__attribute__((alias(__stringify(__se_sys##name))));	\
c9a211951c7c79 Howard McLauchlan   2018-03-21  243  	ALLOW_ERROR_INJECTION(sys##name, ERRNO);			\
e145242ea0df6b Dominik Brodowski   2018-04-09  244  	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__));\
e145242ea0df6b Dominik Brodowski   2018-04-09  245  	asmlinkage long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__));	\
e145242ea0df6b Dominik Brodowski   2018-04-09  246  	asmlinkage long __se_sys##name(__MAP(x,__SC_LONG,__VA_ARGS__))	\
1a94bc34768e46 Heiko Carstens      2009-01-14  247  	{								\
e145242ea0df6b Dominik Brodowski   2018-04-09  248  		long ret = __do_sys##name(__MAP(x,__SC_CAST,__VA_ARGS__));\
07fe6e00f6cca6 Al Viro             2013-01-21  249  		__MAP(x,__SC_TEST,__VA_ARGS__);				\
2cf0966683430b Al Viro             2013-01-21  250  		__PROTECT(x, ret,__MAP(x,__SC_ARGS,__VA_ARGS__));	\
2cf0966683430b Al Viro             2013-01-21  251  		return ret;						\
1a94bc34768e46 Heiko Carstens      2009-01-14  252  	}								\
bee20031772af3 Arnd Bergmann       2018-06-19  253  	__diag_pop();							\
e145242ea0df6b Dominik Brodowski   2018-04-09  254  	static inline long __do_sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))
1bd21c6c21e848 Dominik Brodowski   2018-04-05  255  #endif /* __SYSCALL_DEFINEx */
1a94bc34768e46 Heiko Carstens      2009-01-14  256  

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

#4 lore raw

RFC PATCH 2/3 Publish tasks's scheduler stats thru the shared structure

In reply to Prakash Sangappa

Define a 'struct task_schedstat' which contains members corresponding to scheduler stats that are currently available thru /proc/pid/tasks/pid /schedstats. Update scheduler stats in this structure in kernel at the same time stats in the 'struct task_struct' are updated. Add a TASK_SCHEDSTAT option to task_getshared system call to request these per thread scheduler stats thru the shared structure.

---
 include/linux/task_shared.h      | 35 ++++++++++++++++++++++++++++++++++-
 include/uapi/linux/task_shared.h | 22 ++++++++++++++++++++++
 kernel/sched/deadline.c          |  1 +
 kernel/sched/fair.c              |  1 +
 kernel/sched/rt.c                |  1 +
 kernel/sched/sched.h             |  1 +
 kernel/sched/stats.h             |  3 +++
 kernel/sched/stop_task.c         |  1 +
 mm/task_shared.c                 | 13 +++++++++++++
 9 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/task_shared.h
include/linux/task_shared.h +34 -1
index de17849..62793e4 100644
@@ -3,6 +3,7 @@
 #define	__TASK_SHARED_H__
 
 #include <linux/mm_types.h>
+#include <uapi/linux/task_shared.h>
 
 /*
  * Track user-kernel shared pages referred by mm_struct
@@ -18,7 +19,7 @@ struct ushared_pages {
  * fast communication.
  */
 struct task_ushared {
-	long version;
+	struct task_schedstat ts;
 };
 
 /*
@@ -52,6 +53,38 @@ struct task_ushrd_struct {
 	struct ushared_pg *upg;
 };
 
+
+#ifdef CONFIG_SCHED_INFO
+
+#define task_update_exec_runtime(t)					\
+	do {								\
+		struct task_ushrd_struct *shrdp = t->task_ushrd;	\
+		if (shrdp != NULL && shrdp->kaddr != NULL)		\
+			shrdp->kaddr->ts.sum_exec_runtime =		\
+				 t->se.sum_exec_runtime;		\
+	} while (0)
+
+#define task_update_runq_stat(t, p)					\
+	do {								\
+		struct task_ushrd_struct *shrdp = t->task_ushrd;	\
+		if (shrdp != NULL && shrdp->kaddr != NULL) {		\
+			shrdp->kaddr->ts.run_delay =			\
+				 t->sched_info.run_delay;		\
+			if (p) {					\
+				shrdp->kaddr->ts.pcount =		\
+					 t->sched_info.pcount;		\
+			}						\
+		}							\
+	} while (0)
+#else
+
+#define task_update_exec_runtime(t)	do { } while (0)
+#define task_update_runq_stat(t, p)	do { } while (0)
+
+#endif
+
+
+
 extern void task_ushared_free(struct task_struct *t);
 extern void mm_ushared_clear(struct mm_struct *mm);
 #endif /* __TASK_SHARED_H__ */
include/uapi/linux/task_shared.h new file +22 -0
new file mode 100644
index 0000000..06a8522
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef LINUX_TASK_SHARED_H
+#define LINUX_TASK_SHARED_H
+
+/*
+ * Per task user-kernel mapped structure for faster communication.
+ */
+
+/*
+ * Following is the option to request struct task_schedstats shared structure,
+ * in which kernel shares the task's exec time and time on run queue & number
+ * of times it was scheduled to run on a cpu. Requires kernel with
+ * CONFIG_SCHED_INFO enabled.
+ */
+#define TASK_SCHEDSTAT 1
+
+struct task_schedstat {
+	volatile u64	sum_exec_runtime;
+	volatile u64	run_delay;
+	volatile u64	pcount;
+};
+#endif
kernel/sched/deadline.c +1 -0
index aaacd6c..189c74c 100644
@@ -1270,6 +1270,7 @@ static void update_curr_dl(struct rq *rq)
 
 	curr->se.sum_exec_runtime += delta_exec;
 	account_group_exec_runtime(curr, delta_exec);
+	task_update_exec_runtime(curr);
 
 	curr->se.exec_start = now;
 	cgroup_account_cputime(curr, delta_exec);
kernel/sched/fair.c +1 -0
index 44c4520..cbd182b 100644
@@ -817,6 +817,7 @@ static void update_curr(struct cfs_rq *cfs_rq)
 	if (entity_is_task(curr)) {
 		struct task_struct *curtask = task_of(curr);
 
+		task_update_exec_runtime(curtask);
 		trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
 		cgroup_account_cputime(curtask, delta_exec);
 		account_group_exec_runtime(curtask, delta_exec);
kernel/sched/rt.c +1 -0
index 3daf42a..61082fc 100644
@@ -1014,6 +1014,7 @@ static void update_curr_rt(struct rq *rq)
 
 	curr->se.sum_exec_runtime += delta_exec;
 	account_group_exec_runtime(curr, delta_exec);
+	task_update_exec_runtime(curr);
 
 	curr->se.exec_start = now;
 	cgroup_account_cputime(curr, delta_exec);
kernel/sched/sched.h +1 -0
index 14a41a2..4ebbd8f 100644
@@ -67,6 +67,7 @@
 #include <linux/syscalls.h>
 #include <linux/task_work.h>
 #include <linux/tsacct_kern.h>
+#include <linux/task_shared.h>
 
 #include <asm/tlb.h>
 
kernel/sched/stats.h +3 -0
index d8f8eb0..6b2d69c 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
 #ifdef CONFIG_SCHEDSTATS
+#include <linux/task_shared.h>
 
 /*
  * Expects runqueue lock to be held for atomicity of update
@@ -166,6 +167,7 @@ static inline void sched_info_dequeue(struct rq *rq, struct task_struct *t)
 	delta = rq_clock(rq) - t->sched_info.last_queued;
 	t->sched_info.last_queued = 0;
 	t->sched_info.run_delay += delta;
+	task_update_runq_stat(t, 0);
 
 	rq_sched_info_dequeue(rq, delta);
 }
@@ -188,6 +190,7 @@ static void sched_info_arrive(struct rq *rq, struct task_struct *t)
 	t->sched_info.run_delay += delta;
 	t->sched_info.last_arrival = now;
 	t->sched_info.pcount++;
+	task_update_runq_stat(t, 1);
 
 	rq_sched_info_arrive(rq, delta);
 }
kernel/sched/stop_task.c +1 -0
index f988ebe..7b9b60f 100644
@@ -82,6 +82,7 @@ static void put_prev_task_stop(struct rq *rq, struct task_struct *prev)
 			max(curr->se.statistics.exec_max, delta_exec));
 
 	curr->se.sum_exec_runtime += delta_exec;
+	task_update_exec_runtime(curr);
 	account_group_exec_runtime(curr, delta_exec);
 
 	curr->se.exec_start = rq_clock_task(rq);
mm/task_shared.c +13 -0
index 3ec5eb6..7169ccd 100644
@@ -275,6 +275,14 @@ static long task_getshared(u64 opt, u64 flags, void __user *uaddr)
 {
 	struct task_ushrd_struct *ushrd = current->task_ushrd;
 
+	/* Currently only TASK_SCHEDSTAT supported */
+#ifdef CONFIG_SCHED_INFO
+	if (opt != TASK_SCHEDSTAT)
+		return (-EINVAL);
+#else
+	return (-EOPNOTSUPP);
+#endif
+
 	/* We have address, return. */
 	if (ushrd != NULL && ushrd->upg != NULL) {
 		if (copy_to_user(uaddr, &ushrd->uaddr,
@@ -286,6 +294,11 @@ static long task_getshared(u64 opt, u64 flags, void __user *uaddr)
 	task_ushared_alloc();
 	ushrd = current->task_ushrd;
 	if (ushrd != NULL && ushrd->upg != NULL) {
+		if (opt == TASK_SCHEDSTAT) {
+			/* init current values */
+			task_update_exec_runtime(current);
+			task_update_runq_stat(current, 1);
+		}
 		if (copy_to_user(uaddr, &ushrd->uaddr,
 			sizeof(struct task_ushared *)))
 			return (-EFAULT);
Signature · 1 line
2.7.4
#5 lore raw

Re: [RFC PATCH 2/3] Publish tasks's scheduler stats thru the shared structure

[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]

Hi Prakash,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on linus/master]
[also build test ERROR on v5.14-rc7]
[cannot apply to tip/sched/core hnaz-linux-mm/master tip/x86/asm next-20210827] [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/Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 8f9d0349841a2871624bb1e85309e03e9867c16e
config: i386-randconfig-m021-20210827 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/26a66b61c86c043ea1923de63885408c50dbf02c
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Prakash-Sangappa/Provide-fast-access-to-thread-specific-data/20210828-073533 git checkout 26a66b61c86c043ea1923de63885408c50dbf02c
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

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

All errors (new ones prefixed by >>):

In file included from <command-line>:32:

>> ./usr/include/linux/task_shared.h:18:15: error: unknown type name 'u64'
      18 |  __volatile__ u64 sum_exec_runtime;
         |               ^~~
   ./usr/include/linux/task_shared.h:19:15: error: unknown type name 'u64'
      19 |  __volatile__ u64 run_delay;
         |               ^~~
   ./usr/include/linux/task_shared.h:20:15: error: unknown type name 'u64'
      20 |  __volatile__ u64 pcount;
         |               ^~~

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

#6 lore raw

RFC PATCH 3/3 Introduce task's 'off cpu' time

In reply to Prakash Sangappa

Add a task's 'off cpu' time in nanoseconds to sched_info, that represents accumulated time spent either on run queue or blocked in the kernel. Publish the off cpu time thru the shared structure. This will be used by an application to determine cpu time consumed(time executing on a cpu) as accurately as possible, by taking elapsed time and subtracting off cpu time.

---
 include/linux/sched.h            |  6 +++++
 include/linux/task_shared.h      |  2 ++
 include/uapi/linux/task_shared.h |  1 +
 kernel/sched/stats.h             | 56 ++++++++++++++++++++++++++--------------
 4 files changed, 45 insertions(+), 20 deletions(-)
include/linux/sched.h +6 -0
index 237aa21..a63e447 100644
@@ -311,6 +311,12 @@ struct sched_info {
 	/* When were we last queued to run? */
 	unsigned long long		last_queued;
 
+	/* When did we last leave cpu */
+	unsigned long long		last_depart;
+
+	/* Time spent off cpu */
+	unsigned long long		off_cpu;
+
 #endif /* CONFIG_SCHED_INFO */
 };
 
include/linux/task_shared.h +2 -0
index 62793e4..ce475c4 100644
@@ -70,6 +70,8 @@ struct task_ushrd_struct {
 		if (shrdp != NULL && shrdp->kaddr != NULL) {		\
 			shrdp->kaddr->ts.run_delay =			\
 				 t->sched_info.run_delay;		\
+			shrdp->kaddr->ts.off_cpu =			\
+				 t->sched_info.off_cpu;			\
 			if (p) {					\
 				shrdp->kaddr->ts.pcount =		\
 					 t->sched_info.pcount;		\
include/uapi/linux/task_shared.h +1 -0
index 06a8522..c867c09 100644
@@ -18,5 +18,6 @@ struct task_schedstat {
 	volatile u64	sum_exec_runtime;
 	volatile u64	run_delay;
 	volatile u64	pcount;
+	volatile u64	off_cpu;
 };
 #endif
kernel/sched/stats.h +36 -20

83 lines not on this page · raw message

Signature · 1 line
2.7.4