From patchwork Wed Apr 5 20:14:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Almeida X-Patchwork-Id: 79915 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp571241vqo; Wed, 5 Apr 2023 13:28:08 -0700 (PDT) X-Google-Smtp-Source: AKy350Z2ygkw/NWLyjGdUajp9T/NaRhU+Xk0O2Z5nZy3Op2B/H/pB0O39C4Cszxjtt9w0Gy86B8r X-Received: by 2002:aa7:968d:0:b0:627:df8d:350f with SMTP id f13-20020aa7968d000000b00627df8d350fmr6362044pfk.4.1680726488753; Wed, 05 Apr 2023 13:28:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680726488; cv=none; d=google.com; s=arc-20160816; b=Y9UUy614ZBkVgY9nKA9TpvNSr0RqACJ4HecjuIU2Qbo6xkFi1L59LJeLLF/sxccqWj 9Efnl82qDHI+F9dH0v2et/2Wm7QdDu4eNjd7z8K1dxl6hlaLUZOoTWxrMFrVL0bQxrvP EZGj0B3nDRFEir8TLJW5/gsz7Bv2L7ujLdib5lyw7UEZZeWdB5egNNBjj2uhIdZIiD7O SXJqymBm6FC/8ZzRoNG6GI3ZO5NEqqn5gpiJcUSf4Jl2bcjftGkJqZWN6UMILAKdNIJl JXnsSjm+5A/UfszNlzizrsEoEdvYViXbD3WtFolYdpVHVzDHfvY6q+aOc3XLcuLqChCG b3cA== 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=EvjJfp5mgXUJT7wgV8wBAT0V8NGo7uUA/WU1YErWi/0=; b=exB77/pwlD1BklY77sq2v8absVizYvKhz9GUoeRVWzxGN9Ha83deKs9ZTnDKptfALr kkFYdh6BWFeftPU1wveJ2HdPZvrQdntD9PDVxGsAbL9969pyuSBIQNu1Rayo0orBpM1n dDC4L1frJSry28PxRJ7csvvxA29+aeXy4UtHUXeUA5VwldpslYCA/mDIiINY5wDc4Rag wGhaUkD0GsdFCGpQEdYs55iQ6D57BYejUPNKxxhnz3d11S1J0syxJKiUtV+Z9IRt2vDs lCyCf1nI7JZBzgqQ67V5B5xvqFoP2X67v+LmSvnBS5IcOI5GIQ6h+mnrXDd06SkRAQZ/ C17g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=aMR4Oy+K; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j191-20020a638bc8000000b0050fb412dcc2si4091773pge.380.2023.04.05.13.27.52; Wed, 05 Apr 2023 13:28:08 -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=@collabora.com header.s=mail header.b=aMR4Oy+K; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234461AbjDEUPN (ORCPT + 99 others); Wed, 5 Apr 2023 16:15:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234427AbjDEUOr (ORCPT ); Wed, 5 Apr 2023 16:14:47 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B416B59E2; Wed, 5 Apr 2023 13:14:39 -0700 (PDT) Received: from localhost.localdomain (unknown [IPv6:2804:14d:72b4:8284:32a8:8167:f815:2895]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dwlsalmeida) by madras.collabora.co.uk (Postfix) with ESMTPSA id CF81D6603197; Wed, 5 Apr 2023 21:14:36 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1680725678; bh=Msw7QEEtZ+8jJUyb1J6l3LyE9/JSE7aFA2Xyrz/Kgk8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aMR4Oy+KKxBNmZ0CKywbxKCyxfmHAvw+OZpqyGSCE6giSTjWLCxnIF8/Kq9m8mRYi 3AjwJ6PMX56bADWvFvXhAwRbhEaIqFOZb9q+OpBGGdPwFpUHXsFicCNv3ghXnsIzz9 gqxk1Rv2UwJ1KCU2Gpg0KdhYQoIfBD4EDGwHGBdJyN4uzJNMpWqN1m6AkCbmirvIN0 f79FUdQrEt3VRydz2Z/EJSQPNR9pk7OQu389uyBGU+TkmkPXElnsJDx+kUEQ/Xx2PS fFYL8mVVo2Gi9ji6GzIKY3PgxILygTY6Q5TChsrV7p6SkXwrCTkDLCqMVj6QD7OEU0 nfKIVafuEwGAw== From: Daniel Almeida To: wedsonaf@gmail.com, ojeda@kernel.org Cc: Daniel Almeida , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v2 1/2] rust: add scatterlist support Date: Wed, 5 Apr 2023 17:14:15 -0300 Message-Id: <20230405201416.395840-2-daniel.almeida@collabora.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405201416.395840-1-daniel.almeida@collabora.com> References: <20230405201416.395840-1-daniel.almeida@collabora.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762369458487024733?= X-GMAIL-MSGID: =?utf-8?q?1762369458487024733?= This patch adds a scatterlist abstraction. It is then used and tested by the new rust virtio sample module. Signed-off-by: Daniel Almeida --- rust/kernel/lib.rs | 1 + rust/kernel/scatterlist.rs | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 rust/kernel/scatterlist.rs diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index c20b37e88ab2..b23be69919cc 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -71,6 +71,7 @@ pub mod net; pub mod pages; pub mod power; pub mod revocable; +pub mod scatterlist; pub mod security; pub mod task; pub mod workqueue; diff --git a/rust/kernel/scatterlist.rs b/rust/kernel/scatterlist.rs new file mode 100644 index 000000000000..fe036af13995 --- /dev/null +++ b/rust/kernel/scatterlist.rs @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Scatterlist abstractions. +//! +//! C header: [`include/linux/virtio.h`](../../../../include/linux/scatterlist.h) + +use core::cell::UnsafeCell; + +/// Scatterlist abstraction over the C side `struct scatterlist`. +pub struct Scatterlist { + /// The C side `struct scatterlist`. + inner: UnsafeCell, +} + +impl Scatterlist { + /// A wrapper over the C-side `sg_init_one()`. Initialize a single entry sg + /// list. + /// + /// # Safety + /// + /// Caller must ensure that `buf` is valid and `buflen` really + /// represents the size of `buf`. + pub unsafe fn init_one(buf: *const core::ffi::c_void, buflen: u32) -> Self { + // SAFETY: There are no references or function pointers in this + // `Scatterlist`. + let mut sg: Scatterlist = unsafe { core::mem::zeroed() }; + // SAFETY: we pass a valid sg entry, which points to stack-allocated + // variable above. `buf` and `buflen` are presumed valid as per this + // function's safety requirements. + unsafe { + bindings::sg_init_one(sg.inner.get_mut(), buf, buflen); + } + + sg + } + + pub(crate) fn inner(&self) -> &UnsafeCell { + &self.inner + } +}