Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp608333lqp; Wed, 12 Jun 2024 10:37:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW3DBp5obiUqyCuymMaPBzE6U/Av+h0Yfw6gdQXaOkxUT4xenBd2ylCBDhF21gw3XlKF4Xx13u9M3LEpMXRnn9r81Ek6azzot9TDJR3gA== X-Google-Smtp-Source: AGHT+IGpoqlCW88QBdXHizIRC1Yqtgi2sdZfIxeOJ4deoJ0GQdTKFt62wxK4bS9eLv6s2nlKbJM8 X-Received: by 2002:a17:90b:1bca:b0:2c2:cf71:4977 with SMTP id 98e67ed59e1d1-2c4a762700amr2481260a91.16.1718213844013; Wed, 12 Jun 2024 10:37:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718213843; cv=pass; d=google.com; s=arc-20160816; b=vo1KLKP45SwTXC/Gn4i5//mhgNu0q8FcrBNPa+I6U4Jfc/R0XlBdiRCIVh4tDXo0xT YNShFHtA0nOIdDIqdKTcG5MDgs9lhGHTQCST1byESpLcc6d5GrR/CxqCvHDEA5cXjFf1 srQI/xAKrZ6UmzUtjc+iFzyQuFJVbCpfCMsEtDgciPDVNEs7wbRF821gRZn0C4l6eqyz ONuMuLJjbU+bfWOVw0zdNtcZW6tN87bxrHK3/l6ss01EaLDBo7ehNj3pZmMt4l3GUy2g ZA0d711GA7jX02PIMWrG4vJdDNQtl4k0PYslSaI5o4v5aUI+2TgrK7fbaisG8Nvi3dls //yg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=HSnoaDjlXTn+zoSX2pmDACoa4QXMjbSNpzbxfXrFR0M=; fh=4bInQGLnqQ2JwA4ZIId/tnybN1Ad1gzab4Zt2fOUHNA=; b=0+vV7kGmwXE9NFhFXC8LH9jBiOjX704C7xZzTicDYcZaUx+1CSYWnCaOalWT4p2SZQ J1vHD3sNQjST2uAhQAv0sEBHwgimhjhBDh0To65FaY1ek8i4s7yD0liAGJhvrsts2bp4 s81NexQJ3oAuIGz6SSCBKeTVeDg9BxOMXE/rUUyyfBhJsEgnfkTcglg0j6RlwDSaNczA 8YcdncTezXSvwPyqPg+66vO4B+XrMKGUOPQUJqk9v8t2jNkMaZhS+jGkyn3a66gaDJc9 KzPmnPsTjYwUd738kRt0ZOeV/kD1eiDzvKiCNe6SjFZUx3+66nv4/esXn99o3Y2pdc2Z A8fQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=m4b2nDT+; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-211988-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211988-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c4a76e0548si1919248a91.142.2024.06.12.10.37.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 10:37:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211988-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=m4b2nDT+; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-211988-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211988-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org 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 4C4BFB22C40 for ; Wed, 12 Jun 2024 17:13:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 072A01822E4; Wed, 12 Jun 2024 17:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="m4b2nDT+" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 F287417FAC5; Wed, 12 Jun 2024 17:13:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718212415; cv=none; b=ZcdYdAK78A2iBRf6/2lyTfYqYR6O5i9VxWVbfGCWN6ZxGJLnu4+B7jIFWrFneJnC8XVdTlWJhVcXsSmp3EHgIblStlOtQ3/g6cA8j/raLPMZY0ZzA/FOOgniD95VpKo6EYylWCspOBnL+CD27a2fz2SAgYFFnY/DaR0CHHYmGKg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718212415; c=relaxed/simple; bh=YddBzQ3+G8dpMI1SPWT5hGHfO/uLv6eA4KaATQnMwGM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=jSOXqYksUHd98L47E4SRjkNdqdsucMxEb7G1gfcesSCJXLUvnObJIO3jA5eVLahoKT6/lRXoP+sdzeQsq36aOTxr7Ly8vAmaJLIEXB9ocgzb3DYN9bMrYkrjwHayQoiIs0k0CpOxesAhrTZ7bs7y+6dC6+nfdnmb3wcS7qHKyhk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=m4b2nDT+; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBB70C116B1; Wed, 12 Jun 2024 17:13:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1718212414; bh=YddBzQ3+G8dpMI1SPWT5hGHfO/uLv6eA4KaATQnMwGM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=m4b2nDT+30zAJ957wVy6JfNicWjRb6NzPw96SZL/2Bmj225KyELj9hHjlePM1ndUf TcslL1jxexz28yc+sQba7/LjPhi8vbCrkPKAML1FKqeBnKnpjzyYJbxK7rkWc+TNPH sbSMIMu8540WoQS9/DlhiOMhYpOaP60iBCGDZFsw= Date: Wed, 12 Jun 2024 19:13:31 +0200 From: Greg KH To: Danilo Krummrich Cc: Boqun Feng , rafael@kernel.org, mcgrof@kernel.org, russell.h.weight@intel.com, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, airlied@gmail.com, fujita.tomonori@gmail.com, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] rust: add abstraction for struct device Message-ID: <2024061254-scoured-gallantly-5e41@gregkh> References: <20240610180318.72152-2-dakr@redhat.com> <2024061136-unbridle-confirm-c653@gregkh> <2024061245-kangaroo-clothes-76e1@gregkh> <2024061214-dusk-channel-e124@gregkh> 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=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Jun 12, 2024 at 06:18:38PM +0200, Danilo Krummrich wrote: > On Wed, Jun 12, 2024 at 05:50:42PM +0200, Greg KH wrote: > > On Wed, Jun 12, 2024 at 05:35:21PM +0200, Danilo Krummrich wrote: > > > On Wed, Jun 12, 2024 at 05:02:52PM +0200, Greg KH wrote: > > > > On Wed, Jun 12, 2024 at 04:51:42PM +0200, Danilo Krummrich wrote: > > > > > On 6/11/24 18:13, Boqun Feng wrote: > > > > > > On Tue, Jun 11, 2024 at 03:29:22PM +0200, Greg KH wrote: > > > > > > > On Tue, Jun 11, 2024 at 03:21:31PM +0200, Danilo Krummrich wrote: > > > > > > > > ...hence, I agree we should indeed add to the #Invariants and #Safety section > > > > > > > > that `->release` must be callable from any thread. > > > > > > > > > > > > > > > > However, this is just theory, do we actually have cases where `device::release` > > > > > > > > > > > > @Danilo, right, it's only theorical, but it's good to call it out since > > > > > > it's the requirement for a safe Rust abstraction. > > > > > > > > > > Similar to my previous reply, if we want to call this out as safety requirement > > > > > in `Device::from_raw`, we probably want to add it to the documentation of the C > > > > > `struct device`, such that we can argue that this is an invariant of C's > > > > > `struct device`. > > > > > > > > > > Otherwise we'd have to write something like: > > > > > > > > > > "It must also be ensured that the `->release` function of a `struct device` can > > > > > be called from any non-atomic context. While not being officially documented this > > > > > is guaranteed by the invariant of `struct device`." > > > > > > > > In the 20+ years of the driver model being part of the kernel, I don't > > > > think this has come up yet, so maybe you can call the release function > > > > in irq context. I don't know, I was just guessing :) > > > > > > Ah, I see. I thought you know and it's defined, but just not documented. > > > > > > This means it's simply undefined what we expect to happen when the last > > > reference of a device is dropped from atomic context. > > > > > > Now, I understand (and would even expect) that practically this has never been > > > an issue. You'd need two circumstances, release() actually does something that > > > is not allowed in atomic context plus the last device reference is dropped from > > > atomic context - rather unlikely. > > > > > > > > > > > So let's not go adding constraints that we just do not have please. > > > > Same goes for the C code, so the rust code is no different here. > > > > > > I agree we shouldn't add random constraints, but for writing safe code we also > > > have to rely on defined behavior. > > > > As the rust code is relying on C code that could change at any point in > > time, how can that ever be "safe"? :) > > That's the same as with any other API. If the logic of an API is changed the > users (e.g a Rust abstraction) of the API have to be adjusted. Agreed, just like any other in-kernel code, so there shouldn't be anything special here. > > Sorry, this type of definition annoys me. > > > > > I see two options: > > > > > > (1) We globally (for struct device) define from which context release() is > > > allowed to be called. > > > > If you want, feel free to do that work please. And then find out how to > > enforce it in the driver core. > > If we *would* define non-atomic context only, we could enforce it with > might_sleep() for instance. might_sleep() isn't always correct from what I remember. > If we *would* define any context, there is nothing to enforce, but we'd need to > validate that no implementer of release() voids that. Trying to validate that might be hard, again, I don't think it's worth it. > The former is a constaint you don't want to add, the latter a lot of work. What > if we at least define that implementers of release() must *minimally* make sure > that it can be call from any non-atomic context. > > That'd be something we can rely on in Rust. Determining if you are, or are not, in atomic context is almost impossible in C, I don't know how you are going to do that at build time in Rust. Good luck! > Oh, I fully agree with that. Let me try to explain a bit what this is about: > > In Rust we have the `Send` and `Sync` marker traits. If a type (e.g. `Device`) > implements `Send` it means that it's safe to pass an instance of this type > between threads. Which is clearly something we want to do with a `Device`. > > If I don't implement `Sync` for `Device` the compiler will prevent me from > sending it between threads, e.g. by disallowing me to put an instance of > `Device` into another data structure that is potentially passed between threads. > > If I implement `Sync` I have to add a safety comment on why it is safe to pass > `Device` between threads. And here we have what Boqun pointed out: `Device` can > only be passed between threads when we're allowed to drop the last reference > from any thread. In the case of the kernel this can be any non-atomic context, > any context or any other subset. But I have to write something here that is > a defined rule and can be relied on. You really have two things here, a matrix of: can transfer between threads can call in irq context that are independent and not related to each other at all. Looks like Rust has built in support for the first. And nothing for the second as that is a very kernel-specific thing. So let's not confuse the two please. `Send` and `Sync` should be fine for a device pointer to be passed around, as long as the reference is incremented, as that's what all of the kernel C code does today. Let's not worry about irq context at all, that's independent and can be handled at a later time, if at all, with a different "marking" as it's independent of the current two things. thanks, greg k-h