From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756400AbcLNRJH (ORCPT ); Wed, 14 Dec 2016 12:09:07 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34374 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755925AbcLNRJF (ORCPT ); Wed, 14 Dec 2016 12:09:05 -0500 Date: Wed, 14 Dec 2016 15:08:37 -0200 From: Marcelo Tosatti To: Thomas Gleixner Cc: Fenghua Yu , linux-kernel@vger.kernel.org Subject: [PATCH v3] intelrdt: resctrl: recommend locking for resctrlfs Message-ID: <20161214170835.GA16924@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.39]); Wed, 14 Dec 2016 17:09:05 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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 +#include + +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); +} + +