From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754298AbcLBWHd (ORCPT ); Fri, 2 Dec 2016 17:07:33 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49596 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751512AbcLBWHc (ORCPT ); Fri, 2 Dec 2016 17:07:32 -0500 Date: Fri, 2 Dec 2016 20:07:04 -0200 From: Marcelo Tosatti To: Thomas Gleixner Cc: Fenghua Yu , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] intelrdt: resctrl: recommend locking for resctrlfs Message-ID: <20161202220702.GA31213@amt.cnet> References: <20161130154809.GA27444@amt.cnet> <20161130220530.GG35583@linux.intel.com> <20161201145541.GA19232@amt.cnet> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 02 Dec 2016 22:07:31 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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?