From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E263EE69188 for ; Fri, 22 Nov 2024 18:48:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59A9C6B0083; Fri, 22 Nov 2024 13:48:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 54A696B0085; Fri, 22 Nov 2024 13:48:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 412676B0088; Fri, 22 Nov 2024 13:48:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1C8106B0083 for ; Fri, 22 Nov 2024 13:48:32 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B8B80ADF92 for ; Fri, 22 Nov 2024 18:48:31 +0000 (UTC) X-FDA: 82814613000.17.9E01AAB Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by imf21.hostedemail.com (Postfix) with ESMTP id C1D1F1C000A for ; Fri, 22 Nov 2024 18:46:46 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=E3axAeC3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732301241; a=rsa-sha256; cv=none; b=gZhVskXYzdOmR8meYcbmW6B3nfchnZfkRROj8yBauPe0HvcxsINiM4VamgGZ3vThu5oJTz SAvXkfTjbnelzVrHjRooVkDaC83Gpmmk+zqhdB1BfUP3v1o0hZgVczhNJZBtyL+3Ql6ljh Apu00PV1AR6N0ax1MpCCoqdTicZZQnY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=E3axAeC3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of aliceryhl@google.com designates 209.85.221.52 as permitted sender) smtp.mailfrom=aliceryhl@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732301241; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mxIeahTsMKLoh9Cd1c7NSRJawE4dDHGCsOK9b9C7oa8=; b=miDtok2QerkLMaHzaKDSE5ouxREwOBYMmEHeKO01DDRNxj1fhj4CfGI6y0Zoqzwq/OrsSC +vhnBftDsZ2e2yYXJKulii0121x/nXURiCfjEVOOtHQP9Hx0cb5XPcsm/iKlnSDvkvj+kY hZb9c7OQCKwi7D5u/JW0dvFlFP/sg+g= Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-37ed3bd6114so1464895f8f.2 for ; Fri, 22 Nov 2024 10:48:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732301308; x=1732906108; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mxIeahTsMKLoh9Cd1c7NSRJawE4dDHGCsOK9b9C7oa8=; b=E3axAeC3iPe69oj0BZRqbPpD+0lefBb+EHDDbA//eRh7OWzaOMfRnEGYHLZkXFss9W ELfYkwO4ag9eCiGB7xHSoNKrRPoMw4zqVGKDMjUjIGQN2MGYNjBVM3dP92CuT0DaNXBf LTyuAgS0NjqJ8bisaXRDvivPAaiit2B9W8YHlT5fLplbogE4Ei1a3HBVo9VHBRh2xim1 2PxxTdOWqEZmJEbHI+4xSOQGE3nC1V9jVvkxgbCVbC2iUvwo5OFsoOi+qTUfnBsOMJN6 pCgk6RHAc3b26D9pX7K2JtG5GeLRCnJHQNi8SzSuRxl4IsvVuxV2HeJX1SfoqhtNJgi/ 5vyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732301308; x=1732906108; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mxIeahTsMKLoh9Cd1c7NSRJawE4dDHGCsOK9b9C7oa8=; b=hUkg+j2WWF+PyGz3lctuAQm3n+j72EUL4hAqtjBnS07U8kyFos4Tu+JZC+pvGV32Um Zx+eIas106yiO74W6EOImDRStkSuarM+m2FnYrwOvSJGUUKl2/SfaZ1HHboKTxTLxeo8 FQGeA7PSAPk/PYiTt0sjBAt8Foy/eX68X4Kv7GDNXktuXLCPGkWqeY2B/2YLOsQrOgOg 7xWvePCJOF/iPVJe69rW+S+PkzUaC/jNvQ0ZrDOCSOZC3mfaGJZUShqzv8txuxAivReP Nzk8a+w0Ch7JfQm3j0A0K16VMRccTbOrjONTaD0t0dITIs6UyBaj6+DFDX0D1LnzTaKv 98IA== X-Forwarded-Encrypted: i=1; AJvYcCUAWg0MeAClObIlghazUMZiJFmSbpSC76iXIfbih247eQr/zW6ef3BnG1yR1HWz1UTUKZo7bofPaQ==@kvack.org X-Gm-Message-State: AOJu0Yyy5UmLEq9muhwJNZSKb6SnWld3rVvn2wlElCHWXY+MAdxWjNFv 5iQuSMFVkH4DjdXiFaHm4Wp2CN0xxbqJUK+CVuuTF4jMq5wyY1lgRev4xkV718uoDAHkBGhGz/u 1oM7Hzb/PVUn2DD8Rqe1/vcL5RU1w94zu+6T4 X-Gm-Gg: ASbGncvBRNafirEiVj9npx7w5TA9HGCkBkb0V7UnnPxcZAceJLWtH0eHagl7y94mEeo fZHvHfs3sn6RZDBIZMR+z+Gyuv4AYOlM4 X-Google-Smtp-Source: AGHT+IHaew+kp1my7q0xI6I8koJgRiIyjZFN91JPlAzIjCxjDZ9Xi1o5BjsQ2ZFr68SvzqEGgIZc5l0od7huX7RZf0c= X-Received: by 2002:a5d:5f86:0:b0:381:f5c2:df24 with SMTP id ffacd0b85a97d-38260bf39a6mr3130037f8f.57.1732301308033; Fri, 22 Nov 2024 10:48:28 -0800 (PST) MIME-Version: 1.0 References: <20241122-vma-v9-0-7127bfcdd54e@google.com> <20241122-vma-v9-8-7127bfcdd54e@google.com> <6740c786.050a0220.31315a.5363@mx.google.com> In-Reply-To: <6740c786.050a0220.31315a.5363@mx.google.com> From: Alice Ryhl Date: Fri, 22 Nov 2024 19:48:16 +0100 Message-ID: Subject: Re: [PATCH v9 8/8] task: rust: rework how current is accessed To: Boqun Feng Cc: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Christian Brauner , Jann Horn , Suren Baghdasaryan , Alex Gaynor , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Andreas Hindborg Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Queue-Id: C1D1F1C000A X-Rspamd-Server: rspam01 X-Stat-Signature: h4mbxowifudaezdikcz3r43yknhriex3 X-HE-Tag: 1732301206-234014 X-HE-Meta: U2FsdGVkX19B4LTkg5V4CeNVI08w1OuUa4UdhbaYYuop+yvnQK9iXuA058UgBdX3inaEE/tzPMW8wv1pcwkf2lk1nDfgy9konS5MqM1j/ftdAWuVtt/a/RcArykc3apGDxVmIwr3Qq/QfZrreFPgslonydCKsgOu1TU1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4SO20Cqp3k3aJfaTYeLbhqkQVpcQP3 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000016, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Nov 22, 2024 at 7:03=E2=80=AFPM Boqun Feng w= rote: > > On Fri, Nov 22, 2024 at 03:40:33PM +0000, Alice Ryhl wrote: > > +/// Represents a [`Task`] obtained from the `current` global. > > +/// > > +/// This type exists to provide more efficient operations that are onl= y valid on the current task. > > +/// For example, to retrieve the pid-namespace of a task, you must use= rcu protection unless it is > > +/// the current task. > > +/// > > +/// # Invariants > > +/// > > +/// Must be equal to `current` of some thread that is currently runnin= g somewhere. > > +pub struct CurrentTask(Task); > > + > > I think you need to make `CurrentTask` `!Sync`, right? Otherwise, other > threads can access the shared reference of a `CurrentTask` and the ->mm > field. I'm thinking if we have a scoped thread/workqueue support in the > future: > > let task =3D current!(); > Workqueue::scoped(|s| { > s.spawn(|| { > let mm =3D task.mm(); > // do something with the mm > }); > }); I don't think this is a problem? As long as a thread exists somewhere with `current` being equal to the task, we should be fine? > > +impl CurrentTask { > > + /// Access the address space of this task. > > + /// > > + /// To increment the refcount of the referenced `mm`, you can use = `ARef::from`. > > + #[inline] > > + pub fn mm(&self) -> Option<&MmWithUser> { > > Hmm... similar issue, `MmWithUser` is `Sync`. What is the problem with that? > > + let mm =3D unsafe { (*self.as_ptr()).mm }; > > + > > + if mm.is_null() { > > + None > > + } else { > > + // SAFETY: If `current->mm` is non-null, then it reference= s a valid mm with a non-zero > > + // value of `mm_users`. The returned `&MmWithUser` borrows= from `CurrentTask`, so the > > + // `&MmWithUser` cannot escape the current task, meaning `= mm_users` can't reach zero > > + // while the reference is still live. > > Regards, > Boqun > > > + Some(unsafe { MmWithUser::from_raw(mm) }) > > + } > > + } > > +} > > + > > // SAFETY: The type invariants guarantee that `Task` is always refcoun= ted. > > unsafe impl crate::types::AlwaysRefCounted for Task { > > fn inc_ref(&self) { > > > > -- > > 2.47.0.371.ga323438b13-goog > >