Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751654AbcJASzn (ORCPT ); Sat, 1 Oct 2016 14:55:43 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33035 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750949AbcJASzh (ORCPT ); Sat, 1 Oct 2016 14:55:37 -0400 From: Manfred Spraul To: Peter Zijlstra , Andrew Morton , Davidlohr Bueso Cc: LKML , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , 1vier1@web.de, felixh@informatik.uni-bremen.de, Manfred Spraul Subject: [PATCH 1/2] ipc/sem.c: Avoid using spin_unlock_wait() Date: Sat, 1 Oct 2016 20:40:16 +0200 Message-Id: <1475347217-2143-2-git-send-email-manfred@colorfullife.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1475347217-2143-1-git-send-email-manfred@colorfullife.com> References: <1475347217-2143-1-git-send-email-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2114 Lines: 72 a) The ACQUIRE in spin_lock() applies to the read, not to the store, at least for powerpc. This forces to add a smp_mb() into the fast path. b) The memory barrier provided by spin_unlock_wait() is right now arch dependent. Therefore: Use spin_lock()/spin_unlock() instead of spin_unlock_wait(). Advantage: faster single op semop calls(), observed +8.9% on x86. (the other solution would be arch dependencies in ipc/sem). Disadvantage: slower complex op semop calls, if (and only if) there are no sleeping operations. The next patch adds hysteresis, this further reduces the probability that the slow path is used. Signed-off-by: Manfred Spraul --- ipc/sem.c | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/ipc/sem.c b/ipc/sem.c index 5e318c5..d5f2710 100644 --- a/ipc/sem.c +++ b/ipc/sem.c @@ -280,24 +280,13 @@ static void complexmode_enter(struct sem_array *sma) return; } - /* We need a full barrier after seting complex_mode: - * The write to complex_mode must be visible - * before we read the first sem->lock spinlock state. - */ - smp_store_mb(sma->complex_mode, true); + sma->complex_mode = true; for (i = 0; i < sma->sem_nsems; i++) { sem = sma->sem_base + i; - spin_unlock_wait(&sem->lock); + spin_lock(&sem->lock); + spin_unlock(&sem->lock); } - /* - * spin_unlock_wait() is not a memory barriers, it is only a - * control barrier. The code must pair with spin_unlock(&sem->lock), - * thus just the control barrier is insufficient. - * - * smp_rmb() is sufficient, as writes cannot pass the control barrier. - */ - smp_rmb(); } /* @@ -363,14 +352,6 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops, */ spin_lock(&sem->lock); - /* - * See 51d7d5205d33 - * ("powerpc: Add smp_mb() to arch_spin_is_locked()"): - * A full barrier is required: the write of sem->lock - * must be visible before the read is executed - */ - smp_mb(); - if (!smp_load_acquire(&sma->complex_mode)) { /* fast path successful! */ return sops->sem_num; -- 2.7.4