Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1477816lqe; Mon, 8 Apr 2024 09:55:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXtdZ7AKVdMwC2wvHby/wNT+YJLQctt48s/gyOItiTQmV18qmWek2ziY5BBqXqxSSTmP1njkH4TXx7Gpn+9516GPjP1QAuI+B+rFqMStA== X-Google-Smtp-Source: AGHT+IEqyFW4B63d9DXgkBHVjN60gj99PXrDylpTdUcbPz2ueDu5cX6UsTCb99SS9CjzAhs7fAsn X-Received: by 2002:a05:6808:aa8:b0:3c5:fac5:6c9b with SMTP id r8-20020a0568080aa800b003c5fac56c9bmr1214682oij.3.1712595341068; Mon, 08 Apr 2024 09:55:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712595341; cv=pass; d=google.com; s=arc-20160816; b=QqAQLgJyu1y1pPiFz2IUA4lF1rtgebRTsa+VinqkWMJYFQMJuYHBgD2iRJKNXahCaj WI1jIWP761YkMZLW28Z9bAkP/ZpiIjeMr1s/0bneHWgc/wBu4Lwnthlh8Y1dDRxmaqt7 X4xxUaaEB0V6NkKEAcSRjaZpK718LyVta9tPHFQg2JPM95UpcxWJRN+OuQBjkGwsmFoo sgg7MO+8p1PiMfmP8L1MWNM/Ys3nK/xjfi9XiZIIlYDyI+Sv/vOwzBlpEObjl92Ir39g /r3uaPJDqvVy889R9pDMP7nDYo4UCknMd0+e2dXSfRe3UT02W/Dda/6HnlSl0PsEpknh u8YA== 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:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=7YVVJj8S3gucYumEfGsVPX8Vy2UwoFvIyCOy8QmCuAs=; fh=ytFnz+FK7KJDZVVTRJOUsmoc8DH+sh5mp/erd8xNAsQ=; b=wk+aCL/AYj+kH82CgHN7+MOxxdtk3hSHv5dzLkaE+9cWoDL+MQC+Pr6pGH1+wHqR8I Tbx3kJfqZ58gW8Ez96pFlUh3+a2Bkv2wBKRSn6fv0y1P7bOuUcTsKZ6sXhQkK7vrbZoD lx6lsZyhTRRTOF3EUDiyvpxSvdVHXP2gq5M47C+cI+ocUJImXX1PiJFotH/XwMLM9lui uyvMDjXqrnyOZuDv3DjFVlnbrXC0wlg+eh6QJrrphStN0Fghw1isCm6B+0kE1g64lLRe ZnN5cDc4UDWQq0nbKOnBNohjUSbw+nASwpNBipQO0jzTxJFvPMm0lMXhIOGc07y/B4b6 6E9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CBqmxFJN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-135713-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135713-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id n20-20020a05620a295400b0078d6413b4basi3488786qkp.288.2024.04.08.09.55.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 09:55:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135713-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CBqmxFJN; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-135713-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135713-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 C541D1C2428C for ; Mon, 8 Apr 2024 16:55:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D7D7F143890; Mon, 8 Apr 2024 16:55:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CBqmxFJN" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 BE6F3142E7D; Mon, 8 Apr 2024 16:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712595325; cv=none; b=fbkNVTLH4HEZ+0sEnPsqqV5S4XLBamKxwF6azTGTITQcLR3S6WfGSpRezrZ5QRxu17KMXqG0uocaZPKtceOa9fgiR15fACZ81WnW4be8IYGNmzZfXSc0rRZNPKGEzxsGs4zr6I4+egxxp/NtyUnCUQFVNYuzMG960W5Wco0rJKo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712595325; c=relaxed/simple; bh=YFesB505hWnJdX9pdGDcBCrm9rzqVPPUgN5AuPqk1jo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MjNO6rovc5DZ3TixSze9tlIwAfN0rMtZtri7/8JgdEOHoJx/xfM2pg6CJzNkyS73j7VRfjTpxzCQkK0GXuw1SpxSiSH8qGUnh2xyFjzNLqBdN2Z7JcaJOEk4p4tNs13byolTJpsqhTL70Rfn662+9pmsr8dEw/sEchZheDBIPBM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CBqmxFJN; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5068EC433F1; Mon, 8 Apr 2024 16:55:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712595324; bh=YFesB505hWnJdX9pdGDcBCrm9rzqVPPUgN5AuPqk1jo=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=CBqmxFJNWXXmB6Mytn75OQxNrcngJMryHSEI+Hp/ETWN1cAsGUcQGncp66pzuie3g glriyS4CYQaYGBNVTbzUEP4ZAR3mDwqKs7WKq9UFu3L8lA632C5CN9LhoPlvCgNjir qnHTDCaE7XOnpNDku2GaUCUZukZTycMhid4NyM3moCZIx0R6y2X0HtKXMtbHZ1WuXz 4dqXce9nprWNNPskUmQgAlDfHVHVxTaJGkqZLgpI+V+x0YSjWHAdfcQs7bvOfAf9M5 h6PcDlYaKvgcNNRgT6xaTSiUS7o39LVA3AvYyBEgsyF1EaAnLyFWVTK2ppk2ktwpxv YYKk/40Nxicww== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id E96B8CE126C; Mon, 8 Apr 2024 09:55:23 -0700 (PDT) Date: Mon, 8 Apr 2024 09:55:23 -0700 From: "Paul E. McKenney" To: Matthew Wilcox Cc: Linus Torvalds , Philipp Stanner , Kent Overstreet , 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 , =?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 , 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: Reply-To: paulmck@kernel.org 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: On Mon, Apr 08, 2024 at 05:02:37PM +0100, Matthew Wilcox wrote: > On Mon, Mar 25, 2024 at 10:44:43AM -0700, Linus Torvalds wrote: > > So I actually think most compiler people are perfectly fine with the > > kernel model of mostly doing 'volatile' not on the data structures > > themselves, but as accesses through casts. > > > > It's very traditional C, and there's actually nothing particularly odd > > about it. Not even from a compiler standpoint. > > > > In fact, I personally will argue that it is fundamentally wrong to > > think that the underlying data has to be volatile. A variable may be > > entirely stable in some cases (ie locks held), but not in others. > > > > So it's not the *variable* (aka "object") that is 'volatile', it's the > > *context* that makes a particular access volatile. > > > > That explains why the kernel has basically zero actual volatile > > objects, and 99% of all volatile accesses are done through accessor > > functions that use a cast to mark a particular access volatile. > > What annoys me is that 'volatile' accesses have (at least) two distinct > meanings: > - Make this access untorn > - Prevent various optimisations (code motion, > common-subexpression-elimination, ...) > > As an example, folio_migrate_flags() (in mm/migrate.c): > > if (folio_test_error(folio)) > folio_set_error(newfolio); > if (folio_test_referenced(folio)) > folio_set_referenced(newfolio); > if (folio_test_uptodate(folio)) > folio_mark_uptodate(newfolio); > > ... which becomes... > > 1f: f6 c4 04 test $0x4,%ah > 22: 74 05 je 29 > 24: f0 80 4f 01 04 lock orb $0x4,0x1(%rdi) > 29: 48 8b 03 mov (%rbx),%rax > 2c: a8 04 test $0x4,%al > 2e: 74 05 je 35 > 30: f0 80 4d 00 04 lock orb $0x4,0x0(%rbp) > 35: 48 8b 03 mov (%rbx),%rax > 38: a8 08 test $0x8,%al > 3a: 74 05 je 41 > 3c: f0 80 4d 00 08 lock orb $0x8,0x0(%rbp) > > In my ideal world, the compiler would turn this into: > > newfolio->flags |= folio->flags & MIGRATE_MASK; > > but because folio_test_foo() and folio_set_foo() contain all manner of > volatile casts, the compiler is forced to do individual tests and sets. > > Part of that is us being dumb; folio_set_foo() should be __folio_set_foo() > because this folio is newly allocated and nobody else can be messing > with its flags word yet. I failed to spot that at the time I was doing > the conversion from SetPageFoo to folio_set_foo. > > But if the compiler people could give us something a little more > granular than "scary volatile access disable everything", that would > be nice. Also hard, because now you have to figure out what this new > thing interacts with and when is it safe to do what. OK, I will bite... Why not accumulate the changes in a mask, and then apply the mask the one time? (In situations where __folio_set_foo() need not apply.) If it turns out that we really do need a not-quite-volatile, what exactly does it do? You clearly want it to be able to be optimized so as to merge similar accesses. Is there a limit to the number of accesses that can be merged or to the region of code over which such merging is permitted? Either way, how is the compiler informed of these limits? (I admit that I am not crazy about this sort of proposal, but that might have something to do with the difficulty of repeatedly convincing people that volatile is necessary and must be retained...) Thanx, Paul