From patchwork Thu Dec 19 21:08:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mitchell Levy X-Patchwork-Id: 13915783 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 050C5E7718B for ; Thu, 19 Dec 2024 21:08:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E267C6B008C; Thu, 19 Dec 2024 16:08:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DAF766B0092; Thu, 19 Dec 2024 16:08:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A561D6B0093; Thu, 19 Dec 2024 16:08:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7F6816B008C for ; Thu, 19 Dec 2024 16:08:53 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0396E431B9 for ; Thu, 19 Dec 2024 21:08:53 +0000 (UTC) X-FDA: 82912947726.28.79456FC Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf02.hostedemail.com (Postfix) with ESMTP id 006178000E for ; Thu, 19 Dec 2024 21:07:46 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nExRnxk5; spf=pass (imf02.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=1734642493; 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:in-reply-to:references:references:dkim-signature; bh=8a+U3PThiCkoSQIjCSeZhobcfZFrIgDLb2tUjSaHx4Y=; b=VqmCQYRQHPQzQgpQAlVkP2J4EgUS7/OvrMxsL1BvYPsWx8J8gokX4jvsQO7BN6XBqeKP8l u6ZprElNQxR5+Ortgif2KqcM/TKuFZdUpaaDn7JGCjukfo4VOkdR4BEmtHAsMTM5bclI1D 8n4fQ+iGxsUdN+kQ0Y4K3QCuDbpisKI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734642493; a=rsa-sha256; cv=none; b=HgQopojiXGxFHUivHzY5HAcepQinpQpA3uiOxJ6iKjuRw9wAPIxUJSoJGEYFpF+S80pOl1 jrc7Y7OI6BfHO1YHO/8SeRzZbbdEEo7b107qOjN9Kk0A/C6QGX6VAzN9exSJZcgcDDVir0 1KLNsAL/He7bTsEB0uo+L4wx4YxACTs= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=nExRnxk5; spf=pass (imf02.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-2165cb60719so12343455ad.0 for ; Thu, 19 Dec 2024 13:08:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734642530; x=1735247330; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=8a+U3PThiCkoSQIjCSeZhobcfZFrIgDLb2tUjSaHx4Y=; b=nExRnxk5mZAo+mIU+npPuFApy430I3lMIudJtoCXU7M4D2jUMyq+of/y1/QDxqgWpU HlchMKznmCjLSqrVQZ0Q++65ORXwHZqcWO5DOjg5VSZ8ZKwpKI3buba2wY8Ua3jNSroo PKwx8aGVqqG7TUEkF/+MNmOiUgtMNr6Ke4w8kjJtuOQ0EdUKtBLtCyMJbUTDCCgblGmo qFs+HSoUPO7xBVVNjUt6tHxTPVd3ln9EOPpjvdNAv+q+bx2mr28jjeSqkLIjEcdXkEcR 1NTGynZkoTCmfifHN5WGUqatg306DbZ7tht3j7rB3b1xCW9A9ozUISEawiwNYUfRpgAS SH5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734642530; x=1735247330; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8a+U3PThiCkoSQIjCSeZhobcfZFrIgDLb2tUjSaHx4Y=; b=UbKbPJDU+oVb6WE5awJ/GokRK5iTP5MUnBPH8gQXE+XvWBZZM2UNVZs9H4G+dDjgNG /3sXzEDWUnS/eMXmqzz+kqrMBoy/Pj6NTPM1FHiD5LytqBkrTMwNTDPdDJ4P/74XhDjE 5d5WwJQ85MinuKYIqtO2iqMFtLQEDb++vaql/LsGqmnhrlEFAUuY2Et3LlkOyTR1Zyrs oPhxqGuQ1fXYnF/riHqTl8sRwErlHm3d47PMvVO29IAnJOVzH4j5TkzMGodrqY0MbR7a aD9oLKss/Ghp6AiI1QwfqI9zUDZicK5fc6ba3PQXmYMnbZplxbE4Kp87W87srqI0r/AZ zh/w== X-Forwarded-Encrypted: i=1; AJvYcCXq38lz3/81Vb+z/qFhq2ZFaaRfm/ksQOlOw0IXJeeAVLwMz1rB8YFcgrwBgGopHY6KBURWpfpfGw==@kvack.org X-Gm-Message-State: AOJu0Yw24j7/0/AS/GKN7jXUh+tLKI+b3oJFwG0a9Pt9BwwLvdK1rFJT CMUSvwr9ZKG6DsD/z1vkSUDES4ft2JOjVJCT4EyD/x3Igr8AbmFi X-Gm-Gg: ASbGncsoPRpe3ZZecF5J5u7AuHEoHjfGYbWuLRi9k1DcXYrOE02XZa97EM+KO9W1O0K MOeUonRVrjnnpzCFoNv4AtFRerAwwVcqaiKwwv7rlcotX787a5KxLsQtF11kiKf56pDhbVvbxfj Ot+Kp8gqMCzv7spPAt3WYc39mfLGDIhTtizeGmtOLwYDuzZ1Ilb9jLL9UPWVz+63PUDYB1RwkYv iLPAtaOFuFd5quxgwRaUK14yt5e/TQzDnNeQaT8+cbO3eGDUQW2TPbm86PK3mQ06w== X-Google-Smtp-Source: AGHT+IFlNR+5xs9cq8NpBSRrS++zF1DZpmnXPo5bkWp0elc/Vz4dUHAxNDYhcGSHRx+In+/jxMSo4Q== X-Received: by 2002:a17:902:e5c7:b0:215:19ae:77bf with SMTP id d9443c01a7336-219e6e9df25mr3132215ad.19.1734642529695; Thu, 19 Dec 2024 13:08:49 -0800 (PST) Received: from mitchelllevy. ([174.127.224.194]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc962cc3sm16831755ad.28.2024.12.19.13.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Dec 2024 13:08:49 -0800 (PST) From: Mitchell Levy Date: Thu, 19 Dec 2024 13:08:28 -0800 Subject: [PATCH RFC 3/3] rust: percpu: add a rust per-CPU variable test MIME-Version: 1.0 Message-Id: <20241219-rust-percpu-v1-3-209117e822b1@gmail.com> References: <20241219-rust-percpu-v1-0-209117e822b1@gmail.com> In-Reply-To: <20241219-rust-percpu-v1-0-209117e822b1@gmail.com> 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=3571; i=levymitchell0@gmail.com; s=20240719; h=from:subject:message-id; bh=Fg46h50fzfyqtIe9aDLmvc/Zzgw12ckY/xFnT5ZEMe0=; b=gRK8pwjQ4BF4Qdvi0zi3CaQNn7uKJ8Q0n/gQMpX5Cf5J3e0PE0ANTZflDXvPIbayToRUWQhb6 +IFU6CcSailB8h8pm97y0hJlg8MH3QTdhZycACwBxubTmMZBxvl+/zV X-Developer-Key: i=levymitchell0@gmail.com; a=ed25519; pk=n6kBmUnb+UNmjVkTnDwrLwTJAEKUfs2e8E+MFPZI93E= X-Rspamd-Queue-Id: 006178000E X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 8rdbqawxici4pw7oxp5nsj98aobbzf64 X-HE-Tag: 1734642466-310925 X-HE-Meta: U2FsdGVkX19rAXVWsBfczeYUBIfRO9ap+RllLDOvKz8wBA0N94jdAruxdsHScDUwpSOqYwBZDm0v672bmjHYk7Hu+xh8W1Bsw9GoNUNzkRH+jhOQuTrezepojjL119YZh3YmFBH7ll+npqguBSIJyxtzxUbnUjJGFi04S2sTJRT28jZDUV4MAopp/0K6UC1Lk+pQbDeU0QBwOoimz29KVWiA9V0II22XpBnsp5c9Tzntrty+QwZ/M10yvWiMnNiV5nxHgY4lHJuMLPq1LWssvAAs+PtsqeuIp2iuA29Y4/itqkaWYJtW9dnI7PM8M7dDE4kJHFZBWlAeKcdo3CCn4m6Vfbllrt/kC6xIbrnF3G3PlQ2TXAsZrTj8a5oRbBuBm3JAQTdP0fZXWZrXmlGVivz5GeyA/SnQgLVT9v5sz6he2prgGcjj0ns9iiHOag1DI5oNQOoiMXXACAvJ+RrcwwcYOai1EVtI3MHCZbeogcaIrlPq5l350B9kIe98yvLCPjqsnoYHQCDUj7YB6qy88sBOYOhU/QFtRFHI7sFQ0mFxxstOUVdIcy3enQ5PrB4DMdE4b1TZfhhDT4NBfJMVYBkkXeEVhO+okk6UbBcqflDIYVAn5Q/vf/fyayn2wyTSfxkyMHeTFY5p4ywAAjXlTTRpFkNN08bJwwS0KYv8hCGCj5XSqIxRVTnxegMlkzUhUsUmui9P4W76qNGujyX8ifTdXbBgr1wL75nSXvs3dyGxeqxRBOm50jVhSzwblZErU8OnJB3bSovHOeFiLD2gwFo3+XeA9+HO1DKZ6RkmBklqB6/DFQhaMAi3l+kYX4PqeZT/lg1/UUrfQ/pyPJMkBJqvioQJQjoYIryDWS17u4D7zuhoISRtYfGVSariwqThvuBDO4dB4Zlq1gVJq7Lf0E4EiH8ERVOSMCHxjfuWv5/or/OttnNdsajI+tgx3J3G2JFakBhQ2gF8Zvs0kRA PuD7iU49 yKNK0oweL1FDoWYGK7W2r3f8N2Yx/5DTc+GWKLxcx3X1bTN+CwRPjoSgu9DMW/CyPPCpjL3VKF0POW10P27t9Usyvnt0Htw5h9WmY7ro6u3lmnTyDZ+XWatKzVotN1mNlCfn6y/CLjnWkoftY4YIPusJPAXoqLR52piqE0QyBwtXvEA+D5GhYpPYY/dI1lo4nH+kewiTJZQ1BZsWH29blymOhrOuNu0Q4E2Aqige9cmRiMUmHF+OuknRUMweHti8JE0458EvKm6BLBHuDS6Aol77tskUOMv+C5j74Z8vamFphg1eYEbc479BdcqDwcUZM/v7oh+JbxYlC5hy/ehSvmNwQbwrYQBpUj01BVqDDLDcO+EoJZZjPSVXe8SY71HjCmtEeOe4Cb8tJMcQNSVh897m+Tgakkcmmzod0ItlR2Dm9LTCLb8tPrBoE+73Rf8T9oa4QFc8QSsHsO5U= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499991, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add a short exercise for Rust's per-CPU variable API, modelled after lib/percpu_test.c Signed-off-by: Mitchell Levy --- lib/Kconfig.debug | 9 +++++++ lib/Makefile | 1 + lib/percpu_test_rust.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index f3d723705879..75a91f1766ce 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2404,6 +2404,15 @@ config PERCPU_TEST If unsure, say N. +config PERCPU_TEST_RUST + tristate "Rust per cpu operations test" + depends on m && DEBUG_KERNEL && RUST + help + Enable this option to build a test module which validates Rust per-cpu + operations. + + If unsure, say N. + config ATOMIC64_SELFTEST tristate "Perform an atomic64_t self-test" help diff --git a/lib/Makefile b/lib/Makefile index a8155c972f02..0ea8d414763c 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -301,6 +301,7 @@ obj-$(CONFIG_RBTREE_TEST) += rbtree_test.o obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o obj-$(CONFIG_PERCPU_TEST) += percpu_test.o +obj-$(CONFIG_PERCPU_TEST_RUST) += percpu_test_rust.o obj-$(CONFIG_ASN1) += asn1_decoder.o obj-$(CONFIG_ASN1_ENCODER) += asn1_encoder.o diff --git a/lib/percpu_test_rust.rs b/lib/percpu_test_rust.rs new file mode 100644 index 000000000000..60df44332d7a --- /dev/null +++ b/lib/percpu_test_rust.rs @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +//! A simple self test for the rust per-CPU API. +use kernel::{ + define_per_cpu, percpu::cpu_guard::*, percpu::*, pr_info, prelude::*, unsafe_get_per_cpu_ref, +}; + +module! { + type: PerCpuTestModule, + name: "percpu_test_rust", + author: "Mitchell Levy", + description: "Test code to exercise the Rust Per CPU variable API", + license: "GPL v2", +} + +struct PerCpuTestModule; + +define_per_cpu!(PERCPU: i64 = 0); +define_per_cpu!(UPERCPU: u64 = 0); + +impl kernel::Module for PerCpuTestModule { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("rust percpu test start\n"); + + let mut native: i64 = 0; + let mut pcpu: PerCpuRef = unsafe { unsafe_get_per_cpu_ref!(PERCPU, CpuGuard::new()) }; + + native += -1; + *pcpu += -1; + assert!(native == *pcpu && native == -1); + + native += 1; + *pcpu += 1; + assert!(native == *pcpu && native == 0); + + let mut unative: u64 = 0; + let mut upcpu: PerCpuRef = + unsafe { unsafe_get_per_cpu_ref!(UPERCPU, CpuGuard::new()) }; + + unative += 1; + *upcpu += 1; + assert!(unative == *upcpu && unative == 1); + + unative = unative.wrapping_add((-1i64) as u64); + *upcpu = upcpu.wrapping_add((-1i64) as u64); + assert!(unative == *upcpu && unative == 0); + + unative = unative.wrapping_add((-1i64) as u64); + *upcpu = upcpu.wrapping_add((-1i64) as u64); + assert!(unative == *upcpu && unative == (-1i64) as u64); + + unative = 0; + *upcpu = 0; + + unative = unative.wrapping_sub(1); + *upcpu = upcpu.wrapping_sub(1); + assert!(unative == *upcpu && unative == (-1i64) as u64); + assert!(unative == *upcpu && unative == u64::MAX); + + pr_info!("rust percpu test done\n"); + + // Return Err to unload the module + Result::Err(EINVAL) + } +} +