Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4352582rdb; Mon, 11 Dec 2023 17:27:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IFodfYN3U53cairOiE6Ir3FDkl9t0+wgjG7JW8laEH2/SK333H+2BlJAwEjY9PqkhPwyIYw X-Received: by 2002:a17:902:ea0e:b0:1d0:cec3:4568 with SMTP id s14-20020a170902ea0e00b001d0cec34568mr6444729plg.54.1702344446046; Mon, 11 Dec 2023 17:27:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702344446; cv=none; d=google.com; s=arc-20160816; b=y+MvD5uZTGPEYPF/eCQZiiC5kiCx/1CWwjxGoC2HcRRwGZmF6UDO8OJf1BT0ca20xY 5aoNqTyEzGPXvtau5HiOrObXtEBJsmzNc168wZCprsDYpZhYQ7G7/JQI5qH9m6k/LZZ7 dixO6LRWKmcdWbx4Q/utfNZOJm7Tb3racjSotu6HPlaHfRHAUdUhqDnKrrr/5J6SkGvA oxcHQo2lIfPOXdOnuVPDs7z2yIrdoSp/Uv78A0qGRZ/06MpcG9YwBx7w0j68BTDg6UnI B33yw+hfGnyQ2jfay8jUfio1VWywf1whg5xw8rrvi1aYMWm2OMFWYS6NGCqqD07AepD3 Y4cg== 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=acdHOCAiQMqkPWM1t1b37j8hmWv8asGLnF3nSjNDtxY=; fh=3xNEesUYtd1BaP23DNO3uUvkDd9LLQZaPpP/t1W0nQ0=; b=owI0y6+UcHbcJpna2UMRCU+Qovdsy8XB85tH/jzSXiCYQ9AnYtgkMw679ZI0fkP0i1 XxL7VEjMFj47ldRB6RBLM8YsSMDhc2Si9w7JeYxlvk1wyzKvC1yFxEd5gj5+Y5CP4WUs 742tJQrU3RQuS0YGyW6f4/S6J1DjSPwi6pxCgiGTnJ92EIOli1yeI9D9ei8OopF4BjH4 vup8rccbQXpoqeddMLMxgyFwh/n2RRq3tpPC5uOcZxlxxsnTfMefHOrpGKpioB3Qj9Ls oWVZFihcpiNL0rHgKhXj/sijdwM+eNfk4TXWjAHYSLjylZFQAh7ieWA+3+csSUGlj7Dz DqWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=POMy0qqK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id c17-20020a170902d49100b001d054a8f128si7126656plg.451.2023.12.11.17.27.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 17:27:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=POMy0qqK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A837D80D0CF7; Mon, 11 Dec 2023 17:27:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345676AbjLLBZV (ORCPT + 99 others); Mon, 11 Dec 2023 20:25:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345638AbjLLBZS (ORCPT ); Mon, 11 Dec 2023 20:25:18 -0500 Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 178A0CE; Mon, 11 Dec 2023 17:25:25 -0800 (PST) Received: by mail-qk1-x732.google.com with SMTP id af79cd13be357-77f380d8f6aso304722685a.2; Mon, 11 Dec 2023 17:25:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702344324; x=1702949124; darn=vger.kernel.org; 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=acdHOCAiQMqkPWM1t1b37j8hmWv8asGLnF3nSjNDtxY=; b=POMy0qqKXSsPfkEQ0KV0+WC8FdZirzY0ulauLmG+mcz7YUKEg4Mf+e7cIEZcb5IWAA ZVrk3jusPz+arx697XjTxGBSd58UqDnGX1asSfVcu0aqL+NOkl7UV3a9mnMS+pGLxpKu MYRTVda/CHdYjvWQ7BgnqvFngD8742QZx0uW8XAGwWEdlPwvmOcDOHviIy/V1+4p9Rbs /Ms4T+LDdaqiIXYOWpp8+vV53VaZtudpDOxFoTsVHIiSomUWGW6EkbUMrDyD/y8YTSxm 8jpzcL5VpKj3gKKEDlXZyC6zVO5vzSFMo4d/OYHiJ4rpzGiUWIFPinXxz7DkUEo6F+5Q Jp/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702344324; x=1702949124; 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=acdHOCAiQMqkPWM1t1b37j8hmWv8asGLnF3nSjNDtxY=; b=a+DHjoNl7Rtv9YDaoKWwjthh4cRCO6xrSlrW3GReIueKB5Kq0vsQtxGbi/cF99f+dd cZsEs7DBkZTwiSUOFqb+UrwJeE9JugX9H4GUXFalBixkMCz5aWd+XxPWQc6TrvSl4s0f Y1B2NOuTX5ni4hEy/f8FxnBMopqQVabszOhedDs/i5qpBx6a+PmkAHa0q8S8K+yR2k2d PQq8zZRfyEPhsBYvSEyjEgHwzGmi3CQczICgDduVSlopaYMNRply+l40A6nR0VnR8Ikq Sll408hZTh/gzNB0oHkEdei1YSIIBjFnlP6987zNxCIpQnk9kjlip6nkGdz2v7NSrsp/ vpiQ== X-Gm-Message-State: AOJu0YyXGG3BOFAjVrcuAR0U/0welxqccecOw57nbT5QY7o0/g99krkV OezfTEnCir6EZ1Y8nXwLGSAGj6OF1O4= X-Received: by 2002:ae9:e905:0:b0:77d:4cb9:89ac with SMTP id x5-20020ae9e905000000b0077d4cb989acmr6056119qkf.32.1702344324191; Mon, 11 Dec 2023 17:25:24 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id g3-20020a05620a278300b0077f435ed844sm3350413qkp.112.2023.12.11.17.25.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 17:25:23 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 149FE27C0054; Mon, 11 Dec 2023 20:25:23 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 11 Dec 2023 20:25:23 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudelfedgfeehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 11 Dec 2023 20:25:21 -0500 (EST) Date: Mon, 11 Dec 2023 17:25:18 -0800 From: Boqun Feng To: Alice Ryhl Cc: benno.lossin@proton.me, a.hindborg@samsung.com, alex.gaynor@gmail.com, arve@android.com, bjorn3_gh@protonmail.com, brauner@kernel.org, cmllamas@google.com, dan.j.williams@intel.com, dxu@dxuuu.xyz, gary@garyguo.net, gregkh@linuxfoundation.org, joel@joelfernandes.org, keescook@chromium.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, maco@android.com, ojeda@kernel.org, peterz@infradead.org, rust-for-linux@vger.kernel.org, surenb@google.com, tglx@linutronix.de, tkjos@android.com, viro@zeniv.linux.org.uk, wedsonaf@gmail.com, willy@infradead.org Subject: Re: [PATCH v2 6/7] rust: file: add `DeferredFdCloser` Message-ID: References: <20231211153440.4162899-1-aliceryhl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 11 Dec 2023 17:27:23 -0800 (PST) On Mon, Dec 11, 2023 at 09:41:28AM -0800, Boqun Feng wrote: > On Mon, Dec 11, 2023 at 03:34:40PM +0000, Alice Ryhl wrote: > > Benno Lossin writes: > > > On 12/6/23 12:59, Alice Ryhl wrote: > > > > + /// Schedule a task work that closes the file descriptor when this task returns to userspace. > > > > + /// > > > > + /// Fails if this is called from a context where we cannot run work when returning to > > > > + /// userspace. (E.g., from a kthread.) > > > > + pub fn close_fd(self, fd: u32) -> Result<(), DeferredFdCloseError> { > > > > + use bindings::task_work_notify_mode_TWA_RESUME as TWA_RESUME; > > > > + > > > > + // In this method, we schedule the task work before closing the file. This is because > > > > + // scheduling a task work is fallible, and we need to know whether it will fail before we > > > > + // attempt to close the file. > > > > + > > > > + // SAFETY: Getting a pointer to current is always safe. > > > > + let current = unsafe { bindings::get_current() }; > > > > + > > > > + // SAFETY: Accessing the `flags` field of `current` is always safe. > > > > + let is_kthread = (unsafe { (*current).flags } & bindings::PF_KTHREAD) != 0; > > > > > > Since Boqun brought to my attention that we already have a wrapper for > > > `get_current()`, how about you use it here as well? > > > > I can use the wrapper, but it seems simpler to not go through a > > reference when we just need a raw pointer. > > > > Perhaps we should have a safe `Task::current_raw` function that just > > returns a raw pointer? It can still be safe. > > > > I think we can have a `as_ptr` function for `Task`? > > impl Task { > pub fn as_ptr(&self) -> *mut bindings::task_struct { > self.0.get() > } > } Forgot mention, yes a ptr->ref->ptr trip may not be ideal, but I think eventually we will have a task work wrapper, in that case maybe Task::as_ptr() is still needed somehow. Regards, Boqun