Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp1178347rdb; Wed, 1 Nov 2023 13:42:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFn2BL0amRPubwBrcbeObfD1rYFaJ8I8c0bjumGgBdBm5Y+P+qA5jJSbSi++QpQR/zmJB+h X-Received: by 2002:a17:90a:b387:b0:280:a01a:906a with SMTP id e7-20020a17090ab38700b00280a01a906amr5220210pjr.5.1698871358952; Wed, 01 Nov 2023 13:42:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698871358; cv=none; d=google.com; s=arc-20160816; b=XYNyU7bFISnBzbWjs/HxGTR93QTc1bF2OqOef1BUPr1aAGdAqbsNBQq3J7M+d1FsBB C5/4E9Q40LOqkV6c4m05223WPRGZoHwncqi348EsKsEulSze/GO3AgLBhto/Y4EWvcHN XuAajpIykW0JnEe9RQ+KRIIvgg0GHYwFYenAwns9EYt+5cn5ttfIksxtODRg0UJubwsx +VrUew99fGknPhs/wwq8F7DeT+ulN1XSl7rC6xWtEIOkbADrBkUEE9S7w2Hwp3f62zRU vFKFk0IqM4VOZ5TAELzwI7t+C8tiQop+C82/Ne37G8+8IJgiPxIdboajXTDXZfdBMFBt j73w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=BrCQTK+msYYunSQbQ7QqLC4ERfMyHGDaa10A5wvKQSE=; fh=rLah2dbOxp23DZUj6pQYOcgsGIAYDbLwLDNc6sfnH8c=; b=kc99+CVkmL5mTN3+B3zqKTFxcYmlE3AjabiEh61gva9w7N1esIzQvR8h6KIZwy/7JP hV47GSuq0V/bB6W4bQ3u6DW493A4Iyj1CHzLBf98NJwo8LZOqlSsDr7aM7i00HJiWr87 hwCqpVETk0NeZBR24zx6SmtXokuRD77XWl60OB/yPzdIMRDr8woaJqDxxDI3Kvko1R5W nuYNLq9j5KhVma3bLcEKycKlbNuTVnN7gnZUwlGFrCagGA3kkZXoUcSpKMZDBBx93qYd P9stnKyV3C/JKGfwcon8jzvRptFRxqat+ZlpKPFacjfCuXIdfX+ilkImid90K2gC0AcV m6/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fmfaluh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id me7-20020a17090b17c700b002806bdcaa5esi1773397pjb.110.2023.11.01.13.42.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 13:42:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fmfaluh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id E726F80B79EA; Wed, 1 Nov 2023 13:42:28 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346382AbjKAUmW (ORCPT + 99 others); Wed, 1 Nov 2023 16:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231461AbjKAUmV (ORCPT ); Wed, 1 Nov 2023 16:42:21 -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 A61E210D for ; Wed, 1 Nov 2023 13:41:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698871292; 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: in-reply-to:in-reply-to:references:references; bh=BrCQTK+msYYunSQbQ7QqLC4ERfMyHGDaa10A5wvKQSE=; b=fmfaluh45EL18eQoLiLdvwC5EvfuKk/a96UBD0zrEEn1YozZDOqQ7T0lmEQepz4Br0jzgt 1+xEGpijOCguPdYcGDPz5L0Gt5vwOBMrl8gKwkf16VQZhUItzArH3AkKmGQcgLjeIyOzeu TjPOefIorNARkoZeCfpXlWHARW2Y4QI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-417-ZQ9mVp4aM5S4Z82mCHgXQw-1; Wed, 01 Nov 2023 16:41:29 -0400 X-MC-Unique: ZQ9mVp4aM5S4Z82mCHgXQw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 B6A99185A780; Wed, 1 Nov 2023 20:41:28 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.224.94]) by smtp.corp.redhat.com (Postfix) with SMTP id 42821492BE0; Wed, 1 Nov 2023 20:41:26 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Wed, 1 Nov 2023 21:40:27 +0100 (CET) Date: Wed, 1 Nov 2023 21:40:23 +0100 From: Oleg Nesterov To: David Howells Cc: Marc Dionne , Alexander Viro , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Chuck Lever , linux-afs@lists.infradead.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] rxrpc_find_service_conn_rcu: use read_seqbegin() rather than read_seqbegin_or_lock() Message-ID: <20231101204023.GC32034@redhat.com> References: <20231027095842.GA30868@redhat.com> <1952182.1698853516@warthog.procyon.org.uk> <20231101202302.GB32034@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231101202302.GB32034@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Spam-Status: No, score=-1.3 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Wed, 01 Nov 2023 13:42:29 -0700 (PDT) In case I was not clear, I am not saying this code is buggy. Just none of read_seqbegin_or_lock/need_seqretry/done_seqretry helpers make any sense in this code. It can use read_seqbegin/ read_seqretry and this won't change the current behaviour. On 11/01, Oleg Nesterov wrote: > > On 11/01, David Howells wrote: > > > > Oleg Nesterov wrote: > > > > > read_seqbegin_or_lock() makes no sense unless you make "seq" odd > > > after the lockless access failed. > > > > I think you're wrong. > > I think you missed the point ;) > > > write_seqlock() turns it odd. > > It changes seqcount_t->sequence but not "seq" so this doesn't matter. > > > For instance, if the read lock is taken first: > > > > sequence seq CPU 1 CPU 2 > > ======= ======= =============================== =============== > > 0 > > 0 0 seq = 0 MUST BE EVEN > > This is correct, > > > ACCORDING TO DOC > > documentation is wrong, please see > > [PATCH 1/2] seqlock: fix the wrong read_seqbegin_or_lock/need_seqretry documentation > https://lore.kernel.org/all/20231024120808.GA15382@redhat.com/ > > > 0 0 read_seqbegin_or_lock() [lockless] > > ... > > 1 0 write_seqlock() > > 1 0 need_seqretry() [seq=even; sequence!=seq: retry] > > Yes, if CPU_1 races with write_seqlock() need_seqretry() returns true, > > > 1 1 read_seqbegin_or_lock() [exclusive] > > No. "seq" is still even, so read_seqbegin_or_lock() won't do read_seqlock_excl(), > it will do > > seq = read_seqbegin(lock); > > again. > > > Note that it spins in __read_seqcount_begin() until we get an even seq, > > indicating that no write is currently in progress - at which point we can > > perform a lockless pass. > > Exactly. And this means that "seq" is always even. > > > > See thread_group_cputime() as an example, note that it does nextseq = 1 for > > > the 2nd round. > > > > That's not especially convincing. > > See also the usage of read_seqbegin_or_lock() in fs/dcache.c and fs/d_path.c. > All other users are wrong. > > Lets start from the very beginning. This code does > > int seq = 0; > do { > read_seqbegin_or_lock(service_conn_lock, &seq); > > do_something(); > > } while (need_seqretry(service_conn_lock, seq)); > > done_seqretry(service_conn_lock, seq); > > Initially seq is even (it is zero), so read_seqbegin_or_lock(&seq) does > > *seq = read_seqbegin(lock); > > and returns. Note that "seq" is still even. > > Now. If need_seqretry(seq) detects the race with write_seqlock() it returns > true but it does NOT change this "seq", it is still even. So on the next > iteration read_seqbegin_or_lock() will do > > *seq = read_seqbegin(lock); > > again, it won't take this lock for writing. And again, seq will be even. > And so on. > > And this means that the code above is equivalent to > > do { > seq = read_seqbegin(service_conn_lock); > > do_something(); > > } while (read_seqretry(service_conn_lock, seq)); > > and this is what this patch does. > > Yes this is confusing. Again, even the documentation is wrong! That is why > I am trying to remove the misuse of read_seqbegin_or_lock(), then I am going > to change the semantics of need_seqretry() to enforce the locking on the 2nd > pass. > > Oleg.