From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752070AbcF1F1e (ORCPT ); Tue, 28 Jun 2016 01:27:34 -0400 Received: from mx2.suse.de ([195.135.220.15]:42142 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750921AbcF1F1c (ORCPT ); Tue, 28 Jun 2016 01:27:32 -0400 Date: Mon, 27 Jun 2016 22:27:17 -0700 From: Davidlohr Bueso To: Manfred Spraul Cc: Stephen Rothwell , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Peter Zijlstra , Andrew Morton , LKML , linux-next@vger.kernel.org, 1vier1@web.de, felixh@informatik.uni-bremen.de, stable@vger.kernel.org Subject: Re: [PATCH 1/2] ipc/sem.c: Fix complex_count vs. simple op race Message-ID: <20160628052717.GA13793@linux-80c1.suse> References: <20160615152318.164b1ebd@canb.auug.org.au> <1466280142-19741-1-git-send-email-manfred@colorfullife.com> <20160621003015.GA15593@linux-80c1.suse> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 23 Jun 2016, Manfred Spraul wrote: >What I'm not sure yet is if smp_load_acquire() is sufficient: > >Thread A: >> if (!READ_ONCE(sma->complex_mode)) { >The code is test_and_test, no barrier requirements for first test Yeah, it would just make us take the big lock unnecessarily, nothing fatal and I agree its probably worth the optimization. It still might be worth commenting. >> /* >> * It appears that no complex operation is around. >> * Acquire the per-semaphore lock. >> */ >> spin_lock(&sem->lock); >> >> if (!smp_load_acquire(&sma->complex_mode)) { >> /* fast path successful! */ >> return sops->sem_num; >> } >> spin_unlock(&sem->lock); >> } > >Thread B: >> WRITE_ONCE(sma->complex_mode, true); >> >> /* We need a full barrier: >> * The write to complex_mode must be visible >> * before we read the first sem->lock spinlock state. >> */ >> smp_mb(); >> >> for (i = 0; i < sma->sem_nsems; i++) { >> sem = sma->sem_base + i; >> spin_unlock_wait(&sem->lock); >> } > >If thread A is allowed to issue read_spinlock;read complex_mode;write >spinlock, then thread B would not notice that thread A is in the >critical section Are you referring to the sem->lock word not being visibly locked before we read complex_mode (for the second time)? This issue was fixed in 2c610022711 (locking/qspinlock: Fix spin_unlock_wait() some more). So smp_load_acquire should be enough afaict, or are you referring to something else? Thanks, Davidlohr