Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp213279pxb; Mon, 2 Nov 2020 19:23:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJyj5mB4kYUn3sFr/iG+QWZ9JHGVaaNPuTebtz5Mfkelb32C0G8qiXhqgG1o9oXxzHnH8VbU X-Received: by 2002:a17:906:512:: with SMTP id j18mr18776721eja.370.1604373825060; Mon, 02 Nov 2020 19:23:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604373825; cv=none; d=google.com; s=arc-20160816; b=AwwVxRcEKOPnFMJXBeh+kJcIbnqhfbUYWBUr0oiHvIG9tSC2UojNszGhbe15S9iZXD xHXrrNKxxWYWCe3ZuZsVOI1ZbMNPQhlwgeG0hjlK7IBspPMgmsNi/I8mPNKmZVYIGs28 u3EGKMXY+bwr0r37ZSRe4FqbFCAXQFjfO2f237hS4nZaVtmqTTAjiM6J2grh16tv2yZ+ Z5HAKNYNaGnJvz9ZQM3OUSbi0ycDuRDnP1qBa7GA+p2ByKKM+jfo4v26Ne7LckLRLh/G BKEMqxhoTaL7BuLcRm/p5w1zKTeVjfsCgI1GTm0OlxByvQbafiREH837U0Ir89mH0B7O OkCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wGFIq0N/0aUl+phcBiONvIjSg1NTDz3wTK7dzOr5chM=; b=NiEQubf1BC52GamLdE/XrRCiYv4uRyxBVOj2uYybh2yDDbEjGgFfYrwfh8K3h0zpDj c8XJNYlstFlvdLMS7ofd3e0X4aOZWlAcuvPV17WNu5m7tfgjEb2PWDSOk9FEfuhafzWY Xi8kKDGYX3jcAFnNvkZel35z5z+N0mNizDuqyWGDoTTesUoxnyyns8buAhUpCJS1e/u+ Uu7Mr0V2gvmVxnPb996WBTDooNUTrv5GWWitYD0Iz5poEt8bGn3pSKsTJ0NGAmt6SbsC 99A7tiCEZwJS4jQh89RmEhxCQTdSrJD8EdNavPOu+RiP40pyNmnpyJMOy1wgZif6dHiJ OXFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Zg5KF+3K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p14si11517409eju.470.2020.11.02.19.23.21; Mon, 02 Nov 2020 19:23:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Zg5KF+3K; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725968AbgKCDTk (ORCPT + 99 others); Mon, 2 Nov 2020 22:19:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725913AbgKCDTj (ORCPT ); Mon, 2 Nov 2020 22:19:39 -0500 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 836D6C0617A6 for ; Mon, 2 Nov 2020 19:19:39 -0800 (PST) Received: by mail-lf1-x144.google.com with SMTP id l28so20219155lfp.10 for ; Mon, 02 Nov 2020 19:19:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=wGFIq0N/0aUl+phcBiONvIjSg1NTDz3wTK7dzOr5chM=; b=Zg5KF+3KOopx63jJc4i6NewIlZ0HW1FuJAwYCvwxwmkGP6R8q4SUFjih25qIte59DZ bJiANDAXk+laUxVRGIgi2cH/wPQJ3ihaMjNTSJzkxtEdAdupNxRiAcJPNuip1HJpcI6V 8265I/d1SUaeqfTqk98FHza+94z/lBcb6CeCFMM2ubGGUVSVspdJ4sCFGZrw5X8cLF0m qFmZt6JXZp/D0q6JTtKqw3rcrJQFgO0Z6Ysx5tBT/WtgoOa2OPn92iLVbGz7D8i0tE4o TMKY/c2Zoy31VdNF0nmNR1+SgOXgYHuBBjJtDbdKf8u0OTsbtFPyOkzFs1FradqtxbSQ pJ2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=wGFIq0N/0aUl+phcBiONvIjSg1NTDz3wTK7dzOr5chM=; b=tNzZqVgQibxpeTyR+Ag1UMhgIhNZvfaPuKAcR5QOVA2fiV/oIKZrpbA0oDu23X6SIP SsGgvDDGjt9B5t6lHiwwZWymQL1gU3pVRLOcqXQcYrUA7H2s5u0+lRSP9WyvfZaaL/V/ kPo+oi85a9pUiCbNRjogQmijvp8rjUmB2Zv39jcd+MCu2CCcYHx2lGOgV/UbEZiRuIV5 lq+2wjv+tMVEpmRUXPb8ERlUK0mIcuG89U/Qct9khM3Nwn1b+Kmv64aMum9AG/dd479V zvAnHHVcVRzcdGrHHotREB2oUf5eKdxK1pEK9NQW84EI1ZpxXKQkVyIM3SZQctVIuuOo PYgQ== X-Gm-Message-State: AOAM533sAwOZzcasECe5WFjhwXalkQNO2wb0sbcWmAdqCySzOK3wmpQe 1ZrgdepXMAWODFTRWyiFRCUEefImTsQ0FGjwZizNKSrpVv8= X-Received: by 2002:a05:6512:51a:: with SMTP id o26mr6375560lfb.381.1604373577739; Mon, 02 Nov 2020 19:19:37 -0800 (PST) MIME-Version: 1.0 References: <20201016230915.1972840-1-jannh@google.com> <20201016230915.1972840-4-jannh@google.com> <20201016232153.GD37159@ziepe.ca> In-Reply-To: From: Jann Horn Date: Tue, 3 Nov 2020 04:19:11 +0100 Message-ID: Subject: Re: [RFC PATCH resend 3/6] mm: Add refcount for preserving mm_struct without pgd To: Jason Gunthorpe Cc: Andrew Morton , Linux-MM , Eric Biederman , Oleg Nesterov , kernel list , Will Deacon , Kees Cook , Ingo Molnar Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 3, 2020 at 3:11 AM Jann Horn wrote: > On Sat, Oct 17, 2020 at 2:30 AM Jann Horn wrote: > > On Sat, Oct 17, 2020 at 1:21 AM Jason Gunthorpe wrote: > > > On Sat, Oct 17, 2020 at 01:09:12AM +0200, Jann Horn wrote: > > > > Currently, mm_struct has two refcounts: > > > > > > > > - mm_users: preserves everything - the mm_struct, the page tables, the > > > > memory mappings, and so on > > > > - mm_count: preserves the mm_struct and pgd > > > > > > > > However, there are three types of users of mm_struct: > > > > > > > > 1. users that want page tables, memory mappings and so on > > > > 2. users that want to preserve the pgd (for lazy TLB) > > > > 3. users that just want to keep the mm_struct itself around (e.g. for > > > > mmget_not_zero() or __ptrace_may_access()) > > > > > > > > Dropping mm_count references can be messy because dropping mm_count to > > > > zero deletes the pgd, which takes the pgd_lock on x86, meaning it doesn't > > > > work from RCU callbacks (which run in IRQ context). In those cases, > > > > mmdrop_async() must be used to punt the invocation of __mmdrop() to > > > > workqueue context. > > > > > > > > That's fine when mmdrop_async() is a rare case, but the preceding patch > > > > "ptrace: Keep mm around after exit_mm() for __ptrace_may_access()" makes it > > > > the common case; we should probably avoid punting freeing to workqueue > > > > context all the time if we can avoid it? > > > > > > > > To resolve this, add a third refcount that just protects the mm_struct and > > > > the user_ns it points to, and which can be dropped with synchronous freeing > > > > from (almost) any context. > > > > > > > > Signed-off-by: Jann Horn > > > > --- > > > > arch/x86/kernel/tboot.c | 2 ++ > > > > drivers/firmware/efi/efi.c | 2 ++ > > > > include/linux/mm_types.h | 13 +++++++++++-- > > > > include/linux/sched/mm.h | 13 +++++++++++++ > > > > kernel/fork.c | 14 ++++++++++---- > > > > mm/init-mm.c | 2 ++ > > > > 6 files changed, 40 insertions(+), 6 deletions(-) > > > > > > I think mmu notifiers and the stuff in drivers/infiniband/core/ can be > > > converted to this as well.. > > > > > > Actually I kind of wonder if you should go the reverse and find the > > > few callers that care about the pgd and give them a new api with a > > > better name (mmget_pgd?). > > > > Yeah, that might make more sense... as long as I'm really sure about > > all the places I haven't changed. ^^ > > > > I'll try to change it as you suggested for v2. > > Actually, no - I think it ends up being around 30 mentions of the > "take reference without PGD" function and around 35 mentions of the > "take reference with PGD" function (assuming that all the weird > powerpc stuff I don't understand needs the mm_context to not yet be > destroyed). (A decent chunk of which are all the per-arch functions > for starting secondary processors.) So I don't think doing it the way > you suggested would really make the patch(es) smaller. > > And I think that it is helpful for review purposes to have separate > patches for every converted site, and leave things as-is by default. > If the semantics change for every user that is *not* touched by the > patch, that makes it really easy for mistakes to slip through. > > I could try to convert more callers though? But really, I would be happiest if I could just leave all the callers where both refcounts work as-is, and let people more familiar with the subsystems switch them over when that actually becomes necessary. Is that not acceptable?