From patchwork Tue Oct 24 12:08:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleg Nesterov X-Patchwork-Id: 157401 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1894461vqx; Tue, 24 Oct 2023 05:10:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7sLVT9uuZRzVWEdADCyt99BIACSzLB9PTWRONeEj0y9Bipp5/DRp8AL8vSo+fN6e0UG8n X-Received: by 2002:a05:6359:d12:b0:168:e777:adf9 with SMTP id gp18-20020a0563590d1200b00168e777adf9mr3316123rwb.18.1698149437885; Tue, 24 Oct 2023 05:10:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698149437; cv=none; d=google.com; s=arc-20160816; b=ONttIurvhKabojUYsPQBzWQTJYw/FZGYdkoe50ApNsYvZy9nrH5l0CngzvqZbTfEOP UglMXtH72r6EeTcG+RU9+1YNgbpAL4GEfNBeu6BMfDlnaI/FzU4VBwDSy78Kdj7vWaRg AzKB0MyZCqCKGbTXbeljFqBuHHoheiKgC6b9/RpZSiqqfqEm1kS00iE9TYOt8pi1+VQF 3x/Jy9ohdDMZgysO+/rPYouYnC0oRlMYGRgtnXKRz26HSIRBGy3ioUviyydHZvsIRk7H 3neoUjV7S7YubWcaDtfkECrbwEaievBp5XRRzKn0KK73HTbF+pGbI7WnsUmCnTw9UMKx hdWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:content-disposition:mime-version :message-id:subject:cc:to:from:date:dkim-signature; bh=iZkArARh9BTcR7kL2t19I65TEaHt5O6mRvItemHD3G4=; fh=+Q0A7yH/24szLnVJ2ZJNg7LKA0/TnmLjGytBQxPStmI=; b=csDQjZ8+SiG+VwS6hntw0xelgUADlczytP16YSlkRFsEJ6SnrXQKvt0j+IlGx+RKU9 Jh9GBcMKSnxT0ia/qAwOPqirfMdUu1x/2gYrtBOFjRtYxDHRu7c9/r99L+Z8arwFOIqL w3n/wMxa24EMyiCRtWiRAYuxo3kscBw3V9F4fv4Fd9txo4DJLZ0n+goWdDeLippSO4ae wPqCJpF3bsFn8785c32rYCJtAVHSkMU5qm6I9g2yOxIKrf/S2scRnmhG11H6/kl4q/qC UZxy21eAh0lKP6gA12FfiWj6eyRcdaDGVOJkZOd26h4LDR+PSUuh99hkyb3+CoUCjqAJ qYuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NypWtWb7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id k2-20020a633d02000000b00565f0e9cfbbsi8655249pga.382.2023.10.24.05.10.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 05:10:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NypWtWb7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id D0B3980782BC; Tue, 24 Oct 2023 05:10:15 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233892AbjJXMKJ (ORCPT + 26 others); Tue, 24 Oct 2023 08:10:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233761AbjJXMKI (ORCPT ); Tue, 24 Oct 2023 08:10:08 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB9B910C8 for ; Tue, 24 Oct 2023 05:09:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698149357; 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=iZkArARh9BTcR7kL2t19I65TEaHt5O6mRvItemHD3G4=; b=NypWtWb7xFu1fSw7jJAWlaaETp4Kt0nK5WRngi7kXJSyiIz1zdXujuMLTJeb+vxa3VVJB/ V4HolRiZGO9FLpUQqre0WCBg8rylEVOEQVsXeJ1TS7mbonJCodqXooTinBNdxrSBDiHhzR LoNZjXCJXodVgtdkqHv6NHvD1tmFWuw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-79-KvQp0O3iNNKk8r5KugO8QQ-1; Tue, 24 Oct 2023 08:09:13 -0400 X-MC-Unique: KvQp0O3iNNKk8r5KugO8QQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 096AC280605F; Tue, 24 Oct 2023 12:09:13 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.226.12]) by smtp.corp.redhat.com (Postfix) with SMTP id 889C4492BFB; Tue, 24 Oct 2023 12:09:10 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Tue, 24 Oct 2023 14:08:11 +0200 (CEST) Date: Tue, 24 Oct 2023 14:08:08 +0200 From: Oleg Nesterov To: Ingo Molnar , Peter Zijlstra Cc: Alexey Gladkov , "Ahmed S. Darwish" , Boqun Feng , Jonathan Corbet , Waiman Long , Will Deacon , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Subject: [PATCH 1/2] seqlock: fix the wrong read_seqbegin_or_lock/need_seqretry documentation Message-ID: <20231024120808.GA15382@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 05:10:15 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780638744518789379 X-GMAIL-MSGID: 1780638744518789379 Half of the read_seqbegin_or_lock's users are buggy (I'll send the fixes), and I guess this is because the documentation and the pseudo code in Documentation/locking/seqlock.rst are wrong. Pseudo code: int seq = 0; do { read_seqbegin_or_lock(&foo_seqlock, &seq); /* ... [[read-side critical section]] ... */ } while (need_seqretry(&foo_seqlock, seq)); read_seqbegin_or_lock() returns with the even seq, need_seqretry() doesn't change this counter. This means that seq is always even and thus the locking pass is simply impossible. IOW, "_or_lock" has no effect and this code doesn't differ from do { seq = read_seqbegin(&foo_seqlock); /* ... [[read-side critical section]] ... */ } while (read_seqretry(&foo_seqlock, seq)); Signed-off-by: Oleg Nesterov --- Documentation/locking/seqlock.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Documentation/locking/seqlock.rst b/Documentation/locking/seqlock.rst index bfda1a5fecad..4bdf8d4ed2a2 100644 --- a/Documentation/locking/seqlock.rst +++ b/Documentation/locking/seqlock.rst @@ -218,13 +218,14 @@ Read path, three categories: according to a passed marker. This is used to avoid lockless readers starvation (too much retry loops) in case of a sharp spike in write activity. First, a lockless read is tried (even marker passed). If - that trial fails (odd sequence counter is returned, which is used as - the next iteration marker), the lockless read is transformed to a - full locking read and no retry loop is necessary:: + that trial fails (sequence counter doesn't match), make the marker + odd for the next iteration, the lockless read is transformed to a + full locking read and no retry loop is necessary, for example:: /* marker; even initialization */ - int seq = 0; + int seq = 1; do { + seq++; /* 2 on the 1st/lockless path, otherwise odd */ read_seqbegin_or_lock(&foo_seqlock, &seq); /* ... [[read-side critical section]] ... */