Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp2150556rwa; Mon, 22 Aug 2022 03:06:08 -0700 (PDT) X-Google-Smtp-Source: AA6agR6Xknk5lhGuJFDTM+V7NSCtAi3zUiYbs7oncbHc4k74AgXsjRz9UkszqDDHVK4m2egXxCMb X-Received: by 2002:a05:6402:3881:b0:445:f8bf:5a1c with SMTP id fd1-20020a056402388100b00445f8bf5a1cmr15667945edb.13.1661162767927; Mon, 22 Aug 2022 03:06:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661162767; cv=none; d=google.com; s=arc-20160816; b=KuTg4VTLMQa4pGq91FTl3WoVesdGjji8yo8T9qN3n3SCmoWdfPDozPiGuAyKxMjOyA SVsezgot+MA2oI+prxKkmj0U2BMO5rSoAiZkZjNij0VbaSMeuGi2jyiHtMDZLeCAU4dk 0DnAabcuSm8a7+S6NeNDIQm1W/B8P3xZkwIprEBkaS1EFj0JaKdzQgoy8BFI19TaLkiT rXkYlYaDdrNNZhezow1maJLC+dHqcbJ6rwEaFxqrijDlKdtFj1u/S0zYTCjnLqIhVciD zCTuzrw1zzwAmeXtgdYsaPs1/C92Wj3Hfl6GhjqCb1TFnPMDHv7gDfo+Oyym8sT2cO7X 5orw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:message-id:subject:cc:to :from:date:dkim-signature; bh=zfpY3ryladZYLXHgzfscfqjYbCZN7BmzG+/csBCpaf0=; b=OdNNBjnO+zUnSvkkCdkpKW/UJztQa3PEDVh4PlnwbcpHSQQnvxooOUlf7in0QJaOW9 5rywG7p+aU+otig2SyqYkzOyhe1NE5D7qnsfFJqbzbyzve4Bo7dx7Xb6J74d/cjIgedl iC5woHrgjJZQkfQ5pZ8x3M5ztMgsgjBuootJMVBLh2I8PNJvhP4DklXJ3zU4oE92MgDv hIAt1tI141PBu7PjvK3W1im+pnubOUl3ZMKNkL23rPhXnSFm0m4zTJhyqRNNBVFf1RL/ +E0+h5I5xPFGtiZd2Zm9gA7UhlLFZO1cd897zy39pKSEQMSW7t9er0W+7WEMPtrjpbfH 1XYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QCHThwmL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f10-20020a0564021e8a00b0043d1d2bda9bsi10804816edf.92.2022.08.22.03.05.42; Mon, 22 Aug 2022 03:06:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QCHThwmL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234036AbiHVJi7 (ORCPT + 99 others); Mon, 22 Aug 2022 05:38:59 -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 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi I'd like to ask what do you think about this patch? Do you want to commit it - or do you think that the barrier should be added to the callers of 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 =================================================================== --- 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); } Index: linux-2.6/kernel/sched/wait_bit.c =================================================================== --- linux-2.6.orig/kernel/sched/wait_bit.c 2022-08-20 14:33:44.000000000 +0200 +++ 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 = (*action)(&wbq_entry->key, mode); } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret); + smp_acquire__after_ctrl_dep(); /* should pair with clear_bit_unlock */ + finish_wait(wq_head, &wbq_entry->wq_entry); return ret;