Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp3176168rwp; Fri, 14 Jul 2023 19:49:15 -0700 (PDT) X-Google-Smtp-Source: APBJJlF4+QDJQav5MkkmKl+mq8tQLtzO13lJ2FoqZKTfgCRGFdm+2vA05H2RnqK/iwhmMvYhIxj8 X-Received: by 2002:a17:906:24f:b0:993:d88e:41e6 with SMTP id 15-20020a170906024f00b00993d88e41e6mr778043ejl.4.1689389355310; Fri, 14 Jul 2023 19:49:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689389355; cv=none; d=google.com; s=arc-20160816; b=wzC/qNEK0ixco6fX+ENld0W5nnk8o64dAVlFmOXhbwjTynX6g61WdCGO18pS6dXDmp p+csqY2C6KBfexUQNRCAomGxrakfb2z5ZITKSu9ApeCelBo+de2Hzh7FKs2QR60q128O TeeFVKufnRlUyzEwGDRBj7z11kcNM+KlYrjO/l3FufZ37VumzaiqcsvrcKKGlgUFQz1g bSsFvoyqDby/115r66z4az+kZy5am3kn5O9z6XIx0aSaD33QsBnvIe9c4ZS+DIl7FT4l 6gzLMYtqyBgRQONoTkw1kRa8MSEvjjlS8Jw/J1P48d5DG/T8tAR7y11YquykHZAYyv3I U9Lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=eHk2tOCqQMTJhAv0OhLX1AImQflejUiinIzgDnEw15o=; fh=Xv1tY5ccBZaHOaai21RORxjszhkLBFOeR3Pkl8bFwWc=; b=0tHQlyHtAjpnorHATq3z2o4fYckZ7roa5hG5AoaSFsvbkrVzwC8T6opIr+JkDxKg0w g8SUad7zhRjy1FpU6U7P5+RPxgMAjyjwut5kKVCLinpY0xsXEHEGOicFNxqCZzUs3HlP dZ9dVnsBE/bquRfP+a/M9dWsJuSFDUTsEEw2UE4Lx6/C32LGY4kHak/LAdUpPcKVNk3I dfkv+hgf3YT9yHqLEnRnkgnDrMOVzIlf4oAlfMpr+X1jh3y9/8zn8ByNWY/60tJLod7A 714ow/zZkS+eCu4no6kHT1zOYPcwM8g6BgXyWxaVMldY7zQOM2GggembEklRq4L83WI4 RhQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=4FfZzhpg; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u26-20020a1709064ada00b009924c43105csi9827143ejt.94.2023.07.14.19.48.51; Fri, 14 Jul 2023 19:49:15 -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=@linutronix.de header.s=2020 header.b=4FfZzhpg; dkim=neutral (no key) header.i=@linutronix.de; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229526AbjGOBR7 (ORCPT + 99 others); Fri, 14 Jul 2023 21:17:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229452AbjGOBR6 (ORCPT ); Fri, 14 Jul 2023 21:17:58 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB59F30F2; Fri, 14 Jul 2023 18:17:56 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1689383874; h=from:from:reply-to:subject:subject: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=eHk2tOCqQMTJhAv0OhLX1AImQflejUiinIzgDnEw15o=; b=4FfZzhpgC69m4IIl+wXu19M/lJvDJ2/smwPCfc9uu2TrAfng9K9zKXo8YDVCPUfKG6pjn6 W0nuuS8sXuXGR5mmsDKiXhv90LkpZ9PltSc4qUT58Y1IyksSP/ofwrrAcqTsJ6AW9s/dh7 ND2f+FwR3HteWdsfTidwRBO7zxuSQ2Z7nD1LRgVVL+Rj2eXqVd6wxxiJj4Sdq8EK3WJ9N6 Opn0wa2B1b90aVAs7vMeyBRlDriY6zzpeBevexoPfHkFr60PlFS9QsSaTOM0CSNHokmMT7 aUYrCSAVaGkwjqlTfLzmLsxOa8P1Sitmj0KVqLCa51+d5QmgMm9Dy5Dp+0YWZg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1689383874; h=from:from:reply-to:subject:subject: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=eHk2tOCqQMTJhAv0OhLX1AImQflejUiinIzgDnEw15o=; b=Mvu8OEbiZ2tqFwZjYX93HcfO5oGZFo+VN91sd5JBlc2gjlhA7jjCtowATAKIw2w7OIvM4F PPg1FFjo8aphc0Ag== To: Asahi Lina , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , John Stultz , Stephen Boyd , Josh Stone , Gaelan Steele , Heghedus Razvan Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, asahi@lists.linux.dev, Asahi Lina Subject: Re: [PATCH v2] rust: time: New module for timekeeping functions In-Reply-To: <20230714-rust-time-v2-1-f5aed84218c4@asahilina.net> References: <20230714-rust-time-v2-1-f5aed84218c4@asahilina.net> Date: Sat, 15 Jul 2023 03:17:53 +0200 Message-ID: <87r0pax9a6.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, 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 Lina! On Fri, Jul 14 2023 at 16:55, Asahi Lina wrote: > +ktime_t rust_helper_ktime_get_real(void) { > + return ktime_get_real(); > +} > +EXPORT_SYMBOL_GPL(rust_helper_ktime_get_real); Colour me confused. But why does this need another export? This just creates yet another layer of duct tape. If it's unsafe from the rust perspective then wrapping it into rust_"unsafe_function" does not make it any better. Why on earth can't you use the actual C interfaces diretly which are already exported? > +use crate::{bindings, pr_err}; > +use core::marker::PhantomData; > +use core::time::Duration; > + > +/// Represents a clock, that is, a unique time source. > +pub trait Clock: Sized {} > + > +/// A time source that can be queried for the current time. I doubt you can read anything else than current time from a time source. At least the C side does not provide time traveling interfaces unless the underlying hardware goes south. > +pub trait Now: Clock { > +impl Instant { > + fn new(nanoseconds: i64) -> Self { > + Instant { > + nanoseconds, > + _type: PhantomData, > + } > + } > + > + /// Returns the time elapsed since an earlier Instant, or > + /// None if the argument is a later Instant. > + pub fn since(&self, earlier: Instant) -> Option { > + if earlier.nanoseconds > self.nanoseconds { > + None > + } else { > + // Casting to u64 and subtracting is guaranteed to give the right > + // result for all inputs, as long as the condition we checked above > + // holds. > + Some(Duration::from_nanos( > + self.nanoseconds as u64 - earlier.nanoseconds as u64, Clever, but any timestamp greater than KTIME_MAX or less than 0 for such a comparison is invalid. I'm too lazy to do the math for you.. > + )) > + } > + } > +} > + > +impl Instant { > + /// Returns the time elapsed since this Instant. > + /// > + /// This is guaranteed to return a positive result, since > + /// it is only implemented for monotonic clocks. > + pub fn elapsed(&self) -> Duration { > + T::now().since(*self).unwrap_or_else(|| { > + pr_err!( > + "Monotonic clock {} went backwards!", > + core::any::type_name::() Can you please write this in one line? + pr_err!("Monotonic clock {} went backwards!", core::any::type_name::() The above is unreadable gunk for no reason. > +/// Contains the various clock source types available to the kernel. > +pub mod clock { > + use super::*; > + > + /// A clock representing the default kernel time source. > + /// > + /// This is `CLOCK_MONOTONIC` (though it is not the only > + /// monotonic clock) and also the default clock used by > + /// `ktime_get()` in the C API. This "(though it is not the only monotonic clock)" phrase is irritating at best. CLOCK_MONOTONIC is well defined as the other CLOCK_* variants. > + /// > + /// This is like `BootTime`, but does not include time > + /// spent sleeping. > + > + pub struct KernelTime; > + > + impl Clock for KernelTime {} > + impl Monotonic for KernelTime {} > + impl Now for KernelTime { > + fn now() -> Instant { > + Instant::::new(unsafe { bindings::ktime_get() }) > + } > + } > + > + /// A clock representing the time elapsed since boot. > + /// > + /// This is `CLOCK_MONOTONIC` (though it is not the only > + /// monotonic clock) and also the default clock used by > + /// `ktime_get()` in the C API. The wonders of copy and pasta... > + /// > + /// This is like `KernelTime`, but does include time > + /// spent sleeping. Can you please expand your editors line wrap limit to the year 2023 standards? This looks like a IBM 2260 terminal. > + /// A clock representing TAI time. > + /// > + /// This clock is not monotonic and can be changed from userspace. > + /// However, it is not affected by leap seconds. I'm not impressed by this at all. Lots of copy and pasta with zero content. I don't see how this is an improvement over the admittedly lousy or non-existant kernel interface documentations. I thought Rust is set out to be better, but obviously it's just another variant of copy & pasta and sloppy wrappers with useless documentation around some admittedly not well documented, but well understood C interfaces. So the right approach to this is: 1) Extend the kernel C-API documentations first if required 2) Build your wrapper so that it can refer to the documentation which was either there already or got refined/added in #1 3) Add one clock per patch with a proper changelog and not some wholesale drop it all. It's well documented in Documentation/process/*, no? Thanks, tglx