Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp987354rdh; Fri, 27 Oct 2023 01:03:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFpW9GNTTng4DD7m8uyu+HAQoKBdEp7au3UtCiExiebxkszOSU7m9spckeNQuCNXIxrevhZ X-Received: by 2002:a05:6830:442a:b0:6be:e553:334 with SMTP id q42-20020a056830442a00b006bee5530334mr2035837otv.7.1698393821633; Fri, 27 Oct 2023 01:03:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698393821; cv=none; d=google.com; s=arc-20160816; b=oB/wxG2qfceSko6AIw6KzgEh1bggCBmfrU6uNWREHv2/Bm6QOdvRluBlbOzDwo2+X2 URp++QmBhbDImqvd5TGi9aLNzKLMOnO6RMz7HmvnmLAyfhUr6Maja2fasnAPCF+qlREE 10e40scU9I1n7TRUCwFnlxCdB2P7n309z9WaMJ8IbKjywkFf7IybvEOTtGR7auOmU5IY QgeGrnxIHerDCq6ZX3/PeTT3xbfUedpRJXJjduBvt1mss9OsqkmWUE/S9YyWVA9y4Y8t SdrCjqo5gZIz++mFfleXhiMr9+eqzX5HtVh1WyuUAsgqSRn4p0vUMFWjIare4/N3j6CW yOCg== 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:dkim-signature :from; bh=28rUFQhaEDsdQWxzMiXwFJiB6uc5xzFDDph6JYWbtHM=; fh=rQ8lT69/Ho5moLGlWyHE08HyOrPe1WyxzcoKMNKou4g=; b=BAqY72wYhSzpp76kypN0Ao0tt0gQvB1AwXeMEHWV4R7dzjXMRtQkDTbz3dyrY1IhqG zeaqagGj1aznAydz54r82RcwKlNAfm6poryCM0kaZbCX1Nxhicj/cf2CFYIHYqf729hG 7NFWIoxqpQcQ1YEqCxiD8UXMYrUMY0/rzATZ9sgvgEAqSUXBxDYuoPnY94CTOyrRp8Bi 78MPb5PPEvGv8yq2ZiF6NXydSxz1RomDieMDyd0zgrS5qnoqwhs5r9/GGJhuGcbRzcr6 ivnE3ZEEmf5cLsFk/rtcwqB2q4bSVZstjZ6ljWxhpnrjB/Yd1+4/tu+nKwe1waKll0GT BWaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kloenk.dev header.s=mail header.b=dXCBI2Mb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id r2-20020a818102000000b005a7fa1592e1si1673944ywf.243.2023.10.27.01.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 01:03:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@kloenk.dev header.s=mail header.b=dXCBI2Mb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=kloenk.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 4CD8480698E7; Fri, 27 Oct 2023 01:02:50 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231388AbjJ0ICi (ORCPT + 99 others); Fri, 27 Oct 2023 04:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjJ0ICf (ORCPT ); Fri, 27 Oct 2023 04:02:35 -0400 Received: from gimli.kloenk.dev (gimli.kloenk.dev [49.12.72.200]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79495111; Fri, 27 Oct 2023 01:02:33 -0700 (PDT) From: Finn Behrens DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kloenk.dev; s=mail; t=1698393750; bh=28rUFQhaEDsdQWxzMiXwFJiB6uc5xzFDDph6JYWbtHM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=dXCBI2MbzckTQiGWSYieVoxuc4bOzN9HKyWyRNRlkVd3tUTM0PZvKn015dDadGsAh ipqfIcbCCS/YP4Fz0Ra0CDRmp3XjG+3z1owXK3a+cCLzoLBcml5tqEgPDzonuOOS+N Q0SLA9xE2AIgr4EWTfibjmHPRzG7EFN1nXpyJAto= To: 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 Date: Fri, 27 Oct 2023 10:02:26 +0200 Message-ID: In-Reply-To: <20231026201855.1497680-1-benno.lossin@proton.me> References: <20231026201855.1497680-1-benno.lossin@proton.me> MIME-Version: 1.0 Content-Type: text/plain 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.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 (fry.vger.email [0.0.0.0]); Fri, 27 Oct 2023 01:02:50 -0700 (PDT) On 26 Oct 2023, at 22:19, Benno Lossin wrote: > 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 defaul= t > 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 declare= d and > -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associat= ed constant > -/// will be generated for each method in the trait, indicating if the = implementor > -/// 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 trai= t with `#[vtable]`. This > +/// attribute generates a `HAS_*` associated constant bool for each me= thod 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 implemen= tation. > +/// This is also the case for traits annotated with `#[vtable]`, but i= n this > +/// case the default implementation will never be executed. The reason= for this > +/// is that the functions will be called through function pointers ins= talled 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 designe= d to be > +/// used on the Rust side, it should not be possible to call the defau= lt > +/// implementation. This is done to ensure that we call the vtable met= hods > +/// through the C vtable, and not through the Rust vtable. Therefore, = the > +/// default implementation should call `kernel::build_error`, which pr= events > +/// 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. > +/// > +/// ```compile_fail > +/// # use kernel::error::VTABLE_DEFAULT_ERROR; > +/// kernel::build_error(VTABLE_DEFAULT_ERROR) > +/// ``` > +/// > +/// note that you might need to import [`kernel::error::VTABLE_DEFAULT= _ERROR`]. > /// > -/// This attribute is not needed if all methods are required. > +/// This macro should not be used when all functions are required. Reviewed-by: Finn Behrens