Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2807883pxv; Sun, 27 Jun 2021 09:24:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJylfb7oNV8qAokEqJEjB+GwbnmKIS+PdngHC5vz5a4F6MqR4nmSfJ1vK1fxXTPiRTGr483v X-Received: by 2002:a6b:8e4c:: with SMTP id q73mr16011373iod.39.1624811082258; Sun, 27 Jun 2021 09:24:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624811082; cv=none; d=google.com; s=arc-20160816; b=S+dueYcNAIlfIkuqG0Bs2seO8ZkP9dOa7PHS8BSp2XKKet5K0+HSxIcvVMvPmc6QQI ahAuY+sUOE3Aa+c7JJrLCvr4gGx15NNXj5Ps3lZMc1LsnzDdUPa26rqW3ygSEMCiGFLV +uZ2HVtLutL/mhtqE6eHiGvRrgsDtvF5ZHZ6x8xcfDmMpn9YwOUit81HdOGsmzyxFXBM 5DR2LAtmR3Zh8o9cRHfQuXrrNZ3kJfR8i1j74qoLff8xS3thr1kVaph3VWbQe+i5hagf V4cpC6Ef1C11FKbhqjvYbkAqFPuW/F9/+cHz2faPPmUOekfYG1jN/LiOF2xyGnVN0u4Y VxJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2Ya8bcE5B9mZqBLYjxYQKOGyjXrk4mCJTYJHXgJ9Ev4=; b=NamHX+vOT+FpzYh69V/owL4nbOaSTKME9tEAx01spAnfUUeltwkhzmKqWh6kz8mY9G KHVhPKfsCZKsg/jK566n1icLXeaDWTUaR4j4dpaEoPiaPuBblWyZb1W/50+JmUVp6HBU Zlr4/RzorITqUFTfJ5LF4H8uNoFkPFd1joOdWhm2ILT4Q8P0CKn8OghegMjw0DlcSv0A 6U75r62/sTqVKzUG1tC8LosC5obnI1pxav6BGvi9cNyEQa67aAoJ4SsJei5pXwSnOTf4 K0tIroYa9qR7ZRGEJoub/lVhzZyUfi7OS7m+wEohYxYIPkBjAImp5TnH0L/Rw8J8Cg7g AF8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=0PLvLYBi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i18si14759746jal.90.2021.06.27.09.24.30; Sun, 27 Jun 2021 09:24:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=0PLvLYBi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231348AbhF0QWJ (ORCPT + 99 others); Sun, 27 Jun 2021 12:22:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbhF0QWE (ORCPT ); Sun, 27 Jun 2021 12:22:04 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BBDCC061767 for ; Sun, 27 Jun 2021 09:19:39 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id u20so4393898wmq.4 for ; Sun, 27 Jun 2021 09:19:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2Ya8bcE5B9mZqBLYjxYQKOGyjXrk4mCJTYJHXgJ9Ev4=; b=0PLvLYBiPtLVvFHh8ba/btIkDxuHk1lDhsClFiz0uoTeYC6DpGbRtItFCe/cgciHPu MfW4th291+fdJRQdQgk/7OHIvyWUI7gqF7QJWIdcSD6RS+9EGAdKH2gyLAJFjhVzPBSc SzRIhtNzHtkUwXvh2swIwTFA0GF+7Ozflk37HrFPz1v4J3OaDkRilzHgWlDk14hHVw7U SoQsrYQ7v/svrMpr7fImnIfGXM7TNHt4P0by2LjEAS5fD93SMYwl7TCSibuQZ5SxT2p2 g93mo54H57WTE7muNA/ak56aguktakndATpL3WLVjBRPbZWxdgSgzV/NHxWc4RU4GbRm n2rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2Ya8bcE5B9mZqBLYjxYQKOGyjXrk4mCJTYJHXgJ9Ev4=; b=uKx4pTnrlzUqrQGj1/bt4Khhvl6o0kXIsF+P11ccPfw9rOECLu3XmOoNiHrksyeH8L 6vQyDW70MOCKfLyrB2oNq79Wj1CsUO8CFkrki6XOpi1G1JNanMGtZTA/ZASjcghQWB4M v4ZMtuBvNNgW5uoHiUwLMDFmsEAdTIXBDfrD/FeZK1p1sb3NBYRY5zvnM7axhCzzjPJ5 lILhq4MuLw+ff4ZYMA7eBA1LNUKGP4S/I6ksVMXwP0fYVv8BnDIl0UqwycYRUcYGs6Hv TB5/htdrEzWOkeKWCdqaE+gciAUNWF0HBxpLE2x6cVxVJOdMzevmQMzxZ8rqRXnGh8yQ BCYQ== X-Gm-Message-State: AOAM531k6VWyDSMQiR0LQaL1ERl6oQXNXT6dqeHWabt6xqq1L1quBZPH AqLP1G6oK6aKwsP3G7GUE6uE3QQ+Uh5aIw== X-Received: by 2002:a7b:c4da:: with SMTP id g26mr21518677wmk.3.1624810777414; Sun, 27 Jun 2021 09:19:37 -0700 (PDT) Received: from localhost.localdomain (p200300d9974f98002cd84be72c5877b5.dip0.t-ipconnect.de. [2003:d9:974f:9800:2cd8:4be7:2c58:77b5]) by smtp.googlemail.com with ESMTPSA id f22sm10820384wmb.46.2021.06.27.09.19.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 27 Jun 2021 09:19:37 -0700 (PDT) From: Manfred Spraul To: LKML , Andrew Morton , netfilter-devel@vger.kernel.org Cc: Davidlohr Bueso , "Paul E . McKenney" , 1vier1@web.de, Manfred Spraul Subject: [PATCH 2/2] ipc/sem.c: use READ_ONCE()/WRITE_ONCE() for use_global_lock Date: Sun, 27 Jun 2021 18:19:19 +0200 Message-Id: <20210627161919.3196-3-manfred@colorfullife.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210627161919.3196-1-manfred@colorfullife.com> References: <20210627161919.3196-1-manfred@colorfullife.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The patch solves three weaknesses in ipc/sem.c: 1) The initial read of use_global_lock in sem_lock() is an intentional race. KCSAN detects these accesses and prints a warning. 2) The code assumes that plain C read/writes are not mangled by the CPU or the compiler. 3) The comment it sysvipc_sem_proc_show() was hard to understand: The rest of the comments in ipc/sem.c speaks about sem_perm.lock, and suddenly this function speaks about ipc_lock_object(). To solve 1) and 2), use READ_ONCE()/WRITE_ONCE(). Plain C reads are used in code that owns sma->sem_perm.lock. The comment is updated to solve 3) Signed-off-by: Manfred Spraul Reviewed-by: Paul E. McKenney --- ipc/sem.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ipc/sem.c b/ipc/sem.c index bf534c74293e..b7608502f9d8 100644 --- a/ipc/sem.c +++ b/ipc/sem.c @@ -217,6 +217,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it); * this smp_load_acquire(), this is guaranteed because the smp_load_acquire() * is inside a spin_lock() and after a write from 0 to non-zero a * spin_lock()+spin_unlock() is done. + * To prevent the compiler/cpu temporarily writing 0 to use_global_lock, + * READ_ONCE()/WRITE_ONCE() is used. * * 2) queue.status: (SEM_BARRIER_2) * Initialization is done while holding sem_lock(), so no further barrier is @@ -342,10 +344,10 @@ static void complexmode_enter(struct sem_array *sma) * Nothing to do, just reset the * counter until we return to simple mode. */ - sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS; + WRITE_ONCE(sma->use_global_lock, USE_GLOBAL_LOCK_HYSTERESIS); return; } - sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS; + WRITE_ONCE(sma->use_global_lock, USE_GLOBAL_LOCK_HYSTERESIS); for (i = 0; i < sma->sem_nsems; i++) { sem = &sma->sems[i]; @@ -371,7 +373,8 @@ static void complexmode_tryleave(struct sem_array *sma) /* See SEM_BARRIER_1 for purpose/pairing */ smp_store_release(&sma->use_global_lock, 0); } else { - sma->use_global_lock--; + WRITE_ONCE(sma->use_global_lock, + sma->use_global_lock-1); } } @@ -412,7 +415,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops, * Initial check for use_global_lock. Just an optimization, * no locking, no memory barrier. */ - if (!sma->use_global_lock) { + if (!READ_ONCE(sma->use_global_lock)) { /* * It appears that no complex operation is around. * Acquire the per-semaphore lock. @@ -2435,7 +2438,8 @@ static int sysvipc_sem_proc_show(struct seq_file *s, void *it) /* * The proc interface isn't aware of sem_lock(), it calls - * ipc_lock_object() directly (in sysvipc_find_ipc). + * ipc_lock_object(), i.e. spin_lock(&sma->sem_perm.lock). + * (in sysvipc_find_ipc) * In order to stay compatible with sem_lock(), we must * enter / leave complex_mode. */ -- 2.31.1