Message ID | 20250414-rust-percpu-v2-0-5ea0d0de13a5@gmail.com (mailing list archive) |
---|---|
Headers | show
Return-Path: <owner-linux-mm@kvack.org> X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CEBFC369B2 for <linux-mm@archiver.kernel.org>; Mon, 14 Apr 2025 19:29:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A96E228007C; Mon, 14 Apr 2025 15:28:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A43CD280077; Mon, 14 Apr 2025 15:28:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 875D528007C; Mon, 14 Apr 2025 15:28:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6236C280077 for <linux-mm@kvack.org>; Mon, 14 Apr 2025 15:28:57 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 07F4F12059C for <linux-mm@kvack.org>; Mon, 14 Apr 2025 19:28:59 +0000 (UTC) X-FDA: 83333637198.24.50F25F0 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by imf26.hostedemail.com (Postfix) with ESMTP id F0AC1140008 for <linux-mm@kvack.org>; Mon, 14 Apr 2025 19:28:56 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fvrMfcS+; spf=pass (imf26.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744658937; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=gFS3X/OrT8fO8A2ptqVzZaLjnbKEAJqbwwH6K2uGZ1s=; b=T28HR+nW/dfEdt6RXDEFCmr6Ufb8E9YHmhsMcljXkY2iCqUzUrO9sf6+Ez6/pJf2GT0LF+ XcB703yM86meFUuVQ/aDJyIKC2t9TEdCFe014DiqRCIfjlaQRJlx1sYipBlYUXoCJ87uql ydO5aJqUlpsNsIh7qFLWW730WJ0uyQY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fvrMfcS+; spf=pass (imf26.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.215.175 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744658937; a=rsa-sha256; cv=none; b=p+vDU+3XyjpRtTgg9rNzUf6ghp4bGV2WC2XLMbRBxj7fiIvfAem9Tlm1vRtXxHu6ux/FXG GeRXsvJXJ8kECDH5h2M3n2x1SlNETfRpK4IQw6CMXp6f9FGo5x+979tGA2Js0pQgTp8wvI Q7YQXgqdjFKDnBWw7mT+yJMEn5/Lu3w= Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-af579e46b5dso3460097a12.3 for <linux-mm@kvack.org>; Mon, 14 Apr 2025 12:28:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744658936; x=1745263736; darn=kvack.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=gFS3X/OrT8fO8A2ptqVzZaLjnbKEAJqbwwH6K2uGZ1s=; b=fvrMfcS+9Lg8rJ7+WO0y94q2hiqOqqo231jETfsW7+KIcvd3oZkfbBLyhF76jKHxbg 0wiBzbxmtgcdJO7En/5+TT6eFSTco1Oa5p5aPHGQLors+ujrJvcNoQoX4BRyyw4W1aKs FJXunTZ4gy1xjzikeNIZCtTL90b49QcvkfnQ4oiBbNuA+CP3I4OQB6qN8zNUi5Eb2GmX QN7rnn7PrO1C/WWk1/u4fteiuLWM2VQgA/jivq5HdKO2/mnrsdEnXsU1wcXTsBDxvrO1 Z9cyx1C/aOWpQDjf4zMgJxQVVHjDPyz/3G4Q0/akZFAHycQVuyeCTEfyJnfL5ukSdPD7 ia3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744658936; x=1745263736; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gFS3X/OrT8fO8A2ptqVzZaLjnbKEAJqbwwH6K2uGZ1s=; b=rZE8e4svaFoEsfUUhTJPDBYCfu0PXfVv701dIfwSiihnemYKSyys3fmCy1XsgLyfK4 GuT8amQIr1abtZRZN7A0QlfWRDMe/Gf3KGvX5+HgEH+ct4HhmI7jyUNWcvSPyenhG+mM Sc4eis+e8xfVnnZ+QK0Gx87RGtpkWkzk7KtfXHOD3HQx5N6lPQI4tUmY8RhfxTlM/tGY PfjZYGWlrJveFSLVXnvTW6ulxURlRE0CC8McuXH6mEeBfbhgafW55t5NN2PiFV/OERzX jFWBUTqtj2/Ifsdm92Rso7XAYMI17thuKnmTwWJ3AqB45l8EcGi/1C3VxsKyGh8XwUWU oaVg== X-Forwarded-Encrypted: i=1; AJvYcCVmejqaraxER68BnzfC0wZKR43XQKoDyUKH1ztjP6HTMR1jicBComiLGHjcvtv0v3RAZOaNTc+W9A==@kvack.org X-Gm-Message-State: AOJu0YwohnZ61bBeUJGU2sEJAW2cxZ14WTEyJ0/jXeJC685cbNSEgpIP yoSTeucFdBAL6eJawEzKdQJJmj7YcaAhoA5xJ9tsT63EZ0hxNBRT X-Gm-Gg: ASbGncuZ7qryDpZOgq2ryKx4RDtq3yF1VVtqLnFmPcf/+BiyJJLqfMHtLalN4YMsIJg 0XTTZ2nGGAxSOALxnPWfHrtb1ncI8KjZX/L3o0155xsqp+cMMSXwwfGYPUUvgwyTOXSG5S6zIaD 4PrlOLI+Dmh2UPsTVykkjne7OcnIqU/eh/QQoQhiG1q0fZRLCMxISL0Pz/qu3Sf+SaefGNNowUS f3r5AiUIDW0oqOxIMIXMVbgt60HIVx1KFieq1lWmJ7cIWXFeyNslzj2fLH7mg8+0+pSvarKWxYw Izcmr3eSks7CyVukqUFmy4KOVxdmq1GFOGvEze8bWNBFDjtbIjxMXbZFqQ19X38y X-Google-Smtp-Source: AGHT+IGApKdaH7X+8I19ZcTXbxTvcBWcVLSNn10u07GXbXxjfm6pmm1OfmYmXFjGDk/53fTz6utHXQ== X-Received: by 2002:a05:6a00:ace:b0:736:6ac4:d1ff with SMTP id d2e1a72fcca58-73bd119d019mr20472149b3a.3.1744658935521; Mon, 14 Apr 2025 12:28:55 -0700 (PDT) Received: from mitchelllevy.localdomain ([131.107.174.166]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd22f0f9bsm7223159b3a.104.2025.04.14.12.28.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Apr 2025 12:28:55 -0700 (PDT) From: Mitchell Levy <levymitchell0@gmail.com> Subject: [PATCH RFC v2 0/3] rust: Add Per-CPU Variable API Date: Mon, 14 Apr 2025 12:28:26 -0700 Message-Id: <20250414-rust-percpu-v2-0-5ea0d0de13a5@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIANph/WcC/1WNwQqDMBBEf0X23JTsqlR7Egr9gF6Lh6irLlQNi UqL+O9NvfX4Zpg3G3h2wh6u0QaOV/EyjQHoFEHdm7FjJU1gIE2JzjBWbvGzsuxquyg21KZJlTY mjiEsrONW3oftCY/7DcoQ9uLnyX2OhxWP6idDwvxPtqLSinSOeOGMqMKiG4y8zvU0QLnv+xemK QzsqwAAAA== To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Benno Lossin <benno.lossin@proton.me>, Andreas Hindborg <a.hindborg@kernel.org>, Alice Ryhl <aliceryhl@google.com>, Trevor Gross <tmgross@umich.edu>, Andrew Morton <akpm@linux-foundation.org>, Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>, Christoph Lameter <cl@linux.com>, Danilo Krummrich <dakr@kernel.org> Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Mitchell Levy <levymitchell0@gmail.com> X-Mailer: b4 0.14.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1744658935; l=3782; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=QxD8SEeNg4LzLLT38lSE+fXXb/oKKvXm8xZQuoGO484=; b=PZxT2pwH1qG8y6oZkAkd9ckp5L98cjzxp3gKStsaCxKn0Ni1U3/97uqxINi7sOPZ1qv10WdPT m/JiRqme18TB8UWXfK1qiUoqfRNEFvJjLYew3nF80JxPAQaNQLuAxL9 X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= X-Rspamd-Queue-Id: F0AC1140008 X-Rspamd-Server: rspam05 X-Rspam-User: X-Stat-Signature: jygjjk7q5hobesjg5uhujss7rbmb6ogy X-HE-Tag: 1744658936-79487 X-HE-Meta: U2FsdGVkX1+VDn3k2D+8f60sGtL4WYPNPxcMIiExrsbrCQObedRH4szQAnnxvHzQISvCIMEPVCWtbCIJcw4OzITotMMG+EOx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uTSkYCXE4c937MEUp/hOL8tf7iYws9vmvJ8NaeRMkCsgJ3te3UJYcqicDZ2l2vxhLcuRPErIgwLDoZmOmPWiOvcNJzExa15OdIGPc5ieTw37QnJ/OSwdjYKYiRT92xFylK+VohNTsH6Tlo36DS1wnvb3H/wSoS/jL3yqhp6vQwr9GJq+JSlMa68cwNod7ZGCct+j/pODzz79CpntFWr3zs073ORA6pCGje3/97XiBjGvlWEhKy7fWIIhf3XHOUNLzYYLdb1kgQZB3owAMLnriAMaumiJp/dzecPZJQxtvmRy9Kbvqq5RGmgv X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499948, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: <linux-mm.kvack.org> List-Subscribe: <mailto:majordomo@kvack.org> List-Unsubscribe: <mailto:majordomo@kvack.org> |
Series | rust: Add Per-CPU Variable API | expand |
This series adds an API for declaring an using per-CPU variables from Rust, and it also adds support for Rust access to C per-CPU variables (subject to some soundness requirements). It also adds a small test module, lib/percpu_test_rust.rs, in the vein of lib/percpu_test.c. --- This series builds on my earlier RFC by adding support for dynamically allocated per-CPU variables. In the process of this work, I became convinced that the API in the original RFC was untenable. In particular, copying a PerCpuRef would need to be an unsafe operation whose safety requirements would be very onerous to verify. Essentially, a PerCpuRef would need to be entirely unique on each CPU core (except for the moment the copy is created, immediately before it is sent off to another thread --- technically the requirement is that the copy could not be Deref'd before being sent). This is essentially impossible to satisfy if you wish to use on_each_cpu, and it is certainly not ergonomic. The key challenge was preventing the creation of aliasing &mut T's while still allowing PerCpu<T>'s to be freely copied. Ultimately, Boqun suggested a closure-based API in which the user asserts that the contents of the closure don't "nest" with clones. That is, the user asserts that they're *not* doing something like: ``` let mut my_pcpu: PerCpu<u32> = /* ... */; let mut my_pcpu2: PerCpu<u32> = my_pcpu.clone(); unsafe { my_pcpu.get(CpuGuard::new()) }.with(|my_pcpu_val: &mut u32| { // UNSAFETY: This is bad unsafe { my_pcpu2.get(CpuGuard::new()) } .with(|aliasing_ref: &mut u32| { /* ... */ }); } ``` This safety condition is (in my opinion) much easier to verify because we must only ensure the contents of the closure are well-behaved, rather than try and trace all possible paths that a PerCpuRef may take. This API is certainly different from the original RFC, which is why I've sent another RFC. It's now much closer to the user-space thread-local API, though still avoids requiring the use of Cell-like types. A small number of simplifying choices are made here, namely that allocations are always GFP_KERNEL. This was done with the intention of keeping the patch small while the overall shape of the API is evaluated. Signed-off-by: Mitchell Levy <levymitchell0@gmail.com> --- Changes from RFC: - Renamed PerCpuVariable to StaticPerCpuSymbol to be more descriptive - Support dynamically allocated per-CPU variables via the PerCpuAllocation type. Rework statically allocated variables to use this new type. - Make use of a token/closure-based API via the PerCpu and PerCpuToken types, rather than an API based on PerCpuRef that automatically Deref(Mut)'s into a &(mut) T. - Rebased - Link to RFC: https://lore.kernel.org/r/20241219-rust-percpu-v1-0-209117e822b1@gmail.com --- Mitchell Levy (3): rust: percpu: introduce a rust API for per-CPU variables rust: rust-analyzer: add lib to dirs searched for crates rust: percpu: add a rust per-CPU variable test lib/Kconfig.debug | 9 ++ lib/Makefile | 1 + lib/percpu_test_rust.rs | 119 +++++++++++++++++++ rust/helpers/helpers.c | 2 + rust/helpers/percpu.c | 20 ++++ rust/helpers/preempt.c | 14 +++ rust/kernel/lib.rs | 3 + rust/kernel/percpu.rs | 239 ++++++++++++++++++++++++++++++++++++++ rust/kernel/percpu/cpu_guard.rs | 29 +++++ scripts/generate_rust_analyzer.py | 2 +- 10 files changed, 437 insertions(+), 1 deletion(-) --- base-commit: a2cc6ff5ec8f91bc463fd3b0c26b61166a07eb11 change-id: 20240813-rust-percpu-ea2f54b5da33 Best regards,