Re: [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs

From: Marcelo Tosatti
Date: Fri Dec 02 2016 - 17:07:36 EST


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?