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=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 D4EABC47082 for ; Tue, 8 Jun 2021 04:11:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7ABB761139 for ; Tue, 8 Jun 2021 04:11:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7ABB761139 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C194F6B006C; Tue, 8 Jun 2021 00:11:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BA2556B006E; Tue, 8 Jun 2021 00:11:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F4856B0070; Tue, 8 Jun 2021 00:11:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0113.hostedemail.com [216.40.44.113]) by kanga.kvack.org (Postfix) with ESMTP id 68DD96B006C for ; Tue, 8 Jun 2021 00:11:11 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id EC60A181AEF0B for ; Tue, 8 Jun 2021 04:11:10 +0000 (UTC) X-FDA: 78229231500.30.7AF0D8F Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf16.hostedemail.com (Postfix) with ESMTP id 3A2B580192EE for ; Tue, 8 Jun 2021 04:11:08 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id h1so2264958plt.1 for ; Mon, 07 Jun 2021 21:11:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:subject:to:cc:references:in-reply-to:mime-version :message-id:content-transfer-encoding; bh=fujJUO0q2KI09Gf2Ts/MK4OrEH1MH3pS5M4uIA8d5KY=; b=tcYZlrbvJ6bwWx5hOtunH5FlXCQSIc1mB7bzpNZe23fnuXN0P1B9qK3liym3q2fqMF TUBPyWK1ZOMpCS5yddkJ84bAoIVftpAefDggUsr/uTdyBxgzLoSfNQgO8rOiBSXxqygT GFbGXnJNu5IgI+IGstU3e+dbZmSTU3+aKi63tb64KMuXH0FHCXbVV3RvKJg36CCH1FMk IKqtLtxeEw6JgGPC948jypG9g07rOnFHiNY01N8t+NBzZqm0DAIvsdbIQqiGg0jLoAB9 iXjAI2gZS5Du/dCK9shEQibTPfE88qR6UO3ZkZNZMTDFXB7KAlo11gDoRr6gyUtrDQRk cREA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:subject:to:cc:references:in-reply-to :mime-version:message-id:content-transfer-encoding; bh=fujJUO0q2KI09Gf2Ts/MK4OrEH1MH3pS5M4uIA8d5KY=; b=f6JGbUUDZfrfaL6dETtntM2jHTL7D4g9EDP8CSC+41BWQnFDfbCNrOcnZs5eHC4K+b 3in3IbGfvqYNaTsM+IAC0dFq8nomJjFkddsrueXhKNR0HH0zasoIXu8TC6VSbCIqfSVx Vm64GRIjV96hpeGdNy7wGHePeGFli7W2QoGqYSnGOAb2goXePlwrYG06DFE9L+/7U1f8 u2L5SSXXIOS9WGEKZcIdyKEoRXhRoYkXtDHSeTZ0Kvs5vD5QdMeEjM6cTGhVId05hzgy KANa7R4nnsyBXmSFjGOWgLwFIg77RNcb+2rT3WO82WYnI5HELFrqV4S8xsQlRs7QcrxP 0CXw== X-Gm-Message-State: AOAM530I1gXMIUc5ICEDmHXNGNZKl46lSCz2Wn207G2/puo3aeecz56B zs9zTDxdlkXAFV75oe8mBFvr3Scg1cY= X-Google-Smtp-Source: ABdhPJxfxri2+uhmnYtzY5IcP7B7q38QCMIjrONmVg0s3CUrHRF1z33+8Ff6Hymls1OmPLXdOM7vxA== X-Received: by 2002:a17:90a:8b0d:: with SMTP id y13mr4219564pjn.88.1623125469552; Mon, 07 Jun 2021 21:11:09 -0700 (PDT) Received: from localhost (60-242-147-73.tpgi.com.au. [60.242.147.73]) by smtp.gmail.com with ESMTPSA id w79sm2876301pff.21.2021.06.07.21.11.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:11:09 -0700 (PDT) Date: Tue, 08 Jun 2021 14:11:04 +1000 From: Nicholas Piggin Subject: Re: [PATCH v4 1/4] lazy tlb: introduce lazy mm refcount helper functions To: Andrew Morton Cc: Anton Blanchard , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, Andy Lutomirski , =?iso-8859-1?q?Randy=0A?= Dunlap References: <20210605014216.446867-1-npiggin@gmail.com> <20210605014216.446867-2-npiggin@gmail.com> <20210607164934.d453adcc42473e84beb25db3@linux-foundation.org> <1623116020.vyls9ehp49.astroid@bobo.none> <20210607184805.eddf8eb26b80e8af85d5777e@linux-foundation.org> In-Reply-To: <20210607184805.eddf8eb26b80e8af85d5777e@linux-foundation.org> MIME-Version: 1.0 Message-Id: <1623125298.bx63h3mopj.astroid@bobo.none> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=tcYZlrbv; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of npiggin@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=npiggin@gmail.com X-Stat-Signature: jgwke646zgk464ys5ft4hsoicrz6hhda X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3A2B580192EE X-HE-Tag: 1623125468-734349 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: Excerpts from Andrew Morton's message of June 8, 2021 11:48 am: > On Tue, 08 Jun 2021 11:39:56 +1000 Nicholas Piggin wr= ote: >=20 >> > Looks like a functional change. What's happening here? >>=20 >> That's kthread_use_mm being clever about the lazy tlb mm. If it happened= =20 >> that the kthread had inherited a the lazy tlb mm that happens to be the=20 >> one we want to use here, then we already have a refcount to it via the=20 >> lazy tlb ref. >>=20 >> So then it doesn't have to touch the refcount, but rather just converts >> it from the lazy tlb ref to the returned reference. If the lazy tlb mm >> doesn't get a reference, we can't do that. >=20 > Please cover this in the changelog and perhaps a code comment. >=20 Yeah fair enough, I'll even throw in a bug fix as well (your nose was right= ,=20 and it was too clever for me by half...) Thanks, Nick -- Fix a refcounting bug in kthread_use_mm (the mm reference is increased unconditionally now, but the lazy tlb refcount is still only dropped only if mm !=3D active_mm). And an update for the changelog: If a kernel thread's current lazy tlb mm happens to be the one it wants to use, then kthread_use_mm() cleverly transfers the mm refcount from the lazy tlb mm reference to the returned reference. If the lazy tlb mm reference is no longer identical to a normal reference, this trick does not work, so that is changed to be explicit about the two references. Signed-off-by: Nicholas Piggin --- kernel/kthread.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index b70e28431a01..5e9797b2d06e 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1314,6 +1314,11 @@ void kthread_use_mm(struct mm_struct *mm) WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); WARN_ON_ONCE(tsk->mm); =20 + /* + * It's possible that tsk->active_mm =3D=3D mm here, but we must + * still mmgrab(mm) and mmdrop_lazy_tlb(active_mm), because lazy + * mm may not have its own refcount (see mmgrab/drop_lazy_tlb()). + */ mmgrab(mm); =20 task_lock(tsk); @@ -1338,12 +1343,9 @@ void kthread_use_mm(struct mm_struct *mm) * memory barrier after storing to tsk->mm, before accessing * user-space memory. A full memory barrier for membarrier * {PRIVATE,GLOBAL}_EXPEDITED is implicitly provided by - * mmdrop(), or explicitly with smp_mb(). + * mmdrop_lazy_tlb(). */ - if (active_mm !=3D mm) - mmdrop_lazy_tlb(active_mm); - else - smp_mb(); + mmdrop_lazy_tlb(active_mm); =20 to_kthread(tsk)->oldfs =3D force_uaccess_begin(); } --=20 2.23.0