From patchwork Thu Dec 19 21:08:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mitchell Levy X-Patchwork-Id: 13915780 Return-Path: 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 B5A9BE7718A for ; Thu, 19 Dec 2024 21:08:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 414B56B0083; Thu, 19 Dec 2024 16:08:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 39CA46B0085; Thu, 19 Dec 2024 16:08:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 216F76B0088; Thu, 19 Dec 2024 16:08:49 -0500 (EST) 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 F2EFA6B0083 for ; Thu, 19 Dec 2024 16:08:48 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 71513C0323 for ; Thu, 19 Dec 2024 21:08:48 +0000 (UTC) X-FDA: 82912947222.18.F282CED Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf18.hostedemail.com (Postfix) with ESMTP id 468C11C000F for ; Thu, 19 Dec 2024 21:08:31 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eMsQc+Ay; spf=pass (imf18.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.214.174 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=1734642489; 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=R1MhMROjcsA7Jc28Vl9zHVu2WGdgwV5F2CsZPNH8R9E=; b=FGbrxa307WdVmFU9oIMDWl3QPrkFE2LgYl3ExZLyIpJz6/BL3mxqx250s42h7q1Mv7cpIB lGU5+cSNa92Z2S2ED91oT7SJIOcUp6EQX82YmKMVMYj/3aT/3BIdA4AVPJ4b8dF+pZ2KTT VapgyLAfl7XlCw17G7ER/VfbqYVQJ+k= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734642489; a=rsa-sha256; cv=none; b=zKQkXm+o1sMuB4hjwfbrTM9zkngqVch5qbov+E0+PyUbMTCe/SSfihibHl6z36YjhcXWMJ uHysqYtU7P1bbBLQYTjz2FihZELtO4Vzcc+5Uimnyb/4Z2f3Ghej9WPLh86woL1ERVffQt 1O0Ln3qSqNaGyu/GBDpEnMek0EHH3yE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eMsQc+Ay; spf=pass (imf18.hostedemail.com: domain of levymitchell0@gmail.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=levymitchell0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2163dc5155fso11724095ad.0 for ; Thu, 19 Dec 2024 13:08:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734642525; x=1735247325; 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=R1MhMROjcsA7Jc28Vl9zHVu2WGdgwV5F2CsZPNH8R9E=; b=eMsQc+AyHSwPXEMON5E5+l8Tx1zfX3FAtv+2joPGzVyzSznRIMJITGACV0LPJZ1t4G kLBrloxA7yh2qYsw6OabRlw4M2aKAB3VYMzN5s3zwZA4eL5TakVKNgBbJreNvk1X2e6F r9u7x3/qf4UBAcEycgojgCqvw62CHiUqifkBwPqVGQbsn1gCc9Lt7zpueD6PuWqaYIEp E02H8RBn/k9tKLn+h3E/oTrm5nqK5oYmtQUzQASa6tp7Tzz3xQo3LXf+audTBprvr7tS 0TOKnUW48XhH3j8BB5SyMXyfA/W5rV+HhsQI3dUw2VSmioUBifNdCQy9Fd7F/2pvKo25 A8YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734642525; x=1735247325; 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=R1MhMROjcsA7Jc28Vl9zHVu2WGdgwV5F2CsZPNH8R9E=; b=THxW3zC3+XeJQncZ4MHAobmlXN0Be/kjzzCcWYsNk4umslgld7hrrtARrmlTTkUU7Q FT0RSRsYnr7LGXV8pQ/7QsCcW1HsX5oV4Asdib1/pi2ZefBsgoEi/E1F2eG+jrRY4of0 WHHQs0jlRe28mDn5UU/vavSJA5EdtBpP0G6oIxJb93cpGqS3DWay2nfhPzdunoPtl/YA N2I+LYXzWoKz3cSIosOvbW0d+L+nm5a/290P5YeCs1A1D0Wje5qqQ3xikmETTTwYslo6 IDX8rB0VmGdbdtZPx76G/G1YnRiqWW+MQxYPv9GnsJuM4W97ucMZGUVp1zkxrxFme3N7 Gjow== X-Forwarded-Encrypted: i=1; AJvYcCVTtL36C/lKd6fXh+h43o50G/bbQNrvP+dqcej/NxXbBSnxDCQ0BtxGsBTEU+6ZdvWdxvbIAbgepg==@kvack.org X-Gm-Message-State: AOJu0YzNeR4trNJBGQbY8CdASOHfVJjQKduys/a2xYqJZlQ4fn+9uBiN U3w243hFp1h4z/lw/WvJ/9Ld/oJwMoYCf524gn+z0GwLHkTlWxwl X-Gm-Gg: ASbGncvL4QIB1lin5BRHDLS9MRxGrdGVayXCz9ExMo4ssaEuKFYQc87eOI24lD4iKUq WLTmrGfRlU7jlYKvIStyBEQUH7qIZPTIpupqVJfb+f7++sY2tiAKG5Kf8cCPsZENEIsAF1RJiAr 3/yb1IaMrPyCGANVXCSdHClBqQE+exHpy9BCMSp61CKzHDVJIayvMy60WBmDWc7wvMXuaiL+Hvc Vlq0SagzxTICK9ziDWEtzp0sRQpIek6XmyESPJLHeD56rTWysegEzroCROA67WBxg== X-Google-Smtp-Source: AGHT+IE8GygFZK2sr4giHtblhflzh5vPhhJiukJzG2Z6JgvItycKwayoh0hhUg6QYmk5pzEKoPn9jw== X-Received: by 2002:a17:902:d582:b0:216:5e6e:68ca with SMTP id d9443c01a7336-219e6f26685mr3011085ad.49.1734642525040; Thu, 19 Dec 2024 13:08:45 -0800 (PST) Received: from mitchelllevy. ([174.127.224.194]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc962cc3sm16831755ad.28.2024.12.19.13.08.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2024 13:08:44 -0800 (PST) From: Mitchell Levy Subject: [PATCH RFC 0/3] rust: Add Per-CPU Variable API Date: Thu, 19 Dec 2024 13:08:25 -0800 Message-Id: <20241219-rust-percpu-v1-0-209117e822b1@gmail.com> MIME-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAEmLZGcC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIxMDC0Nj3aLS4hLdgtSi5IJS3dREozRTkyTTlERjYyWgjoKi1LTMCrBp0Up Bbs5KsbW1AF18I6xiAAAA To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Mitchell Levy X-Mailer: b4 0.14.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1734642524; l=2462; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=AkV6eFg+cDu97OxGU2Wl3OJrwxMNC8s4nUitgc+7UN8=; b=OTMUfRXX2rfTnhPVl12wpkjmAwrzPGZ+W0l9f787UHmkIGMCZ24WPvarSKbSCWVrpcsQsFvbb crEiT2plwJSBEBL0uaIrRQoQZZZcFIuvt/jZ1boRebrVM1E6ZAoLsJf X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= X-Rspamd-Queue-Id: 468C11C000F X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 8euiutjcc5468kxe6gxanyzzwrr7y548 X-HE-Tag: 1734642511-663052 X-HE-Meta: U2FsdGVkX1+XzvFvrt33yQIq9IddJRpA1P0Pbx9s42eSl7rMrkmIIESGGLBIoJkIzbqzBynZrTwfy4hayeu4Nczj97Kj5oMVe68XNA0XVn5vz8tUJgeNVsQC/lBWj2eihnlq/g4wn1KAWfoFnte5JB2dKh5fKn4mwKwA7oMR2UPF1p+EvgCIwPpqYsdzvaN6KBhlzD034BsueC9y4NzOi1QQ2ZUIr9knhLVlODAO7xomzc6PQP0Rk8O3zmyfh/vujwdC7xIt4g4eqemWykLPQxMihOxW3JNxzLZ7Ymh9TkMMoGUlGJpykkn70D+OeQia1C1a3GNLksbqX1Atijdv7EIefVNc+kE89BEzFdvV1yfIQ8HPhQ2QN7bxKGoGuROQBVORwl1Z/9ROj2qRjYsG91H2K1/O0KcIAw/zR2pVPwREUSuoTV5k2ElQmxhgfxBTVvWyBgPRgrQnY6Rdbua2MRcSREVuYo6/HkdKUfd0WTpWTnY98NpZd09y0BOl8wKqnbfBMdoStHyBBKuu9OX632oMPIMlwMrm0TiXqAooy9xPHuRdpQMa0VY99yOZtvJdFxKQUwFNC4bNeuy5+KkAPvVn1Z8J+e0//+PL6ziZuBUnK3TDmHej3OriQUjrM3eq/gedaxQgIh65yi1V9FQkPwJ+7hu7IVnBi7HYl7Qg3PNmiUTatJBjddJOwC+u1YVDZtJz0d+WMH0qPXimegqGrgXXSSpVPXqk6BmZrqIq+euPtWxBIk5392pdaDY6GVD6dWHF5vS2BkKWbv7pmG5t5cdgUqNADV4YwnWP9Luztipn3bCENhOza607/4ro67PLbiuQS1aPX+rTc7QlIqgKvhOF1ot+IzNVTSXUWXo1Uz35ckf2Fo3+fxDqH+l/NEJUg+oyQXD5AjWwlzJLGJoPTzevrvN/vbxcxioYPqDaUTPROPGQd53bt8ZMLEyviYDY8BB7fn3WhUr+FWXTbuW FCSLgGHP 6u8NUe6ccdlQNKQy+94zu/I4xeqMFVuWZiaDGAeJdxgxB5rPthOGuwjVEvz5bzzLaH+vYuvNgfNP1QAD/hSH+6xzU9+FHz+fMFXin9PmZzuPMp1lMzkgkK0obws1Y2dY6RZkx7qF372VAu3GXYGLOVjxLx1dmHFwkqW7tVaq9IMHYTcF2m0XYS5RTiiL8y7GraJZE8rlLVElCXQBiCSLMcF26pGZtTrRC2nuk7aFO1CcsDAvBOL7BeLb5+qnaAk4IZKWEAa5Mn4QboF2gdkxFOOUTEoECq70y4NH+zW4CyME4jzJFfst3NKVN0uOgxUGB+lgEeXnJKnrKw6eUvD2YzInYkewyekIjPNvi5hfHD2BS7jKYkDsFj8a1C2PGEoH/NRqZEgPV0uEykSqk1IQB7702jgUFDIW42pSXO/Pm0kHkMA2kziNBgmItmLWzh1oc1oBrPX/Enohf9hg= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499999, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. --- I am sending this patch as an RFC to gather feedback on the API as well as get some input on correctness. In particular, the unsafe getter macro is somewhat cumbersome, though its safety requirements for pure-Rust code aren't too difficult to verify (interoperation with C code is certainly much more tricky). It was suggested that I base my implementation on Rust's thread-local storage API. However, this API requires that for a thread-local T, any particular usage must be via a &T. Thus, many times it is required that thread-local variables be in the form RefCell or Cell. This has some pretty severe disadvantages in the context of per-CPU variables. Namely, RefCell may panic if .borrow_mut is called multiple times at different points in the call stack. This is similar to the problem inherent in my current implementation (that is, unsafe_get_per_cpu_ref must be used carefully so as to not create aliasing mut references), but this implementation flags this potential problem via an unsafe block and the resulting PerCpuRef can be easily passed along and used fairly easily. Cell on the other hand requires a copy any time the underlying value is used, which is entirely avoided here. Signed-off-by: Mitchell Levy --- 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 | 65 +++++++++++++++ rust/helpers/helpers.c | 1 + rust/helpers/preempt.c | 14 ++++ rust/kernel/lib.rs | 3 + rust/kernel/percpu.rs | 161 ++++++++++++++++++++++++++++++++++++++ rust/kernel/percpu/cpu_guard.rs | 29 +++++++ scripts/generate_rust_analyzer.py | 2 +- 9 files changed, 284 insertions(+), 1 deletion(-) --- base-commit: 0c5928deada15a8d075516e6e0d9ee19011bb000 change-id: 20240813-rust-percpu-ea2f54b5da33 Best regards,