Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1529736lqp; Fri, 22 Mar 2024 20:11:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWyVW+VNmLBSN+8goW4qsSD+JB931aA/6XRUrbKU5g8dd/g4wKNMI184zWIorCl23yUD2hUdqjitl68mHezuwhFNI1MYxV+l2X/D1CmSA== X-Google-Smtp-Source: AGHT+IFGtkKK6Il5FPrhlp/sUtiACPF9kv4Aw4NWLpfPKDK2RglsJY5PbXSU8o5j9vClA2tLwv8o X-Received: by 2002:ad4:5d6f:0:b0:696:455c:a849 with SMTP id fn15-20020ad45d6f000000b00696455ca849mr1620724qvb.25.1711163462151; Fri, 22 Mar 2024 20:11:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711163462; cv=pass; d=google.com; s=arc-20160816; b=wClVJncFlk7b1kgZ25Wze6KDxWn9hXy1ZCMZm93ABqAn6H3h20yXNjlpevawddzXIJ m4cI5A2SrDdoT0q0zRKWOlasO2fDAebRlwJF5E/q2fHkc39o7SvZiSI5WFRhpZWqBvXe cwhih0yJDL/SZWqMPr8Y6AJpybwKT42eMuuB8JaRDKyoQG72AAvjVjzJ/sPJWKY7/uyt wpnPNeK4HNopvNMIv1xqAJjD0/SvlHLGZ5dYHEzQg3f/213fdaW0A2QQNYV1e54S8qMo k/8CKEttvgYp4r4hTwyWh5b0lRv3X1vjKkqqFrLRHTl5VkcIORVbxzmLWVVe4gGBLzh9 9SwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=NRsLRl009I/kLedXp+jvC5j//X0aN6cM7vMnePx2fhQ=; fh=7GtkrGMbvqMq516zkYdg0b+b6arxeTWND55dXWnl/z8=; b=vxEY0QnJGfQDK7fwmRluvHUPCAyny7avKcVX4YAIsWm7zziDG/OQ0wC4/KFBRa3x7z GK3AJOlVv/kNBr4TOKVuh6dLFqps3ztDhk4vpHgsVKwKIhhuH0oaVekiZkU/JUe0sMiM i7KG0XAYWm1Tqp86X4vvpZkt4p+MG+tD/nXlDK+0mnykYoWtOxnq2HYLt3R20nFHihAV 0pxmCGhx0mVsieBYZX8K3DYqWSNouCXdODxxBzxH2fyM2ikWECF4DKw7toeU6ow5fwDm 6eMsWKEK7qJFwX/gP56UzIjuCkRc/c5U2B6IUOvBRsgwY2SkwVId9KYI67weCDjYVHuj DRNQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qRcnYUkK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-112198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112198-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id q13-20020ad45cad000000b0068f6f28fe7bsi3518411qvh.541.2024.03.22.20.11.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 20:11:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=qRcnYUkK; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-112198-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112198-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BE1F11C22337 for ; Sat, 23 Mar 2024 03:11:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 147AB523A; Sat, 23 Mar 2024 03:10:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="qRcnYUkK" Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) (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 E7C78137E for ; Sat, 23 Mar 2024 03:10:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711163449; cv=none; b=JrNcAWKcTAP4Z9HrmZBqC0r9BnC7uSv0RTfOP6dUhKoR0356PtVLUM6UMT4QrGViF9S3PXMNstinFLNaq5YdBCa/EXeT1s7QJpCKL8JnjZiSiKpsuPu9pfXn7nPFNKyspome1zseaGGQ9HyTAOq+SSWbZlkwhNDSDDHEz/qanNE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711163449; c=relaxed/simple; bh=XZ+oepjEpjTRHlPulS36z+3rH+ZNG2g9We+tEZ8XLkM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=OndVA4mYQgrriW2S5dJ5qs2jSBCUnt0nhN+TS8Yjvpi8XTjSy+xuqWxpNxdNfCq3QbglriOHm43OSfhCgc5u8ASLc3/5Ygos2aZ1K3SfuFa6zg52hr8Cf3dFslo6HKFT0+9vRniBjqo7k6car20bu4w2UZeRcIKYFJUKQWuhFYc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qRcnYUkK; arc=none smtp.client-ip=95.215.58.183 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Fri, 22 Mar 2024 23:10:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1711163444; 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=NRsLRl009I/kLedXp+jvC5j//X0aN6cM7vMnePx2fhQ=; b=qRcnYUkKd/nFwSplScYeak6xiUsHIiT3V/t/TWhqR5Q20T4cNCZzmJtGAL4NPL60BnNZ3t 9Ng0i7CQGOkbXXf/IPAAd2KEQSci8DPYV4SDtUb5V9RgtWKx2H165dyE24xFfEq8WgvXm0 mEfYAi9Zi1BQEtd/byhxAkvV3IS5/vQ= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Boqun Feng Cc: Linus Torvalds , 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 , =?utf-8?B?QmrDtnJu?= 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> <3modld2dafaqjxa2b7jln47ws4ylzhbsvhvnphoklwvzange5p@wlir7276aitp> <34r4signulvsclmsiqgghskmj5xce3zs5hwgfulzaez2wdyklr@ck6zrj732c4m> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT On Fri, Mar 22, 2024 at 07:57:20PM -0700, Boqun Feng wrote: > On Fri, Mar 22, 2024 at 10:33:13PM -0400, Kent Overstreet wrote: > > On Fri, Mar 22, 2024 at 07:26:28PM -0700, Boqun Feng wrote: > > > On Fri, Mar 22, 2024 at 10:07:31PM -0400, Kent Overstreet wrote: > > > [...] > > > > > Boqun already mentioned the "mixing access sizes", which is actually > > > > > quite fundamental in the kernel, where we play lots of games with that > > > > > (typically around locking, where you find patterns line unlock writing > > > > > a zero to a single byte, even though the whole lock data structure is > > > > > a word). And sometimes the access size games are very explicit (eg > > > > > lib/lockref.c). > > > > > > > > I don't think mixing access sizes should be a real barrier. On the read > > > > > > Well, it actually is, since mixing access sizes is, guess what, > > > an undefined behavior: > > > > > > (example in https://doc.rust-lang.org/std/sync/atomic/#memory-model-for-atomic-accesses) > > > > > > thread::scope(|s| { > > > // This is UB: using different-sized atomic accesses to the same data > > > s.spawn(|| atomic.store(1, Ordering::Relaxed)); > > > s.spawn(|| unsafe { > > > let differently_sized = transmute::<&AtomicU16, &AtomicU8>(&atomic); > > > differently_sized.store(2, Ordering::Relaxed); > > > }); > > > }); > > > > > > Of course, you can say "I will just ignore the UB", but if you have to > > > ignore "compiler rules" to make your code work, why bother use compiler > > > builtin in the first place? Being UB means they are NOT guaranteed to > > > work. > > > > That's not what I'm proposing - you'd need additional compiler support. > > Ah, OK. > > > but the new intrinsic would be no different, semantics wise for the > > compiler to model, than a "lock orb". > > Be ready to be disappointed: > > https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/is.20atomic.20aliasing.20allowed.3F/near/402078545 > https://rust-lang.zulipchat.com/#narrow/stream/136281-t-opsem/topic/is.20atomic.20aliasing.20allowed.3F/near/402082631 > > ;-) > > In fact, if you get a chance to read the previous discussion links I > shared, you will find I was just like you in the beginning: hope we > could extend the model to support more kernel code properly. But my > overall feeling is that it's either very challenging or lack of > motivation to do. That's casting - that doesn't work because compiler people hate aliasing. But intrinsics for e.g. __atomic32_read_u8(atomic_u32_t *a, unsigned byte) __atomic32_write_u8(atomic_u32_t a*, unsigned byte) should be doable - that's perfectly fine for the compiler to model. That would admittedly be ugly to use. But, if Rust ever allowed for marking any struct up to word size as atomic (which we want anyways...), it could use that under the hood for setting a member variable without cmpxchg.