From patchwork Wed Jun 14 18:08:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108115 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp155087vqr; Wed, 14 Jun 2023 11:43:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7CF+ZjYbgT+VCUBlU42nuemw0cJRlLU/ADBqaKLZB4e12tiOEOi9DtO06FCS4Wn4MQmj9E X-Received: by 2002:a17:903:1104:b0:1af:d225:9002 with SMTP id n4-20020a170903110400b001afd2259002mr13684173plh.14.1686768211559; Wed, 14 Jun 2023 11:43:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686768211; cv=none; d=google.com; s=arc-20160816; b=j2cUbhro0Gz8f88iJdTEISxyDqexBwzohY3xRuXGHwiQMkVjK+j15ravSEmYiHLjO2 Xh6y/q/vJEyrQ8kgoF4lelJ2OVjdkzUjOTgupNXYwvlWhdAd5mfZEwXVVEpgkv1/9rvN 3R/1zUwF48fqokrrLy9e8pcKZE2LxjJsOS+mR9mU/diJ3b59PiFSyk4t/xJYqwjinSrY Z0VyNr6ZoWhRneUvoEymwa8gckRO9NULQs6RP2mUMFl8xm+rpivtNqu1LcPQtJeRN1sL tjszl+rnsLvyZzNVHVgItSThP959PFG7hL60+SC91gNS0wEjm0/6V2Rz205P2nzQDQap uLWw== 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=CTWtKIv517YppowslY96Yv4PE7gJ80PnzIMGXW9gwTA=; b=H9ICURbRsHZ3P8YyRNuqth9RajuBxLZpI0PfDKiKvo15BS6/2fqd/sIFDogHzXtG/u Nystw7ROx0YzSS0l81DadPjh9XzDGJWKFW0O6Y2jpiCWqND+TeneVFeV31CDJeUOn90D xCiXLRKzNMFuC34InsKgh5yzhFGUc3GuBkJPFrUEZBENdg1MQx8cYp2uAJKKir9xW5S7 Y4hCF82aVORy6WCOmh1Wc7e/oo3jHDUi3hbWfGzjd6ZTT1uus5bSn6LByufgRoPIvpSY LodlakN/M997FVzqZoXzC1P9W4dtFuqQrjN8h6oD/nBuRP2VoWR4JJQJHAQo1dHoEU1L QtbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ORwQCmDp; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t8-20020a170902bc4800b001a699fc81e8si181758plz.98.2023.06.14.11.43.18; Wed, 14 Jun 2023 11:43:31 -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=ORwQCmDp; 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 S232081AbjFNSKy (ORCPT + 99 others); Wed, 14 Jun 2023 14:10:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241941AbjFNSKi (ORCPT ); Wed, 14 Jun 2023 14:10:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14E9B26BE; Wed, 14 Jun 2023 11:09:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 93BE8643F8; Wed, 14 Jun 2023 18:09:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C196C433C9; Wed, 14 Jun 2023 18:09:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766166; bh=TfRlhBGxo3h+MgvwWv9kx22B1kJhHcF/Z0Gy3SwIz98=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ORwQCmDp2rnw3G5HOz9fnVuS/GOwHPQcQFvCBFSNw9eDVvAkVQ1hMuIkmXUpt3pE2 8GGoIzE4tF32NriWHlgX5x3oVqabZRWwKlgT2OuXJqXqI1Zk1SKVNJEato34GLlHSa r0JdF6XUfxHT0NiwzgBXbCz4uxHtkSTnTRqcYMqhukSp9PzDdYR/lmqJgeU6vfuBqo JNVLwVhIL3JsXMGPqCQJilg/2BMF3Zy9Qc93/16RIgMSplZhwth4YnU4Kls+Tr/xg0 6KqyfPvEWmRxUh2dSypiNIeQLNRV7gJ5XtPO/dJ5BuE9oVPQjTcfDe6mJeqDdNDhAB DcOEFtKNUrBPQ== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 1/6] rust: init: make doctests compilable/testable Date: Wed, 14 Jun 2023 20:08:25 +0200 Message-ID: <20230614180837.630180-2-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768704664126593634?= X-GMAIL-MSGID: =?utf-8?q?1768704664126593634?= Rust documentation tests are going to be build/run-tested with the KUnit integration added in a future patch, thus update them to make them compilable/testable so that we may start enforcing it. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Vincenzo Palazzo Reviewed-by: David Gow Reviewed-by: Björn Roy Baron Reviewed-by: Benno Lossin --- rust/kernel/init.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/rust/kernel/init.rs b/rust/kernel/init.rs index b4332a4ec1f4..1073515ed40e 100644 --- a/rust/kernel/init.rs +++ b/rust/kernel/init.rs @@ -120,14 +120,23 @@ //! `slot` gets called. //! //! ```rust -//! use kernel::{prelude::*, init}; +//! # #![allow(unreachable_pub, clippy::disallowed_names)] +//! use kernel::{prelude::*, init, types::Opaque}; //! use core::{ptr::addr_of_mut, marker::PhantomPinned, pin::Pin}; //! # mod bindings { +//! # #![allow(non_camel_case_types)] //! # pub struct foo; //! # pub unsafe fn init_foo(_ptr: *mut foo) {} //! # pub unsafe fn destroy_foo(_ptr: *mut foo) {} //! # pub unsafe fn enable_foo(_ptr: *mut foo, _flags: u32) -> i32 { 0 } //! # } +//! # trait FromErrno { +//! # fn from_errno(errno: core::ffi::c_int) -> Error { +//! # // Dummy error that can be constructed outside the `kernel` crate. +//! # Error::from(core::fmt::Error) +//! # } +//! # } +//! # impl FromErrno for Error {} //! /// # Invariants //! /// //! /// `foo` is always initialized @@ -158,7 +167,7 @@ //! if err != 0 { //! // Enabling has failed, first clean up the foo and then return the error. //! bindings::destroy_foo(Opaque::raw_get(foo)); -//! return Err(Error::from_kernel_errno(err)); +//! return Err(Error::from_errno(err)); //! } //! //! // All fields of `RawFoo` have been initialized, since `_p` is a ZST. @@ -226,8 +235,7 @@ /// /// ```rust /// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)] -/// # use kernel::{init, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex}; -/// # use macros::pin_data; +/// # use kernel::{init, macros::pin_data, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex}; /// # use core::pin::Pin; /// #[pin_data] /// struct Foo { @@ -277,7 +285,7 @@ macro_rules! stack_pin_init { /// /// # Examples /// -/// ```rust +/// ```rust,ignore /// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)] /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex}; /// # use macros::pin_data; @@ -303,7 +311,7 @@ macro_rules! stack_pin_init { /// pr_info!("a: {}", &*foo.a.lock()); /// ``` /// -/// ```rust +/// ```rust,ignore /// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)] /// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex}; /// # use macros::pin_data; @@ -513,8 +521,7 @@ macro_rules! stack_try_pin_init { /// For instance: /// /// ```rust -/// # use kernel::pin_init; -/// # use macros::pin_data; +/// # use kernel::{macros::pin_data, pin_init}; /// # use core::{ptr::addr_of_mut, marker::PhantomPinned}; /// #[pin_data] /// struct Buf { @@ -841,7 +848,7 @@ macro_rules! init { /// # Examples /// /// ```rust -/// use kernel::{init::PinInit, error::Error, InPlaceInit}; +/// use kernel::{init::{PinInit, zeroed}, error::Error}; /// struct BigBuf { /// big: Box<[u8; 1024 * 1024 * 1024]>, /// small: [u8; 1024 * 1024], From patchwork Wed Jun 14 18:08:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108103 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp154221vqr; Wed, 14 Jun 2023 11:41:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ77oOrzFxl87HSrYKE2P756/3fljU/IXvZE3mbbOvOucV+Sq/bdDuuSw7CEgnjKa23F9psd X-Received: by 2002:a17:902:e314:b0:1b1:b0d6:669c with SMTP id q20-20020a170902e31400b001b1b0d6669cmr9814938plc.18.1686768095022; Wed, 14 Jun 2023 11:41:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686768095; cv=none; d=google.com; s=arc-20160816; b=nhikdEhj84fox7pxpRjUbgSXYMwzjJsy/y8NknRv5fwBB+Bx/RsnuXs3Q/H/gLHv31 pGjg06dcIQ7KO+UZOn9AvdyX/hBUXEof7ElCUd8KvSI1+Dce3zyZuo3jrbz1LkLuZyOs c83ai7xWur6suo3Cwiuf86YJ+OROqu5VEIiC0YAgEjIApKusPyyPRkj2YvuBrSBf7JKs YsiCm+Ymmj3uSzusKKzrsbbKha2VFnGIoU7XjSojf/VZccZ99I1CrhHeHOgF9MNL3AWF Dh1hloxX2ZKKgLmCDcJ2maNfOS87k6cyrZWX+/0NNBX0H9rTsSKMJ0W5pc52sOU3H7Qz bZHw== 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=ipjZXb1VzUHl3jjrHU65YZcrCDgPLWvlgsppHU10GVA=; b=JhRiaERBmrmo9aodi+4aKNoU8pvDFRH+jdF4tVItnH4zTOngX6ERrlB8jkXYEUa2cP jtUKVceWlR3dEC7hXdpZEkH9wOPmbRSa1NzB5UEKTt5ol9BLm4Pye+eNYl79xwfYl5H6 xztQsjumqD3duGdYRkycfI554vA68oObOe5euEov7Qzsd/dBGBvCqSjWQCLihwVthgip lp6R9hM8OFyCmB5BXjyr2Wvor7J8D7jzn2pbptNfxt7eLxfejgdFotXNnXe7NSA/WxpE uxT59sSBEKz0Vqc9J8UqnkERXiUO3bt1niC5zlox9LHxrxpYCWyCoppJhDsNEvW+Pkyq PEew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jp5+Lria; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f17-20020a170903105100b001aad291018fsi3745591plc.350.2023.06.14.11.41.21; Wed, 14 Jun 2023 11:41:35 -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=jp5+Lria; 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 S236270AbjFNSLZ (ORCPT + 99 others); Wed, 14 Jun 2023 14:11:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241008AbjFNSKx (ORCPT ); Wed, 14 Jun 2023 14:10:53 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 330AA2686; Wed, 14 Jun 2023 11:10:23 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DC2A964593; Wed, 14 Jun 2023 18:09:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A76AC433CB; Wed, 14 Jun 2023 18:09:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766170; bh=5RT+1Ed3wlNsUFIiAUAkZSAQ11PIAvj/hYAZC9K3F20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jp5+Lria9yAWeZ9iJIHusdDPiBgUStgV1fZbEIkuGm0YYbXiN2gdl5764EREsXEzj 18FTtiUIgcYWl4Oi1wu1loAaLje/vubVlOBYjTgqvPA3eUTBctLd3cHS8OcRulyh5E K1Z/6A9kX9pacfQa2UCAeVQDnHZm9dJvDviqjtdrWCpex4IzYPToohgXvYwOeCb1hB gD+iCd85RSQKnwREDCJAzRh0OgASYUzVi3Rvk0t+gCje/dyeL5aM27i2ves126vdVu k196MykQcl6X4iLIc06xPHrGbJYp9VIE87x+8Hq6GMXEiQGaRteuzw9INUgXPAMV+e wfis52c4BHgKA== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 2/6] rust: str: make doctests compilable/testable Date: Wed, 14 Jun 2023 20:08:26 +0200 Message-ID: <20230614180837.630180-3-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 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,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768704541876456810?= X-GMAIL-MSGID: =?utf-8?q?1768704541876456810?= Rust documentation tests are going to be build/run-tested with the KUnit integration added in a future patch, thus update them to make them compilable/testable so that we may start enforcing it. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Vincenzo Palazzo Reviewed-by: David Gow Reviewed-by: Björn Roy Baron --- rust/kernel/str.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index c9dd3bf59e34..c41607b2e4fe 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -213,6 +213,7 @@ impl fmt::Display for CStr { /// /// ``` /// # use kernel::c_str; + /// # use kernel::fmt; /// # use kernel::str::CStr; /// # use kernel::str::CString; /// let penguin = c_str!("🐧"); @@ -241,6 +242,7 @@ impl fmt::Debug for CStr { /// /// ``` /// # use kernel::c_str; + /// # use kernel::fmt; /// # use kernel::str::CStr; /// # use kernel::str::CString; /// let penguin = c_str!("🐧"); @@ -529,7 +531,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// # Examples /// /// ``` -/// use kernel::str::CString; +/// use kernel::{str::CString, fmt}; /// /// let s = CString::try_from_fmt(fmt!("{}{}{}", "abc", 10, 20)).unwrap(); /// assert_eq!(s.as_bytes_with_nul(), "abc1020\0".as_bytes()); From patchwork Wed Jun 14 18:08:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108105 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp154381vqr; Wed, 14 Jun 2023 11:41:56 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5oqE9Y+qKzoygN99sSTlZhZqZOcZ3FWM7BPkl5YN6GCN5J+i+wknoJy3w8edmD1VMVIppa X-Received: by 2002:a05:6a20:144c:b0:10e:de4f:3437 with SMTP id a12-20020a056a20144c00b0010ede4f3437mr3235788pzi.39.1686768116686; Wed, 14 Jun 2023 11:41:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686768116; cv=none; d=google.com; s=arc-20160816; b=lYA+DZs+E6Rt/LZIEKAK7ZdrUrwWbF/RkIXPNqNqMGh0rI9hEEOgTu0fsuiJ7FYBB2 yiUsmfiNLW/hFPxpUCzESurX/zaqn/XGWaxj2324zsOR9tqh8awewPFBQAw0WR4sTLNU /bTx6Q69KFwlqucp9dHA1JRFbo0gVkoeD6IkVVN9dfn8huzJq1+mH9dUNukq0o0f3axy lqN/qQOYZv1IKhf0S4d/oWoGHY94wmIdLQ3NUyzafRwMTaA2MQ4zAbVX96HC4qhMOkU2 b7U1xStfdYqMnwBCpBLO35fbshoCn63W1LsA6pM1KthvpEMsGvut3Kmg5M6gB0T4uUk0 nLzA== 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=QpxlNTkur7AAd84KhePz37Gu3znCdCACJGuoJPopUVg=; b=U1E8POyAfDoKo62TP7zcZn9jWA+HGupVzKa4Pyull33HT30xlYhsQZ/RfKr/oWdLvf Eyx9khXT2S+6VYWZJizBk/Dyikq2d4HR40xqaVzxJJmn6h8lLeYz+2iqdHKUdO8uQedo 257CMc44nlC2i4fr+vk6pKW/nN24SNdRbu4WGou6sbjFsKs4rkmTEXS71B3bkeLNDLnk Ag2NJRUXhgiNfimrxhAp0I+ACLzYAme7Lf4Fb34dldkYKzmBVA5xBDdDYRXDI02OYqcn t8sl2CzivQK/1dkjS1vL17sidMNqaQaLNELtEgxZ1ULo2w74tOz39ChVoljqHeNo2iT2 xwHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G38HL9+7; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d24-20020a637358000000b00547b25ea09asi11174107pgn.685.2023.06.14.11.41.44; Wed, 14 Jun 2023 11:41:56 -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=G38HL9+7; 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 S235123AbjFNSK6 (ORCPT + 99 others); Wed, 14 Jun 2023 14:10:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232670AbjFNSKk (ORCPT ); Wed, 14 Jun 2023 14:10:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 345FC2D4F; Wed, 14 Jun 2023 11:09:58 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3491564592; Wed, 14 Jun 2023 18:09:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C6764C433D9; Wed, 14 Jun 2023 18:09:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766174; bh=yuicnU1+K5kamMw4AKK6QIF4bp4V9ExVM3jTIfRBEOw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G38HL9+74I16rMOjwjJnhHjOLlUbn3uZEChcDwRfH6N6PITDqjR2o+2U4crPNMyqj igjbV068IX0zfVweh8fbiZTDv+XKVNNOkyEc/kH5jnA39c4vQ+5sTrKYQxUHHBGF8b fZ9HRRI0vE9qhbla4pFDDIBzLRJo4nlrjbPGGYwgObKFNSCbC4Rfzy5SsGO9PiIYF7 sW/h3vbDpkWLaLzts7tZIS6ouR8ovcnvyFYuh22R2lRyERDvDtEqiHvWtC1KdDNvSa eEXUKCp9wOusXG4/9uyAmks3iBdFx1FB1lc1TYaDFSEAGNXH+L8a1PloQIZcyvhon9 yISoA5LF5C4og== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 3/6] rust: sync: make doctests compilable/testable Date: Wed, 14 Jun 2023 20:08:27 +0200 Message-ID: <20230614180837.630180-4-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768704564253376843?= X-GMAIL-MSGID: =?utf-8?q?1768704564253376843?= Rust documentation tests are going to be build/run-tested with the KUnit integration added in a future patch, thus update them to make them compilable/testable so that we may start enforcing it. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: David Gow Reviewed-by: Björn Roy Baron --- rust/kernel/sync/arc.rs | 9 +++++++-- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index a89843cacaad..1ecb2efab51e 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -73,6 +73,7 @@ /// assert_eq!(cloned.b, 20); /// /// // The refcount drops to zero when `cloned` goes out of scope, and the memory is freed. +/// # Ok::<(), Error>(()) /// ``` /// /// Using `Arc` as the type of `self`: @@ -98,6 +99,7 @@ /// let obj = Arc::try_new(Example { a: 10, b: 20 })?; /// obj.use_reference(); /// obj.take_over(); +/// # Ok::<(), Error>(()) /// ``` /// /// Coercion from `Arc` to `Arc`: @@ -121,6 +123,7 @@ /// /// // `coerced` has type `Arc`. /// let coerced: Arc = obj; +/// # Ok::<(), Error>(()) /// ``` pub struct Arc { ptr: NonNull>, @@ -337,7 +340,7 @@ fn from(item: Pin>) -> Self { /// # Example /// /// ``` -/// use crate::sync::{Arc, ArcBorrow}; +/// use kernel::sync::{Arc, ArcBorrow}; /// /// struct Example; /// @@ -350,12 +353,13 @@ fn from(item: Pin>) -> Self { /// /// // Assert that both `obj` and `cloned` point to the same underlying object. /// assert!(core::ptr::eq(&*obj, &*cloned)); +/// # Ok::<(), Error>(()) /// ``` /// /// Using `ArcBorrow` as the type of `self`: /// /// ``` -/// use crate::sync::{Arc, ArcBorrow}; +/// use kernel::sync::{Arc, ArcBorrow}; /// /// struct Example { /// a: u32, @@ -370,6 +374,7 @@ fn from(item: Pin>) -> Self { /// /// let obj = Arc::try_new(Example { a: 10, b: 20 })?; /// obj.as_arc_borrow().use_reference(); +/// # Ok::<(), Error>(()) /// ``` pub struct ArcBorrow<'a, T: ?Sized + 'a> { inner: NonNull>, diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 923472f04af4..09276fedc091 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -63,6 +63,7 @@ macro_rules! new_mutex { /// assert_eq!(e.c, 10); /// assert_eq!(e.d.lock().a, 20); /// assert_eq!(e.d.lock().b, 30); +/// # Ok::<(), Error>(()) /// ``` /// /// The following example shows how to use interior mutability to modify the contents of a struct diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs index 979b56464a4e..91eb2c9e9123 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -61,6 +61,7 @@ macro_rules! new_spinlock { /// assert_eq!(e.c, 10); /// assert_eq!(e.d.lock().a, 20); /// assert_eq!(e.d.lock().b, 30); +/// # Ok::<(), Error>(()) /// ``` /// /// The following example shows how to use interior mutability to modify the contents of a struct From patchwork Wed Jun 14 18:08:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108117 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp155384vqr; Wed, 14 Jun 2023 11:44:18 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7syfe2gC0C1cbRDrJLpGhaIz4bzzFfx26rHGCjGz02Bc/vw5Q3rmVLTtBifQ62tNoU/aX3 X-Received: by 2002:a05:6a00:15ca:b0:665:bd58:c954 with SMTP id o10-20020a056a0015ca00b00665bd58c954mr3720756pfu.10.1686768257695; Wed, 14 Jun 2023 11:44:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686768257; cv=none; d=google.com; s=arc-20160816; b=Dmz9kpMuRB80s5cos4JwG0CbNNsSKJ1K2elwAVo6XVJ7Kng7Kyx85u7PKwqs156l1G ui6Iygh6qyfQlsV25kxO9+Ofq71LczeGV2RmRM+1LQAc5zvdyFuEvonlaqthZh9awr6G QBK5Isbu19sSW7Zxt92/0BWat5XmrwYxVLMFOlR1zurl5fUGPa7Mr11WCL8ezDrzdXUF 2nIm2gwkDBk+e1pGYyxw8xDn8b//4KQJhLQm73dfvYMNMSQc7Rb+bddxb2FS6GGpjrYF z63nUsWzPdSHk6p1EjjnTQA8akiWR469g00wtzPNzs4SDUp+jmGwNXQlUZunIrjkTVwD 6d0A== 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=hBumrll4Q7alm3AFN39Ok9C2N7fY2B2j8pb2igZiLxo=; b=w7WoF62eR6pnpXtYubpSA3iY92mJJ5m2aWOgVMQbPTHyUr64qphCvaAqdgjaNG/eki bwIEcLN5V90aJrA9Kb5q8KsRnmURdFBhBwqbJcqjuqJRt1rMPjmxt7tGP66UtRez6+10 0/Mr9/6VK2CdySENLqpLIDBEdVPCyWmH3oFGjC41xEXFUDccvtxNP9CPuvBXNLfJ1anj J67HMdK9T72nXgcnT+8ZlPxZUBGxj4ntjlE7MZN/b7+cglbIPxz2+WyqGGcIe+esbvtu H/Ag30P/H8uvXpRGPC2DboG/q228WG4bBJrGCwaMe1aIISewPFVmESSgGKUNqvdlE+kU 6juw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oT2G6znf; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c1-20020a637241000000b005481915c0c6si74018pgn.31.2023.06.14.11.44.03; Wed, 14 Jun 2023 11:44:17 -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=oT2G6znf; 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 S241544AbjFNSL7 (ORCPT + 99 others); Wed, 14 Jun 2023 14:11:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241546AbjFNSLe (ORCPT ); Wed, 14 Jun 2023 14:11:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9981C2D45; Wed, 14 Jun 2023 11:11:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4EADC60B50; Wed, 14 Jun 2023 18:09:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D662C433C9; Wed, 14 Jun 2023 18:09:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766178; bh=ehcluNxu9TSlphXGqNkHOa/SpBNYxUq22Vm5jtRhQQU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oT2G6znf/4h74wnGmSfriDkF2nvXPdzTqMI4UMOv+vWu3KFM9VaVXeGXH0g5Vjy7Z SORLaZWOttT/1lVnc56iIxYDSGEfBO2/dwxvZLgyKUDvn9wS0NJRNaoKliVqZKPruh FC30wTiLC7Tp5k0qFdl8brvltePdc0M3OOlTgOxfrGRizWVOgmldXKqq7mTuV+3ykb 3QpJjdx+pyFADdUWuF989LJjFAPWyTNjfCS1yvN6TPdSfITrg81O41ACS5aEbIDsrM TR7hBTRSuNiDqYDj2va8BL3InVnvLJyTdUmE6QddoICDQFNSI5Rj7bDkBLkB2LZAEu fa3Bs9ZYFGmZQ== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 4/6] rust: types: make doctests compilable/testable Date: Wed, 14 Jun 2023 20:08:28 +0200 Message-ID: <20230614180837.630180-5-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 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,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768704712753857550?= X-GMAIL-MSGID: =?utf-8?q?1768704712753857550?= Rust documentation tests are going to be build/run-tested with the KUnit integration added in a future patch, thus update them to make them compilable/testable so that we may start enforcing it. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: David Gow Reviewed-by: Björn Roy Baron --- rust/kernel/types.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 1e5380b16ed5..696d6c5a3b9d 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -109,7 +109,7 @@ unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} /// In the example below, we have multiple exit paths and we want to log regardless of which one is /// taken: /// ``` -/// # use kernel::ScopeGuard; +/// # use kernel::types::ScopeGuard; /// fn example1(arg: bool) { /// let _log = ScopeGuard::new(|| pr_info!("example1 completed\n")); /// @@ -127,7 +127,7 @@ unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} /// In the example below, we want to log the same message on all early exits but a different one on /// the main exit path: /// ``` -/// # use kernel::ScopeGuard; +/// # use kernel::types::ScopeGuard; /// fn example2(arg: bool) { /// let log = ScopeGuard::new(|| pr_info!("example2 returned early\n")); /// @@ -148,7 +148,7 @@ unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} /// In the example below, we need a mutable object (the vector) to be accessible within the log /// function, so we wrap it in the [`ScopeGuard`]: /// ``` -/// # use kernel::ScopeGuard; +/// # use kernel::types::ScopeGuard; /// fn example3(arg: bool) -> Result { /// let mut vec = /// ScopeGuard::new_with_data(Vec::new(), |v| pr_info!("vec had {} elements\n", v.len())); From patchwork Wed Jun 14 18:08:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108107 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp154433vqr; Wed, 14 Jun 2023 11:42:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Bxsod3uH00JZPCkhV/btB4tB/E2ahlOkvEE63n5NQKFWuh+HYFksXZ+ixtP7uiP4r+YOK X-Received: by 2002:a05:6a20:2590:b0:11d:1d3d:143c with SMTP id k16-20020a056a20259000b0011d1d3d143cmr2182017pzd.56.1686768123498; Wed, 14 Jun 2023 11:42:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686768123; cv=none; d=google.com; s=arc-20160816; b=d1fbmtoO5jChcPA//5gN7yT+KjwL3v1yEfeFeWwDBneFKDwkFAuWnctNgEOGRsE7+n 5d6ke5NwA8xJjlAY0hb902r+2diQ5j+ZOHMceTbBY84zDlTwDNFEEvZmIYYBs0iYQCEi y+EXqLfjZPPF7pOTS47SU5/VHkQITdd2xjmZWbnDV3jjgrEA6exke939mUceVIBvR50/ IIGcyf/Odp67SZwi+YZAB+IlpfU0x2cZwGfoVvFz8HQVr4fv3ZdxOkDuEZmJcCYxr8x5 9rId3+GnEBy8LnTQlu5CVS5XxmXsKUcM8lHgnJDRQkUd/S3Wfrx7+fL/XVezAlJqaN/5 vAww== 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=83+/Cql/6i5oaRqqEK5XDbiDPiAqqKUhuYsEUhwCGgk=; b=jlrfOJCTMZ/RQYRuBimQzpsWC6zhScrmacoRP9CZVlPvTqEx4iz8jAKFHS7qSpDGmU lTUapqF1oc2pPlBtMMhTlRQ+FllIgzwzmFaOnyCdXHiRL8ZwE8L70NPFfaiJ1UykGjAs PRzJkwllEKd9ePZ9zIiH8DAKOKI2GMPHJRje/PwRG8yQW7Bp154SmDvpAYn20GRYRmzE vFuSPSerPoru8r59K5RKDhlqCW5xjKcVok9SlJ1ul1RCbFgBmtsD8yCL+O8YoR0K9aZ6 B2+CtDKoHk44DZOiy+EcCL0KgMQdNRfEiND1Ch6ymXy4lLidcAxqoPr2StyJpG4Mo9xA LCHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HK5ymU1R; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 72-20020a63004b000000b00541a9b822cdsi8252958pga.520.2023.06.14.11.41.50; Wed, 14 Jun 2023 11:42:03 -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=HK5ymU1R; 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 S242310AbjFNSLu (ORCPT + 99 others); Wed, 14 Jun 2023 14:11:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236647AbjFNSLZ (ORCPT ); Wed, 14 Jun 2023 14:11:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6381270A; Wed, 14 Jun 2023 11:10:54 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A5A7B645A7; Wed, 14 Jun 2023 18:09:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3CBB2C433CA; Wed, 14 Jun 2023 18:09:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766183; bh=dAry38HwQqO2PU4cvP48WChrQuS+8IqI/4qxj484H2g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HK5ymU1RRuVIXN+EnYXTBzUiNVXPgPfr7cRryPhPBW65Xz1ryA5kbzICbOQb8GPnB Fjn/8R3z7cD8CAb4sAYf8Kka4sF5KCwvqK02iHj7UIrb2yuX6Od9AbgA3dkROLDtHa k2rQG3TNhznu+NF4ACjep73KEqGhzXbvg6uQc1j/mnuKaOqEjj1R+LLIBZtwYWzcUG 0+zlzWJXI4Lto/e/ZF8hvbcSIzG8hkejVkpQA/BMJGccGNRQhMuUu7tA+CrvWpxkSi V/ECF0tt8lLTxpVZkOKqo6r+aMewqc3TwRIiE4SoRC3IoWN8tqPPAj8GjNTBY0suB2 IPWEoawlQNyJA== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 5/6] rust: support running Rust documentation tests as KUnit ones Date: Wed, 14 Jun 2023 20:08:29 +0200 Message-ID: <20230614180837.630180-6-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768704572203941371?= X-GMAIL-MSGID: =?utf-8?q?1768704572203941371?= Rust has documentation tests: these are typically examples of usage of any item (e.g. function, struct, module...). They are very convenient because they are just written alongside the documentation. For instance: /// Sums two numbers. /// /// ``` /// assert_eq!(mymod::f(10, 20), 30); /// ``` pub fn f(a: i32, b: i32) -> i32 { a + b } In userspace, the tests are collected and run via `rustdoc`. Using the tool as-is would be useful already, since it allows to compile-test most tests (thus enforcing they are kept in sync with the code they document) and run those that do not depend on in-kernel APIs. However, by transforming the tests into a KUnit test suite, they can also be run inside the kernel. Moreover, the tests get to be compiled as other Rust kernel objects instead of targeting userspace. On top of that, the integration with KUnit means the Rust support gets to reuse the existing testing facilities. For instance, the kernel log would look like: KTAP version 1 1..1 KTAP version 1 # Subtest: rust_doctests_kernel 1..59 # Doctest from line 13 ok 1 rust_doctest_kernel_build_assert_rs_0 # Doctest from line 56 ok 2 rust_doctest_kernel_build_assert_rs_1 # Doctest from line 122 ok 3 rust_doctest_kernel_init_rs_0 ... # Doctest from line 150 ok 59 rust_doctest_kernel_types_rs_2 # rust_doctests_kernel: pass:59 fail:0 skip:0 total:59 # Totals: pass:59 fail:0 skip:0 total:59 ok 1 rust_doctests_kernel Therefore, add support for running Rust documentation tests in KUnit. Some other notes about the current implementation and support follow. The transformation is performed by a couple scripts written as Rust hostprogs. Tests using the `?` operator are also supported as usual, e.g.: /// ``` /// # use kernel::{spawn_work_item, workqueue}; /// spawn_work_item!(workqueue::system(), || pr_info!("x"))?; /// # Ok::<(), Error>(()) /// ``` The tests are also compiled with Clippy under `CLIPPY=1`, just like normal code, thus also benefitting from extra linting. The names of the tests are currently automatically generated. This allows to reduce the burden for documentation writers, while keeping them fairly stable for bisection. This is an improvement over the `rustdoc`-generated names, which include the line number; but ideally we would like to get `rustdoc` to provide the Rust item path and a number (for multiple examples in a single documented Rust item). In order for developers to easily see from which original line a failed doctests came from, a KTAP diagnostic line is printed to the log. In the future, we may be able to use a proper KUnit facility to append this sort of information instead. A notable difference from KUnit C tests is that the Rust tests appear to assert using the usual `assert!` and `assert_eq!` macros from the Rust standard library (`core`). We provide a custom version that forwards the call to KUnit instead. Importantly, these macros do not require passing context, unlike the KUnit C ones (i.e. `struct kunit *`). This makes them easier to use, and readers of the documentation do not need to care about which testing framework is used. In addition, it may allow us to test third-party code more easily in the future. However, a current limitation is that KUnit does not support assertions in other tasks. Thus we presently simply print an error to the kernel log if an assertion actually failed. This should be revisited to properly fail the test, perhaps saving the context somewhere else, or letting KUnit handle it. Signed-off-by: Miguel Ojeda Reviewed-by: Alice Ryhl Tested-by: Matt Gilbride Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Vincenzo Palazzo Reviewed-by: David Gow Tested-by: Sergio González Collado --- lib/Kconfig.debug | 13 +++ rust/.gitignore | 2 + rust/Makefile | 29 ++++++ rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 7 ++ rust/kernel/kunit.rs | 156 ++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + scripts/.gitignore | 2 + scripts/Makefile | 4 + scripts/rustdoc_test_builder.rs | 73 ++++++++++++++ scripts/rustdoc_test_gen.rs | 162 ++++++++++++++++++++++++++++++++ 11 files changed, 451 insertions(+) create mode 100644 rust/kernel/kunit.rs create mode 100644 scripts/rustdoc_test_builder.rs create mode 100644 scripts/rustdoc_test_gen.rs diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index ce51d4dc6803..49f5e9c42200 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2911,6 +2911,19 @@ config RUST_BUILD_ASSERT_ALLOW If unsure, say N. +config RUST_KERNEL_KUNIT_TEST + bool "KUnit test for the `kernel` crate" if !KUNIT_ALL_TESTS + depends on RUST && KUNIT=y + default KUNIT_ALL_TESTS + help + This builds the documentation tests of the `kernel` crate + as KUnit tests. + + For more information on KUnit and unit tests in general, + please refer to the KUnit documentation in Documentation/dev-tools/kunit/. + + If unsure, say N. + endmenu # "Rust" endmenu # Kernel hacking diff --git a/rust/.gitignore b/rust/.gitignore index 21552992b401..d3829ffab80b 100644 --- a/rust/.gitignore +++ b/rust/.gitignore @@ -2,6 +2,8 @@ bindings_generated.rs bindings_helpers_generated.rs +doctests_kernel_generated.rs +doctests_kernel_generated_kunit.c uapi_generated.rs exports_*_generated.h doc/ diff --git a/rust/Makefile b/rust/Makefile index 7c9d9f11aec5..e4e8b83752e2 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -27,6 +27,12 @@ endif obj-$(CONFIG_RUST) += exports.o +always-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated.rs +always-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated_kunit.c + +obj-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated.o +obj-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += doctests_kernel_generated_kunit.o + # Avoids running `$(RUSTC)` for the sysroot when it may not be available. ifdef CONFIG_RUST @@ -39,9 +45,11 @@ ifeq ($(quiet),silent_) cargo_quiet=-q rust_test_quiet=-q rustdoc_test_quiet=--test-args -q +rustdoc_test_kernel_quiet=>/dev/null else ifeq ($(quiet),quiet_) rust_test_quiet=-q rustdoc_test_quiet=--test-args -q +rustdoc_test_kernel_quiet=>/dev/null else cargo_quiet=--verbose endif @@ -157,6 +165,27 @@ quiet_cmd_rustdoc_test = RUSTDOC T $< -L$(objtree)/$(obj)/test --output $(objtree)/$(obj)/doc \ --crate-name $(subst rusttest-,,$@) $< +quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $< + cmd_rustdoc_test_kernel = \ + rm -rf $(objtree)/$(obj)/test/doctests/kernel; \ + mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \ + OBJTREE=$(abspath $(objtree)) \ + $(RUSTDOC) --test $(rust_flags) \ + @$(objtree)/include/generated/rustc_cfg \ + -L$(objtree)/$(obj) --extern alloc --extern kernel \ + --extern build_error --extern macros \ + --extern bindings --extern uapi \ + --no-run --crate-name kernel -Zunstable-options \ + --test-builder $(objtree)/scripts/rustdoc_test_builder \ + $< $(rustdoc_test_kernel_quiet); \ + $(objtree)/scripts/rustdoc_test_gen + +%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \ + $(src)/kernel/lib.rs $(obj)/kernel.o \ + $(objtree)/scripts/rustdoc_test_builder \ + $(objtree)/scripts/rustdoc_test_gen FORCE + $(call if_changed,rustdoc_test_kernel) + # We cannot use `-Zpanic-abort-tests` because some tests are dynamic, # so for the moment we skip `-Cpanic=abort`. quiet_cmd_rustc_test = RUSTC T $< diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 3e601ce2548d..0f8d37c31ac2 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -6,6 +6,7 @@ * Sorted alphabetically. */ +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index bb594da56137..49a5e1a4f0ae 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -18,6 +18,7 @@ * accidentally exposed. */ +#include #include #include #include @@ -135,6 +136,12 @@ void rust_helper_put_task_struct(struct task_struct *t) } EXPORT_SYMBOL_GPL(rust_helper_put_task_struct); +struct kunit *rust_helper_kunit_get_current_test(void) +{ + return kunit_get_current_test(); +} +EXPORT_SYMBOL_GPL(rust_helper_kunit_get_current_test); + /* * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type * as the Rust `usize` type, so we can use it in contexts where Rust diff --git a/rust/kernel/kunit.rs b/rust/kernel/kunit.rs new file mode 100644 index 000000000000..3c94efcd7f76 --- /dev/null +++ b/rust/kernel/kunit.rs @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! KUnit-based macros for Rust unit tests. +//! +//! C header: [`include/kunit/test.h`](../../../../../include/kunit/test.h) +//! +//! Reference: + +use core::{ffi::c_void, fmt}; + +/// Prints a KUnit error. +/// +/// Public but hidden since it should only be used from KUnit generated code. +#[doc(hidden)] +pub fn err(args: fmt::Arguments<'_>) { + // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we + // are passing. + #[cfg(CONFIG_PRINTK)] + unsafe { + bindings::_printk( + b"\x013%pA\0".as_ptr() as _, + &args as *const _ as *const c_void, + ); + } +} + +/// Prints a KUnit error. +/// +/// Public but hidden since it should only be used from KUnit generated code. +#[doc(hidden)] +pub fn info(args: fmt::Arguments<'_>) { + // SAFETY: The format string is null-terminated and the `%pA` specifier matches the argument we + // are passing. + #[cfg(CONFIG_PRINTK)] + unsafe { + bindings::_printk( + b"\x016%pA\0".as_ptr() as _, + &args as *const _ as *const c_void, + ); + } +} + +/// Asserts that a boolean expression is `true` at runtime. +/// +/// Public but hidden since it should only be used from generated tests. +/// +/// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit +/// facilities. See [`assert!`] for more details. +#[doc(hidden)] +#[macro_export] +macro_rules! kunit_assert { + ($name:literal, $condition:expr $(,)?) => { + 'out: { + // Do nothing if the condition is `true`. + if $condition { + break 'out; + } + + static LINE: i32 = core::line!() as i32; + static FILE: &'static $crate::str::CStr = $crate::c_str!(core::file!()); + static CONDITION: &'static $crate::str::CStr = $crate::c_str!(stringify!($condition)); + + // SAFETY: FFI call without safety requirements. + let kunit_test = unsafe { $crate::bindings::kunit_get_current_test() }; + if kunit_test.is_null() { + // The assertion failed but this task is not running a KUnit test, so we cannot call + // KUnit, but at least print an error to the kernel log. This may happen if this + // macro is called from an spawned thread in a test (see + // `scripts/rustdoc_test_gen.rs`) or if some non-test code calls this macro by + // mistake (it is hidden to prevent that). + // + // This mimics KUnit's failed assertion format. + $crate::kunit::err(format_args!( + " # {}: ASSERTION FAILED at {FILE}:{LINE}\n", + $name + )); + $crate::kunit::err(format_args!( + " Expected {CONDITION} to be true, but is false\n" + )); + $crate::kunit::err(format_args!( + " Failure not reported to KUnit since this is a non-KUnit task\n" + )); + break 'out; + } + + #[repr(transparent)] + struct Location($crate::bindings::kunit_loc); + + #[repr(transparent)] + struct UnaryAssert($crate::bindings::kunit_unary_assert); + + // SAFETY: There is only a static instance and in that one the pointer field points to + // an immutable C string. + unsafe impl Sync for Location {} + + // SAFETY: There is only a static instance and in that one the pointer field points to + // an immutable C string. + unsafe impl Sync for UnaryAssert {} + + static LOCATION: Location = Location($crate::bindings::kunit_loc { + file: FILE.as_char_ptr(), + line: LINE, + }); + static ASSERTION: UnaryAssert = UnaryAssert($crate::bindings::kunit_unary_assert { + assert: $crate::bindings::kunit_assert {}, + condition: CONDITION.as_char_ptr(), + expected_true: true, + }); + + // SAFETY: + // - FFI call. + // - The `kunit_test` pointer is valid because we got it from + // `kunit_get_current_test()` and it was not null. This means we are in a KUnit + // test, and that the pointer can be passed to KUnit functions and assertions. + // - The string pointers (`file` and `condition` above) point to null-terminated + // strings since they are `CStr`s. + // - The function pointer (`format`) points to the proper function. + // - The pointers passed will remain valid since they point to `static`s. + // - The format string is allowed to be null. + // - There are, however, problems with this: first of all, this will end up stopping + // the thread, without running destructors. While that is problematic in itself, + // it is considered UB to have what is effectively a forced foreign unwind + // with `extern "C"` ABI. One could observe the stack that is now gone from + // another thread. We should avoid pinning stack variables to prevent library UB, + // too. For the moment, given that test failures are reported immediately before the + // next test runs, that test failures should be fixed and that KUnit is explicitly + // documented as not suitable for production environments, we feel it is reasonable. + unsafe { + $crate::bindings::kunit_do_failed_assertion( + kunit_test, + core::ptr::addr_of!(LOCATION.0), + $crate::bindings::kunit_assert_type_KUNIT_ASSERTION, + core::ptr::addr_of!(ASSERTION.0.assert), + Some($crate::bindings::kunit_unary_assert_format), + core::ptr::null(), + ); + } + } + }; +} + +/// Asserts that two expressions are equal to each other (using [`PartialEq`]). +/// +/// Public but hidden since it should only be used from generated tests. +/// +/// Unlike the one in `core`, this one does not panic; instead, it is mapped to the KUnit +/// facilities. See [`assert!`] for more details. +#[doc(hidden)] +#[macro_export] +macro_rules! kunit_assert_eq { + ($name:literal, $left:expr, $right:expr $(,)?) => {{ + // For the moment, we just forward to the expression assert because, for binary asserts, + // KUnit supports only a few types (e.g. integers). + $crate::kunit_assert!($name, $left == $right); + }}; +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 85b261209977..3642cadc34b1 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -34,6 +34,8 @@ pub mod error; pub mod init; pub mod ioctl; +#[cfg(CONFIG_KUNIT)] +pub mod kunit; pub mod prelude; pub mod print; mod static_assert; diff --git a/scripts/.gitignore b/scripts/.gitignore index 6e9ce6720a05..3dbb8bb2457b 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -5,6 +5,8 @@ /kallsyms /module.lds /recordmcount +/rustdoc_test_builder +/rustdoc_test_gen /sign-file /sorttable /target.json diff --git a/scripts/Makefile b/scripts/Makefile index 32b6ba722728..d5a5382e753c 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -9,6 +9,8 @@ hostprogs-always-$(CONFIG_BUILDTIME_TABLE_SORT) += sorttable hostprogs-always-$(CONFIG_ASN1) += asn1_compiler hostprogs-always-$(CONFIG_MODULE_SIG_FORMAT) += sign-file hostprogs-always-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE) += insert-sys-cert +hostprogs-always-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += rustdoc_test_builder +hostprogs-always-$(CONFIG_RUST_KERNEL_KUNIT_TEST) += rustdoc_test_gen always-$(CONFIG_RUST) += target.json filechk_rust_target = $< < include/config/auto.conf @@ -18,6 +20,8 @@ $(obj)/target.json: scripts/generate_rust_target include/config/auto.conf FORCE hostprogs += generate_rust_target generate_rust_target-rust := y +rustdoc_test_builder-rust := y +rustdoc_test_gen-rust := y HOSTCFLAGS_sorttable.o = -I$(srctree)/tools/include HOSTLDLIBS_sorttable = -lpthread diff --git a/scripts/rustdoc_test_builder.rs b/scripts/rustdoc_test_builder.rs new file mode 100644 index 000000000000..e3b7138fb4f9 --- /dev/null +++ b/scripts/rustdoc_test_builder.rs @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Test builder for `rustdoc`-generated tests. +//! +//! This script is a hack to extract the test from `rustdoc`'s output. Ideally, `rustdoc` would +//! have an option to generate this information instead, e.g. as JSON output. +//! +//! The `rustdoc`-generated test names look like `{file}_{line}_{number}`, e.g. +//! `...path_rust_kernel_sync_arc_rs_42_0`. `number` is the "test number", needed in cases like +//! a macro that expands into items with doctests is invoked several times within the same line. +//! +//! However, since these names are used for bisection in CI, the line number makes it not stable +//! at all. In the future, we would like `rustdoc` to give us the Rust item path associated with +//! the test, plus a "test number" (for cases with several examples per item) and generate a name +//! from that. For the moment, we generate ourselves a new name, `{file}_{number}` instead, in +//! the `gen` script (done there since we need to be aware of all the tests in a given file). + +use std::fs::File; +use std::io::{BufWriter, Read, Write}; + +fn main() { + let mut stdin = std::io::stdin().lock(); + let mut body = String::new(); + stdin.read_to_string(&mut body).unwrap(); + + // Find the generated function name looking for the inner function inside `main()`. + // + // The line we are looking for looks like one of the following: + // + // ``` + // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_28_0() { + // fn main() { #[allow(non_snake_case)] fn _doctest_main_rust_kernel_file_rs_37_0() -> Result<(), impl core::fmt::Debug> { + // ``` + // + // It should be unlikely that doctest code matches such lines (when code is formatted properly). + let rustdoc_function_name = body + .lines() + .find_map(|line| { + Some( + line.split_once("fn main() {")? + .1 + .split_once("fn ")? + .1 + .split_once("()")? + .0, + ) + .filter(|x| x.chars().all(|c| c.is_alphanumeric() || c == '_')) + }) + .expect("No test function found in `rustdoc`'s output."); + + // Qualify `Result` to avoid the collision with our own `Result` coming from the prelude. + let body = body.replace( + &format!("{rustdoc_function_name}() -> Result<(), impl core::fmt::Debug> {{"), + &format!("{rustdoc_function_name}() -> core::result::Result<(), impl core::fmt::Debug> {{"), + ); + + // For tests that get generated with `Result`, like above, `rustdoc` generates an `unwrap()` on + // the return value to check there were no returned errors. Instead, we use our assert macro + // since we want to just fail the test, not panic the kernel. + // + // We save the result in a variable so that the failed assertion message looks nicer. + let body = body.replace( + &format!("}} {rustdoc_function_name}().unwrap() }}"), + &format!("}} let test_return_value = {rustdoc_function_name}(); assert!(test_return_value.is_ok()); }}"), + ); + + // Figure out a smaller test name based on the generated function name. + let name = rustdoc_function_name.split_once("_rust_kernel_").unwrap().1; + + let path = format!("rust/test/doctests/kernel/{name}"); + + write!(BufWriter::new(File::create(path).unwrap()), "{body}").unwrap(); +} diff --git a/scripts/rustdoc_test_gen.rs b/scripts/rustdoc_test_gen.rs new file mode 100644 index 000000000000..793885c32c0d --- /dev/null +++ b/scripts/rustdoc_test_gen.rs @@ -0,0 +1,162 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Generates KUnit tests from saved `rustdoc`-generated tests. +//! +//! KUnit passes a context (`struct kunit *`) to each test, which should be forwarded to the other +//! KUnit functions and macros. +//! +//! However, we want to keep this as an implementation detail because: +//! +//! - Test code should not care about the implementation. +//! +//! - Documentation looks worse if it needs to carry extra details unrelated to the piece +//! being described. +//! +//! - Test code should be able to define functions and call them, without having to carry +//! the context. +//! +//! - Later on, we may want to be able to test non-kernel code (e.g. `core`, `alloc` or +//! third-party crates) which likely use the standard library `assert*!` macros. +//! +//! For this reason, instead of the passed context, `kunit_get_current_test()` is used instead +//! (i.e. `current->kunit_test`). +//! +//! Note that this means other threads/tasks potentially spawned by a given test, if failing, will +//! report the failure in the kernel log but will not fail the actual test. Saving the pointer in +//! e.g. a `static` per test does not fully solve the issue either, because currently KUnit does +//! not support assertions (only expectations) from other tasks. Thus leave that feature for +//! the future, which simplifies the code here too. We could also simply not allow `assert`s in +//! other tasks, but that seems overly constraining, and we do want to support them, eventually. + +use std::io::{BufWriter, Read, Write}; +use std::{fs, fs::File}; + +fn main() { + let mut paths = fs::read_dir("rust/test/doctests/kernel") + .unwrap() + .map(|entry| entry.unwrap().path()) + .collect::>(); + + // Sort paths for clarity. + paths.sort(); + + let mut rust_tests = String::new(); + let mut c_test_declarations = String::new(); + let mut c_test_cases = String::new(); + let mut body = String::new(); + let mut last_file = String::new(); + let mut number = 0; + for path in paths { + // The `name` follows the `{file}_{line}_{number}` pattern (see description in + // `scripts/rustdoc_test_builder.rs`). Discard the `number`. + let name = path.file_name().unwrap().to_str().unwrap().to_string(); + + // Extract the `file` and the `line`, discarding the `number`. + let (file, line) = name.rsplit_once('_').unwrap().0.rsplit_once('_').unwrap(); + + // Generate an ID sequence ("test number") for each one in the file. + if file == last_file { + number += 1; + } else { + number = 0; + last_file = file.to_string(); + } + + // Generate a KUnit name (i.e. test name and C symbol) for this test. + // + // We avoid the line number, like `rustdoc` does, to make things slightly more stable for + // bisection purposes. However, to aid developers in mapping back what test failed, we will + // print a diagnostics line in the KTAP report. + let kunit_name = format!("rust_doctest_kernel_{file}_{number}"); + + // Read the test's text contents to dump it below. + body.clear(); + File::open(path).unwrap().read_to_string(&mut body).unwrap(); + + let line = line.parse::().unwrap(); + + use std::fmt::Write; + write!( + rust_tests, + r#"/// Generated `{name}` KUnit test case from a Rust documentation test. +#[no_mangle] +pub extern "C" fn {kunit_name}(__kunit_test: *mut kernel::bindings::kunit) {{ + /// Overrides the usual [`assert!`] macro with one that calls KUnit instead. + #[allow(unused)] + macro_rules! assert {{ + ($cond:expr $(,)?) => {{{{ + kernel::kunit_assert!("{kunit_name}", $cond); + }}}} + }} + + /// Overrides the usual [`assert_eq!`] macro with one that calls KUnit instead. + #[allow(unused)] + macro_rules! assert_eq {{ + ($left:expr, $right:expr $(,)?) => {{{{ + kernel::kunit_assert_eq!("{kunit_name}", $left, $right); + }}}} + }} + + // Many tests need the prelude, so provide it by default. + #[allow(unused)] + use kernel::prelude::*; + + // Display line number so that developers can map the test easily to the source code. + kernel::kunit::info(format_args!(" # Doctest from line {line}\n")); + + {{ + {body} + main(); + }} +}} + +"# + ) + .unwrap(); + + write!(c_test_declarations, "void {kunit_name}(struct kunit *);\n").unwrap(); + write!(c_test_cases, " KUNIT_CASE({kunit_name}),\n").unwrap(); + } + + let rust_tests = rust_tests.trim(); + let c_test_declarations = c_test_declarations.trim(); + let c_test_cases = c_test_cases.trim(); + + write!( + BufWriter::new(File::create("rust/doctests_kernel_generated.rs").unwrap()), + r#"//! `kernel` crate documentation tests. + +const __LOG_PREFIX: &[u8] = b"rust_doctests_kernel\0"; + +{rust_tests} +"# + ) + .unwrap(); + + write!( + BufWriter::new(File::create("rust/doctests_kernel_generated_kunit.c").unwrap()), + r#"/* + * `kernel` crate documentation tests. + */ + +#include + +{c_test_declarations} + +static struct kunit_case test_cases[] = {{ + {c_test_cases} + {{ }} +}}; + +static struct kunit_suite test_suite = {{ + .name = "rust_doctests_kernel", + .test_cases = test_cases, +}}; + +kunit_test_suite(test_suite); + +MODULE_LICENSE("GPL"); +"# + ) + .unwrap(); +} From patchwork Wed Jun 14 18:08:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 108091 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp139846vqr; Wed, 14 Jun 2023 11:13:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5QjNb6qFZfmMcXxXkvBXZB3pen5l+PB2jP0I/wBBRStZ4oPMnIET1UCly285H6/WUH451J X-Received: by 2002:a05:6402:350b:b0:514:9311:e83a with SMTP id b11-20020a056402350b00b005149311e83amr2500024edd.8.1686766380778; Wed, 14 Jun 2023 11:13:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686766380; cv=none; d=google.com; s=arc-20160816; b=kR88t/ym15hqlwQVEdEwGbaYFykc4g7dyuWm+G3KKGFKB+WLvo2uwBBoNsu/TBy2HA EmDrObD1Zhum7s4l9E4eoRk8jHNc6DgV1revDevCyvPpWl79E9NMv9qjwuAe/Ci4c6XW nKFbTDNIR0z3qAjDDk0iAfUGqEypcAhdezJ13cCxKTnI2V1wP6VurRgg+sQ1XQnZQjHT zS+UNRaoA1HzwLm6tsJvRDkDpTCAfRhP3m+M6xyF0mJFCCuKYpMP24IK3lTgduQhibQd rJhL5T6G0ipui2sFMi8Ro/YcB3bc6ylyt+QirRHHa1vCU2wdT7z8EBkFXh2MIzlNK+mY oHUg== 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=Xn0I/Rdv7MBNf0QjF+YMv5KiZrF1ny4O9LJfpM8Uxvs=; b=S4reQKfs9aLIyJM2X92/2legxMiRL7rkVrPHotxevulaqcTYSF1Wvkoe1iCX9OVZrS 0MQ5fvdP8nbx8iQn62mqMcDOLKvPgIhZIJcnlXCjd/czAutg6xJ9zS3pOTLrkh6NVUj7 nCuKFVk/WymLgUloDWPXZIIxrus6gezDXypSmvz3LvwwpmXkqNzdgELguobAwcRxE1Vd pbKinUYfQUabbr4pQaREN5LdW2uN/jKzmD4jkVWDER6B5acYtVe79VkEbZ97iIMMPh7W p6RgarEoc0QMDbQ/jUBN2lIsxgZL/8avst3LNpEFv1Z0vJLSqM2T7fkRPgIqAE/V20kK KGSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QNZyTTxJ; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n25-20020a056402515900b005187ad3094esi2010102edd.403.2023.06.14.11.12.25; Wed, 14 Jun 2023 11:13:00 -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=QNZyTTxJ; 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 S242175AbjFNSLr (ORCPT + 99 others); Wed, 14 Jun 2023 14:11:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242120AbjFNSLT (ORCPT ); Wed, 14 Jun 2023 14:11:19 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2BDA295F; Wed, 14 Jun 2023 11:10:52 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C7384645A6; Wed, 14 Jun 2023 18:09:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8F014C433C0; Wed, 14 Jun 2023 18:09:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686766187; bh=0ayyCNVLvQE2fk7z+0Ei1x6tEumXh4v5ec9O/CPruY8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QNZyTTxJc9utGF/3mf0rAOtZQR+boC8DqmZxwZyydUlpvmlVQXjANtWERlTthIZZY EqDtj5dgu7lZSg3LliI8UAvQT6mPbUbQV+FeTrvi4xGAX+X1tbgRTdeAJVksh54PTW YekVTKn5gPS9uftsbBK83DGujHWAksD7EiHhBvhn1kvXFeNely0QZO8QQFjzJX0s9H uAOPIVhleWc3HBvZXt6/aDd+QbZ4alocbt929MZZEQ8YKA79lgpwTFq72kKaa0Ab4S AfcPHrZfahrJ13TogluIsMD1gld6H6vjja2dhr1m4FrTMxbaLzHOTPrw19lezqcMNX Iu6KyR6h0QDDg== From: Miguel Ojeda To: David Gow , Brendan Higgins , Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor Cc: Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , Philip Li , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH 6/6] MAINTAINERS: add Rust KUnit files to the KUnit entry Date: Wed, 14 Jun 2023 20:08:30 +0200 Message-ID: <20230614180837.630180-7-ojeda@kernel.org> In-Reply-To: <20230614180837.630180-1-ojeda@kernel.org> References: <20230614180837.630180-1-ojeda@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768702744451724776?= X-GMAIL-MSGID: =?utf-8?q?1768702744451724776?= The KUnit maintainers would like to maintain these files on their side too (thanks!), so add them to their entry. With this in place, `scripts/get_maintainer.pl` prints both sets of maintainers/reviewers (i.e. KUnit and Rust) for those files, which is the behavior we are looking for. Signed-off-by: Miguel Ojeda Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Vincenzo Palazzo Reviewed-by: David Gow --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 250518fc70ff..f4c9ce1b685f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11323,6 +11323,8 @@ W: https://google.github.io/kunit-docs/third_party/kernel/docs/ F: Documentation/dev-tools/kunit/ F: include/kunit/ F: lib/kunit/ +F: rust/kernel/kunit.rs +F: scripts/rustdoc_test_* F: tools/testing/kunit/ KERNEL USERMODE HELPER