Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2305245imn; Mon, 1 Aug 2022 20:03:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tK8bM/fiAHVzNHgKT2pp8WxV5PP1KY73Q70jOcRtK0iv8t86ukxUQ5dlMvJ5SezMTkb5sQ X-Received: by 2002:a65:6b95:0:b0:408:9d1b:2e56 with SMTP id d21-20020a656b95000000b004089d1b2e56mr15104503pgw.381.1659409425488; Mon, 01 Aug 2022 20:03:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659409425; cv=none; d=google.com; s=arc-20160816; b=eOVSyljWnLpc/XLooLxbdScFm9zmuO5Rb9MRxxYMBn6Zmzz9ncWnTWTESBjrRzt7b/ WfQufoPmyyIsGFxtW7eFEQqCvRngbxHxfLWG8DpHkTjQrroLIl1Wj0XQ/gjdjzXivh60 sSdaFVQXKGRp003WItAp7KubZl8Pqcev5pELdiljfzja/HxWCt9VHJsYMG9alyItWHfp IXdfc2f0PtYdGxf1aQJEAT2I1HYtuj1JA+OnhqB8jn8iHeC+ZRGdc3X+w3MgVZL/BrB2 jLG3jhoYDDeEo2rBKaUVQcyj34e7geYg0Pt84g5c4cWDLT2Jy19VsDQKnyStNntdVfpN fdWw== 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=tTq0SNOIhlYCGMv3lSTGqYDtWQB+o9/JSZzDE7Vsk08=; b=TOfHrrghC4lvEwB6QCkrr/s7dlnq31/ALOqPomEkOX2QWwxSw6lty+c/+VsByDKFwC wFrwbF9zaYhIdA2CVmFxVTzbT59OdyDwRLF+g/mSubwuXTa5fCkPrUpxKkRSb3ND6YoS 0ydmSkDxUGCNQ8DSeFBBmfYjYeLKqMnm5GbupsjZJpfwcim2MSXm5ixSR6rdTjXy4T04 dOmohFIEe1FQLPd3fUr3T1e2OFo2l4WmFf5wjel8f204IL4kaCsZ4/+KEDZ33P2R33vD AedHBIj3mZUUT83PvdqBylkL0s5vnfqrLT2Td3jczFDvG8d48HmAbKqie4UZn5ekTcpt AFAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Xc7BQytn; 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 np18-20020a17090b4c5200b001ecfe8d871csi18729080pjb.24.2022.08.01.20.03.30; Mon, 01 Aug 2022 20:03:45 -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=Xc7BQytn; 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 S235508AbiHBBxr (ORCPT + 99 others); Mon, 1 Aug 2022 21:53:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235619AbiHBBxV (ORCPT ); Mon, 1 Aug 2022 21:53:21 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 594E6481FB; Mon, 1 Aug 2022 18:52:37 -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 8949DB81913; Tue, 2 Aug 2022 01:52:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D8D5BC433D6; Tue, 2 Aug 2022 01:52:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659405154; bh=lhMljuujdVd5PTPW7PSgX+3MAMFgmyobkMe+iKIw06c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xc7BQytn42BuiFgqiLnoqOi0c1Bp+l1wyI3ucpODkPNlUsY3y55VcISDZVg019NVZ CyrNCUxBR6SMqXAfmKd6UEMMJ8DEGw6BdrldJ/uu9hzBghL39btt+Jflce+wKDnPGB 5BFBcIb1CfY+yWfxj8twTScTNw7IxedN75rcvpBAklCWsAjPPBAWHrgPya+oO95q8g 0R21cRPZjN1yA+fbH6FU+WIuFPpBWJgT6hlDKJmxUpEbYLUsn4lzrNjjChs3rze8Nu TLQSjv9bB0wzj5jMWleqFBJJzkB+jSf5/AhDXROYIobde2rHGxu3VAJsDB4WKGZ3yo s3bD577byoBKA== From: Miguel Ojeda To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Jarkko Sakkinen , Miguel Ojeda , Alex Gaynor , Finn Behrens , Wedson Almeida Filho , Sven Van Asbroeck , Gary Guo , Maciej Falkowski , Jiapeng Chong , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= Subject: [PATCH v8 15/31] rust: add `bindings` crate Date: Tue, 2 Aug 2022 03:50:02 +0200 Message-Id: <20220802015052.10452-16-ojeda@kernel.org> In-Reply-To: <20220802015052.10452-1-ojeda@kernel.org> References: <20220802015052.10452-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 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 crate contains the bindings to the C side of the kernel. Calling C (in general, FFI) is assumed to be unsafe in Rust and, in many cases, this is accurate. For instance, virtually all C functions that take a pointer are unsafe since, typically, it will be dereferenced at some point (and in most cases there is no way for the callee to check its validity beforehand). Since one of the goals of using Rust in the kernel is precisely to avoid unsafe code in "leaf" kernel modules (e.g. drivers), these bindings should not be used directly by them. Instead, these bindings need to be wrapped into safe abstractions. These abstractions provide a safe API that kernel modules can use. In this way, unsafe code in kernel modules is minimized. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens 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: Maciej Falkowski Signed-off-by: Maciej Falkowski Co-developed-by: Jiapeng Chong Signed-off-by: Jiapeng Chong Co-developed-by: Björn Roy Baron Signed-off-by: Björn Roy Baron Signed-off-by: Miguel Ojeda --- rust/bindings/bindings_helper.h | 49 ++++++++++++++++++++++++++++ rust/bindings/lib.rs | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 rust/bindings/bindings_helper.h create mode 100644 rust/bindings/lib.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h new file mode 100644 index 000000000000..284793085d55 --- /dev/null +++ b/rust/bindings/bindings_helper.h @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Header that contains the code (mostly headers) for which Rust bindings + * will be automatically generated by `bindgen`. + * + * Sorted alphabetically. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* `bindgen` gets confused at certain things. */ +const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL; +const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO; +const __poll_t BINDINGS_EPOLLIN = EPOLLIN; +const __poll_t BINDINGS_EPOLLOUT = EPOLLOUT; +const __poll_t BINDINGS_EPOLLERR = EPOLLERR; +const __poll_t BINDINGS_EPOLLHUP = EPOLLHUP; + +const loff_t BINDINGS_MAX_LFS_FILESIZE = MAX_LFS_FILESIZE; diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs new file mode 100644 index 000000000000..7fdd74e68b60 --- /dev/null +++ b/rust/bindings/lib.rs @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Bindings. +//! +//! Imports the generated bindings by `bindgen`. +//! +//! This crate may not be directly used. If you need a kernel C API that is +//! not ported or wrapped in the `kernel` crate, then do so first instead of +//! using this crate. + +#![no_std] +#![feature(core_ffi_c)] +// See . +#![cfg_attr(test, allow(deref_nullptr))] +#![cfg_attr(test, allow(unaligned_references))] +#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))] +#![allow( + clippy::all, + missing_docs, + non_camel_case_types, + non_upper_case_globals, + non_snake_case, + improper_ctypes, + unreachable_pub, + unsafe_op_in_unsafe_fn +)] + +mod bindings_raw { + // Use glob import here to expose all helpers. + // Symbols defined within the module will take precedence to the glob import. + pub use super::bindings_helper::*; + include!(concat!( + env!("OBJTREE"), + "/rust/bindings/bindings_generated.rs" + )); +} + +// When both a directly exposed symbol and a helper exists for the same function, +// the directly exposed symbol is preferred and the helper becomes dead code, so +// ignore the warning here. +#[allow(dead_code)] +mod bindings_helper { + // Import the generated bindings for types. + use super::bindings_raw::*; + include!(concat!( + env!("OBJTREE"), + "/rust/bindings/bindings_helpers_generated.rs" + )); +} + +pub use bindings_raw::*; + +pub const GFP_KERNEL: gfp_t = BINDINGS_GFP_KERNEL; +pub const __GFP_ZERO: gfp_t = BINDINGS___GFP_ZERO; +pub const __GFP_HIGHMEM: gfp_t = ___GFP_HIGHMEM; + +pub const MAX_LFS_FILESIZE: loff_t = BINDINGS_MAX_LFS_FILESIZE; -- 2.37.1