From mboxrd@z Thu Jan 1 00:00:00 1970 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 DA204ECAAA2 for ; Fri, 26 Aug 2022 00:57:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235039AbiHZA5q (ORCPT ); Thu, 25 Aug 2022 20:57:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbiHZA5p (ORCPT ); Thu, 25 Aug 2022 20:57:45 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id B5C2DC888B for ; Thu, 25 Aug 2022 17:57:44 -0700 (PDT) Received: (qmail 21882 invoked by uid 1000); 25 Aug 2022 20:57:43 -0400 Date: Thu, 25 Aug 2022 20:57:43 -0400 From: Alan Stern To: Mikulas Patocka Cc: Linus Torvalds , 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: Re: [PATCH] wait_on_bit: add an acquire memory barrier Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-arch@vger.kernel.org On Thu, Aug 25, 2022 at 05:03:40PM -0400, Mikulas Patocka wrote: > 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 introducing a new function "test_bit_acquire" > that works like test_bit, but has acquire memory ordering semantics. > > Signed-off-by: Mikulas Patocka ... > --- linux-2.6.orig/include/asm-generic/bitops/generic-non-atomic.h > +++ linux-2.6/include/asm-generic/bitops/generic-non-atomic.h > @@ -4,6 +4,7 @@ > #define __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H > > #include > +#include > > #ifndef _LINUX_BITOPS_H > #error only can be included directly > @@ -127,6 +128,18 @@ generic_test_bit(unsigned long nr, const > return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); > } > > +/** > + * generic_test_bit - Determine whether a bit is set with acquire semantics Trivial: Name in the kerneldoc isn't the same as the actual function name. (Also, "with acquire semantics" is supposed to modify "Determine", not "is set" -- we don't set bits using acquire semantics. You could change this to "Determine, with acquire semantics, whether a bit is set".) Alan Stern > + * @nr: bit number to test > + * @addr: Address to start counting from > + */ > +static __always_inline bool > +generic_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr) > +{ > + unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); > + return 1UL & (smp_load_acquire(p) >> (nr & (BITS_PER_LONG-1))); > +}