From patchwork Wed Nov 20 14:49:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881316 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 CB242D711A5 for ; Wed, 20 Nov 2024 14:56:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5510B6B009C; Wed, 20 Nov 2024 09:56:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 528186B009D; Wed, 20 Nov 2024 09:56:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37E986B009E; Wed, 20 Nov 2024 09:56:31 -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 1294A6B009C for ; Wed, 20 Nov 2024 09:56:31 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C4D48A06C8 for ; Wed, 20 Nov 2024 14:56:30 +0000 (UTC) X-FDA: 82806772188.08.C22F6FE Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf09.hostedemail.com (Postfix) with ESMTP id 46377140006 for ; Wed, 20 Nov 2024 14:55:53 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m9Tpf92s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114522; a=rsa-sha256; cv=none; b=BwlooIim23JV85bXsn64kJ8FhVHXpRnNS6sq+BvOl55henesk7a7GxP/HksUiW68Xl3uyB Dc7fRDY1etZRN19VkKi29SmvhQaX7SvusSz9dYamazTgr2ZFAci6j6wS9IB6hCFc6wsTHV 0qmoW7nOXXNVsdLNefKpSSUl8TwhhuM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m9Tpf92s; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3m_g9ZwkKCMQkvsmo18rvqyyqvo.mywvsx47-wwu5kmu.y1q@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732114522; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=smyO4TBHtPIyS3hFuWyJmych+o6ML2qjqxWGJHqgaWzWSBbfR6yBd1iZfyvfSczdhGUYbh okHV36wuPWOJF7tNZwITID8a8mNisrunqZ9cH8QDXut24XvnqKb5gECPvxMTScME0RFsQ8 kgC9WRBYpDgbwq3tduyDDjUJ5qYKTM8= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee61c44c69so42232327b3.3 for ; Wed, 20 Nov 2024 06:56:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114588; x=1732719388; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=m9Tpf92s9hBbupL6gE0XP/1zlgJx4ZF6E7moz1Ce5YQOKBwzlqyGbWWJvDxoqtPJSl vp6dHAFf4sqjnmNX/W6rVrbCngDDyN4Vh9sG3ulYmG8s/FK5+oZwmxIuB4M4Tl7Tu0td HG8oB2BrPB8jnrgo3YAbuipI5RY1iNiV1iQ9mtzksOKY4p51ya1lZNsbP8Psr4/c7BYe 09+qar8nuwpXsOKLCdcYdZIzNZIgzkmxRe0ZOt7yskDCTV37rOIH9NbhBtiMkMLtOyyf xf99/FYk5wu2zy+2dgJ4vJhmAtcG167Jm9L8fOCnOXJuthesnKxuoAUl9Gb0LvO6Fqpi U6Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114588; x=1732719388; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=B1qhmYZFewino7xIAVBLotqrei4FahWNdMxHocqLM/I=; b=ahdtZinKpAWztzYpRnhCLQ4401NVXLPo/AdTYlXH7hQbMwidzAVTISXgniG/vxrHU8 GS0KH1VOtYzR200sfz3awDvhw1Uq/rnHaTYm03aWkFmUsgsevedu5N5sEdqpM9SuU5/W bVS9gFi9n1+vSQXhBDn+pPhgBVphq5SS2QHnZ0orRpnf4rnGCerH50NEPz2/CHJnjjTv 5vnLvOtZfL2QfpHpwW7WM1TVQZHfmymBneV4CTggytC6YWrBDaKzyB216uHJoV71ZFPz LwqxiwbbIzDAoxcSsHDwSCb1W46xXzPPHp18eowW+culLSlVrJNm+Uws5WXZRJ3l7K9y mWVA== X-Forwarded-Encrypted: i=1; AJvYcCX9YEvkMOSo1au6DpL6EyN+bQGu/E+wVMHEkPVc6D6WWBsDrSPod0iwvAoiWpHDfm/Y9kHpowhAjw==@kvack.org X-Gm-Message-State: AOJu0YzR1ESgZbNEDjsxeXu4pwDJbALijMawIu+qQZO4Y/fR2KaU1MdD QYF0ty17Dw8jIgdYXRrhYgmEZ8w7O2sXtyG85iV/x9m9ebD1BIjEOKRZNn6H7Ra3G39OwiJyAuT I9Q1j2nx9h9/s5g== X-Google-Smtp-Source: AGHT+IF/fEeF7RZo4n1G/IhIgluwurThfGhJ8HHwALNv+tngK6vv1K4wFovn4nEL5pwq1i5uR+Y80sYHZ33r9Rg= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a25:abee:0:b0:e38:bdf2:17f2 with SMTP id 3f1490d57ef6-e38cb73348emr3963276.10.1732114587698; Wed, 20 Nov 2024 06:56:27 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:59 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3012; i=aliceryhl@google.com; h=from:subject:message-id; bh=zMBEVvavLox9MacZywaW5TO54AiJm41R1sgIdY4Cj4c=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiLa5bb5Royej/gvacnIe/2ZvajVUxGyAgMa dNsj+onQTmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34iwAKCRAEWL7uWMY5 RumNEACU95kezGcsO/Br+P6owGa2xELObLK9zCf/u/XtIn3c8FNg85uhnzJziaGAaAd2cYqUpzy RwmIPH+7ZyFBtfJpVKn7NLRLtdVo+klyy9JVT2z6vKZBJr9VJUJSDEG6gC3gRxqE6UgrRlWd2Ys wIWtHMXp4KouQyAZWtSablEJ5spRaujZDuYaA8KD9UICei4ceVZAkgpluSWANS9W7P/465YuKd6 kV+/4zDJVskpsK+bllZ8ONgS6OImPjDavA+ZBLXqGtcpBGVPfVQ71N25J8y0YjUgksmvXzoB6/7 93aKzR2oaK6rWaCZbzQvh1UWQoJZe606PiqvMHGKvngM1Z8RA+d71ce0Cf2n4r1EyHyzAeU6WJL cOW3Ru9xOnLlGfECNblgdzVjOzhFqAuukhuzo54ZhUVmkpCwOdmXRUlOke5rCDEE5cJ5Qqd7Wak tAvR5mkY4rG3zy2C174XQRFs95SBBQmOs4YrEDCXnIUgB6ZB3hVwUmLlU8TJNsnFRxKLa/zgdI5 6cQislthRO7rqBODUOhHBjizlWYGcoFwRkxjjS+nT7oRo8DpxZcCQ2tGV5z9cQbX3QEp1KuxDe4 dGYcwM0fgLH1M09SIqdoGE3bIMWuplVaerFAXZKqKpYgC4by/xXMkwXkE0WGAKavS4c4MVBip4d lLCXPgjMMTYXRCg== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-5-eb31425da66b@google.com> Subject: [PATCH v8 5/7] mm: rust: add mmput_async support From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Christian Brauner Cc: Alex Gaynor , Boqun Feng , 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, Alice Ryhl , Andreas Hindborg X-Rspam-User: X-Rspamd-Queue-Id: 46377140006 X-Rspamd-Server: rspam11 X-Stat-Signature: 9trs88c6a4amh1xbtzi5x1mi5g6yzhti X-HE-Tag: 1732114553-288997 X-HE-Meta: U2FsdGVkX19uCXO6vhBx84tNhOebdIaTHV/RDl5a0Y3tX2MJCn3HcKzni6lMi1JgTPK5AWmP6FnQAdlYHKSNj668OQ1EcPOwVlIVVRW/3gukHMJK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cVa5n3awdZ9aeC2wrxjhDgXxzSjKKZgKljRsAKY3WwMso+8sj+YuIOBU/mJeEucu050m/fSzNh3OE16aa7Oa6YmlO1Pl3Za0jdIpx94DmqU/p5vuNm8W8HDk4vz/ubTeutKfbZoqS8nRjBa8zomsYu1Uu3cahQgd59oKcEv4o2mCw9C8qPah6D+yvNGLvciEAWbFky6Q8HtunNaUD/2j7hbS+eTrOHhaNWXY5NjF3fwYzWbdhDnA3T0uqNo5/blv3NLZkpryvC4wxQjhfTVN6D87i7qS/os4RdCji3bcm0WiwGgA4lFUaa9nNOxtNgQgL1vAsNrvh1Tkpq7MTJ+D4ZjZ/obBaGC3Z2+suvus1/h3Cwy9N/BwdH1N8tKlmhkC/5OeaWdhbOnfWDgP+7wVEXe5z6hLVL+HPO7IpDUZHy1jYbK+OhAuBLZsVrGRRlDTm9BcOnfZ7IlFdgChLklSNlj1OTGR1zpOZq8CgxKNymcR6hC6c91L+W8z9HeCG/qG/pWhGVwFK+4DkbvsyR//Ow== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adds an MmWithUserAsync type that uses mmput_async when dropped but is otherwise identical to MmWithUser. This has to be done using a separate type because the thing we are changing is the destructor. Rust Binder needs this to avoid a certain deadlock. See commit 9a9ab0d96362 ("binder: fix race between mmput() and do_exit()") for details. It's also needed in the shrinker to avoid cleaning up the mm in the shrinker's context. Signed-off-by: Alice Ryhl --- rust/kernel/mm.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index a15acb546f68..f800b6c244cd 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -98,6 +98,48 @@ fn deref(&self) -> &Mm { } } +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This type is identical to `MmWithUser` except that it uses `mmput_async` when dropping a +/// refcount. This means that the destructor of `ARef` is safe to call in atomic +/// context. +/// +/// # Invariants +/// +/// Values of this type are always refcounted using `mmget`. The value of `mm_users` is non-zero. +#[repr(transparent)] +pub struct MmWithUserAsync { + mm: MmWithUser, +} + +// SAFETY: It is safe to call `mmput_async` on another thread than where `mmget` was called. +unsafe impl Send for MmWithUserAsync {} +// SAFETY: All methods on `MmWithUserAsync` can be called in parallel from several threads. +unsafe impl Sync for MmWithUserAsync {} + +// SAFETY: By the type invariants, this type is always refcounted. +unsafe impl AlwaysRefCounted for MmWithUserAsync { + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmget(self.as_raw()) }; + } + + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The caller is giving up their refcount. + unsafe { bindings::mmput_async(obj.cast().as_ptr()) }; + } +} + +// Make all `MmWithUser` methods available on `MmWithUserAsync`. +impl Deref for MmWithUserAsync { + type Target = MmWithUser; + + #[inline] + fn deref(&self) -> &MmWithUser { + &self.mm + } +} + // These methods are safe to call even if `mm_users` is zero. impl Mm { /// Call `mmgrab` on `current.mm`. @@ -171,6 +213,13 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } + /// Use `mmput_async` when dropping this refcount. + #[inline] + pub fn use_mmput_async(me: ARef) -> ARef { + // SAFETY: The layouts and invariants are compatible. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + /// Try to lock the vma read lock under rcu. /// /// If this operation fails, the vma may still exist. In that case, you should take the mmap