Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp995318rwb; Tue, 27 Sep 2022 07:11:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM462sN/jdO2+R8gN7JJ+G886JTJc5DNaFQTpyTiCLE05OqzYmlh6JvDRefw737XAW87viRA X-Received: by 2002:a17:906:58d6:b0:783:a081:7a20 with SMTP id e22-20020a17090658d600b00783a0817a20mr7994432ejs.357.1664287898005; Tue, 27 Sep 2022 07:11:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664287897; cv=none; d=google.com; s=arc-20160816; b=JAQg+6JiVJATtTKtVZmos07uTpBRkNeSxS1LzaJIJogUY8SgvlTL1VtodhGJV7JAGz McYhrHet8u5BuLHumRhiYxK7HVNkUJVYY1SB9Yscph7zd4U9HOjqtSew/GN+S5mXN4Dj ZoRmAJKKeE4HxPc42ygY7TVDMGNUM8+3+cWPeg8BTIJ205XvFVy2ChhFsNH3lEihr92d BPEqYIWRv1wl3oyfmNCpqwyOJwDSatvpb+1bc7OHLyrFrRukr2xmipS/xqrXz54cLzTS ornmf6kSfIFtD3rhBSGNr2nhZ6L5QWynJ8MfdDo6JvhNdd9MV9X16FjeNKGnS6YyHz7h ocIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uudWQJrJHwIOMdD9Dz8vfz4xvVv84/lKhNUhuhUuuog=; b=MyMYbMFUX7eGjkCOiE8r6+NWKvrTwJxVW7Fd6A5v33yjqTcTk+3JutyAmawkdEJKcK nstXYITvFb3henpvEZp3w1jJurGgbyOuej6qeeKOkKykhMubsyopioosN2F/P1IMdRgz Ek09qqj1U3zxc7T7p7jkIcHArPpNL5qWcI1/LTooxkTM5q2vm5ab6AXsgxlSwc+RkPPH xFH2NeDmEQ2wDAoJkOqLjHy7sNqt+aOFcYYtH2gVxQ6bl2DsIQxory079A4cCkCsMd2q jtmkp7F4opcqkMpzS6ctC47FrJt/EfQtdf925QHqbWoM6IsEEW4tE4dy+CWbSxHnyTMd XB+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m9wB32ba; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n26-20020aa7c45a000000b00452c2e6b1a2si1709506edr.200.2022.09.27.07.11.01; Tue, 27 Sep 2022 07:11:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=m9wB32ba; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232781AbiI0NW2 (ORCPT + 99 others); Tue, 27 Sep 2022 09:22:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232787AbiI0NUZ (ORCPT ); Tue, 27 Sep 2022 09:20:25 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F15232EDD; Tue, 27 Sep 2022 06:18:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id A9B30B81BD8; Tue, 27 Sep 2022 13:18:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C95A3C433D6; Tue, 27 Sep 2022 13:17:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1664284682; bh=9IVkNPq5zmrDv2ai6orRhQNRN0peedtsjrPEDhU8vGI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m9wB32baNxr6T3sTcPBf62IyXCS8C/WVoFnRhwiHJlvDldFZWjBGWRG324LwyVXxe QFlLEu61e1t469BSVtY+joALJferewSNztmUSdK1vHIcib5xCgmGf0TKocazNVlOk1 rm/xjE/Q4eeyIs+n3RFh/khs0f9sXv53BspP/vXI6wSpNFXMcTwnCHnc2J10j1uNzi IO6VHUJO+TzBkQY9pRf1RMx7OS/tnXgGej/wpz+aDyDmKzgyJXpr0aOVQJFE5B94Tx TvnC7KPwwSO3clf3a7EHULnv2dzVv0H6GYrYzRWDIEYbXiuCUdpGSUCpyQhhxSDmTV yjAVK2Dj5PINw== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, patches@lists.linux.dev, Jarkko Sakkinen , Miguel Ojeda , Kees Cook , Alex Gaynor , Finn Behrens , Wedson Almeida Filho , Milan Landaverde , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Subject: [PATCH v10 26/27] samples: add first Rust examples Date: Tue, 27 Sep 2022 15:14:57 +0200 Message-Id: <20220927131518.30000-27-ojeda@kernel.org> In-Reply-To: <20220927131518.30000-1-ojeda@kernel.org> References: <20220927131518.30000-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The beginning of a set of Rust modules that showcase how Rust modules look like and how to use the abstracted kernel features. It also includes an example of a Rust host program with several modules. These samples also double as tests in the CI. Reviewed-by: Kees Cook Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Milan Landaverde Signed-off-by: Milan Landaverde Signed-off-by: Miguel Ojeda --- samples/Kconfig | 2 ++ samples/Makefile | 1 + samples/rust/Kconfig | 30 ++++++++++++++++++++++++ samples/rust/Makefile | 5 ++++ samples/rust/hostprogs/.gitignore | 3 +++ samples/rust/hostprogs/Makefile | 5 ++++ samples/rust/hostprogs/a.rs | 7 ++++++ samples/rust/hostprogs/b.rs | 5 ++++ samples/rust/hostprogs/single.rs | 12 ++++++++++ samples/rust/rust_minimal.rs | 38 +++++++++++++++++++++++++++++++ 10 files changed, 108 insertions(+) create mode 100644 samples/rust/Kconfig create mode 100644 samples/rust/Makefile create mode 100644 samples/rust/hostprogs/.gitignore create mode 100644 samples/rust/hostprogs/Makefile create mode 100644 samples/rust/hostprogs/a.rs create mode 100644 samples/rust/hostprogs/b.rs create mode 100644 samples/rust/hostprogs/single.rs create mode 100644 samples/rust/rust_minimal.rs diff --git a/samples/Kconfig b/samples/Kconfig index 470ee3baf2e1..0d81c00289ee 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -263,6 +263,8 @@ config SAMPLE_CORESIGHT_SYSCFG This demonstrates how a user may create their own CoreSight configurations and easily load them into the system at runtime. +source "samples/rust/Kconfig" + endif # SAMPLES config HAVE_SAMPLE_FTRACE_DIRECT diff --git a/samples/Makefile b/samples/Makefile index 701e912ab5af..9832ef3f8fcb 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -35,3 +35,4 @@ subdir-$(CONFIG_SAMPLE_WATCH_QUEUE) += watch_queue obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak/ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) += coresight/ obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/ +obj-$(CONFIG_SAMPLES_RUST) += rust/ diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig new file mode 100644 index 000000000000..841e0906e943 --- /dev/null +++ b/samples/rust/Kconfig @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: GPL-2.0 + +menuconfig SAMPLES_RUST + bool "Rust samples" + depends on RUST + help + You can build sample Rust kernel code here. + + If unsure, say N. + +if SAMPLES_RUST + +config SAMPLE_RUST_MINIMAL + tristate "Minimal" + help + This option builds the Rust minimal module sample. + + To compile this as a module, choose M here: + the module will be called rust_minimal. + + If unsure, say N. + +config SAMPLE_RUST_HOSTPROGS + bool "Host programs" + help + This option builds the Rust host program samples. + + If unsure, say N. + +endif # SAMPLES_RUST diff --git a/samples/rust/Makefile b/samples/rust/Makefile new file mode 100644 index 000000000000..1daba5f8658a --- /dev/null +++ b/samples/rust/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o + +subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/hostprogs/.gitignore b/samples/rust/hostprogs/.gitignore new file mode 100644 index 000000000000..a6c173da5048 --- /dev/null +++ b/samples/rust/hostprogs/.gitignore @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +single diff --git a/samples/rust/hostprogs/Makefile b/samples/rust/hostprogs/Makefile new file mode 100644 index 000000000000..8ddcbd7416db --- /dev/null +++ b/samples/rust/hostprogs/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +hostprogs-always-y := single + +single-rust := y diff --git a/samples/rust/hostprogs/a.rs b/samples/rust/hostprogs/a.rs new file mode 100644 index 000000000000..f7a4a3d0f4e0 --- /dev/null +++ b/samples/rust/hostprogs/a.rs @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust single host program sample: module `a`. + +pub(crate) fn f(x: i32) { + println!("The number is {}.", x); +} diff --git a/samples/rust/hostprogs/b.rs b/samples/rust/hostprogs/b.rs new file mode 100644 index 000000000000..c1675890648f --- /dev/null +++ b/samples/rust/hostprogs/b.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust single host program sample: module `b`. + +pub(crate) const CONSTANT: i32 = 42; diff --git a/samples/rust/hostprogs/single.rs b/samples/rust/hostprogs/single.rs new file mode 100644 index 000000000000..8c48a119339a --- /dev/null +++ b/samples/rust/hostprogs/single.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust single host program sample. + +mod a; +mod b; + +fn main() { + println!("Hello world!"); + + a::f(b::CONSTANT); +} diff --git a/samples/rust/rust_minimal.rs b/samples/rust/rust_minimal.rs new file mode 100644 index 000000000000..54ad17685742 --- /dev/null +++ b/samples/rust/rust_minimal.rs @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust minimal sample. + +use kernel::prelude::*; + +module! { + type: RustMinimal, + name: b"rust_minimal", + author: b"Rust for Linux Contributors", + description: b"Rust minimal sample", + license: b"GPL", +} + +struct RustMinimal { + numbers: Vec, +} + +impl kernel::Module for RustMinimal { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("Rust minimal sample (init)\n"); + pr_info!("Am I built-in? {}\n", !cfg!(MODULE)); + + let mut numbers = Vec::new(); + numbers.try_push(72)?; + numbers.try_push(108)?; + numbers.try_push(200)?; + + Ok(RustMinimal { numbers }) + } +} + +impl Drop for RustMinimal { + fn drop(&mut self) { + pr_info!("My numbers are {:?}\n", self.numbers); + pr_info!("Rust minimal sample (exit)\n"); + } +} -- 2.37.3