All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] intelrdt: resctrl: recommend locking for resctrlfs
@ 2016-11-30 15:48 Marcelo Tosatti
  2016-11-30 21:10 ` Thomas Gleixner
  2016-11-30 22:05 ` Fenghua Yu
  0 siblings, 2 replies; 11+ messages in thread
From: Marcelo Tosatti @ 2016-11-30 15:48 UTC (permalink / raw)
  To: Yu, Fenghua, Thomas Gleixner; +Cc: linux-kernel


There is a locking problem between different applications
reading/writing to resctrlfs directory at the same time (read the patch
below for details).

Suggest a standard locking scheme for applications to use.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

--- Documentation/x86/intel_rdt_ui.txt.orig	2016-11-30 13:40:33.080233101 -0200
+++ Documentation/x86/intel_rdt_ui.txt	2016-11-30 13:45:01.253703259 -0200
@@ -212,3 +212,30 @@ Finally we move core 4-7 over to the new
 kernel and the tasks running there get 50% of the cache.
 
 # echo C0 > p0/cpus
+
+4) Locking between applications
+
+The allocation of an exclusive reservation
+of L3 cache involves:
+
+        1. read list of cbmmasks for each directory
+        2. find a contiguous set of bits in the global CBM bitmask
+          that is clear in any of the directory cbmmasks
+        3. create a new directory
+        4. set the bits found in step 2 to the new directory "schemata"
+           file
+
+If two applications attempt to allocate space race with each other
+(if two processes execute the steps above in a interlocked fashion),
+they can end up using the same bits of CBMMASK, which renders the
+reservations non-exclusive but shared.
+
+To coordinate creation of reservations on resctrl and avoid the problem
+above, the following locking procedure is recommended:
+
+A) open /var/lock/resctrl/fs.lock with O_CREAT|O_EXCL.
+B) if success, write pid of program accessing the directory
+   structure to this file.
+C) read/write the directory structure.
+D) remove file.
+

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] intelrdt: resctrl: recommend locking for resctrlfs
  2016-11-30 15:48 [PATCH] intelrdt: resctrl: recommend locking for resctrlfs Marcelo Tosatti
@ 2016-11-30 21:10 ` Thomas Gleixner
  2016-11-30 22:05 ` Fenghua Yu
  1 sibling, 0 replies; 11+ messages in thread
From: Thomas Gleixner @ 2016-11-30 21:10 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Yu, Fenghua, linux-kernel

On Wed, 30 Nov 2016, Marcelo Tosatti wrote:
> 
> There is a locking problem between different applications
> reading/writing to resctrlfs directory at the same time (read the patch
> below for details).
> 
> Suggest a standard locking scheme for applications to use.
> 
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> 
> --- Documentation/x86/intel_rdt_ui.txt.orig	2016-11-30 13:40:33.080233101 -0200
> +++ Documentation/x86/intel_rdt_ui.txt	2016-11-30 13:45:01.253703259 -0200

I can't remember that we changed the -p1 patch format to -p0 :(

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] intelrdt: resctrl: recommend locking for resctrlfs
  2016-11-30 15:48 [PATCH] intelrdt: resctrl: recommend locking for resctrlfs Marcelo Tosatti
  2016-11-30 21:10 ` Thomas Gleixner
@ 2016-11-30 22:05 ` Fenghua Yu
  2016-11-30 22:25   ` Marcelo Tosatti
  2016-12-01 14:55   ` [PATCH v2] " Marcelo Tosatti
  1 sibling, 2 replies; 11+ messages in thread
From: Fenghua Yu @ 2016-11-30 22:05 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Yu, Fenghua, Thomas Gleixner, linux-kernel

On Wed, Nov 30, 2016 at 01:48:10PM -0200, Marcelo Tosatti wrote:
> 
> There is a locking problem between different applications
> reading/writing to resctrlfs directory at the same time (read the patch
> below for details).
> 
> Suggest a standard locking scheme for applications to use.
> 
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> 
> --- Documentation/x86/intel_rdt_ui.txt.orig	2016-11-30 13:40:33.080233101 -0200
> +++ Documentation/x86/intel_rdt_ui.txt	2016-11-30 13:45:01.253703259 -0200
> @@ -212,3 +212,30 @@ Finally we move core 4-7 over to the new
>  kernel and the tasks running there get 50% of the cache.
>  
>  # echo C0 > p0/cpus
> +
> +4) Locking between applications
> +
> +The allocation of an exclusive reservation
> +of L3 cache involves:
> +
> +        1. read list of cbmmasks for each directory
> +        2. find a contiguous set of bits in the global CBM bitmask
> +          that is clear in any of the directory cbmmasks
> +        3. create a new directory
> +        4. set the bits found in step 2 to the new directory "schemata"
> +           file

This is one example of why locking is needed. There are other scenarios
that need the locking as well. For example, two applications scan each
directory to find an empty/less loaded "tasks". Both of them find that
directory p1 has empty "tasks" and write their own thread ids into the
"tasks" in p1. Turns out the "tasks" in p1 will have crowded threads or
workloads. A locking can solve this race scenario too.

As a user interface document, maybe we need a generic explanation why
locking plus the example.

> +
> +If two applications attempt to allocate space race with each other
> +(if two processes execute the steps above in a interlocked fashion),
> +they can end up using the same bits of CBMMASK, which renders the
> +reservations non-exclusive but shared.
> +
> +To coordinate creation of reservations on resctrl and avoid the problem
> +above, the following locking procedure is recommended:
> +
> +A) open /var/lock/resctrl/fs.lock with O_CREAT|O_EXCL.
> +B) if success, write pid of program accessing the directory
> +   structure to this file.
> +C) read/write the directory structure.
> +D) remove file.
> +

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] intelrdt: resctrl: recommend locking for resctrlfs
  2016-11-30 22:05 ` Fenghua Yu
@ 2016-11-30 22:25   ` Marcelo Tosatti
  2016-12-01 21:54     ` Fenghua Yu
  2016-12-01 14:55   ` [PATCH v2] " Marcelo Tosatti
  1 sibling, 1 reply; 11+ messages in thread
From: Marcelo Tosatti @ 2016-11-30 22:25 UTC (permalink / raw)
  To: Fenghua Yu; +Cc: Thomas Gleixner, linux-kernel

On Wed, Nov 30, 2016 at 02:05:31PM -0800, Fenghua Yu wrote:
> On Wed, Nov 30, 2016 at 01:48:10PM -0200, Marcelo Tosatti wrote:
> > 
> > There is a locking problem between different applications
> > reading/writing to resctrlfs directory at the same time (read the patch
> > below for details).
> > 
> > Suggest a standard locking scheme for applications to use.
> > 
> > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > 
> > --- Documentation/x86/intel_rdt_ui.txt.orig	2016-11-30 13:40:33.080233101 -0200
> > +++ Documentation/x86/intel_rdt_ui.txt	2016-11-30 13:45:01.253703259 -0200
> > @@ -212,3 +212,30 @@ Finally we move core 4-7 over to the new
> >  kernel and the tasks running there get 50% of the cache.
> >  
> >  # echo C0 > p0/cpus
> > +
> > +4) Locking between applications
> > +
> > +The allocation of an exclusive reservation
> > +of L3 cache involves:
> > +
> > +        1. read list of cbmmasks for each directory
> > +        2. find a contiguous set of bits in the global CBM bitmask
> > +          that is clear in any of the directory cbmmasks
> > +        3. create a new directory
> > +        4. set the bits found in step 2 to the new directory "schemata"
> > +           file
> 
> This is one example of why locking is needed. There are other scenarios
> that need the locking as well. For example, two applications scan each
> directory to find an empty/less loaded "tasks". Both of them find that
> directory p1 has empty "tasks" and write their own thread ids into the
> "tasks" in p1. Turns out the "tasks" in p1 will have crowded threads or
> workloads. A locking can solve this race scenario too.
> 
> As a user interface document, maybe we need a generic explanation why
> locking plus the example.

Well, agreed there are other races, but in this particular example
taking the file lock does not solve the "tasks" race: the contents of
the tasks file can change in face of fork.

So i've added your suggestion but can't use this example, if you have
another one you'd like to see added, please let me know... Replying with
V2.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs
  2016-11-30 22:05 ` Fenghua Yu
  2016-11-30 22:25   ` Marcelo Tosatti
@ 2016-12-01 14:55   ` Marcelo Tosatti
  2016-12-02 11:20     ` Thomas Gleixner
  1 sibling, 1 reply; 11+ messages in thread
From: Marcelo Tosatti @ 2016-12-01 14:55 UTC (permalink / raw)
  To: Fenghua Yu; +Cc: Thomas Gleixner, linux-kernel


There is a locking problem between different applications
reading/writing to resctrlfs directory at the same time (read the patch
below for details).

Suggest a standard locking scheme for applications to use.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

---
v2: Improve commentary about generality of locking problem ("Yu, Fenghua")
    Proper patch -p level (Thomas Gleixner)

diff --git a/Documentation/x86/intel_rdt_ui.txt b/Documentation/x86/intel_rdt_ui.txt
index d918d26..14b3214 100644
--- a/Documentation/x86/intel_rdt_ui.txt
+++ b/Documentation/x86/intel_rdt_ui.txt
@@ -212,3 +212,33 @@ Finally we move core 4-7 over to the new group and make sure that the
 kernel and the tasks running there get 50% of the cache.
 
 # echo C0 > p0/cpus
+
+4) Locking between applications
+
+Certain operations on the resctrl filesystem, composed of
+read / writes to multiple files, must be atomic.
+
+As an example, the allocation of an exclusive reservation
+of L3 cache involves:
+
+        1. read list of cbmmasks for each directory
+        2. find a contiguous set of bits in the global CBM bitmask
+           that is clear in any of the directory cbmmasks
+        3. create a new directory
+        4. set the bits found in step 2 to the new directory "schemata"
+           file
+
+If two applications attempting to allocate space race with each other
+(if two processes execute the steps above in a interlocked fashion),
+they can end up using the same bits of CBMMASK, which renders the
+reservations non-exclusive but shared.
+
+To coordinate atomic operations on resctrl and avoid the problem
+above, the following locking procedure is recommended:
+
+A) open /var/lock/resctrl/fs.lock with O_CREAT|O_EXCL.
+B) if success, write pid of program accessing the directory
+   structure to this file.
+C) read/write the directory structure.
+D) remove file.
+

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH] intelrdt: resctrl: recommend locking for resctrlfs
  2016-11-30 22:25   ` Marcelo Tosatti
@ 2016-12-01 21:54     ` Fenghua Yu
  0 siblings, 0 replies; 11+ messages in thread
From: Fenghua Yu @ 2016-12-01 21:54 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Fenghua Yu, Thomas Gleixner, linux-kernel

On Wed, Nov 30, 2016 at 08:25:28PM -0200, Marcelo Tosatti wrote:
> On Wed, Nov 30, 2016 at 02:05:31PM -0800, Fenghua Yu wrote:
> > On Wed, Nov 30, 2016 at 01:48:10PM -0200, Marcelo Tosatti wrote:
> > > 
> > > There is a locking problem between different applications
> > > reading/writing to resctrlfs directory at the same time (read the patch
> > > below for details).
> > > 
> > > Suggest a standard locking scheme for applications to use.
> > > 
> > > Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
> > > 
> > > --- Documentation/x86/intel_rdt_ui.txt.orig	2016-11-30 13:40:33.080233101 -0200
> > > +++ Documentation/x86/intel_rdt_ui.txt	2016-11-30 13:45:01.253703259 -0200
> > > @@ -212,3 +212,30 @@ Finally we move core 4-7 over to the new
> > >  kernel and the tasks running there get 50% of the cache.
> > >  
> > >  # echo C0 > p0/cpus
> > > +
> > > +4) Locking between applications
> > > +
> > > +The allocation of an exclusive reservation
> > > +of L3 cache involves:
> > > +
> > > +        1. read list of cbmmasks for each directory
> > > +        2. find a contiguous set of bits in the global CBM bitmask
> > > +          that is clear in any of the directory cbmmasks
> > > +        3. create a new directory
> > > +        4. set the bits found in step 2 to the new directory "schemata"
> > > +           file
> > 
> > This is one example of why locking is needed. There are other scenarios
> > that need the locking as well. For example, two applications scan each
> > directory to find an empty/less loaded "tasks". Both of them find that
> > directory p1 has empty "tasks" and write their own thread ids into the
> > "tasks" in p1. Turns out the "tasks" in p1 will have crowded threads or
> > workloads. A locking can solve this race scenario too.
> > 
> > As a user interface document, maybe we need a generic explanation why
> > locking plus the example.
> 
> Well, agreed there are other races, but in this particular example
> taking the file lock does not solve the "tasks" race: the contents of
> the tasks file can change in face of fork.

The "tasks" example is only for resolving the race when allocating two tasks
to an empty rdtgroup. Once a task is allocated to a "tasks", the task's forked
children will automatically stay with the task unless they are moved.
Without the locking, task A and task B are allocated to the same directory
because both of them thought "tasks" in the directory is empty. Then
all forked children of both A and B will populate the directory and cause
crowded cache.

Sure the contents of the "tasks" can change in face of fork. But the race
of the allocating two groups of tasks can cause wrong decision to allocate
them at the beginning.

> 
> So i've added your suggestion but can't use this example, if you have
> another one you'd like to see added, please let me know... Replying with
> V2.

Thanks.

-Fenghua

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs
  2016-12-01 14:55   ` [PATCH v2] " Marcelo Tosatti
@ 2016-12-02 11:20     ` Thomas Gleixner
  2016-12-02 22:07       ` Marcelo Tosatti
  2016-12-14 17:08       ` [PATCH v3] " Marcelo Tosatti
  0 siblings, 2 replies; 11+ messages in thread
From: Thomas Gleixner @ 2016-12-02 11:20 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Fenghua Yu, linux-kernel

On Thu, 1 Dec 2016, Marcelo Tosatti wrote:
> 
> There is a locking problem between different applications
> reading/writing to resctrlfs directory at the same time (read the patch
> below for details).
> 
> Suggest a standard locking scheme for applications to use.

....

> +To coordinate atomic operations on resctrl and avoid the problem
> +above, the following locking procedure is recommended:
> +
> +A) open /var/lock/resctrl/fs.lock with O_CREAT|O_EXCL.
> +B) if success, write pid of program accessing the directory
> +   structure to this file.
> +C) read/write the directory structure.
> +D) remove file.

What's wrong with using flock, which works from shell scripts as well?

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs
  2016-12-02 11:20     ` Thomas Gleixner
@ 2016-12-02 22:07       ` Marcelo Tosatti
  2016-12-09  9:35         ` Thomas Gleixner
  2016-12-14 17:08       ` [PATCH v3] " Marcelo Tosatti
  1 sibling, 1 reply; 11+ messages in thread
From: Marcelo Tosatti @ 2016-12-02 22:07 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Fenghua Yu, linux-kernel

On Fri, Dec 02, 2016 at 12:20:29PM +0100, Thomas Gleixner wrote:
> On Thu, 1 Dec 2016, Marcelo Tosatti wrote:
> > 
> > There is a locking problem between different applications
> > reading/writing to resctrlfs directory at the same time (read the patch
> > below for details).
> > 
> > Suggest a standard locking scheme for applications to use.
> 
> ....
> 
> > +To coordinate atomic operations on resctrl and avoid the problem
> > +above, the following locking procedure is recommended:
> > +
> > +A) open /var/lock/resctrl/fs.lock with O_CREAT|O_EXCL.
> > +B) if success, write pid of program accessing the directory
> > +   structure to this file.
> > +C) read/write the directory structure.
> > +D) remove file.
> 
> What's wrong with using flock, which works from shell scripts as well?
> 
> Thanks,
> 
> 	tglx

Hi Thomas,

Nothing wrong with it... I'm just copying the behaviour of other
programs.

Actually, using flock(2) allows one to use LOCK_SH for readers and 
this allows consistent writer/reader behaviour (say, a reader
won't see a partially written directory).

NAME
       flock - apply or remove an advisory lock on an open file

SYNOPSIS
       #include <sys/file.h>

       int flock(int fd, int operation);

DESCRIPTION
       Apply  or remove an advisory lock on the open file specified by
fd.  The argument operation is one of
       the following:

           LOCK_SH  Place a shared lock.  More than one process may hold
a shared lock for a given file at a
                    given time.

           LOCK_EX  Place  an  exclusive lock.  Only one process may
hold an exclusive lock for a given file
                    at a given time.

           LOCK_UN  Remove an existing lock held by this process.

--- 

So the procedure would be:

    /var/lock/resctrl/fs.lock created previously in the filesystem.

WRITE LOCK:

A) Take flock(EXCLUSIVE) on /var/lock/resctrl/fs.lock
B) If success, write pid of the program to the file.
C) read/write the directory structure.
D) funlock

READ LOCK:

A) Take flock(SHARED) on /var/lock/resctrl/fs.lock
B) If success read the directory structure.
C) funlock


How about that?

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs
  2016-12-02 22:07       ` Marcelo Tosatti
@ 2016-12-09  9:35         ` Thomas Gleixner
  0 siblings, 0 replies; 11+ messages in thread
From: Thomas Gleixner @ 2016-12-09  9:35 UTC (permalink / raw)
  To: Marcelo Tosatti; +Cc: Fenghua Yu, linux-kernel

On Fri, 2 Dec 2016, Marcelo Tosatti wrote:
> Actually, using flock(2) allows one to use LOCK_SH for readers and 
> this allows consistent writer/reader behaviour (say, a reader
> won't see a partially written directory).

Indeed.

> So the procedure would be:
> 
>     /var/lock/resctrl/fs.lock created previously in the filesystem.
> 
> WRITE LOCK:
> 
> A) Take flock(EXCLUSIVE) on /var/lock/resctrl/fs.lock
> B) If success, write pid of the program to the file.
> C) read/write the directory structure.
> D) funlock
> 
> READ LOCK:
> 
> A) Take flock(SHARED) on /var/lock/resctrl/fs.lock
> B) If success read the directory structure.
> C) funlock
> 
> 
> How about that?

Looks sane. Adding a small example in C and [ba]sh would be nice.

Thanks,

	tglx

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3] intelrdt: resctrl: recommend locking for resctrlfs
  2016-12-02 11:20     ` Thomas Gleixner
  2016-12-02 22:07       ` Marcelo Tosatti
@ 2016-12-14 17:08       ` Marcelo Tosatti
  2016-12-15 13:49         ` [tip:x86/cache] Documentation, x86, resctrl: Recommend " tip-bot for Marcelo Tosatti
  1 sibling, 1 reply; 11+ messages in thread
From: Marcelo Tosatti @ 2016-12-14 17:08 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Fenghua Yu, linux-kernel


There is a locking problem between different applications
reading/writing to resctrlfs directory at the same time
(read the patch below for details).

Suggest a standard locking scheme for applications to use.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

diff --git a/Documentation/x86/intel_rdt_ui.txt b/Documentation/x86/intel_rdt_ui.txt
index d918d26..ec3a58b 100644
--- a/Documentation/x86/intel_rdt_ui.txt
+++ b/Documentation/x86/intel_rdt_ui.txt
@@ -212,3 +212,118 @@ Finally we move core 4-7 over to the new group and make sure that the
 kernel and the tasks running there get 50% of the cache.
 
 # echo C0 > p0/cpus
+
+4) Locking between applications
+
+Certain operations on the resctrl filesystem, composed of
+read / writes to multiple files, must be atomic.
+
+As an example, the allocation of an exclusive reservation
+of L3 cache involves:
+
+        1. read list of cbmmasks for each directory
+        2. find a contiguous set of bits in the global CBM bitmask
+           that is clear in any of the directory cbmmasks
+        3. create a new directory
+        4. set the bits found in step 2 to the new directory "schemata"
+           file
+
+If two applications attempting to allocate space race with each other
+(if two processes execute the steps above in a interlocked fashion),
+they can end up using the same bits of CBMMASK, which renders the
+reservations non-exclusive but shared.
+
+To coordinate atomic operations on resctrl and avoid the problem
+above, the following locking procedure is recommended:
+
+WRITE LOCK:
+
+ A) Take flock(LOCK_EX) on /sys/fs/resctrl
+ B) read/write the directory structure.
+ C) funlock
+
+READ LOCK:
+
+ A) Take flock(LOCK_SH) on /sys/fs/resctrl
+ B) If success read the directory structure.
+ C) funlock
+
+Example with bash:
+
+# Atomically read directory structure
+$ flock -s /sys/fs/resctrl/ find /sys/fs/resctrl
+
+# Read directory contents and create new subdirectory
+
+$ cat create-dir.sh
+find /sys/fs/resctrl/ > output.txt
+mask = function-of(output.txt)
+mkdir /sys/fs/resctrl/newres/
+echo mask > /sys/fs/resctrl/newres/schemata
+
+$ flock /sys/fs/resctrl/ ./create-dir.sh
+
+Example with C:
+
+/*
+ * Example code do take advisory locks
+ * before accessing resctrl filesystem
+ */
+#include <sys/file.h>
+#include <stdlib.h>
+
+void resctrl_take_shared_lock(int fd)
+{
+	int ret;
+
+	/* take shared lock on resctrl filesystem */
+	ret = flock(fd, LOCK_SH);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void resctrl_take_exclusive_lock(int fd)
+{
+	int ret;
+
+	/* release lock on resctrl filesystem */
+	ret = flock(fd, LOCK_EX);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void resctrl_release_lock(int fd)
+{
+	int ret;
+
+	/* take shared lock on resctrl filesystem */
+	ret = flock(fd, LOCK_UN);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void main(void)
+{
+	int fd, ret;
+
+	fd = open("/sys/fs/resctrl", O_DIRECTORY);
+	if (fd == -1) {
+		perror("open");
+		exit(-1);
+	}
+	resctrl_take_shared_lock(fd);
+	/* code to read directory contents */
+	resctrl_release_lock(fd);
+
+	resctrl_take_exclusive_lock(fd);
+	/* code to read and write directory contents */
+	resctrl_release_lock(fd);
+}
+
+

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [tip:x86/cache] Documentation, x86, resctrl: Recommend locking for resctrlfs
  2016-12-14 17:08       ` [PATCH v3] " Marcelo Tosatti
@ 2016-12-15 13:49         ` tip-bot for Marcelo Tosatti
  0 siblings, 0 replies; 11+ messages in thread
From: tip-bot for Marcelo Tosatti @ 2016-12-15 13:49 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: fenghua.yu, linux-kernel, mtosatti, tglx, hpa, mingo

Commit-ID:  3c2a769de7955ff81818b49d388dd771bf6ae29d
Gitweb:     http://git.kernel.org/tip/3c2a769de7955ff81818b49d388dd771bf6ae29d
Author:     Marcelo Tosatti <mtosatti@redhat.com>
AuthorDate: Wed, 14 Dec 2016 15:08:37 -0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Thu, 15 Dec 2016 14:44:27 +0100

Documentation, x86, resctrl: Recommend locking for resctrlfs

Concurrent write or read/write access from applications to the resctrlfs
directory can result in incorrect readouts or setups.

Recommend a standard locking scheme for applications to use.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Link: http://lkml.kernel.org/r/20161214170835.GA16924@amt.cnet
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 Documentation/x86/intel_rdt_ui.txt | 114 +++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

diff --git a/Documentation/x86/intel_rdt_ui.txt b/Documentation/x86/intel_rdt_ui.txt
index d918d26..51cf6fa 100644
--- a/Documentation/x86/intel_rdt_ui.txt
+++ b/Documentation/x86/intel_rdt_ui.txt
@@ -212,3 +212,117 @@ Finally we move core 4-7 over to the new group and make sure that the
 kernel and the tasks running there get 50% of the cache.
 
 # echo C0 > p0/cpus
+
+4) Locking between applications
+
+Certain operations on the resctrl filesystem, composed of read/writes
+to/from multiple files, must be atomic.
+
+As an example, the allocation of an exclusive reservation of L3 cache
+involves:
+
+  1. Read the cbmmasks from each directory
+  2. Find a contiguous set of bits in the global CBM bitmask that is clear
+     in any of the directory cbmmasks
+  3. Create a new directory
+  4. Set the bits found in step 2 to the new directory "schemata" file
+
+If two applications attempt to allocate space concurrently then they can
+end up allocating the same bits so the reservations are shared instead of
+exclusive.
+
+To coordinate atomic operations on the resctrlfs and to avoid the problem
+above, the following locking procedure is recommended:
+
+Locking is based on flock, which is available in libc and also as a shell
+script command
+
+Write lock:
+
+ A) Take flock(LOCK_EX) on /sys/fs/resctrl
+ B) Read/write the directory structure.
+ C) funlock
+
+Read lock:
+
+ A) Take flock(LOCK_SH) on /sys/fs/resctrl
+ B) If success read the directory structure.
+ C) funlock
+
+Example with bash:
+
+# Atomically read directory structure
+$ flock -s /sys/fs/resctrl/ find /sys/fs/resctrl
+
+# Read directory contents and create new subdirectory
+
+$ cat create-dir.sh
+find /sys/fs/resctrl/ > output.txt
+mask = function-of(output.txt)
+mkdir /sys/fs/resctrl/newres/
+echo mask > /sys/fs/resctrl/newres/schemata
+
+$ flock /sys/fs/resctrl/ ./create-dir.sh
+
+Example with C:
+
+/*
+ * Example code do take advisory locks
+ * before accessing resctrl filesystem
+ */
+#include <sys/file.h>
+#include <stdlib.h>
+
+void resctrl_take_shared_lock(int fd)
+{
+	int ret;
+
+	/* take shared lock on resctrl filesystem */
+	ret = flock(fd, LOCK_SH);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void resctrl_take_exclusive_lock(int fd)
+{
+	int ret;
+
+	/* release lock on resctrl filesystem */
+	ret = flock(fd, LOCK_EX);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void resctrl_release_lock(int fd)
+{
+	int ret;
+
+	/* take shared lock on resctrl filesystem */
+	ret = flock(fd, LOCK_UN);
+	if (ret) {
+		perror("flock");
+		exit(-1);
+	}
+}
+
+void main(void)
+{
+	int fd, ret;
+
+	fd = open("/sys/fs/resctrl", O_DIRECTORY);
+	if (fd == -1) {
+		perror("open");
+		exit(-1);
+	}
+	resctrl_take_shared_lock(fd);
+	/* code to read directory contents */
+	resctrl_release_lock(fd);
+
+	resctrl_take_exclusive_lock(fd);
+	/* code to read and write directory contents */
+	resctrl_release_lock(fd);
+}

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2016-12-15 13:49 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-30 15:48 [PATCH] intelrdt: resctrl: recommend locking for resctrlfs Marcelo Tosatti
2016-11-30 21:10 ` Thomas Gleixner
2016-11-30 22:05 ` Fenghua Yu
2016-11-30 22:25   ` Marcelo Tosatti
2016-12-01 21:54     ` Fenghua Yu
2016-12-01 14:55   ` [PATCH v2] " Marcelo Tosatti
2016-12-02 11:20     ` Thomas Gleixner
2016-12-02 22:07       ` Marcelo Tosatti
2016-12-09  9:35         ` Thomas Gleixner
2016-12-14 17:08       ` [PATCH v3] " Marcelo Tosatti
2016-12-15 13:49         ` [tip:x86/cache] Documentation, x86, resctrl: Recommend " tip-bot for Marcelo Tosatti

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.