From patchwork Wed Jan 15 13:35:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13940448 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 EFA76C02185 for ; Wed, 15 Jan 2025 13:36:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A97C06B0092; Wed, 15 Jan 2025 08:36:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A19446B0093; Wed, 15 Jan 2025 08:36:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F6846B0095; Wed, 15 Jan 2025 08:36:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5A5A66B0092 for ; Wed, 15 Jan 2025 08:36:19 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 145F8A12DE for ; Wed, 15 Jan 2025 13:36:19 +0000 (UTC) X-FDA: 83009785278.05.7FDD04B Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf16.hostedemail.com (Postfix) with ESMTP id 2572218000D for ; Wed, 15 Jan 2025 13:36:16 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cYtsF1vk; spf=pass (imf16.hostedemail.com: domain of 3z7mHZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3z7mHZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736948177; 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=9rKypNB63ajEZUNiwKcfWGkLNs9kznO8TMtZ1K+RxGI=; b=jq48w3RrcKau/RSthfsiYtAnup0WAZ6+Qq9iAgaowuHHh1F/tXf3UQIOX38u/r5K/1uyw6 0/Yz6YddKFaLXZDOcYeHEYILf27n+NAK3dmWFMP8M0EGdg800VJlKG8yxvk7laKYGt4h0x d0J3W9k445w/vnHIWDYLXYM9KaQ3eIk= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cYtsF1vk; spf=pass (imf16.hostedemail.com: domain of 3z7mHZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3z7mHZwkKCKQEPMGIVcLPKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736948177; a=rsa-sha256; cv=none; b=KwL3xAWtx/OA3gc+Qp9m9kNaiaIGcZ1H4bNz8amIw6Q/vGBopvZ1QM/7t5NZVyTH62L2m9 2N7ulh2t2fk57NXm42scysKgOpUD6rAAitf3O9JskGWWwCIr49pmpSIEmCGLUhjh93N+Hf qyw04hMUYPb1WnXz//Nn5ZFdI0ho1k4= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43623bf2a83so53778485e9.0 for ; Wed, 15 Jan 2025 05:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736948176; x=1737552976; 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=9rKypNB63ajEZUNiwKcfWGkLNs9kznO8TMtZ1K+RxGI=; b=cYtsF1vkVJljIBPrgSjoP92p9YW0slNJ4mc8P3Z4cxLYSBh6qb/WEiXtFEdyFIFxCF vftw80zp00RZ7hNsKBbUdr+rw5YcI1qhy9tKdGG7utm47W/nlJBIztj2PAuqF+aYiLct jNdHqaqEV8vyPIBbaxxLFIE/LvF6Ox/fbUVH74jDE65+d5MKjiPYFgWbNNOok8V4NrKZ WQHOsF+2DpEyOGRlupubcGjcFQ75yYuDnz4vu9JK77gCVhoHXOpcsqGSIZ089wsWLObE S9NrSXN1sS8xpQMkt6r0LdmrRN9eQUjP5lHOEJqOUA+CAL5pIMXkECMOBUDS/VpD0md4 KPMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736948176; x=1737552976; 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=9rKypNB63ajEZUNiwKcfWGkLNs9kznO8TMtZ1K+RxGI=; b=pIy6BMffp4DGfb/JPzNJq29gTWYgYZUAInNBGl+2KFNA7ffHIfBHMGH14TN/5OFCAr NDvNDiVO1BPm8Y+ukce8xYBUbiYG2WS/PzenmM/rSpqxUO4kTfeHYRnLSuNpTapUBX65 HqxZuYej4iMjErdV00gMuO8NfQDvqW/touKmtmknFTdzjZSgiDSHbEIGOMVEohnZ5p/I nqjDuJkniN1h5vrUjHGwMG1fAJSDoG8Mw2Muf2ShoDCbx7H4vW+KMAVSgiDCknOge0Ag g8AEmLPkMOW2Jn02eVsq7w8vLLukWiSAQyU3vXJiIOLsy67eX273ECKbsnhufbJARz5R 6LZw== X-Forwarded-Encrypted: i=1; AJvYcCVfgHoeOZdxB5kEhXDdf+cxGe2+Y5MW5TiKjJAyRVXDwi67p/xVvIKnona2GF1tQeHCkMOihd6IhQ==@kvack.org X-Gm-Message-State: AOJu0YxdYsoVB0k5GGeIlu9toPU4svRuqSvZOmktGRAxHlJddc3CkvQX v86Jr4UUC9TjclmFZjeGy2KO4wBTB+b+GTJmJzOuboMX402TEUPwRaQf+fyDGQLLgIGKDWEJ/Ge SO0UZOLuGEzoMpg== X-Google-Smtp-Source: AGHT+IGsuwZ1l0g4W3y3Qu+FgJ8iww+9A3FLFIVNNL+wg7yBLfHHRHUpzUWCiouwB9ZiGGEoPnqIBOqi1jWjsVE= X-Received: from wmbfp18.prod.google.com ([2002:a05:600c:6992:b0:434:9fab:eb5]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:348c:b0:436:ed33:1535 with SMTP id 5b1f17b1804b1-436ed3316a4mr227011565e9.12.1736948175968; Wed, 15 Jan 2025 05:36:15 -0800 (PST) Date: Wed, 15 Jan 2025 13:35:08 +0000 In-Reply-To: <20250115-vma-v12-0-375099ae017a@google.com> Mime-Version: 1.0 References: <20250115-vma-v12-0-375099ae017a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3168; i=aliceryhl@google.com; h=from:subject:message-id; bh=30XR2NvyOSBS+jDMS4+B8d4GBDe/RpEg1dR1knprL04=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnh7m/n84l75cPhMeQdlYkKqGCd0G/G16B/EPtL 8EOZEg0z1eJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ4e5vwAKCRAEWL7uWMY5 Rh8YD/0UTFeQviTFRZ0rYolNkH7u7m9ndG6cRTDSh6VUOR6SOTsz3T0HZAXgaIzUB4ansQZ5FZa Hj2AfkIysDwMHYRE792KZNGtY4D5HnhPG0UvY3yCPTVplkT7QXvyCE+/xmFL0Nd8aoJGRKv25M0 hpm0ihktW9H6xXBBuHgGiF2IVx2QRxsUIWDMw8b+1sByiPrjMoc8QhkOcAxiG5bPrTtgdyjuwkz HbFI8TvbCblTU5qgb1syg8w5crYZiXH3RAnZWA4Nykd1csEn6iNepKikxxADNbht3qjj2/owiAQ TXs26xwpbh5H6M3dwl0QOArMRffQ602vtsmTd2zeAjCpSas1BGt/e141lB2pzMImtmmCM7OFu7J 1OeELsA671/xBaQLwFxqyOhI349LfXixRgGleNTwTdzOfMq3bUvzUEcAenXA6N5fxv78lee6klv OmtUgXTkopsrc7fV/IMu+mvhxVBEVYbflPUoPEeNW6R76MEldQR8HoDvmmKweZt3bGRpuR2F7kL Wo6Q27nXpOZsknq2dCGwxH9Y1DyuIT3y0ah+dYMulo2AQy5jM5iZZBM2Yp5xqHJUPW9tazwd6/8 H63sV0iAZhbIci7jP6KlcVGERq6kLTyAfsz3NBmmfedhmfaLo5Ph0qm3v0+wRTr3c9YIljrqHLT qOJ+CFShyk701OA== X-Mailer: b4 0.13.0 Message-ID: <20250115-vma-v12-5-375099ae017a@google.com> Subject: [PATCH v12 5/8] 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 , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl X-Rspamd-Queue-Id: 2572218000D X-Rspamd-Server: rspam12 X-Stat-Signature: u7wo9nbzqf8uuq8byu7y8gemgumwnkff X-Rspam-User: X-HE-Tag: 1736948176-407360 X-HE-Meta: U2FsdGVkX1+mAypTV+TW1GIaKkrNPhePjs1Rnf9aKIGkMyYjaaigh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0XfZrv7B6RMECUGXD+w1Ypw1+/7rDwcCJRvVqQprL3zfeXaQ8MEDp4fD7SMe6X1PZw+JigrfzcvwIEL05X+hhSJBt63oPPsov3oqiaOCtRlCPe8xEz+KJ+ynvqIDiYxTB/TJYLGI6ZGtenKZg1KN1eLQApvwgUhcX33JJGknWdLEkU2uMvAooKaR2ZS7M/hASrZcK41THyWeg9+cyqrGkLerU8ryHZw/hNZ7zliubGXoZ0rvI3U01dtFrMsrU0P0lx42yz9ZSmj7PD0k/hrV1vW44QO90YX137j5o90uqydZUV7tXrPRY8AF3HlLtoryVs0fdyyUqMB0t3yITZNV+JlzqhlOUNjoav+1ePRk3AUdFyMEfsykU8sB/xNz3ESVqoLQRs1GQ7CiPe8H8hZuW8HN1c2NH4Pt+TM6qxTOFp0mi3hKo/mwQUbF3Ty4Nv3gURUIVGqzhIBQOlJCB1B6ruVkAlcHhzC47tkrCS5kMMpEZCkUfNQmVXajmFPKiXrCtQP0bc7BRtdZlXKfVZZLrUH3b6Z/E79TzLaryEZJf9h9/2CRMw5jIURPm52QP8hN5yh3pVlivppIiRPeUXIGHEzIaDMzjidBV9NVgwiCaEgRh1mw= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.477451, 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. Reviewed-by: Andreas Hindborg Acked-by: Lorenzo Stoakes (for mm bits) 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 60dc66972576..3bb2ccd5fda1 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -110,6 +110,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 { /// Returns a raw pointer to the inner `mm_struct`. @@ -161,6 +203,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 into_mmput_async(me: ARef) -> ARef { + // SAFETY: The layouts and invariants are compatible. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + /// Attempt to access a vma using the vma read lock. /// /// This is an optimistic trylock operation, so it may fail if there is contention. In that