Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1660139lqp; Sat, 23 Mar 2024 03:52:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW6NnvejGKyP+44ao8FEcw3CdZsymU53V6HPeJwAdI+JDmmqtCDIF9Aub4aTqdNHsN10JX1Sdh788RTguZs3lheROjMsO9TzhVpq6xYyA== X-Google-Smtp-Source: AGHT+IEwSQPrKQWusMd+XIwZs6+9EMmj4EDfPNUtdDd3zeMsLEerQMxr6GBmQOUpNgMyaFYQdq8r X-Received: by 2002:a05:622a:4d0:b0:431:341c:3e1 with SMTP id q16-20020a05622a04d000b00431341c03e1mr2192296qtx.35.1711191140789; Sat, 23 Mar 2024 03:52:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711191140; cv=pass; d=google.com; s=arc-20160816; b=sfsLW115/JKUGEPDCjDhzYH8CrQwJuqB4L9zexN5NM8OHDwVPGvGFTSwbYOh7wFG7i mByzTqC9GgmjFbBMqTc7YmbK8chwqvI6Kqj7LedqHHHcYc/cGRe4XDn5TQ6/2KDpnf33 ZMcfRYKf1zz/xtStAOO1S8buZNdN5FWni3g0eV/p8HfbZuVwaVrTch/wU5GeLgPw0jfk yYj/dSmh6cxyf5QQ5jd3OipMlJdECURyYQr6PQyWVl4o4na6oBwChFA9S0E20SRV+kUt l5hJwZ4ryJ08i2dq13llDwbK80TZQREqcxj12eHdM3Fb1VVvZPdQQHgPYCQ3rYyo8jDH 1hwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=pkgJxujshGHrdAmanmDwjDVhz1xUKbpjmGsOJY8+AlM=; fh=lLUD0Y0lftUliXRu0ABC3iCsqeJl3bPdDmGTUCgf9RI=; b=OK9tXJHSkHpGbtUGvgn5OBQrfIQyz2OBNQAA85iuDrr6kBDFN3yomFUALZAC/x3y6S UyV0Jf6Y5NR3gm3xOsG5t5gaVj0xE9I1Xcdp8qdlL6kgvBJpCq6MQVXWsB5PbemqA0Zm sbsE6mFyRTELPq0iRw/6eiZr5pcE8J7vvLOWmohgJr3s0O3cL9jhXu2Bvui1pgPAeHUx JnQzPtqEfASVa3GKvIGB2X8zjbm55yotyiqhJ4u5DigGHrgoas4MfS9Qi9tAY+y2x5Ai FqwgWLIvfiBpJXphZAQgNkOHobDnk2EF1AmzIiOKW0G5onthUvuyXB3ok5id/lnN3hc6 FdAQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=7jitdd4ym5b4jkxz7i33jwzxwm.protonmail header.b=jJkd9oiZ; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-112306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h19-20020a05622a171300b00430b652c01esi1437567qtk.112.2024.03.23.03.52.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 03:52:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-112306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=7jitdd4ym5b4jkxz7i33jwzxwm.protonmail header.b=jJkd9oiZ; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-112306-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-112306-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 752981C20EF9 for ; Sat, 23 Mar 2024 10:52:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1D75714ABC; Sat, 23 Mar 2024 10:52:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="jJkd9oiZ" Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2090610957; Sat, 23 Mar 2024 10:52:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711191129; cv=none; b=es/pDlJFQ9eFtfuBQDMnu3vpBfwCZX0nVkjjwy9AbRWPmish6xVwZgWfAv2+OQ2niVIZ1MJj5xOy0xDvADJazvN2AfVwLxvjpcztw/ZpUOL71KO9/K/teFMa0WNZKI86zkjCP/8OgD2CLdMzsIXKCOs9IFbdK+U02tcoYRClr1M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711191129; c=relaxed/simple; bh=lDUbweLZTFKaO03LACvqE5G6z4ibhw1S8CeSImz9FQs=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cFIADQ0lxmb3HU/hS8PyPpJXq5E5JsiribH0EX9AUI1lVbgZSjfpu7GKSlL+cKSBBrv9YF6kBXYOOIF125g+ZdqXdcr1hdE3pfFMntJ9gy0UBmR02zsUeVwACZC8mstU8x7ls122ER8FMbTrccKWsDBFfpLbpD9HfcY/ik4QuTo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=jJkd9oiZ; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=7jitdd4ym5b4jkxz7i33jwzxwm.protonmail; t=1711191123; x=1711450323; bh=pkgJxujshGHrdAmanmDwjDVhz1xUKbpjmGsOJY8+AlM=; 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=jJkd9oiZ69Ou2NS4nQHAOkC6/NYnkBCUOuAAFqkRAWVQIASX5+9jm5MmIO/EQ7bbc x7yv2gVUReEe5+QKwt7X+hqhsiQ+yWHzze+J9JmS7wbKhRwMCSVXgPMODAcnWbkawT hSw9d9h4yMFLpaz2YWeqocU9XCFYTQbEmKXl1aVsUE/fz9myzL8eUeIRgNFJeqS7Wo CNBXT7exxSLPKNbRcRDBRWUKfJF6xyNox8H1/gj/QjAsAKxKvtzV5eYIbde40RwXLi OpAym1nenCTvH+Sd0GsO/adsrrmghad3krGqQHOpHXnIPm2Z11V0rG/MofHSXH0wv2 4ZKVcdyA7fenA== Date: Sat, 23 Mar 2024 10:51:57 +0000 To: Andreas Hindborg From: Benno Lossin Cc: Jens Axboe , Christoph Hellwig , Keith Busch , Damien Le Moal , Bart Van Assche , Hannes Reinecke , "linux-block@vger.kernel.org" , Andreas Hindborg , Niklas Cassel , Greg KH , Matthew Wilcox , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Chaitanya Kulkarni , Luis Chamberlain , Yexuan Yang <1182282462@bupt.edu.cn>, =?utf-8?Q?Sergio_Gonz=C3=A1lez_Collado?= , Joel Granados , "Pankaj Raghav (Samsung)" , Daniel Gomez , open list , "rust-for-linux@vger.kernel.org" , "lsf-pc@lists.linux-foundation.org" , "gost.dev@samsung.com" Subject: Re: [RFC PATCH 3/5] rust: block: allow `hrtimer::Timer` in `RequestData` Message-ID: In-Reply-To: <20240313110515.70088-4-nmi@metaspace.dk> References: <20240313110515.70088-1-nmi@metaspace.dk> <20240313110515.70088-4-nmi@metaspace.dk> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 3/13/24 12:05, Andreas Hindborg wrote:> From: Andreas Hindborg >=20 > Signed-off-by: Andreas Hindborg > --- > rust/kernel/block/mq/request.rs | 67 ++++++++++++++++++++++++++++++++- > 1 file changed, 66 insertions(+), 1 deletion(-) >=20 > diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/reque= st.rs > index cccffde45981..8b7f08f894be 100644 > --- a/rust/kernel/block/mq/request.rs > +++ b/rust/kernel/block/mq/request.rs > @@ -4,13 +4,16 @@ > //! > //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h= ) >=20 > +use kernel::hrtimer::RawTimer; > + > use crate::{ > bindings, > block::mq::Operations, > error::{Error, Result}, > + hrtimer::{HasTimer, TimerCallback}, > types::{ARef, AlwaysRefCounted, Opaque}, > }; > -use core::{ffi::c_void, marker::PhantomData, ops::Deref}; > +use core::{ffi::c_void, marker::PhantomData, ops::Deref, ptr::NonNull}; >=20 > use crate::block::bio::Bio; > use crate::block::bio::BioIterator; > @@ -175,6 +178,68 @@ fn deref(&self) -> &Self::Target { > } > } >=20 > +impl RawTimer for RequestDataRef > +where > + T: Operations, > + T::RequestData: HasTimer, > + T::RequestData: Sync, > +{ > + fn schedule(self, expires: u64) { > + let self_ptr =3D self.deref() as *const T::RequestData; > + core::mem::forget(self); > + > + // SAFETY: `self_ptr` is a valid pointer to a `T::RequestData` > + let timer_ptr =3D unsafe { T::RequestData::raw_get_timer(self_pt= r) }; > + > + // `Timer` is `repr(transparent)` > + let c_timer_ptr =3D timer_ptr.cast::(); > + > + // Schedule the timer - if it is already scheduled it is removed= and > + // inserted > + > + // SAFETY: c_timer_ptr points to a valid hrtimer instance that w= as > + // initialized by `hrtimer_init` > + unsafe { > + bindings::hrtimer_start_range_ns( > + c_timer_ptr as *mut _, > + expires as i64, > + 0, > + bindings::hrtimer_mode_HRTIMER_MODE_REL, > + ); > + } > + } > +} > + > +impl kernel::hrtimer::RawTimerCallback for RequestDataRef > +where > + T: Operations, > + T: Sync, Why is this needed? Shouldn't this be `T::RequestData: Sync`? Is the `run` function below executed on a different thread compared to the `schedule` function above? If yes, then `T::RequestData` probably also needs to be `Send`. You also would need to adjust the bounds in the impl above. --=20 Cheers, Benno > + T::RequestData: HasTimer, > + T::RequestData: TimerCallback, > +{ > + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::h= rtimer_restart { > + // `Timer` is `repr(transparent)` > + let timer_ptr =3D ptr.cast::>(); > + > + // SAFETY: By C API contract `ptr` is the pointer we passed when > + // enqueing the timer, so it is a `Timer` embedd= ed in a `T::RequestData` > + let receiver_ptr =3D unsafe { T::RequestData::timer_container_of= (timer_ptr) }; > + > + // SAFETY: The pointer was returned by `T::timer_container_of` s= o it > + // points to a valid `T::RequestData` > + let request_ptr =3D unsafe { bindings::blk_mq_rq_from_pdu(receiv= er_ptr.cast::()) }; > + > + // SAFETY: We own a refcount that we leaked during `RawTimer::sc= hedule()` > + let dref =3D RequestDataRef::new(unsafe { > + ARef::from_raw(NonNull::new_unchecked(request_ptr.cast::>())) > + }); > + > + T::RequestData::run(dref); > + > + bindings::hrtimer_restart_HRTIMER_NORESTART > + } > +} > + > // SAFETY: All instances of `Request` are reference counted. This > // implementation of `AlwaysRefCounted` ensure that increments to the r= ef count > // keeps the object alive in memory at least until a matching reference= count > -- > 2.44.0 >