From mboxrd@z Thu Jan 1 00:00:00 1970 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 D6AFB1FE469; Wed, 5 Mar 2025 21:26:51 +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=1741210012; cv=none; b=UdjOWSKpH/sjN/2KBHSjaZ531NxTiuwNYIWW5jEjiByYf8Eb6Eq1aCfudNUhUFodCjnEZg3DNsVtCO3UvxH/PtMVv7SePg1VAJqHDLHd3ILq7/vgM7mIwiYo99/E94WcXgz7Kf+byuXQwXxHklj/0qJVNVUk70QVW0RdVCQz0is= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741210012; c=relaxed/simple; bh=mCVySOkr7YXVjXIB2HwJbECGnpXQPFKjKUezXQs5VPY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=JA3QcCtNYxLLhyByG8APZU3AH4r0PJUGHhSV4F30rc85PxA5ZqA4PNLG75/p/m5DmwEZq/FZWfaalffJCCyakvwHYZKWho6bOmWbvQ68lafN+U5QkPxsn664JkMpzqBy5mBKT5V8UDT5O4c0AioW3IH2Y9eBVXI2W6e4pffqj9Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kovSYpc6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kovSYpc6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5639AC4CED1; Wed, 5 Mar 2025 21:26:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741210011; bh=mCVySOkr7YXVjXIB2HwJbECGnpXQPFKjKUezXQs5VPY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=kovSYpc636KtErlQgI2ZrNJBoQ/pjYkksty4vMPUUMpBGOyxFAPMBz/6FNkHTWoRx L62czdOmKEJTVno2l7heHrhYrcfcrkjHDRlhX5bmCerH5ugD5wL301q4PwlF0tiFZ0 5DlA96gMAopnFu6YrJqabIu3H1xyarWhBvcthEzJAccKUIuCTkGywBipQ/+syUOWv4 WEILPjO6jURny7wcmoDW90+0+Qe+WijyJGL1zxnecbDSVVDiVh/QlUgYa5WWcxNFFD zm60kfT4cqPfvWuJiDSbeRfxQAG7Tp6L/ULQzozB0ovV6m32cFp+FU0/eh8zBlHB4W ht/yd8BZwA7Ow== From: Andreas Hindborg To: "Ralf Jung" Cc: "Alice Ryhl" , "Boqun Feng" , "comex" , "Daniel Almeida" , "Benno Lossin" , "Abdiel Janulgue" , , , , "Miguel Ojeda" , "Alex Gaynor" , "Gary Guo" , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , "Trevor Gross" , "Valentin Obst" , , "Christoph Hellwig" , "Marek Szyprowski" , , , Subject: Re: Allow data races on some read/write operations In-Reply-To: <915eacce-cfd8-4bed-a407-32513e43978f@ralfj.de> (Ralf Jung's message of "Wed, 05 Mar 2025 20:42:05 +0100") References: <87bjuil15w.fsf@kernel.org> <87ikoqjg1n.fsf@kernel.org> <87mse2hrd8.fsf@kernel.org> <88456D33-C5CA-4F4F-990E-8C5F2AF7EAF9@gmail.com> <25e7e425-ae72-4370-ae95-958882a07df9@ralfj.de> <18cmxblLU2QAa4YP25RWCKEnxuonOwWXavYmSsS4C5D40o8RaCkIXo0UDZ2SPnksk5nWYB29Y4zHkjQeOgd4ng==@protonmail.internalid> <3aabca39-4658-454a-b0e3-e946e72977e1@ralfj.de> <87eczb71xs.fsf@kernel.org> <915eacce-cfd8-4bed-a407-32513e43978f@ralfj.de> User-Agent: mu4e 1.12.7; emacs 29.4 Date: Wed, 05 Mar 2025 22:26:39 +0100 Message-ID: <87tt875fu8.fsf@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain "Ralf Jung" writes: > Hi all, > >>>> For some kinds of hardware, we might not want to trust the hardware. >>>> I.e., there is no race under normal operation, but the hardware could >>>> have a bug or be malicious and we might not want that to result in UB. >>>> This is pretty similar to syscalls that take a pointer into userspace >>>> memory and read it - userspace shouldn't modify that memory during the >>>> syscall, but it can and if it does, that should be well-defined. >>>> (Though in the case of userspace, the copy happens in asm since it >>>> also needs to deal with virtual memory and so on.) >>> >>> Wow you are really doing your best to combine all the hard problems at the same >>> time. ;) >>> Sharing memory with untrusted parties is another tricky issue, and even leaving >>> aside all the theoretical trouble, practically speaking you'll want to >>> exclusively use atomic accesses to interact with such memory. So doing this >>> properly requires atomic memcpy. I don't know what that is blocked on, but it is >>> good to know that it would help the kernel. >> >> I am sort of baffled by this, since the C kernel has no such thing and >> has worked fine for a few years. Is it a property of Rust that causes us >> to need atomic memcpy, or is what the C kernel is doing potentially dangerous? > > It's the same in C: a memcpy is a non-atomic access. If something else > concurrently mutates the memory you are copying from, or something else > concurrently reads/writes the memory you are copying two, that is UB. > This is not specific to memcpy; it's the same for regular pointer loads/stores. > That's why you need READ_ONCE and WRITE_ONCE to specifically indicate to the > compiler that these are special accesses that need to be treated differently. > Something similar is needed for memcpy. I'm not a compiler engineer, so I might be wrong about this, but. If I do a C `memcpy` from place A to place B where A is experiencing racy writes, if I don't interpret the data at place B after the copy operation, the rest of my C program is fine and will work as expected. I may even later copy the data at place B to place C where C might have concurrent reads and/or writes, and the kernel will not experience UB because of this. The data may be garbage, but that is fine. I am not interpreting the data, or making control flow decisions based on it. I am just moving the data. My understand is: In Rust, this program would be illegal and might experience UB in unpredictable ways, not limited to just the data that is being moved. One option I have explored is just calling C memcpy directly, but because of LTO, that is no different than doing the operation in Rust. I don't think I need atomic memcpy, I just need my program not to explode if I move some data to or from a place that is experiencing concurrent writes without synchronization. Not in general, but for some special cases where I promise not to look at the data outside of moving it. Best regards, Andreas Hindborg