From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) (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 7F4A8168 for ; Wed, 7 Jul 2021 21:47:47 +0000 (UTC) Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6C14C466; Wed, 7 Jul 2021 23:47:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1625694465; bh=JPMi1A04YdYsS4AbLOxt6pelAAWFfxNepP55qI0JS44=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=VWfxYMpHSVlcKIfW+OFk+P2PJiiLYi515YHlREmiSEuPUlNC/i4jiH/IMAh4aHQ2w Lcba/yAWQO5YYdILTtgEclHbQjMctVMCRprrwWCkWolXBaySfC+9RFcsfzoblK6Nzq 955mKOm+y4iB2Cyy9h3oxKJpb3CA+7gAlqcDzl7k= Date: Thu, 8 Jul 2021 00:47:01 +0300 From: Laurent Pinchart To: Miguel Ojeda Cc: Greg KH , Wedson Almeida Filho , James Bottomley , Julia Lawall , Linus Walleij , Roland Dreier , ksummit@lists.linux.dev Subject: Re: [TECH TOPIC] Rust for Linux Message-ID: References: <20ad13eb229b15fd14d870832d43a1605ba5c24a.camel@HansenPartnership.com> <1f2a62c19845e4602817c2130d47bbea02d04d1d.camel@HansenPartnership.com> Precedence: bulk X-Mailing-List: ksummit@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: On Wed, Jul 07, 2021 at 10:58:44PM +0200, Miguel Ojeda wrote: > On Wed, Jul 7, 2021 at 7:20 PM Greg KH wrote: > > > > That's not what I meant by bringing up a kref. I was trying to ask > > where the "real" lock here is. It has to be somewhere... > > Given this sentence, I think I understand where the confusion comes > from. The key is that `Ref` (`Arc`) is not adding any thread-safety to > the `T` it wraps -- it is only concerned about providing > reference-counting semantics. > > For that it only needs to keep a thread-safe refcount for a given `T`. > But the `T` might be (or not!) thread-safe. Since the refcount is an > atomic, that is enough for implementing `Ref`, no locks are needed. > > Now, if you need mutable access to the content of an `Ref` that is > shared by several threads at the same time, you need something extra > -- that is where you would need a lock. For instance, in normal Rust > code you may see a `Arc>` being used. > > The big thing is that if you get any of this wrong, you get a > compile-time error. For instance, if you create a `Ref` out of a > `T` that is not marked as safe to be sent across threads, you get an > error when you try to send the `Ref`. Or if you create a `Ref` and > try to call a method that mutates the `T` across threads, you will > also get a compile-time error. Thus you cannot forget to have a lock > if it is needed. Out of curiosity: we have in the kernel objects shared between multiple threads that require locking, but in some contexts taking the lock isn't required. I'm thinking in particular about initialization time when you create the object, before it is made visible to other users, or destruction time, when you nobody else can have a reference to the object anymore. Avoiding lock operations in those cases can be an optimization. Cn rust handle that ? -- Regards, Laurent Pinchart