Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4585569pxb; Tue, 22 Feb 2022 01:54:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxu2M3pPZ32xouq1xQfUQd1w5fSHZ5vSFb4W5LCvVeSoUB9wU44jjwpay7526m7xGy+DEFJ X-Received: by 2002:a05:6a00:ad2:b0:4f1:2734:a3d9 with SMTP id c18-20020a056a000ad200b004f12734a3d9mr9429042pfl.61.1645523646981; Tue, 22 Feb 2022 01:54:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645523646; cv=none; d=google.com; s=arc-20160816; b=may4fdOEZjSpjlPhmoB5axT9Y240WuCmz/SgihSmHWM54dGAoWMZz55XpcwecRPK2f R5Am+vW+RsISiP1XRBL4Y/dA4TOO6ehMMCnJQvRASq+W1n9TeEn0L/1FS6Qh+YBRArOY zbonvOXXp0Von5OdOc8iQZMUIG4AOPcFbJfaSiSHdt4iVcjZQk0RtE92BSFEOgdeBdLY u9OoFvmgjU2uj4yd0sfcdPtttgWW6dpzg90vuVmCCrCWkNPMe9NfucaI2rkJbwsiKba9 sFQOEOYPSQAeZF0WtiubyWeCzXFI8C1N8v4TIe2VHr1pN8pSgYKgGoXLeyk0Su+dd+qK /XVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=/LVQSPM0+gRMDi2FOUGYqJgSwskl7j6aSDk/opAFqC0=; b=IPhibObRnBGb/r1/WQrKV929QO4ZAWzsbHkpNndF7Hh97tEDIveD9uFZs2oHL1+OYg grfs8F+rFprqhAKkkCNYmtm6Dib2R6mKLMC+QaW3TRnjPTuSN+j+5yEgVk663QY41Xd3 6DQIurRiKomq2O9fdq/J4txsMh5VtTD49pXiw6JJs4ZMOCyn+kjSzK5HkbE7Pceg5Mlu zZLVQai82XN9lHlzMPuCLwS7cj3r+1x08Wm3RU1SLqqxkSsDpiw0V+MAV9dpJVoSLzvr TgUEpkj2J0qqG0AeFq0CMj3AgSpBUJR6vzSAxJ7Yod8+afcyUe8YsLZqxcCh95ScP7Wj zNPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=VCYKjD0I; 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=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u10si34973370ple.311.2022.02.22.01.53.52; Tue, 22 Feb 2022 01:54:06 -0800 (PST) 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=@suse.com header.s=susede1 header.b=VCYKjD0I; 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=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbiBVJHI (ORCPT + 99 others); Tue, 22 Feb 2022 04:07:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229463AbiBVJHH (ORCPT ); Tue, 22 Feb 2022 04:07:07 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2A3C91ADD; Tue, 22 Feb 2022 01:06:41 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 50FD21F399; Tue, 22 Feb 2022 09:06:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1645520800; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=/LVQSPM0+gRMDi2FOUGYqJgSwskl7j6aSDk/opAFqC0=; b=VCYKjD0IROJSMzsv6khIE/RrtY9lMiVibJHbTUcNyLUtQVMhbBj7LeKjuRouYBg4kOdfnx qgwvcZEsn2ZI5Rw0aF50c80UPO2wvs1YRRGTnrF8eVOjTJuU6wRl8b8EgSO/m4gFgkFczS mL3wyiZUXeh/hfiefIEsOQrfQtUHveg= Received: from suse.cz (unknown [10.100.224.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id EA392A3B85; Tue, 22 Feb 2022 09:06:37 +0000 (UTC) Date: Tue, 22 Feb 2022 10:06:36 +0100 From: Petr Mladek To: Miguel Ojeda Cc: Linus Torvalds , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Wedson Almeida Filho , Alex Gaynor , Geoffrey Thomas , Finn Behrens , Adam Bratschi-Kaye , Michael Ellerman , Sumera Priyadarsini , Sven Van Asbroeck , Gary Guo , Boris-Chengbiao Zhou , Boqun Feng , Fox Chen , Dan Robertson , Viktor Garske , Dariusz Sosnowski , =?iso-8859-1?B?TOlv?= Lanteri Thauvin , Niklas Mohrin , Gioh Kim , Daniel Xu , Milan Landaverde , Morgan Bartlett , Maciej Falkowski , Jiapeng Chong , Sergey Senozhatsky , Steven Rostedt , John Ogness Subject: Re: [PATCH v4 10/20] rust: add `kernel` crate Message-ID: References: <20220212130410.6901-1-ojeda@kernel.org> <20220212130410.6901-11-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220212130410.6901-11-ojeda@kernel.org> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Sat 2022-02-12 14:03:36, Miguel Ojeda wrote: > From: Wedson Almeida Filho > > The `kernel` crate currently includes all the abstractions that wrap > kernel features written in C. > > These abstractions call the C side of the kernel via the generated > bindings with the `bindgen` tool. Modules developed in Rust should > never call the bindings themselves. > > In the future, as the abstractions grow in number, we may need > to split this crate into several, possibly following a similar > subdivision in subsystems as the kernel itself and/or moving > the code to the actual subsystems. > > diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c > index 82abfaf3c2aa..c042386667f2 100644 > --- a/kernel/printk/printk.c > +++ b/kernel/printk/printk.c > @@ -392,7 +392,10 @@ static struct latched_seq clear_seq = { > /* the maximum size of a formatted record (i.e. with prefix added per line) */ > #define CONSOLE_LOG_MAX 1024 > > -/* the maximum size allowed to be reserved for a record */ > +/* > + * The maximum size allowed to be reserved for a record. > + * Keep in sync with rust/kernel/print.rs. > + */ > #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) What exactly should we keep in sync, please? I see only handling of KERN_* prefix in print.rs. I do not see there any counter part of LOG_LINE_MAX, CONSOLE_LOG_MAX, or PREFIX_MAX. Also note that PREFIX_MAX is the maximal lenght of the prefix printed on console. It is log level + time stamp + caller id. For example: <12>[ 123.632345][ T3260] It seems that print.rs defines max size of the prefix in the printk format where the log level is defined using KERN_* pair of characters. > > #define LOG_LEVEL(v) ((v) & 0x07) > diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs > new file mode 100644 > index 000000000000..dba4ef10c722 > --- /dev/null > +++ b/rust/kernel/print.rs > @@ -0,0 +1,417 @@ > +/// Format strings. > +/// > +/// Public but hidden since it should only be used from public macros. > +#[doc(hidden)] > +pub mod format_strings { > + use crate::bindings; > + > + /// The length we copy from the `KERN_*` kernel prefixes. > + const LENGTH_PREFIX: usize = 2; > + > + /// The length of the fixed format strings. > + pub const LENGTH: usize = 10; I am sorry but I am not familiar with rust. What are these limits 2 and 10 used for, please? I guess that 2 is the size of a single KERN_* identifier. But what is 10? Note that printk() format prefix is typically just a single KERN_* identifier. But there might be more. Well, in practice only the following combination makes sense: KERN_CONT + KERN_. > + /// Generates a fixed format string for the kernel's [`_printk`]. > + /// > + /// The format string is always the same for a given level, i.e. for a > + /// given `prefix`, which are the kernel's `KERN_*` constants. > + /// > + /// [`_printk`]: ../../../../include/linux/printk.h > + const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] { > + // Ensure the `KERN_*` macros are what we expect. > + assert!(prefix[0] == b'\x01'); > + if is_cont { > + assert!(prefix[1] == b'c'); > + } else { > + assert!(prefix[1] >= b'0' && prefix[1] <= b'7'); > + } > + assert!(prefix[2] == b'\x00'); > + > + let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont { > + b"%pA\0\0\0\0\0" > + } else { > + b"%s: %pA\0" > + }; > + > + [ > + prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5], > + suffix[6], suffix[7], > + ] > + } Finally, is there any way to test whether any change in the printk code breaks the rust support? Best Regards, Petr