From patchwork Fri Feb 3 07:18:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 13127089 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 26E22C636CC for ; Fri, 3 Feb 2023 07:19:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 540E56B0073; Fri, 3 Feb 2023 02:19:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F0DC6B0074; Fri, 3 Feb 2023 02:19:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 392416B0075; Fri, 3 Feb 2023 02:19:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2B69B6B0073 for ; Fri, 3 Feb 2023 02:19:03 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CFE3CC02D0 for ; Fri, 3 Feb 2023 07:19:02 +0000 (UTC) X-FDA: 80425128924.23.DC2C3AC Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf21.hostedemail.com (Postfix) with ESMTP id 132A51C000A for ; Fri, 3 Feb 2023 07:19:00 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=AHiRLfcf; spf=pass (imf21.hostedemail.com: domain of npiggin@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=npiggin@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675408741; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XnlBvkqnb+l7IeUx8vg7gSpE5ieSkYXmCCbewvNAOcI=; b=WVwTP2m7a8A+NAI0Vx+qpOxBAhSejpdDFpxymtCuRk4f+Koli+0dv5wYvjozEIain2SCiT gMQYZDsYG0q1QyUeff7wu+haTFAx/+d/SMyJQJN0dI5Yb6K9lFyJRhbvMPCUUra3Kk0+5y vOijuCcdh/CU6CSIRh3j5SZmWAcePs4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=AHiRLfcf; spf=pass (imf21.hostedemail.com: domain of npiggin@gmail.com designates 209.85.214.177 as permitted sender) smtp.mailfrom=npiggin@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675408741; a=rsa-sha256; cv=none; b=pU5Gl2iw5k0SfJB80kCZjpUeXQFROACCNh0LMVVb8sx5RcP4yjDAjy9lxOXM0fjdtAjbZl VdH4O8XxzDODiwsUNF5c/olWwiprXQbbIEFd4Y4AOuxCu2JG+JAVJe9h57GQzwm+AcHMtg EVvBEpTIRLvG4U+tUHQsOQRs5JaIh7M= Received: by mail-pl1-f177.google.com with SMTP id d3so4393258plr.10 for ; Thu, 02 Feb 2023 23:19:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XnlBvkqnb+l7IeUx8vg7gSpE5ieSkYXmCCbewvNAOcI=; b=AHiRLfcf8eUNQBnlAsruaF06DgmSEYeMrWMiRHEZ9X5oCS8EFKY0D1+uSzk001rKHG wM6Q2U10Pn14JQyW+l0O/c8z+fkwxIm/NPitBdfs40sfGlyXsENjatpI1RZhPk232lvA zDdUzhnba9x94swlHDb7UkVrJFnMqWpYzmGo4TPTsGtXHfXv9sfrWlf3syDxwLR5ipEX dFRmiJAgq8XvaTNIFp5Qu+WClm6+oFC9LekqRQUzG9QfdZgJPNJtJkIdPe1uHKJYpQRj YyJEeIrYiO6SEa4xtr2WXCweUe8cAWZ/KQ4ykObFGtMcv3jpBg0TZs/m6RR8iSh/nfe2 i5Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XnlBvkqnb+l7IeUx8vg7gSpE5ieSkYXmCCbewvNAOcI=; b=XTE7KXIRr67TKBGxetO82CmZJVMU4xxzDORE7hMigaIbysNZnjpJ5QJXs7lHlMlbbR L23FTrFlsS+sqL8UtwHpDSfrAZUsmfKBuBmAOVyoP6KynGjata4qYHyxWHHy/DNL+rgk ey9wWvuaB5asqLExmCDPf9kStzK6MX8HKogFnSu++M9XiyBCQr3vLPUCSGJMf5/kqxX5 /CCsiO3bVl1B2QkLwjVf8/Ttu29jL2cafFPK92F6BQeY+hE5B8yTVInJvlsPNm23j3IE Mcz1GzF8fIqNhr/PiOrsF8m2Q7CqUsolF8U6vRdMTj65scYxkt2lc1UjoabD6dARPIc5 x8gw== X-Gm-Message-State: AO0yUKVIEyNvrBJe1G/5G8G0vQsBED/NjsYNEUtjFyXnsw2aAeI55lVJ BFG5ecfObq2pgD+yo4jU7e8= X-Google-Smtp-Source: AK7set9/19Ishv+YbUs//IpJgvi84ti72zlNw3k1E7pi36wh6xfDzDjNIuz8qP6WMzuLeNCLPjc4Ag== X-Received: by 2002:a05:6a20:4413:b0:be:cd2f:1951 with SMTP id ce19-20020a056a20441300b000becd2f1951mr12089686pzb.41.1675408740099; Thu, 02 Feb 2023 23:19:00 -0800 (PST) Received: from bobo.ibm.com (193-116-117-77.tpgi.com.au. [193.116.117.77]) by smtp.gmail.com with ESMTPSA id f20-20020a637554000000b004df4ba1ebfesm877558pgn.66.2023.02.02.23.18.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 23:18:59 -0800 (PST) From: Nicholas Piggin To: Andrew Morton Cc: Nicholas Piggin , Linus Torvalds , Nadav Amit , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Catalin Marinas , Will Deacon , Rik van Riel , linux-arch@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v7 1/5] kthread: simplify kthread_use_mm refcounting Date: Fri, 3 Feb 2023 17:18:33 +1000 Message-Id: <20230203071837.1136453-2-npiggin@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230203071837.1136453-1-npiggin@gmail.com> References: <20230203071837.1136453-1-npiggin@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: ti8kno6ywput44a9prazmyki1f55e4e7 X-Rspamd-Queue-Id: 132A51C000A X-HE-Tag: 1675408740-446276 X-HE-Meta: U2FsdGVkX19Ij2NNjTAd10HtTSn/w5hsuhIXfjUD4xZZWk5CIQ8+NujxBFNLBaeeUs1+nOI9ToimRUmdVl9JP4lf6959ukHJj+sr2HN0NdP8j5boJG52kLZ39fUddnWpXryyD56unrQsGYGvS44bjRB1SpTLXHhFvKlj+tO4mWZ76/X166wwlr8LFS3Itg0srAFLTA7R35ng/PedGi4W1Ua+BUt0po52M15bfJd5fSihveNEIAqY/vVltia/cL+VF0g+ghU89qWSwGe2by5SEqTwtGwBgzcyrlhuniwrYYbYq0TTL9BE2E9PvOlFDYY5qLRvt6X0qRYtKTgZwl94vBrQ/wzzFqSEBg+dKJ7THEGaraXQpcgHNOnt1YoWqhPOX9CMFzYMCLQ15riix27AFN6+RO4evB4ejqHUxV8E/p9bmE8NyMxvt+b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9CKzBWrHX05OmHC0rUfJ9hAGNm9E27x4pwkfmdPg88rq5QAFs3KzZ87jzdLdjnh/iYRjCvPQbBdakaq1YvmxY65rltOTJ1Vr0VmR+4f9o8sUZmefS2zkcrs= 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: Remove the special case avoiding refcounting when the mm to be used is the same as the kernel thread's active (lazy tlb) mm. kthread_use_mm() should not be such a performance critical path that this matters much. This simplifies a later change to lazy tlb mm refcounting. Acked-by: Linus Torvalds Signed-off-by: Nicholas Piggin --- kernel/kthread.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index f97fd01a2932..7424a1839e9a 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1410,14 +1410,13 @@ void kthread_use_mm(struct mm_struct *mm) WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); WARN_ON_ONCE(tsk->mm); + mmgrab(mm); + task_lock(tsk); /* Hold off tlb flush IPIs while switching mm's */ local_irq_disable(); active_mm = tsk->active_mm; - if (active_mm != mm) { - mmgrab(mm); - tsk->active_mm = mm; - } + tsk->active_mm = mm; tsk->mm = mm; membarrier_update_current_mm(mm); switch_mm_irqs_off(active_mm, mm, tsk); @@ -1434,12 +1433,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(). */ - if (active_mm != mm) - mmdrop(active_mm); - else - smp_mb(); + mmdrop(active_mm); } EXPORT_SYMBOL_GPL(kthread_use_mm);