Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp990091lqh; Sat, 1 Jun 2024 05:05:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8oaymknjfZ+AmvbjpnwPPky7cXyL8v1nT5dGDeDtfwjPhS8rLOwb0qnJP12x0dlMBOeDn4InqDYSUbaMvlcDUqtIq4ygXWz5mhRZuvg== X-Google-Smtp-Source: AGHT+IHXv1kTab8sswdTxJlGUwgtsvkP+RU75/pNBRqCBcI0UQz7mwbAUyen0TC3GqAl8v+DJ7bY X-Received: by 2002:a05:6a21:788a:b0:1af:d1f3:2cb5 with SMTP id adf61e73a8af0-1b26f0e64a4mr5467216637.8.1717243532185; Sat, 01 Jun 2024 05:05:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717243532; cv=pass; d=google.com; s=arc-20160816; b=cHXS5RY2UQ6UpfidfKOjs5PfZfjKQZewk7A2jFHzuOAnZBs+QvHKh4C/7AbFygA0HD dUMvhTrtSHudppLGDWk4lV5HvY4Gjlr8LH8KSjX1ufCaaFjZRb1djn0ZIa8tBcDuVZDo H7CR4KTYg2r35Co14InzPkXEpqEY7QlOYfvBnnEI5c21mzoJlJ8g++ZEBhLlj4RHudH4 RCoOlb7JkckHDGnZG6o1kyOhSFQt0ewLjP9Hwg9+cSG4Lkl23fnJuPjqwcXy7AQVj6ms 1defG2gErFHWdbRt3h84tJCwdjRYWWGMxCpMY8sTw2HV22IuCUXko2GoyroOX60+fwar 0k5g== 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=fnwhnTGCPs27zFajfymGkj5Js3/VS4EavIJ+ATMewR4=; fh=l/PEToSkI6aTmWghg6aZZvJ0DKXCR3tW5enXMnUlsGk=; b=GtdHjdzQiqdMuJXaVYAwxGLfCJCPujc1NuUWsY26uEJXJcCN3zqD8LB/NIckVwUSnJ bk8SwOvWgP8KyGStdp2N38XdS6fSn/clKvOjUB3YT7es9X00Ga7dXm2tAphkNxy5GXyd /d72sZTZRNLxpia1WNcloZxytPvGAkhKbnahHA/+U80+FYalDKC73TwmU671212EvItb Uo9LvWROdkETOMLEu+VIGLxoj0sCn4jI5Ryz9Kxoap/cjxZrdf0SE2fRbEQJX8+x95Ug CnPyji8W5H2MxmXV4Lxr1gKIj8Ncihi+BagtJoNiHaZPOUJxcwUys6Dc+gdFMZwG5fVY /vkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=d4nejng1; 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-197822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-197822-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-702425eeb74si3336086b3a.94.2024.06.01.05.05.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Jun 2024 05:05:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-197822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=d4nejng1; 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-197822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-197822-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 7607FB21F5A for ; Sat, 1 Jun 2024 12:05:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A5FD714F11D; Sat, 1 Jun 2024 12:05:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="d4nejng1" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (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 D6CB914F9D6 for ; Sat, 1 Jun 2024 12:05:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717243517; cv=none; b=HyEJjJmijckmENxvDe6Wj0qRPpzpUVuVBxhzFx0gUISL/RF7wpb+wSsumXyFFGXzQD7El7I82kDEoZbtD27M2Ckb7IsGNyRAxc2qY5cFvmBNjD17uH72W3sfwyaaM26VQdNYqf6vSKC58VWLdbUJ5eVt0o0w0bxJDzOOL7oGfec= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717243517; c=relaxed/simple; bh=DsN/1amsqM8cu1/fMXEEj56TsbKtVvc2uJbDvsjVfW8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mqP+MVqgArdllKnsntcsKsxzw/IttMnNU9R6cqvzsB4RKB9RcIzAyMxnFpnG9OTp+/tKoAsoxwZLXaUe0BovW3xDrXZ7hiHP3MgoXVI1BCcoGmfoBhlgVMfonSxA8yK4m2prrjExOtgvt8QhbZFoGEtWQ6xJRmMP1OIhu9p3kVM= 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=d4nejng1; arc=none smtp.client-ip=185.70.43.16 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=protonmail; t=1717243513; x=1717502713; bh=fnwhnTGCPs27zFajfymGkj5Js3/VS4EavIJ+ATMewR4=; 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=d4nejng1rlokHUAqMxs5ReintutcycKMCAS9EWKJK0Ccjow9OCmWPYQIeAfCBxX7l 0F1osZlIz6OIdlD07QfreNFgpbgyihG/L1mDPHV6u3jtwgzwWpALaWFXJ4TLDSAbtO kSQZVcfov1HeOphYlziBrghMTB035d8VTx8mWYZUN2XbCVgD7u+Ds3e+YSmnTDvivB yl/W9NRjAT4MZcqie38U6+iSp06lvLQiVv2ISRLOXvvti8y7kd5BE7PfmjFAMDnexQ ylSkdj6zfjpP/yyq4JgFA7uCDvIGwsZM02eRqDNptlmH62VAvgoyJO4wcH/KSXqBOY vwXgfKlVFkS8w== Date: Sat, 01 Jun 2024 12:05:10 +0000 To: Andreas Hindborg From: Benno Lossin Cc: Jens Axboe , Christoph Hellwig , Keith Busch , Damien Le Moal , Bart Van Assche , Hannes Reinecke , Ming Lei , "linux-block@vger.kernel.org" , Andreas Hindborg , Wedson Almeida Filho , Greg KH , Matthew Wilcox , Miguel Ojeda , Alex Gaynor , 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 , Niklas Cassel , Philipp Stanner , Conor Dooley , Johannes Thumshirn , =?utf-8?Q?Matias_Bj=C3=B8rling?= , open list , "rust-for-linux@vger.kernel.org" , "lsf-pc@lists.linux-foundation.org" , "gost.dev@samsung.com" Subject: Re: [PATCH v3 1/3] rust: block: introduce `kernel::block::mq` module Message-ID: <696d50c4-a2b0-4c72-890e-be27f48f0fb3@proton.me> In-Reply-To: <87ikysor3q.fsf@metaspace.dk> References: <20240601081806.531954-1-nmi@metaspace.dk> <20240601081806.531954-2-nmi@metaspace.dk> <47a8ce04-3901-49ae-abac-a7d85901f980@proton.me> <87ikysor3q.fsf@metaspace.dk> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: d422cc806e8f74df9d24b8e0498c32173c71b22e 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 01.06.24 13:11, Andreas Hindborg wrote: > Benno Lossin writes: >> On 01.06.24 10:18, Andreas Hindborg wrote: >>> + /// This function is called by the C kernel. A pointer to this fun= ction is >>> + /// installed in the `blk_mq_ops` vtable for the driver. >>> + /// >>> + /// # Safety >>> + /// >>> + /// This function may only be called by blk-mq C infrastructure. >>> + unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk= _mq_hw_ctx) { >>> + T::commit_rqs() >>> + } >>> + >>> + /// This function is called by the C kernel. It is not currently >>> + /// implemented, and there is no way to exercise this code path. >> >> Is it also possible to completely remove it? ie use `None` in the >> VTABLE, or will the C side error? >=20 > No, this pointer is not allowed to be null. It must be a callable > function, hence the stub. It will be populated soon enough when I send > patches for the remote completion logic. Makes sense. [...] >>> +impl TagSet { >>> + /// Try to create a new tag set >>> + pub fn try_new( >>> + nr_hw_queues: u32, >>> + num_tags: u32, >>> + num_maps: u32, >>> + ) -> impl PinInit { >>> + try_pin_init!( TagSet { >>> + // INVARIANT: We initialize `inner` here and it is valid a= fter the >>> + // initializer has run. >>> + inner <- unsafe {kernel::init::pin_init_from_closure(move = |place: *mut Opaque| -> Result<()> { >>> + let place =3D place.cast::()= ; >>> + >>> + // SAFETY: pin_init_from_closure promises that `place`= is writable, and >>> + // zeroes is a valid bit pattern for this structure. >>> + core::ptr::write_bytes(place, 0, 1); >>> + >>> + /// For a raw pointer to a struct, write a struct fiel= d without >>> + /// creating a reference to the field >>> + macro_rules! write_ptr_field { >>> + ($target:ident, $field:ident, $value:expr) =3D> { >>> + ::core::ptr::write(::core::ptr::addr_of_mut!((= *$target).$field), $value) >>> + }; >>> + } >>> + >>> + // SAFETY: pin_init_from_closure promises that `place`= is writable >>> + write_ptr_field!(place, ops, OperationsVTable::= ::build()); >>> + write_ptr_field!(place, nr_hw_queues , nr_hw_queue= s); >>> + write_ptr_field!(place, timeout , 0); // 0 means d= efault which is 30 * HZ in C >>> + write_ptr_field!(place, numa_node , bindings::NUMA= _NO_NODE); >>> + write_ptr_field!(place, queue_depth , num_tags); >>> + write_ptr_field!(place, cmd_size , core::mem::size= _of::().try_into()?); >>> + write_ptr_field!(place, flags , bindings::BLK_MQ_F= _SHOULD_MERGE); >>> + write_ptr_field!(place, driver_data , core::ptr::n= ull_mut::()); >>> + write_ptr_field!(place, nr_maps , num_maps); >> >> Did something not work with my suggestion? >=20 > I did not want to change it if we are rewriting it to `Opaque::init` > in a cycle or two, which I think is a better solution. Ah I was suggesting to do it now, but emulate the `Opaque::init` function (I should have been clearer about that). I tried to do exactly that, but failed to easily implement it, since the fields of `blk_mq_tag_set` include some structs, which of course do not implement `Zeroable`. Instead I came up with the following solution, which I find a lot nicer: pub fn try_new( nr_hw_queues: u32, num_tags: u32, num_maps: u32, ) -> impl PinInit { // SAFETY: `blk_mq_tag_set` only contains integers and pointers, wh= ich all are allowed to be 0. let tag_set: bindings::blk_mq_tag_set =3D unsafe { core::mem::zeroe= d() }; let tag_set =3D bindings::blk_mq_tag_set { ops: OperationsVTable::::build(), nr_hw_queues, timeout: 0, // 0 means default which is 30Hz in C numa_node: bindings::NUMA_NO_NODE, queue_depth: num_tags, cmd_size: core::mem::size_of::().try_into()= ?, flags: bindings::BLK_MQ_F_SHOULD_MERGE, driver_data: core::ptr::null_mut::(), nr_maps: num_maps, ..tag_set }; try_pin_init!(TagSet { inner <- PinInit::<_, error::Error>::pin_chain(Opaque::new(tag_= set), |tag_set| { // SAFETY: we do not move out of `tag_set`. let tag_set =3D unsafe { Pin::get_unchecked_mut(tag_set) }; // SAFETY: `tag_set` is a reference to an initialized `blk_= mq_tag_set`. error::to_result(unsafe { bindings::blk_mq_alloc_tag_set(ta= g_set) }) }), _p: PhantomData, }) } I haven't tested it though, let me know if you have any problems. --- Cheers, Benno