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 X-Spam-Level: X-Spam-Status: No, score=-6.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 016CFC433E7 for ; Fri, 16 Oct 2020 23:21:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5A13622201 for ; Fri, 16 Oct 2020 23:21:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="euz46EZx" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A13622201 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B9FE16B005D; Fri, 16 Oct 2020 19:21:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B512E6B0062; Fri, 16 Oct 2020 19:21:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A40BD6B0068; Fri, 16 Oct 2020 19:21:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0118.hostedemail.com [216.40.44.118]) by kanga.kvack.org (Postfix) with ESMTP id 6B0A46B005D for ; Fri, 16 Oct 2020 19:21:57 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 04B6F8249980 for ; Fri, 16 Oct 2020 23:21:57 +0000 (UTC) X-FDA: 77379363474.10.brake22_0004bf327221 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id D61C3169F9A for ; Fri, 16 Oct 2020 23:21:56 +0000 (UTC) X-HE-Tag: brake22_0004bf327221 X-Filterd-Recvd-Size: 5316 Received: from mail-il1-f196.google.com (mail-il1-f196.google.com [209.85.166.196]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Fri, 16 Oct 2020 23:21:56 +0000 (UTC) Received: by mail-il1-f196.google.com with SMTP id i7so2475102ils.7 for ; Fri, 16 Oct 2020 16:21:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=8ulZ+tmdqBb+S0GzKOYD5fGwSZeOurFPRpc1Xyo7iyc=; b=euz46EZxLI9Anl4rbigu2pBhX9aMKb3nLNFgytSoAzzEqHArebFezY2UQFuF6yZi78 7BV5knmNrMDGVavcJpxNx+rfC1XKoF67IV+kiB/vrFKgXWI8/Ha6V0blusE083OXVDbp mVJWjIvA9cwEyqCjyBouIC4W2lsyqurDIAvKefJj0e+CZOwzKhd5aX50Yz/wOaJdmFBx pdIrh/HLFlrHC8NR8zcr1N6shVvwtnP5gD/Q60zy6Yo694nxG8XYi4seRD14PxjJYGU1 KtfzdqUypV2SjOWNCUb4kq6vzGkHXadbDsnI8rFJcQ7knwhKrPgp2Fk9UbaKka1PL/fT kdWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=8ulZ+tmdqBb+S0GzKOYD5fGwSZeOurFPRpc1Xyo7iyc=; b=s5DDJUiBlDO2WTd7R7FUEtRZas2BR1Z0MmG9ID/Eju8GlDY9+eNIWvJf8BflUCJ9ww iifZOF682OVyUZ4wvoYsN81QH0IyQ6T4v8qQyH7Yp3s9POeaQ4pcGeJzSxQsK2OWHcaI 6ukdpbEfv2J17+SePYzFCNMvlEXQlHWyaUxxpHvla0FqIFdZY3M6JBZ5gaRWnPSWLw6t LoeT2UmhmL1JoCKdqh+BrkoAeRBNAzjswJfeTVR/jRO6awWocM14ZCkqc9loEe+Xmq4e YjF2+eJ+4eskGTjgOmNGgS5HBylLo1+WTzqfPkGqbSL6PUoWV2dDZF+jBQmPq+cIXhzT GLyA== X-Gm-Message-State: AOAM5326XRqa0x/vSy7vqU6pa7p8H/hbE1pNf4XgUhxvwXifdynUE71z /b1QNOrKi4wy6HHGJp1mLoZJnA== X-Google-Smtp-Source: ABdhPJzjFd8h3bHaBoh0ogIzaDiyMMdYUfkan6OvINr5ocgY2Acjoku40/3OIptHi7g/ZGy1gzm0Sg== X-Received: by 2002:a92:d5c1:: with SMTP id d1mr4372060ilq.212.1602890515637; Fri, 16 Oct 2020 16:21:55 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id 144sm4089074ila.42.2020.10.16.16.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Oct 2020 16:21:54 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kTZ2n-001Abo-Uc; Fri, 16 Oct 2020 20:21:53 -0300 Date: Fri, 16 Oct 2020 20:21:53 -0300 From: Jason Gunthorpe To: Jann Horn Cc: Andrew Morton , linux-mm@kvack.org, Eric Biederman , Oleg Nesterov , linux-kernel@vger.kernel.org, Will Deacon , Kees Cook , Ingo Molnar Subject: Re: [RFC PATCH resend 3/6] mm: Add refcount for preserving mm_struct without pgd Message-ID: <20201016232153.GD37159@ziepe.ca> References: <20201016230915.1972840-1-jannh@google.com> <20201016230915.1972840-4-jannh@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201016230915.1972840-4-jannh@google.com> 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: 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?). Jason