From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 144B86D1A3 for ; Mon, 25 Mar 2024 21:37:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711402671; cv=none; b=Jt4qcFHJlkUzzPdyRggspB8OYXBBkFNkiAZEihWNwQ6SRv7vU4BFUpaqVKbIfEKnC1Tl3LW8h1QwzCWJIRqKZEJqWFkwJ932ur9G0ctor9rAm+nEuLAHVel3EHL7xG9FeQbR3T2n2+++A5bOPtosXyDIaz2Mg/+qsiVPFNF/g54= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711402671; c=relaxed/simple; bh=PgOCH9l5+TRsC10l8LRgnziKoXQSiBkFJFUbjNNqzk0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=r9MUc2Nd5emeGCJYFgPHsQpP+Rb1eynPOO1KTaZIgC8798RjTuRvEWItQCTZvY6LPxZ/Rp72kguVLF5a3kB/arJ8Gtt9IsKK1nLrYo37pilfYYb5I0GtF19x+2zdOGt3s4pOqAYQrhUg4DW512GJQk2Y94stt+IglyT3ETbVG5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=chWhWs3i; arc=none smtp.client-ip=209.85.222.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="chWhWs3i" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-78a15537fa1so290687085a.1 for ; Mon, 25 Mar 2024 14:37:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711402669; x=1712007469; darn=lists.linux.dev; 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=DXvJB4PTr9BnmGHzkolraHMPsM/PHcpz2o1jxgX00Cw=; b=chWhWs3i+x/HZ1D5Fs9mOKAq1zID1D9an0a7rYQ3ERMRUV8020nEwnEeJ0gGfP+4F7 Fgjx1apEw32qf7i0yMDLmfjXuNpwjknMqC/L/xwG0IOKED2lVCkOfLPuOyB4w60ykqEQ YAlWmU+V6fWUDzFY9QXNOUsOCp0FEodlpKLn4DSf78z+PBdgxdF09kayk0kWRAXtuC5v 4D1v0P0m23jWnpwplh43uMLmMmjKV9JwWC8L3jnrfz+DgOJgDc+4DIyZjCmWt188NHgq odUsC9ImLdXR9G9IPmnGUI3gEPDtWQfkKDYliV4o9cKV1WsbkJsdR9toeGIlF87nqA0H Mk7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711402669; x=1712007469; 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=DXvJB4PTr9BnmGHzkolraHMPsM/PHcpz2o1jxgX00Cw=; b=jI4IZ1icfcs4sqW9uyaTfHRCeBlUtKOSiVVCyCymsKCqw/I8W3WFgzoAg5zr+pGq0v JXJ4w8pKd00s3+zF01QQtCjtLRIb1GvjbME3CcKjNXtAR1IPJWXDr9OCmmqgoE6etSwT /VxA9DwDrG4UtobzYK0fdOKdGOgkqztpuOwTthVwYRS25XMUkVRXSkuEJoDUx0OEc4yd gSQUcYR9cjtNl9iR64ecZF1pVaMY4WJC2Kmw2dwo85QWoU7yHzrx6L2YPPLVmQsAQGFs N/4FjUpyKquGzpj8wrtc9DQFR8HMy4xrziYbRpvQr1A/+d+4JxovzwWqocG5wsFATWvP mlgQ== X-Forwarded-Encrypted: i=1; AJvYcCXCxlHR8U7fijI376ik2YyKAvz3yvFx7GMAGdkeycQx0AwzxlVWufUl9M91/7v2JZriU1pqxRXPrG73a0iAkVOICQd7oQ== X-Gm-Message-State: AOJu0YySD7x3Oh6L7Kw/c08lD407buiNxjfKQjL9TQd9Vc9Waie6tkQA A5/Y3nfwKXnDJ2SIkIm27xVflBHFB+6k4OoFFbMxPg5LTqILZwl/ X-Google-Smtp-Source: AGHT+IEcUMc58g6IuQ5hGxe8QG2RU/su4immaoKU7ZFPkQningk7dx0ah1v7u/eFIaHFnL28zDZMRA== X-Received: by 2002:a05:620a:2211:b0:78a:5c11:e7ac with SMTP id m17-20020a05620a221100b0078a5c11e7acmr2588111qkh.40.1711402669111; Mon, 25 Mar 2024 14:37:49 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id v25-20020ae9e319000000b0078838c7acbfsm2467972qkf.42.2024.03.25.14.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 14:37:48 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailfauth.nyi.internal (Postfix) with ESMTP id 43A091200032; Mon, 25 Mar 2024 17:37:47 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 25 Mar 2024 17:37:47 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudduuddgleduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeejiefhtdeuvdegvddtudffgfegfeehgfdtiedvveevleevhfekhefftdek ieehvdenucffohhmrghinheprhhushhtqdhlrghnghdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgr uhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsoh hquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 25 Mar 2024 17:37:45 -0400 (EDT) Date: Mon, 25 Mar 2024 14:37:14 -0700 From: Boqun Feng To: Kent Overstreet Cc: Linus Torvalds , Philipp Stanner , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, llvm@lists.linux.dev, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , kent.overstreet@gmail.com, Greg Kroah-Hartman , elver@google.com, Mark Rutland , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Catalin Marinas , linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org Subject: Re: [WIP 0/3] Memory model and atomic API in Rust Message-ID: References: <20240322233838.868874-1-boqun.feng@gmail.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Mon, Mar 25, 2024 at 05:14:41PM -0400, Kent Overstreet wrote: > On Mon, Mar 25, 2024 at 12:44:34PM -0700, Linus Torvalds wrote: > > On Mon, 25 Mar 2024 at 11:59, Kent Overstreet wrote: > > > > > > To be fair, "volatile" dates from an era when we didn't have the haziest > > > understanding of what a working memory model for C would look like or > > > why we'd even want one. > > > > I don't disagree, but I find it very depressing that now that we *do* > > know about memory models etc, the C++ memory model basically doubled > > down on the same "object" model. > > > > > The way the kernel uses volatile in e.g. READ_ONCE() is fully in line > > > with modern thinking, just done with the tools available at the time. A > > > more modern version would be just > > > > > > __atomic_load_n(ptr, __ATOMIC_RELAXED) Note that Rust does have something similiar: https://doc.rust-lang.org/std/ptr/fn.read_volatile.html pub unsafe fn read_volatile(src: *const T) -> T (and also write_volatile()). So they made a good design putting the volatile on the accesses rather than the type. However, per the current Rust memory model these two primitives will be UB when data races happen :-( I mean, sure, if I use read_volatile() on an enum (whose valid values are only 0, 1, 2), and I get a value 3, and the compiler says "you have a logic bug and I refuse to compile the program correctly", I'm OK. But if I use read_volatile() to read something like a u32, and I know it's racy so my program actually handle that, I don't know any sane compiler would miss-compile, so I don't know why that has to be a UB. > > > > Yes. Again, that's the *right* model in many ways, where you mark the > > *access*, not the variable. You make it completely and utterly clear > > that this is a very explicit access to memory. > > > > But that's not what C++ actually did. They went down the same old > > "volatile object" road, and instead of marking the access, they mark > > the object, and the way you do the above is > > > > std::atomic_int value; > > > > and then you just access 'value' and magic happens. > > > > EXACTLY the same way that > > > > volatile int value; > > > > works, in other words. With exactly the same downsides. > > Yeah that's crap. Unfortunate too, because this does need to be a type > system thing and we have all the tools to do it correctly now. > > What we need is for loads and stores to be explict, and that absolutely > can and should be a type system thing. > > In Rust terminology, what we want is > > Volatile > > where T is any type that fits in a machine word, and the only operations > it supports are get(), set(), xchg() and cmpxchG(). > > You DO NOT want it to be possible to transparantly use Volatile in > place of a regular T - in exactly the same way as an atomic_t can't be > used in place of a regular integer. Yes, this is useful. But no it's not that useful, how could you use that to read another CPU's stack during some debug functions in a way you know it's racy? Regards, Boqun