Re: [PATCH 1/2 v4] rseq/membarrier: add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ

2020-08-26 Thread kernel test robot
Hi Peter,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on kselftest/next]
[also build test ERROR on tip/sched/core linux/master linus/master v5.9-rc2 
next-20200826]
[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/Peter-Oskolkov/rseq-membarrier-add-MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ/20200827-024952
base:   
https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git next
config: powerpc64-randconfig-r001-20200826 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 
7cfcecece0e0430937cf529ce74d3a071a4dedc6)
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 powerpc64 cross compiling tool for clang build
# apt-get install binutils-powerpc64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross 
ARCH=powerpc64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot 

All errors (new ones prefixed by >>):

>> kernel/sched/membarrier.c:379:1: error: conflicting types for 
>> 'sys_membarrier'
   SYSCALL_DEFINE3(membarrier, int, cmd, int, flags, int, cpu_id)
   ^
   include/linux/syscalls.h:215:36: note: expanded from macro 'SYSCALL_DEFINE3'
   #define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)
  ^
   include/linux/syscalls.h:224:2: note: expanded from macro 'SYSCALL_DEFINEx'
   __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
   ^
   include/linux/syscalls.h:238:18: note: expanded from macro 
'__SYSCALL_DEFINEx'
   asmlinkage long sys##name(__MAP(x,__SC_DECL,__VA_ARGS__))   \
   ^
   :7:1: note: expanded from here
   sys_membarrier
   ^
   include/linux/syscalls.h:977:17: note: previous declaration is here
   asmlinkage long sys_membarrier(int cmd, int flags);
   ^
   1 error generated.

# 
https://github.com/0day-ci/linux/commit/41ee0f192c40af25a53abea1610a9ea4852cf0f4
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review 
Peter-Oskolkov/rseq-membarrier-add-MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ/20200827-024952
git checkout 41ee0f192c40af25a53abea1610a9ea4852cf0f4
vim +/sys_membarrier +379 kernel/sched/membarrier.c

   342  
   343  /**
   344   * sys_membarrier - issue memory barriers on a set of threads
   345   * @cmd:Takes command values defined in enum membarrier_cmd.
   346   * @flags:  Currently needs to be 0 for all commands other than
   347   *  MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ: in the latter
   348   *  case it can be MEMBARRIER_CMD_FLAG_CPU, indicating that 
@cpu_id
   349   *  contains the CPU on which to interrupt (= restart)
   350   *  the RSEQ critical section.
   351   * @cpu_id: if @flags == MEMBARRIER_CMD_FLAG_CPU, indicates the cpu on 
which
   352   *  RSEQ CS should be interrupted (@cmd must be
   353   *  MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ).
   354   *
   355   * If this system call is not implemented, -ENOSYS is returned. If the
   356   * command specified does not exist, not available on the running
   357   * kernel, or if the command argument is invalid, this system call
   358   * returns -EINVAL. For a given command, with flags argument set to 0,
   359   * if this system call returns -ENOSYS or -EINVAL, it is guaranteed to
   360   * always return the same value until reboot. In addition, it can return
   361   * -ENOMEM if there is not enough memory available to perform the system
   362   * call.
   363   *
   364   * All memory accesses performed in program order from each targeted 
thread
   365   * is guaranteed to be ordered with respect to sys_membarrier(). If we 
use
   366   * the semantic "barrier()" to represent a compiler barrier forcing 
memory
   367   * accesses to be performed in program order across the barrier, and
   368   * smp_mb() to represent explicit memory barriers forcing full memory
   369   * ordering across the barrier, we have the following ordering table for
   370   * each pair of barrier(), sys_membarrier() and smp_mb():
   371   *
   372   * The pair ordering is detailed as (O: ordered, X: not ordered):
   373   *
   374   *barrier()   smp_mb() sys_membarrier()
   375   *barrier()  X   XO
   376   *smp_mb()   X   OO
   377   *sys_membarrier()   O   OO
   378   */
 > 379  SYSCALL_DEFINE3(membarrier, int, cmd, int, flags, int, cpu_id)

---
0-DAY CI Kernel Test Service, Intel 

[PATCH 1/2 v4] rseq/membarrier: add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ

2020-08-26 Thread Peter Oskolkov
This patchset is based on Google-internal RSEQ
work done by Paul Turner and Andrew Hunter.

When working with per-CPU RSEQ-based memory allocations,
it is sometimes important to make sure that a global
memory location is no longer accessed from RSEQ critical
sections. For example, there can be two per-CPU lists,
one is "active" and accessed per-CPU, while another one
is inactive and worked on asynchronously "off CPU" (e.g.
garbage collection is performed). Then at some point
the two lists are swapped, and a fast RCU-like mechanism
is required to make sure that the previously active
list is no longer accessed.

This patch introduces such a mechanism: in short,
membarrier() syscall issues an IPI to a CPU, restarting
a potentially active RSEQ critical section on the CPU.

v1->v2:
  - removed the ability to IPI all CPUs in a single sycall;
  - use task->mm rather than task->group_leader to identify
tasks belonging to the same process.
v2->v3:
  - re-added the ability to IPI all CPUs in a single syscall;
  - integrated with membarrier_private_expedited() to
make sure only CPUs running tasks with the same mm as
the current task are interrupted;
  - also added MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ;
  - flags in membarrier_private_expedited are never actually
bit flags but always distinct values (i.e. never two flags
are combined), so I modified bit testing to full equation
comparison for simplicity (otherwise the code needs to
work when several bits are set, for example).
v3->v4:
  - added the third parameter to membarrier syscall: @cpu_id:
if @flags == MEMBARRIER_CMD_FLAG_CPU, then @cpu_id indicates
the cpu on which RSEQ CS should be restarted.

The second patch in the patchset adds a selftest
of this feature.

Signed-off-by: Peter Oskolkov 
---
 include/linux/sched/mm.h|  3 ++
 include/uapi/linux/membarrier.h | 29 ++
 kernel/sched/membarrier.c   | 67 -
 3 files changed, 89 insertions(+), 10 deletions(-)

diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h
index f889e332912f..15bfb06f2884 100644
--- a/include/linux/sched/mm.h
+++ b/include/linux/sched/mm.h
@@ -348,10 +348,13 @@ enum {
MEMBARRIER_STATE_GLOBAL_EXPEDITED   = (1U << 3),
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE_READY  = (1U << 4),
MEMBARRIER_STATE_PRIVATE_EXPEDITED_SYNC_CORE= (1U << 5),
+   MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ_READY   = (1U << 6),
+   MEMBARRIER_STATE_PRIVATE_EXPEDITED_RSEQ = (1U << 7),
 };
 
 enum {
MEMBARRIER_FLAG_SYNC_CORE   = (1U << 0),
+   MEMBARRIER_FLAG_RSEQ= (1U << 1),
 };
 
 #ifdef CONFIG_ARCH_HAS_MEMBARRIER_CALLBACKS
diff --git a/include/uapi/linux/membarrier.h b/include/uapi/linux/membarrier.h
index 5891d7614c8c..98c2b0e7c0d8 100644
--- a/include/uapi/linux/membarrier.h
+++ b/include/uapi/linux/membarrier.h
@@ -114,6 +114,29 @@
  *  If this command is not implemented by an
  *  architecture, -EINVAL is returned.
  *  Returns 0 on success.
+ * @MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ:
+ *  In addition to provide memory ordering
+ *  guarantees described in
+ *  MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE,
+ *  ensure the caller thread, upon return from
+ *  system call, that all its running thread
+ *  siblings have any currently running rseq
+ *  critical sections restarted if @flags
+ *  parameter is 0; if @flags parameter is
+ *  MEMBARRIER_CMD_FLAG_CPU,
+ *  then this operation is performed only
+ *  on CPU indicated by @cpu_id. If this command is
+ *  not implemented by an architecture, -EINVAL
+ *  is returned. A process needs to register its
+ *  intent to use the private expedited rseq
+ *  command prior to using it, otherwise
+ *  this command returns -EPERM.
+ * @MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_RSEQ:
+ *  Register the process intent to use
+ *  MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ.
+ *  If this command is not implemented by an
+ *  architecture, -EINVAL is returned.
+ *  Returns 0 on success.
  * @MEMBARRIER_CMD_SHARED:
  *  Alias to MEMBARRIER_CMD_GLOBAL. Provided for
  *  header backward compatibility.
@@ -131,9 +154,15 @@ enum membarrier_cmd {
MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED   = (1 << 4),