Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1513540lqp; Fri, 22 Mar 2024 19:08:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXKQncEmLylSa2hPXgGIIw0Mval+ZbCXBJTjiZrlQPj3+OSJhosWpUNEbMI1b4PXOmQHTGoIHYd/Wuc8ZIxBHNSDzKmXPCXJ++Kf3q7DQ== X-Google-Smtp-Source: AGHT+IECgnJIl2SHhCkp3+Blt2XHvAMX5ZNAGkRJ4gAbcnZIWmV4C2v6UHsl3uUckFFGkiw59h2b X-Received: by 2002:a05:6a20:918e:b0:1a3:a9e7:200b with SMTP id v14-20020a056a20918e00b001a3a9e7200bmr1685306pzd.42.1711159688584; Fri, 22 Mar 2024 19:08:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711159688; cv=pass; d=google.com; s=arc-20160816; b=TLIl0tiNkR9aKZgynR8jN7Ksp8n6AAAvxTTrvughCDNVUEaUgYRr2Hdx0PHcHL3EEi 2qsN/lCjHOCs4hBYILiZJvexp5M5rVylbehGI3CX9ui3n9Fl8t7PmDgVITlPA4I3dQT7 8BacESzNEY2Z4Ikpgu5PuWjn6dov4yl1yp38oSOYFysvmKefgseBBPN2Ih8WK+GKLAss K79Pqg/A65WV7WhfEqRqXAwZqrHc8sqEan9gmVZU8x0mXXLdHRTIq9TSCN0GQLrLzd9E 7wiYKEHbNmf9JLatr3uyJqSrPoC+DwQkh3/krlwPTSp4ECV8HF2KxBmYqTgSf+coD4MT HosQ== 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=Ki2SqS4TDwK1+oUGgKMCMnFI/a9KHGHslLmG6vAgHw8=; fh=H7P7RRByZh71f3myXxyTXwOcjRwFbg7AdeGQ55lVSbg=; b=zcEvJJD+XeBbCQkDKxutp0VuBg3duvldC3FfI3cPFMn2dwF4Yn04QlBElfrgMqwRWo 5EohPtTucsDBUkDAtlOIjNJc30TcuL+si80oxClDBqkYghtbWhILrjOAP572URykiV/6 kOM3sb0GvypeyVk58hyBzRHWootAd+8zAibH7kJoUPgnQP2hmsStzsZvdhCXFvKBlWy+ cL/CTFbPmkapk55so5+mEkMN7Vm0YGz9m+bbmY0+OK6sBsPH7jFbjYjte7RkgcUOq0qS a3IkBXGSLkx/ELYjQtIzNxb5JFe0zKfwTK/g5792QHV9ZZJPBrMomcb4I+Y/bYccqfRB Q/cw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="V/reHql5"; 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-112175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112175-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bx6-20020a17090af48600b0029df6f09e19si6687469pjb.41.2024.03.22.19.08.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 19:08:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="V/reHql5"; 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-112175-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112175-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 13C07283FB6 for ; Sat, 23 Mar 2024 02:07:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2CD674683; Sat, 23 Mar 2024 02:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="V/reHql5" Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.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 73003A55 for ; Sat, 23 Mar 2024 02:07:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159663; cv=none; b=WuEDSyEZWvmQabbrilkA/qo872j149PSeiw/aTd/8U8/ofdqg9Eepz4c086J1JmJ6HoeJko6pqw6VqJarIZ6q67Y86efOiboX0SIVLWaj5bmle1GXdJPqAekMeecZEi9opdHqIG92hHLC9WL9vH016NqSuHVPs+lTk/Z+u9Ah64= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711159663; c=relaxed/simple; bh=8GsXswQNH/fqxcYIQ/3ggaBwPgXRrvOcvLwU+gyR6g4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=oBxmT8TUftTYUAdb8JBbGrMRN6oKEZIZ3A43hT5LYZ9wVQ0V/V7XiP0IcQpZ4dypo1VbQCq6p7BeIo2OsPBhl80lmvfPBjgSBGjxf672KdKyly/ZhZnvaztarzKQdU/AbzTtrN7w7+g4jN69hvN2DEk0n5cnMWaTrouJebue8Qk= 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=V/reHql5; arc=none smtp.client-ip=91.218.175.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 22:07:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1711159659; 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=Ki2SqS4TDwK1+oUGgKMCMnFI/a9KHGHslLmG6vAgHw8=; b=V/reHql5hOMS9gGdH79dzncDq6IdjoyZaApKWlC7cZ6OzX8l7LjRUbkts5M8RXXkuqXjV3 WyBun2puVO2pPSWekIesncvzcBvPaB2xEP+uyoKDGs2Khwkpocb7aoPvftDTHFQ9dBpOYJ wktB04MA4OATeTiHsnGdycZwKQqlRdg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Linus Torvalds Cc: Boqun Feng , 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: <3modld2dafaqjxa2b7jln47ws4ylzhbsvhvnphoklwvzange5p@wlir7276aitp> References: <20240322233838.868874-1-boqun.feng@gmail.com> 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 05:36:00PM -0700, Linus Torvalds wrote: > On Fri, 22 Mar 2024 at 17:21, Kent Overstreet wrote: > > > > Besides that there's cross arch support to think about - it's hard to > > imagine us ever ditching our own atomics. > > Well, that's one of the advantages of using compiler builtins - > projects that do want cross-architecture support, but that aren't > actually maintaining their _own_ architecture support. > > So I very much see the lure of compiler support for that kind of > situation - to write portable code without having to know or care > about architecture details. > > This is one reason I think the kernel is kind of odd and special - > because in the kernel, we obviously very fundamentally have to care > about the architecture details _anyway_, so then having the > architecture also define things like atomics is just a pretty small > (and relatively straightforward) detail. > > The same argument goes for compiler builtins vs inline asm. In the > kernel, we have to have people who are intimately familiar with the > architecture _anyway_, so inline asms and architecture-specific header > files aren't some big pain-point: they'd be needed _anyway_. > > But in some random user level program, where all you want is an > efficient way to do "find first bit"? Then using a compiler intrinsic > makes a lot more sense. We've got a whole spectrum of kernel code though, and a lot of it is code that - honestly, we'd be better off if it wasn't specific to the kernel. rhashtable comes to mind; it's a fully generic, excellent at what it does, but it's had a number of annoyingly subtle bugs and sharp edges over the years that are really just a result of it not having enough users. So I see some real value in regularizing things. > > I was thinking about something more incremental - just an optional mode > > where our atomics were C atomics underneath. It'd probably give the > > compiler people a much more effective way to test their stuff than > > anything they have now. > > I suspect it might be painful, and some compiler people would throw > their hands up in horror, because the C++ atomics model is based > fairly solidly on atomic types, and the kernel memory model is much > more fluid. > > 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 side we can obviously do that with a helper; the write side needs compiler help, but "writing just a byte out of a word" is no different from a compiler POV that "write a single bit", and we can already mix atomic_or() with atomic_add(), with both C atomics and LKMM atomics. > But it actually goes deeper than that. While we do have "atomic_t" etc > for arithmetic atomics, and that probably would map fairly well to C++ > atomics, in other cases we simply base our atomics not on _types_, but > on code. > > IOW, we do things like "cmpxchg()", and the target of that atomic > access is just a regular data structure field. Well, some of that's historical cruft; cmpxchg() and atomic_cmpxchg() have different orderings, and we can specify that more directly now. But we definitely need the ability to cmpxchg() any struct of a size the machine supports atomic access to. Rust should be able to manage that more easily than C/C++ though - they've got a type system that can sanely represent that.