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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B92F1C369BC for ; Wed, 25 Sep 2024 10:02:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 524976B008A; Wed, 25 Sep 2024 06:02:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D4916B008C; Wed, 25 Sep 2024 06:02:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3756E6B0092; Wed, 25 Sep 2024 06:02:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 15DF06B008A for ; Wed, 25 Sep 2024 06:02:49 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B61E91409AD for ; Wed, 25 Sep 2024 10:02:48 +0000 (UTC) X-FDA: 82602821616.23.E004D0B Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by imf12.hostedemail.com (Postfix) with ESMTP id 8598240016 for ; Wed, 25 Sep 2024 10:02:46 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YeBBwxZA; spf=pass (imf12.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727258530; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZUldRcpWHree9ha+m2OGa8hB6t5ASjRpcAc+wb5KVAU=; b=TcGGw0l+oXUMyBXC7RcOITCfbXmnBP/bLfWo1ZfVLDIZavSjwM/SX1uFkTI/wTTTdKyHpV wAXZl44Frf5/A0l+KayeQeuPrp5Bx/rAJK2meDiwmZFnrrlBxygcjaBFTStyE2JLLtyun3 fn85Kj81ursy7erbh8BqV4ggMdUs450= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=YeBBwxZA; spf=pass (imf12.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.222.175 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727258530; a=rsa-sha256; cv=none; b=cabhS7FoJH8PNZ5LnB4wYu870j9JiIkwjFKtKRkBgC1Di4whBvyaLpsnVvFL+8xwiTcSCX A23z4MgeioDVHwt/n9UNFwtVv3eT78QDh5klV9BBZf7pE/Rk1lXNSKlM/RwK4ku1zgG83T 7e3FaPTu4skEVXnY690inLd8KIQ/SiI= Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-7a99e8c32c0so663117285a.3 for ; Wed, 25 Sep 2024 03:02:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727258565; x=1727863365; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=ZUldRcpWHree9ha+m2OGa8hB6t5ASjRpcAc+wb5KVAU=; b=YeBBwxZALT88r3XdXRAPdQq5HnqiKEvsBUUS7YK9U1a4pFFId2uZRKEA6Cxdz/srnC FPBaBhHKlqdH7TrKFJeu4+EmMMSwKDw71aaodCGau2v7NDeNgKTjArsd3GnmSJnPOBHN +Lv7xwCWa3pUbJBvVQSPIaW/J8xMVC5np4mmz6u1zM5hYHWHvwBn2t7X7P0qR2yRD0W+ dfDVs24Lz62x3WLhnY4Zlj0uu/sO4OyRgm7Q0eamkU2FANyQ5c2UMZfMKsspzwx+4wHA Cbrs3YV/HNTp63R1G9VwLzQCfxpiuiUdKAFYDqGEtH72h/5WWG7x9IoAhI7qN45BqtWj H30Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727258565; x=1727863365; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZUldRcpWHree9ha+m2OGa8hB6t5ASjRpcAc+wb5KVAU=; b=H5qyNxv0CkIf272DPd7f9EPgpSV3D6Nrvww0dacTMv6oyTFJjchP1aIzKgoOARy5mW b/aXwx415OuNeNVcCp3/AHFK6sPGp9ulGCNJn3cAZIdN26sdYmLDKLxbe54B7TD9f4Md Srf9YmZHNsadDu82fNC5yxKXdghlLot2CF2VMCYKfPJs29nbpuPa/qTZcB6zk/lnPxSa I6pFYMg6b8IYAqbHN1Sy+dTkvJpYUqSizQnrReMxFQ8JhIeu01OOu5MyzYPf5kqtYw3Y ETcZZtnZioj9eMrJli0kE7FGQQL17GJ9feLiX+AlibgYF2HYEctWpxNAwRWpeCbyklWU V1XQ== X-Forwarded-Encrypted: i=1; AJvYcCXlsuwVxnMisByaw2hSrNxI502hLr4PmmW0+s1iyAVrLuK6rboXNPisrN/laz9FHEBlDFTfvaCxxA==@kvack.org X-Gm-Message-State: AOJu0YxrvhPCSUq7UGYx6xzXmAgInTORFyk/XSMgWOeojWj/kEL6b/L4 qxEGfuVk97JfbJIgepULxzJOVfJufefC1pUZp8HZu+jyRRd/DoIGn6iR5URH X-Google-Smtp-Source: AGHT+IGVKuLW3nVDBEzBuAX97uyQGAh+2bW9xcs4xJ8/3Dniq7Tabkb462mX0j8Xz5mFA3BAmLmahQ== X-Received: by 2002:a05:6214:3f87:b0:6c5:4f37:2b51 with SMTP id 6a1803df08f44-6cb1dd7d5eemr34515116d6.28.1727258565402; Wed, 25 Sep 2024 03:02:45 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cb0f4c2cb3sm14896766d6.49.2024.09.25.03.02.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2024 03:02:44 -0700 (PDT) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id F0EBD1200043; Wed, 25 Sep 2024 06:02:43 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 25 Sep 2024 06:02:44 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddthedgvdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleei vedtgeeuhfegueevieduffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepvdejpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehjohhnrghsrdhosggvrhhhrghushgvrheshh hurgifvghitghlohhuugdrtghomhdprhgtphhtthhopehlihhnuhigqdhkvghrnhgvlhes vhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehrtghusehvghgvrhdrkhgvrh hnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqmhhmsehkvhgrtghkrdhorhhgpdhr tghpthhtoheplhhkmhhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepph gruhhlmhgtkheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepfhhrvgguvghrihgtsehk vghrnhgvlhdrohhrghdprhgtphhtthhopehnvggvrhgrjhdruhhprgguhhihrgihsehkvg hrnhgvlhdrohhrghdprhgtphhtthhopehjohgvlhesjhhovghlfhgvrhhnrghnuggvshdr ohhrgh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 25 Sep 2024 06:02:43 -0400 (EDT) Date: Wed, 25 Sep 2024 03:02:00 -0700 From: Boqun Feng To: Jonas Oberhauser Cc: linux-kernel@vger.kernel.org, rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@vger.kernel.org, "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Mark Rutland , Thomas Gleixner , Kent Overstreet , Linus Torvalds , Vlastimil Babka , maged.michael@gmail.com, Neeraj Upadhyay Subject: Re: [RFC PATCH 1/4] hazptr: Add initial implementation of hazard pointers Message-ID: References: <20240917143402.930114-1-boqun.feng@gmail.com> <20240917143402.930114-2-boqun.feng@gmail.com> <55975a55-302f-4c45-bfcc-192a8a1242e9@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <55975a55-302f-4c45-bfcc-192a8a1242e9@huaweicloud.com> X-Rspam-User: X-Stat-Signature: nzb99ycn43xrfx8yde4mqb7ap6yuyhpn X-Rspamd-Queue-Id: 8598240016 X-Rspamd-Server: rspam11 X-HE-Tag: 1727258566-491159 X-HE-Meta: U2FsdGVkX18oISjSyN1KXqFu8IGiwy6hHHXZScjY3j+9E6yAJJlsDdxZP3AGLE8UkND8A+JLG4UXa3Ngii0jKQXUssEU596ZvCiNUh4GPQkQoCuljqXyYh+mtx3RnvUC48ySkU5DOlWv8caMIcKlBYymL36imrA+3/fcfsanaEgOYRkRuTtaKnw/1i3amcJikERJmfrhKjiphrk4LoHbW8CN66Uug7c7GIBJFYIlaudiYE8zcLUQU+XFt6MY4K70ta2eCWqVSDrwT5U7XAgfDZ1gRqVotUFGo9JEUZ8E7gWxHl1F92yJ43CZuHnbhD9EITGyKwy5xlbg+cQywCtZhug21zVVoKn6ZbH+o5eI+XwbPisQmqX9q3bUsxg6jO5L69QlE7yblGPr/wQMjJT2w6QfuEktLhfROsNVcFKdgBARwT4BecdV+WuMF6CCvCyhGEULQQOyNj3sxLoXTXwB4UjJXlGdSRM3pf55vAfW+PloygufeUGeLS79gEclY5yaRWvhau+fonHOojby+vfzpLj1Wpxzf1D99G91JHUwYAQXMdoMBmhNxY0RA5/GqrV1glZNOf2cV/8YsM+HSjcOGNi7ohyaq8gwbVUxVhPQc7uZDnD8tsnLrD6PD8nFUuYEtdabBxJq6E96bJozGX8yvWym7qvxBQn9nEws8mTdilfOZVJFShtB9W9imY8BCfNCR+zbXOPTM1Y+HT+4Z8vhKBbJ1H8uRJNM93LA+ouKeOL90cNEh4yKBV2Ps+V25E3mNY6ocGqB9uwXzQLoGzbmsE2F24R7/x7qhrOYaK6liUn4T9zd8yMdtSbf3Y5NTSK/6wEARAk+wDTNWGt/Vh0o502iJcvshN7i2KjPvv49puC0ys+PFd3hL/b9EEb3ejP3xEpVl/tLQrsBlC2aZNVvH9N1XZzcljNEfsDNHzgRBLGFEi8kDUeLJJHyks2fwTvgAFVk+RVk1XTQE6yRGFM hkEgJ9jN phexxBLNd7dmS1aSZ6vjfsKAx+z6mkkE2WPCmSi0weKtXdxnL+qBUzwcCD1ds+3ft7Q1qb3oG20t3nUFFuXzvMtPU8G2dQI48Hxrxq+M9YmOMHUTYm9Q9EiJnTXpRO5EShi3pc8a0mS8OOquoYVhR7e3bGr6x5Z7DgMlXWodSy5oVCffHyxewxhtuT5JBiOy6oVTe1G3tPgwTo+lXEdrldZmxkfaNsm20Z1Jjc2BgAfmNcMTIPXNmlsGLjmbeo6G4pg60P+sKSv9VhO2hWIjfdgJwyyJdWN/iZYWksKXxBpvfycI/kj6aiaL33AUlcg+Vb5Bl49uQcpm2ipa25hkgocyHzuCwe+X3FZ69VMgtUEqlfZeQnUSeKpDVKs19d6J3G9vjvm+WIuLt+eYukOHyQJYrYIjC9riE/w+LLvLcAKG4H4IukiZuPzWvmnGnx/kmERib+nbai3rPtRwL0ciPjjZZZRNQLO4raBDPJskZrv2dYSI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hi Jonas, On Fri, Sep 20, 2024 at 09:41:15AM +0200, Jonas Oberhauser wrote: > > > Am 9/17/2024 um 4:33 PM schrieb Boqun Feng: > > +static inline void *__hazptr_tryprotect(hazptr_t *hzp, > > + void *const *p, > > + unsigned long head_offset) > > +{ > > + void *ptr; > > + struct callback_head *head; > > + > > + ptr = READ_ONCE(*p); > > + > > + if (ptr == NULL) > > + return NULL; > > + > > + head = (struct callback_head *)(ptr + head_offset); > > + > > + WRITE_ONCE(*hzp, head); > > + smp_mb(); > > + > > + ptr = READ_ONCE(*p); // read again > > + > > + if (ptr + head_offset != head) { // pointer changed > > + WRITE_ONCE(*hzp, NULL); // reset hazard pointer > > + return NULL; > > + } else > > + return ptr; > > +} > > There is a subtle potential for ABA issues here. > > If the compiler replaces 'return ptr;' with 'return head - head_offset;', > then you do not have an address dependency from the second read. > > In this case, in ABA, the first read can read from a stale store, then the > second read reads the same value from a newer store but only establishes > control-dependency based synchronization with that store; any reads from > *ptr could be speculatively executed before doing the second ptr = > READ_ONCE(*p). > > Therefore you could read the object state before it is properly > reinitialized by the second store. > Thanks for taking a look, and nice find! > I'm not sure what the most efficient fix is or if you just want to gamble > that "the compiler will never do that". > I guess either READ_ONCE(ptr) or a compiler barrier before return ptr might > do it? > I think the root cause of this is that compiler can replace 'ptr' with 'head - head_offset' based on pointer value comparison. A fix would be converting pointer to unsigned long and doing the comparison: if ((unsigned long)ptr + head_offset != (unsigned long)head) { WRITE_ONCE(*hzp, NULL); return NULL; } else return ptr; because of the conversion, compilers lose the information of pointer equality, therefore cannot replace 'ptr' with 'head - head_offset'. Of course, if we are really worried about compilers being too "smart", we can always do the comparison in asm code, then compilers don't know anything of the equality between 'ptr' and 'head - head_offset'. Regards, boqun > Have fun, > jonas >