From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A9CF1591EA for ; Wed, 18 Dec 2024 18:38:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734547104; cv=none; b=TPH3NquUuk0NNTZBkRcmB/kFADdA6NNhDgp8lBZHUCuwg3ugFuwbQkZIiHNE5lkYMlinF9zfgWaxYJtDphwuudXSv5gKFCfU2wWrVQj8NeQHsVcsnitny+Jecheun6UUWrPkQ5xa/MSmoWifC8Jciv95S1S86BDapx+8F4ejlEA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734547104; c=relaxed/simple; bh=0mF8Di4nl0dlVj9fWgwMrMAWqzW4Ffp/B8oFuis/ALE=; h=From:Message-ID:Date:MIME-Version:Subject:To:Cc:References: In-Reply-To:Content-Type; b=CZ4LTXhfsjVxOiK3yugPupQ65KkddOX++7naFb+pnkjwroa8kJtWW1MVx0souswNZg9WBrfBHc+LK+zmXXTlOpD99p8Djf671m/EME27d8FGeoYxC69KBwT3p2lYWOxwhynFB1+aCPmpvjKVy0aWSZjqZwukJgWe3xGiBAVRZxA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XkQlV5F7; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XkQlV5F7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734547101; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kwNyKmNPKrIBHPzEfvEBSb2QXieTcqj+17Ev5rWzvnU=; b=XkQlV5F7x32Nx4EBuae832cW34HY5E5HC9FvQNTPr6c9qY95q5W+zbzJt34YtIYdEadRjG +VD4WMH4jjBl/cSYtV7Bfq1cGifb63zlw74paSdLqbNqfASU6Ddb2jv9FewMNWd0nwvHFo /cE/xGp+2FvMszyB4uFudtu5R6+5/P0= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-331-DeXCEjfXPDCy3cHCPjMdUw-1; Wed, 18 Dec 2024 13:38:20 -0500 X-MC-Unique: DeXCEjfXPDCy3cHCPjMdUw-1 X-Mimecast-MFC-AGG-ID: DeXCEjfXPDCy3cHCPjMdUw Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-46799aa9755so41784501cf.0 for ; Wed, 18 Dec 2024 10:38:19 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734547099; x=1735151899; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:user-agent:mime-version:date:message-id:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kwNyKmNPKrIBHPzEfvEBSb2QXieTcqj+17Ev5rWzvnU=; b=RMfF3Gmbxx018Dcw/e/CBKvqWx4IANO1hcqiN82XuqFh8jQffGTK8WZGnk49vzqiOF BWj2y+q5N9eeMtIvLizoO/Jvscr0HLMgjYmRo83L0xppBX2lyuT4JVQlD1jJjwsiif+L CngwJyjr8hHaH25edar8SDmG39oXe707hUkJcaD3SKqygAY31DOPpTHrofPOf1vKkt7t pLpLsnRnpqx9zqn67Jm+/4qzWsaSAZAJJNEb7lyw6/jScLJUpphkSZcnzUqSQThmY6sm m8Thzh+oVVbmfiFe62JptNmn0tuWalxITSMFWPxBvG+xV+gxURIKP+czLcc+yLHoHIfO OvfQ== X-Forwarded-Encrypted: i=1; AJvYcCW/hCM65sjlDLs0r9yr7ZRoF6EGaILIzIovypZ5cJsZMe7yuXAwBAGqbPMCG02gA5ZMnqmH/kzspaYErUs=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6IMYDbyMOG7AHgkAuSDwqPWOqww+FCcNB2qNxaJJO2O5WUxAS Raah8HAcfs9pWZItLnXgXv4PfeKp8o/XyF2/emHTKjsW/LcQPn48CumONZFXJLSBOg7iV9bT9l8 SwPg81dppqqAx8IPnN9GzIQGMqmIHVYNfymWp4MsbyNxO9IXj7Yv/pdmWJPIlyg== X-Gm-Gg: ASbGncs5fgTHbYdiEtDsth7KKSP2Elm6EjdlPppPIb4BWSfxAdwE2raN/IJAGs0fg7e Kme8w6Qamu4PinMDDUPDL71IwDFw2gVhU23yk8Noi8nJA0wUKiYU2XAdtDIGp5ovz5V5qz07tfD Yauva6jQFw5OKg6iUljNKJolzVffUY7qrQ9aiMa3B/khYIBSOUqQANuV54eN17Grbqya9HKeDXl VLAaWS4eOCkt4N+EyygaePKtz1tHNsj0brHTv/qmelDuUYyp72p/PnOZmpS0VWYDNguDR90gWQi 0S1bgkFwoZ/ZUrC+HtU38o2a X-Received: by 2002:ac8:7f52:0:b0:467:87f4:a29f with SMTP id d75a77b69052e-46a3a8f4e6dmr7353611cf.45.1734547099326; Wed, 18 Dec 2024 10:38:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IGBDZUNUZwO3jUUgriJG0xy4dy8i8aIady2nOJSqVhDy1mONSDChv9fp3skFfwAVY3wRMXpkQ== X-Received: by 2002:ac8:7f52:0:b0:467:87f4:a29f with SMTP id d75a77b69052e-46a3a8f4e6dmr7353361cf.45.1734547098991; Wed, 18 Dec 2024 10:38:18 -0800 (PST) Received: from ?IPV6:2601:188:ca00:a00:f844:fad5:7984:7bd7? ([2601:188:ca00:a00:f844:fad5:7984:7bd7]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-467b2cbb69asm54061091cf.39.2024.12.18.10.38.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 18 Dec 2024 10:38:18 -0800 (PST) From: Waiman Long X-Google-Original-From: Waiman Long Message-ID: <13ba988e-15d7-4814-91d9-15b6a4cf05ef@redhat.com> Date: Wed, 18 Dec 2024 13:38:17 -0500 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] seqlock: Use WRITE_ONCE() when updating sequence To: paulmck@kernel.org, Waiman Long Cc: Daniel Xu , mingo@redhat.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, linux-kernel@vger.kernel.org References: <0eaea03ecc9df536649763cfecda356fc38b6938.1734477414.git.dxu@dxuuu.xyz> <6fbd26a5-615f-41bf-ad54-91114898cc2c@redhat.com> <21914c89-4f4a-4e09-97de-4d5b99a48243@paulmck-laptop> <8da9c55b-65fb-4c81-b6ea-f80892e3cff7@redhat.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/18/24 11:57 AM, Paul E. McKenney wrote: > On Wed, Dec 18, 2024 at 11:10:01AM -0500, Waiman Long wrote: >> On 12/18/24 10:45 AM, Paul E. McKenney wrote: >>> On Tue, Dec 17, 2024 at 10:30:39PM -0500, Waiman Long wrote: >>>> For seqcount, its actual value isn't important. What is important is whether >>>> the value changes and whether it is even or odd. So even if store tearing is >>>> happening, it shouldn't affect its operation. I doubt we need to use >>>> WRITE_ONCE() here. Could you come up with a scenario where store tearing >>>> will make it behave incorrectly? >>> But why expand the state space? >> I don't quite get what you mean by "state space". > The state space includes the set of possible values that might be stored > in .sequence at any given point in time. So if you allow the compiler to > tear the store, you are increasing the number of values that a concurrent > reader can return, and thus also increasing the number of states that must > be considered. On the other hand, if you use WRITE_ONCE() to constrain > the compiler to do a single store, then you don't have to even worry > about the added states due to these partial updates. Thanks for the explanation. Now I know what you mean. As I said above, the correct operation of seqcount does not depend on getting its intended value right. It is just its odd/even-ness as well as if the value has been changed from previous read. That is the state space where seqcount is operated on. >>> Also, there are potentially "optimizations" other than store tearing. >>> No, I haven't seen them yet, but then again, there were a great many >>> optimizations that were not being used back when I started coding C. >> All the inc's are bounded by smp_wmb()'s which should limit the kind of >> optimizations that can be done by the compiler. That is why I don't see a >> need to use WRITE_ONCE() here. Of course, there may be some corner cases >> that I may miss. So I ask for whether such a case exists. > One other potential optimization is using the variable for scratch storage > immediately prior to the update, which could store any value there. > I haven't seen this myself, but the standard permits it, and in some > cases register pressure might encourage it. > > And who knows what else our creative compiler writers will come up with? If the compiler really uses the variable as a scratch storage, it will be a problem if the variable can be accessed concurrently from multiple CPUs. It is a new compiler optimization strategy that I am aware before. In that case, we may really need a way to mark these variables as not suitable for such advanced optimization. Thanks, Longman > > Thanx, Paul >