From nobody Wed Apr 8 08:01:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC825C28D13 for ; Mon, 22 Aug 2022 09:39:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234274AbiHVJjB (ORCPT ); Mon, 22 Aug 2022 05:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231577AbiHVJi5 (ORCPT ); Mon, 22 Aug 2022 05:38:57 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE2E01401A for ; Mon, 22 Aug 2022 02:38:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661161135; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=zfpY3ryladZYLXHgzfscfqjYbCZN7BmzG+/csBCpaf0=; b=QCHThwmL+b654gaFWwuPU0jKnNy5CSvG3T4EUV26WKf8XGfPMCZyZodx7CMbDBLEwdiUPK 1UPtNylrkriWHsm/NW3BQi9NJPU9BncEtn3X+2zjNRAaT6KzV4LzR8rSvoQEHAE7PQLwkm Ff/9dT7FsQI9c2Qf3k6RyVgdzSKHZ0U= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-654-H8i-0JuyPRi0A0xPOS7Glw-1; Mon, 22 Aug 2022 05:38:52 -0400 X-MC-Unique: H8i-0JuyPRi0A0xPOS7Glw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F19BC8037AF; Mon, 22 Aug 2022 09:38:51 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CB8871121314; Mon, 22 Aug 2022 09:38:51 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 27M9cp1c032579; Mon, 22 Aug 2022 05:38:51 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 27M9colo032575; Mon, 22 Aug 2022 05:38:50 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 22 Aug 2022 05:38:50 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: [PATCH] wait_on_bit: add an acquire memory barrier Message-ID: User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: TEXT/PLAIN; charset="utf-8" Hi I'd like to ask what do you think about this patch? Do you want to commit=20 it - or do you think that the barrier should be added to the callers of=20 wait_on_bit? Mikulas From: Mikulas Patocka There are several places in the kernel where wait_on_bit is not followed by a memory barrier (for example, in drivers/md/dm-bufio.c:new_read). On architectures with weak memory ordering, it may happen that memory accesses that follow wait_on_bit are reordered before wait_on_bit and they may return invalid data. Fix this class of bugs by adding an acquire memory barrier to wait_on_bit, wait_on_bit_io, wait_on_bit_timeout and wait_on_bit_action. The code that uses these functions should clear the bit using the function clear_bit_unlock. Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org --- include/linux/wait_bit.h | 16 ++++++++++++---- kernel/sched/wait_bit.c | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) Index: linux-2.6/include/linux/wait_bit.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/include/linux/wait_bit.h 2022-08-20 14:33:44.000000000 += 0200 +++ linux-2.6/include/linux/wait_bit.h 2022-08-20 15:41:43.000000000 +0200 @@ -71,8 +71,10 @@ static inline int wait_on_bit(unsigned long *word, int bit, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit(bit, word)) { + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ return 0; + } return out_of_line_wait_on_bit(word, bit, bit_wait, mode); @@ -96,8 +98,10 @@ static inline int wait_on_bit_io(unsigned long *word, int bit, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit(bit, word)) { + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ return 0; + } return out_of_line_wait_on_bit(word, bit, bit_wait_io, mode); @@ -123,8 +127,10 @@ wait_on_bit_timeout(unsigned long *word, unsigned long timeout) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit(bit, word)) { + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ return 0; + } return out_of_line_wait_on_bit_timeout(word, bit, bit_wait_timeout, mode, timeout); @@ -151,8 +157,10 @@ wait_on_bit_action(unsigned long *word, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit(bit, word)) { + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ return 0; + } return out_of_line_wait_on_bit(word, bit, action, mode); } =20 Index: linux-2.6/kernel/sched/wait_bit.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- linux-2.6.orig/kernel/sched/wait_bit.c 2022-08-20 14:33:44.000000000 +0= 200 +++ linux-2.6/kernel/sched/wait_bit.c 2022-08-20 15:41:39.000000000 +0200 @@ -49,6 +49,8 @@ __wait_on_bit(struct wait_queue_head *wq ret =3D (*action)(&wbq_entry->key, mode); } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret); =20 + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ + finish_wait(wq_head, &wbq_entry->wq_entry); =20 return ret;