Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1226671rwd; Wed, 7 Jun 2023 13:02:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ565j06WkYJp68mfS6hARH8+qv9vgETsUbtso0Mn4hTv654N6YQ52Y8Nu+ZO/KuRYpBIyGd X-Received: by 2002:a17:902:dacc:b0:1b1:7336:2645 with SMTP id q12-20020a170902dacc00b001b173362645mr3231983plx.60.1686168143708; Wed, 07 Jun 2023 13:02:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686168143; cv=none; d=google.com; s=arc-20160816; b=APR3xGtYVxTXjr7fX6woLTTDZzrFpUy4vIjrfLS8iBpNQxuhNTQUtSo/5bBVu7SMA4 KdS+PTaxc3We8G3hET1jPJx+nrjtEZRBHP+qQRX2hivnGO/4Y4aqjZ0fdrff2Shmgmfv do8TIim7a5FqJtUMg1ODMIY2LkpUNgj1gzeIVhNqK6z95kMGgRAS/1bg1U42RypwGbGt AmpM/Ev8v2EK1QoqlbEW/pBCswHhKFpoZ3B427//bA/zyENUYxBk6ExHP8obHyW3Zs1V Mu6GmcyY8Xmt2Ov90jENur2024ji39vTwNK4XO6feTTW3HyI0qFJ5/CfZ4vwr6nRfshS eZpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=/oF4AC5xHgg8LwfnEbJTH2/uVZSgF0m82b8cRGQMkBc=; b=OIAWCnsIkjj2k6wWGNoMzHgjG11kYh2LROPNr33F0buRxZWzJL92uTpXlSY2aoXtuk uB4D/HKUbU43vHbPNLkdTs01qLBfhfbvJvNx/zg68qjtBDB1qVbLO3t56Qh05JnFPQT1 Zk4UqEQqUWVXTzbQ/RyO939KT9OUhnrP7s74WrZXnCIYk1gvcvBMWGrFGB+jt3rmJJAF d5RT24uYXberix+dUJt8E50Wghzl0GKqXTDcQD6fG52kNFKscLw7xS8epYNJeVP2Twwi 7H1eJRizcPns4uZ8jgCO3BYARBFjkaqESqmmDUcunWuJ/MXs5UXDqYBifmV8iDgJyp7L XgoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=oWRkd82i; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x8-20020a170902b40800b001afb96f4b8fsi9283200plr.519.2023.06.07.13.02.08; Wed, 07 Jun 2023 13:02:23 -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=@gmail.com header.s=20221208 header.b=oWRkd82i; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231276AbjFGTmQ (ORCPT + 99 others); Wed, 7 Jun 2023 15:42:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230034AbjFGTmP (ORCPT ); Wed, 7 Jun 2023 15:42:15 -0400 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A3F71FE0; Wed, 7 Jun 2023 12:42:14 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-75d5469c856so407238085a.2; Wed, 07 Jun 2023 12:42:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686166933; x=1688758933; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=/oF4AC5xHgg8LwfnEbJTH2/uVZSgF0m82b8cRGQMkBc=; b=oWRkd82izCC3ALVh//TuRqme/kv6uihxU5NoL8EwK/jJsDDZvkqgKfzcQ4v9vgvdvu 2+J7jPIoUfmsNDkCgD9SOtXFAwiXxlCtwxuj3ocMcYHoIsDa7rm0B06MNcPY1zdp22VH oXh3/fnqD18Yei+gJ6zokAJUkiiVYLl90hrs43Fir8vLSDW8pTFusQOWmMQHWM9gLXAe lLnlhLsjKCWHDMGUVV7dRtD+kcm8j6cZP9LrLcMa7ttTgEYW+MPUs5/g08a+eYZp7lAF AehFUKFdI+YrZ6sZdq+YRJVf7QxTvuck5ie0YXSGUU08jdzureZH3uETF0R4ppRmJioL 82yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686166933; x=1688758933; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/oF4AC5xHgg8LwfnEbJTH2/uVZSgF0m82b8cRGQMkBc=; b=RtbhgsrB0sEsjvDv8WnTMkozfgJFUfkNheEhTDIp3Bqb2tl94qKKiH1g7LbuGLElwC DF5zC98BosmFYeV/4+Yt3GTl05NcOO4MUWXr1Yry3pVv9UkcJpDvS6iJQIhJwvGh+1HS HhME1WQYdmLgsUwuRLgCfWQ2JAz8hKMvz2WKSjVD7a7D/2gImQtwOBLsUpQBtxzmRf3a 9vqz0M0c8vYjLScVlxgUAs255DbyICk/Qi8CP1yDhxJEry0hz34A2tlAPF+nhBWLhxRL SIPMobxesdQPiplGGE9dkTP1S1khQ7IuVoSUcPte5OSY8f4YuXBawgIqb0snG047hBY2 qBvw== X-Gm-Message-State: AC+VfDw4ijQdw6QeI7X29Lo1OOq7odGoUTzVFY408kyR/Ad4wZjFlbxJ qEw/UgkwdJ9U8QAAcUuAy74= X-Received: by 2002:a05:620a:8a01:b0:75b:23a1:3670 with SMTP id qt1-20020a05620a8a0100b0075b23a13670mr2692621qkn.49.1686166933309; Wed, 07 Jun 2023 12:42:13 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id d7-20020a05620a158700b0075cc804ee88sm144730qkk.30.2023.06.07.12.42.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jun 2023 12:42:12 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 27F2427C0054; Wed, 7 Jun 2023 15:42:11 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 07 Jun 2023 15:42:12 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgedtgedgudegtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepjeeihfdtuedvgedvtddufffggeefhefgtdeivdevveelvefhkeehffdt keeihedvnecuffhomhgrihhnpehruhhsthdqlhgrnhhgrdhorhhgnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhp rghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsg hoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 7 Jun 2023 15:42:10 -0400 (EDT) Date: Wed, 7 Jun 2023 12:40:43 -0700 From: Boqun Feng To: Gary Guo Cc: Qingsong Chen , linux-kernel@vger.kernel.org, =?utf-8?B?55Sw5rSq5Lqu?= , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Martin Rodriguez Reboredo , Alice Ryhl , Asahi Lina , rust-for-linux@vger.kernel.org Subject: Re: [PATCH v2 1/3] rust: kernel: add ScatterList abstraction Message-ID: References: <20230602101819.2134194-1-changxian.cqs@antgroup.com> <20230602101819.2134194-2-changxian.cqs@antgroup.com> <20230607194210.380d195f.gary@garyguo.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230607194210.380d195f.gary@garyguo.net> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED 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 On Wed, Jun 07, 2023 at 07:42:10PM +0100, Gary Guo wrote: > On Mon, 5 Jun 2023 08:26:04 -0700 > Boqun Feng wrote: > > > On Fri, Jun 02, 2023 at 06:18:17PM +0800, Qingsong Chen wrote: > > [...] > > > +impl<'a> ScatterList<'a> { > > > + /// Construct a new initializer. > > > + pub fn new(buf: &'a Pin<&mut [u8]>) -> impl PinInit> { > > > + // SAFETY: `slot` is valid while the closure is called, the memory > > > + // buffer is pinned and valid. > > > + unsafe { > > > + init::pin_init_from_closure(move |slot: *mut Self| { > > > + (*slot).set_buf(buf); > > > + (*slot).mark_end(); > > > > Benno can provide more information, but you cannot dereference or create > > a reference to `*slot`, since `slot` points to an uninitialized object > > (see `try_pin_init` implementations), and referencing uninitialized > > objects is UB (or may cause UB). > > This is fine for `Self`, because the only non-ZST field in there is > `Opaque`, which can be uninitialised. > OK, but if you do a // this is OK let slot_ref = unsafe { &(*slot) }; // reads uninitialised data. let o = slot_ref.offset(); here (before any `set_buf()` or `mark_end()`), isn't it an UB? Regards, Boqun > > > > Note that you could do the following for `set_buf`: > > > > // `addr_of!`[1] is special since it won't create references > > // (even temporary onces). > > let opaque = addr_of!((*slot).opaque); // <- *const Opaque > > > > let ptr = Opaque::raw_get(opaque); // <- *mut bindings::scatterlist > > > > // Maybe this can be wrapped as a Rust function with a > > // parameter: *mut bindings::scatterlist. > > unsafe { > > bindings::sg_set_buf(ptr, buf.as_ptr(), buf.len()); > > } > > > > [1]: https://doc.rust-lang.org/core/ptr/macro.addr_of.html > > > > Regards, > > Boqun > > > > > + Ok(()) > > > + }) > > > + } > > > + } > > > + > > [...] >