Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1023483rdh; Fri, 27 Oct 2023 02:25:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG7/P9sAd3x+0f5XXyOj34xmYhdmoTl1ivDLtQ9eT6MAHkebg9/UWk2NvaPBf/Y4vFohd3G X-Received: by 2002:a05:6102:160d:b0:457:b29e:c0de with SMTP id cu13-20020a056102160d00b00457b29ec0demr2413815vsb.21.1698398742613; Fri, 27 Oct 2023 02:25:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698398742; cv=none; d=google.com; s=arc-20160816; b=GcG6aAduLaVzAmLnirCDyELH45y0PqaNKUG+eo6M1AHNsRUSK0kOwWpTrQVg+D7ZJv 0qbZcX7iayjyuOR1NqNgyruGa+1x4fjDTZ0sAfaF+6RVgQ7NF3FIAzu4i/8w/80oE6H2 b8z/FlKKcWZ7fhGXkjC2WT9jJZTr/fyaTU++8QKKmOmNnBDamOkSsLBkOaBCdaHcfglR lApJHR+67GHQMXYkqz8jQmJUV0fzvTEvpfOLRiBO2DvBvbmYapcc2A9PQ5gPHEjprcT7 VlRbDWqF4YMarevhVtJx646qzRdFAcYvUQ5D5y83+OrmGirliwWy5uU/hufS4ujYSyJJ mykg== 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 :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :date:dkim-signature; bh=rAE1unuqAReK5tQe44MDBpN7xsnzUeFVP5X5ExJVFq4=; fh=rmKIjj5LRWMijCd2E4OM3+3rWXjC0rYat3LH3tBjtIc=; b=UX27gVcI9MRQ5qKINSxSW+HnqZQDU6BOafpvhFXxzM9sF9jGQbj7iOINb4H/uhlrCg /z008+F8LvyVfkadxj75PCN9SESsqDtCH6lm4UCI38yD8AlgfoG+IAciDFbrPzHoclNM b5I8DSsRlVNtffpuLJ66NCLAGVUPU5KLxE4KvAffzUmeWVAP38mhQzJopAEOgzpw3LU1 IUwvjk6y0tf7CTa12p8xbCC0s3DWiPU+gxhvGB4y8PDF2q24kBuhciMceg9G1PkcV7gm NGLUhayld6U62ucnLySgjuZ8JcwGHpWsYlIk17Wp24ZutuqGdKHV91OuKtPwECuB3FZ7 dOZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=N5JiH2hU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id k8-20020a252408000000b00da03d94334asi2090550ybk.94.2023.10.27.02.25.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 02:25:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=N5JiH2hU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 59AE383A2BF3; Fri, 27 Oct 2023 02:25:39 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231386AbjJ0JZW (ORCPT + 99 others); Fri, 27 Oct 2023 05:25:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjJ0JZV (ORCPT ); Fri, 27 Oct 2023 05:25:21 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EF82AF; Fri, 27 Oct 2023 02:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1698398714; x=1698657914; bh=rAE1unuqAReK5tQe44MDBpN7xsnzUeFVP5X5ExJVFq4=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=N5JiH2hUwyEGDrzeR9qkEhUVe4QwHUf7F8xhhMIPE5CA7K5mk7zOruR/fINlPJVdR UhBssQ0elNJS0GBfBymd/g3J18UBf2cDLGVBHmeI4JCkFeE8zmNcbRRBVRVPWGbCuq Z7aEKfvef/7WvFnLQkeYH0UawWKQbD4hV5v4U9yqD9LW0HID4hLBB6Rrf/HbQKzm+A v12XXxHnaRwplwLDwdhKFuDmWZMdm8lNxfSobv2hvrYBObWqvGdRtzh8EGGaiZJhk9 Gl+gGhtQqNndWiaVsYJfB50ihlpJEsG+YdYOoXd+iR0v4YbzXb8Jf0uolB/WEfIlUV vvAe5G3+npgqQ== Date: Fri, 27 Oct 2023 09:25:06 +0000 To: Finn Behrens From: Benno Lossin Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo , Asahi Lina , Sven Van Asbroeck , Viktor Garske , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] rust: macros: improve `#[vtable]` documentation Message-ID: In-Reply-To: References: <20231026201855.1497680-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Fri, 27 Oct 2023 02:25:39 -0700 (PDT) On 10/27/23 10:02, Finn Behrens wrote: >=20 >=20 > On 26 Oct 2023, at 22:19, Benno Lossin wrote: >=20 >> Traits marked with `#[vtable]` need to provide default implementations >> for optional functions. The C side represents these with `NULL` in the >> vtable, so the default functions are never actually called. We do not >> want to replicate the default behavior from C in Rust, because that is >> not maintainable. Therefore we should use `build_error` in those default >> implementations. The error message for that is provided at >> `kernel::error::VTABLE_DEFAULT_ERROR`. >> >> Signed-off-by: Benno Lossin >> --- >> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs >> index c42105c2ff96..917a51183c23 100644 >> --- a/rust/macros/lib.rs >> +++ b/rust/macros/lib.rs >> @@ -87,27 +87,48 @@ pub fn module(ts: TokenStream) -> TokenStream { >> /// implementation could just return `Error::EINVAL`); Linux typically = use C >> /// `NULL` pointers to represent these functions. >> /// >> -/// This attribute is intended to close the gap. Traits can be declared= and >> -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associate= d constant >> -/// will be generated for each method in the trait, indicating if the i= mplementor >> -/// has overridden a method. >> +/// This attribute closes that gap. A trait can be annotated with the `= #[vtable]` attribute. >> +/// Implementers of the trait will then also have to annotate the trait= with `#[vtable]`. This >> +/// attribute generates a `HAS_*` associated constant bool for each met= hod in the trait that is set >> +/// to true if the implementer has overridden the associated method. >> +/// >> +/// For a trait method to be optional, it must have a default implement= ation. >> +/// This is also the case for traits annotated with `#[vtable]`, but in= this >> +/// case the default implementation will never be executed. The reason = for this >> +/// is that the functions will be called through function pointers inst= alled in >> +/// C side vtables. When an optional method is not implemented on a `#[= vtable]` >> +/// trait, a NULL entry is installed in the vtable. Thus the default >> +/// implementation is never called. Since these traits are not designed= to be >> +/// used on the Rust side, it should not be possible to call the defaul= t >> +/// implementation. This is done to ensure that we call the vtable meth= ods >> +/// through the C vtable, and not through the Rust vtable. Therefore, t= he >> +/// default implementation should call `kernel::build_error`, which pre= vents >> +/// calls to this function at compile time: > In the future it would be nice to have something like `#[default]` or `#[= optional]` to automatically derive the implementation. I brought this up in the discussion on zulip [1]. But Wedson argued that macros make it more magical and less easy to understand. So for the time being, I just wanted to improve the docs. [1]: https://rust-for-linux.zulipchat.com/#narrow/stream/288089-General/top= ic/.60bool.3A.3Athen.60.20helper/near/395659471 --=20 Cheers, Benno