Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7870928rwl; Thu, 23 Mar 2023 09:36:25 -0700 (PDT) X-Google-Smtp-Source: AK7set/tJruCIrvKIqkV9lVPfmCWsvNro0QJNrhw459ZJ7GCLxsTotLJ50jgC94jMxb0EthN0Ki3 X-Received: by 2002:a17:906:c28b:b0:8ed:e8d6:42c4 with SMTP id r11-20020a170906c28b00b008ede8d642c4mr11756615ejz.12.1679589385437; Thu, 23 Mar 2023 09:36:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679589385; cv=none; d=google.com; s=arc-20160816; b=M0fTH3EAG/BpsJydpvx4SnQbxsAj+ryzitQd27Yyr9ZJbHy5UVIIbMw1hqdXWA6EL6 XzzG32ACQmAzRBCIHmDbHqsQmxYGoB/yLfbx5s2ldJmHOSQL/zZPAmB0aLo/sqyuukvy hEkVWNM12lIToZXLSQGFmnN+JUtWjsgxCV6Pd/+a03bUO96HjYbDrZTvQ4B9FLhAfSiy DdBGSSC2iX4Il8MJ2ZifknSJj0lmM0kmdKlmobNHHoWE/3z/rM9QxwH1Th1eHg27Ly3D aIILhbZYcVBZNgr6fnpPC7wWF8ry1RisGGG5paNBSOX95b5UqCs9OKU4GHD3MYUCpGmu JW0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=8wMft88SIFOnMGHCYWiWnoky4Y8pUG6hfczevRZ/RAI=; b=UGqni8LM22p9AJ6f0mH2Gj0aI4YTmTmPwHVnF328nose/cb/72gzteCkI6pCGqAXJ5 aChlLgWjn91qB354OvPIK3PskW6vO5Ijhw4q/uhnoyY/FTW2O6pjS/J3F2OX/wOadEaj 3igB3/cDynhr11mOOKv0hokqUkhVfr1IwlVMjgK7n/MefVFXbzUvyGlll2iOrlQzaBIH k2Uw3/TedSXmMV1/lRoIx/uue3wnTMTNVRWZ4iAbnqg4JHqtRKWMTPPctkEViac6ENT7 KXofO4QInvN8KU4MFJR3juekoWiOMr3KEhK1fYbm9MIG6elMhBB+eU1595U310R7a1oC hAdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@asahilina.net header.s=default header.b=sPErOouV; 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=asahilina.net Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v5-20020a17090690c500b0091f8c8135c2si17802416ejw.551.2023.03.23.09.35.59; Thu, 23 Mar 2023 09:36:25 -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=@asahilina.net header.s=default header.b=sPErOouV; 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=asahilina.net Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230205AbjCWQal (ORCPT + 99 others); Thu, 23 Mar 2023 12:30:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232029AbjCWQaD (ORCPT ); Thu, 23 Mar 2023 12:30:03 -0400 Received: from mail.marcansoft.com (marcansoft.com [212.63.210.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B53EE298F8; Thu, 23 Mar 2023 09:29:59 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (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: lina@asahilina.net) by mail.marcansoft.com (Postfix) with ESMTPSA id 29C2E420CF; Thu, 23 Mar 2023 16:29:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=asahilina.net; s=default; t=1679588998; bh=oNvVr1kFt3SmK/C28H/bAWInhx7VA90WwPgLpowzoxE=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=sPErOouVd0l1ICYYe6Ol29w/aHljjg7cNEsNKlDDY9NGBAAvvHr/4+OnOazGB/kh2 C4I0TR9PbjFRz5yfE2vFPkJ+HGOjOC3+SdkwIitW8t8rz9yx+CQnq85sRjf/Jr/OdD VlzFlwBBb3jJ/vUF2xzFYVwRORaUbQnnV+bVt5bRTrBOoyJpaTS3OSm66j2Sh9jhhH 87KYiYuFN6dAnJVuPiUQjYDG3C8X1tULvZUkFJ1DN8amz+zCz6TcJRKA0QXZn8qnMY FgfJnqM3fFQiG5RDRMPAtA+Gkn4bnaPayUndjV6CHe7i6F7ZqhrjEnCp+c9MpIfmIA XDkNE0/wBCzkQ== Message-ID: Date: Fri, 24 Mar 2023 01:29:53 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH v2] rust: xarray: Add an abstraction for XArray Content-Language: en-US To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=c3=b6rn_Roy_Baron?= , Matthew Wilcox Cc: Martin Rodriguez Reboredo , Neal Gompa , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, asahi@lists.linux.dev References: <20230224-rust-xarray-v2-1-4eeb0134944c@asahilina.net> From: Asahi Lina In-Reply-To: <20230224-rust-xarray-v2-1-4eeb0134944c@asahilina.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,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 Why do I always notice mistakes after sending the patch... this is missing the comment I intended to add because I edited the file in the wrong git checkout. Sorry! The intended comment is below, I'll roll it into v3 when I make the Error API change. On 24/03/2023 01.23, Asahi Lina wrote: > The XArray is an abstract data type which behaves like a very large > array of pointers. Add a Rust abstraction for this data type. > > The initial implementation uses explicit locking on get operations and > returns a guard which blocks mutation, ensuring that the referenced > object remains alive. To avoid excessive serialization, users are > expected to use an inner type that can be efficiently cloned (such as > Arc), and eagerly clone and drop the guard to unblock other users > after a lookup. > > Future variants may support using RCU instead to avoid mutex locking. > > This abstraction also introduces a reservation mechanism, which can be > used by alloc-capable XArrays to reserve a free slot without immediately > filling it, and then do so at a later time. If the reservation is > dropped without being filled, the slot is freed again for other users, > which eliminates the need for explicit cleanup code. > > Signed-off-by: Asahi Lina [...] > +impl Drop for XArray { > + fn drop(&mut self) { > + // SAFETY: `self.xa` is valid by the type invariant, and as we have the only reference to > + // the `XArray` we can safely iterate its contents and drop everything. > + unsafe { > + let mut index: core::ffi::c_ulong = 0; > + let mut entry = bindings::xa_find( > + self.xa.get(), > + &mut index, > + core::ffi::c_ulong::MAX, > + bindings::BINDINGS_XA_PRESENT, > + ); > + while !entry.is_null() { > + T::from_foreign(entry); > + entry = bindings::xa_find_after( > + self.xa.get(), > + &mut index, > + core::ffi::c_ulong::MAX, > + bindings::BINDINGS_XA_PRESENT, > + ); > + } > + Add for v3: > + // Locked locks are not safe to drop. Normally we would want to try_lock()/unlock() here > + // for safety or something similar, but in this case xa_destroy() is guaranteed to > + // acquire the lock anyway. This will deadlock if a lock guard was improperly dropped, > + // but that is not UB, so it's sufficient for soundness purposes. > + bindings::xa_destroy(self.xa.get()); > + } > + } > +} > + > +// SAFETY: XArray is thread-safe and all mutation operations are internally locked. > +unsafe impl Send for XArray {} > +unsafe impl Sync for XArray {} > > --- > base-commit: 3ef2e9730ba4bb914dd8654ae34aef3f486c8c58 > change-id: 20230224-rust-xarray-f503f9e5455e > > Thank you, > ~~ Lina > > ~~ Lina