Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp1893298rwb; Fri, 5 Aug 2022 09:26:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR7bN1puJrshHlQJuPDQCiDJXM+zG0SNPbbUs0B12A9cwKcGeNnvrGIWPCFAjGB31aYMvs6e X-Received: by 2002:a17:90a:ead3:b0:1f3:366d:5005 with SMTP id ev19-20020a17090aead300b001f3366d5005mr17075013pjb.1.1659716795628; Fri, 05 Aug 2022 09:26:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659716795; cv=none; d=google.com; s=arc-20160816; b=vyb/LHuRqdYvUcVFw0GuKBLmcWM+5jpxyncaTPoY0vSoYEYNLG0z0Dk2vYW5UfUACO yVePsnC2b3v1M/vJ3oBmZdQ6AGHV32V/TgyHP9FidnDLkFtzaAMq+ibBk+RgW98Mm74+ DsZWdCH+YUm8snpueN0d1VOjQDth4M8CvY9d52Y9jkOx/+Nggg6/1iPDOMSRHKFr2JwJ t+AbosvyuAbTIAewOAwgZag2EAz2Ee5tWiSwbvptUCH1RcVz+3svw6vsJH2JAo4ZxRTF NWBwGCMXTqpC0q0TynwbybTeqER2q+tlLFNdIQxZy/pupVaOFS6wavOfTFb/w0nARFWj DlnA== 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=+X+S0InyVddjxCJuhmydMqG2oJFCOzPWeoM0cRYWBuk=; b=GANATRetNJpVNaXbyIHL9A9fe+kxoCKR15NIMOK6SrvD2c1nEAUWvFy9O4dd3SI6Ny jekaE5DfYqTP+osyEm9C6OZzy65m85tCBEw1ogbRghMebz1/yI12uShwu3GI5U9IyDnt olJk6kPBxV6gfgaFwEd/SdAb/boyLRtXXsOX9dPanU1h8fjAxnj8udtULI2jG88d83ap /ICqtgOPjSIm0Zm7+fepnCw0eaUYw+gOXPh4QIMUGRs9EQ4r8paRYu2nE4KdZ1pvBUsJ Ych8keAn5Z7inwVXXhLmqfotrTOaYpQVgiZmvwW61lZzi1/h46xQ5Zu8FxMter0qAIUR cQ6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KbdD3UH6; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pq5-20020a17090b3d8500b001f617f409f9si1019106pjb.118.2022.08.05.09.26.20; Fri, 05 Aug 2022 09:26: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=KbdD3UH6; 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 S240932AbiHEPoB (ORCPT + 99 others); Fri, 5 Aug 2022 11:44:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241139AbiHEPnd (ORCPT ); Fri, 5 Aug 2022 11:43:33 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96FE86611B; Fri, 5 Aug 2022 08:43:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 1EFB8B82987; Fri, 5 Aug 2022 15:43:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F810C433C1; Fri, 5 Aug 2022 15:43:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659714196; bh=7yjY5Y6g1qtoUnDrqwaXLYugm5cDoSo3+7AjfeIMPHY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KbdD3UH6sLyDQiKBd++2wnIlTMwEfJCJ4b2NcsrkLtz0MpyHm9ewKLpmzqsACnBGi MHEQ6V8CPSKs5dBG5U8KR2894aZKZxPxHgvTNFN2S0GS5wr9e6h1aX69poQ532HDJ1 ZswIfAHkOO0XYVMBfYmGup6+U82B6dJPJ/PprlTqIjgdktkoNrvksZQu6oo/7AHMfw cp5KlyJKat+hmXXEhdqkYDAgH9xAI+7rfmm9hmHlYKazlanTY/mFd3tjNk7f2IUi9M Khc99oey1BXZzoXobM7HhYtrTqrdHa0ouDp8vKPWZ9rQ/43JGHiifcUshzKhy8gw2e FrDYek0c4xd5A== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, patches@lists.linux.dev, Jarkko Sakkinen , Miguel Ojeda , Alex Gaynor , Geoffrey Thomas , Wedson Almeida Filho , Sven Van Asbroeck , Gary Guo , Boqun Feng , Maciej Falkowski , Wei Liu , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Subject: [PATCH v9 06/27] rust: add C helpers Date: Fri, 5 Aug 2022 17:41:51 +0200 Message-Id: <20220805154231.31257-7-ojeda@kernel.org> In-Reply-To: <20220805154231.31257-1-ojeda@kernel.org> References: <20220805154231.31257-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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 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 This source file contains forwarders to C macros and inlined functions. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Geoffrey Thomas Signed-off-by: Geoffrey Thomas Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Co-developed-by: Sven Van Asbroeck Signed-off-by: Sven Van Asbroeck Co-developed-by: Gary Guo Signed-off-by: Gary Guo Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Co-developed-by: Maciej Falkowski Signed-off-by: Maciej Falkowski Co-developed-by: Wei Liu Signed-off-by: Wei Liu Signed-off-by: Miguel Ojeda --- rust/helpers.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 rust/helpers.c diff --git a/rust/helpers.c b/rust/helpers.c new file mode 100644 index 000000000000..b4f15eee2ffd --- /dev/null +++ b/rust/helpers.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Non-trivial C macros cannot be used in Rust. Similarly, inlined C functions + * cannot be called either. This file explicitly creates functions ("helpers") + * that wrap those so that they can be called from Rust. + * + * Even though Rust kernel modules should never use directly the bindings, some + * of these helpers need to be exported because Rust generics and inlined + * functions may not get their code generated in the crate where they are + * defined. Other helpers, called from non-inline functions, may not be + * exported, in principle. However, in general, the Rust compiler does not + * guarantee codegen will be performed for a non-inline function either. + * Therefore, this file exports all the helpers. In the future, this may be + * revisited to reduce the number of exports after the compiler is informed + * about the places codegen is required. + * + * All symbols are exported as GPL-only to guarantee no GPL-only feature is + * accidentally exposed. + */ + +#include +#include + +__noreturn void rust_helper_BUG(void) +{ + BUG(); +} +EXPORT_SYMBOL_GPL(rust_helper_BUG); + +/* + * 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 + * expects a `usize` like slice (array) indices. `usize` is defined to be + * the same as C's `uintptr_t` type (can hold any pointer) but not + * necessarily the same as `size_t` (can hold the size of any single + * object). Most modern platforms use the same concrete integer type for + * both of them, but in case we find ourselves on a platform where + * that's not true, fail early instead of risking ABI or + * integer-overflow issues. + * + * If your platform fails this assertion, it means that you are in + * danger of integer-overflow bugs (even if you attempt to remove + * `--size_t-is-usize`). It may be easiest to change the kernel ABI on + * your platform such that `size_t` matches `uintptr_t` (i.e., to increase + * `size_t`, because `uintptr_t` has to be at least as big as `size_t`). + */ +static_assert( + sizeof(size_t) == sizeof(uintptr_t) && + __alignof__(size_t) == __alignof__(uintptr_t), + "Rust code expects C `size_t` to match Rust `usize`" +); -- 2.37.1