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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9114C433FE for ; Thu, 12 May 2022 13:30:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 515866B0075; Thu, 12 May 2022 09:30:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49EB96B0078; Thu, 12 May 2022 09:30:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3164B6B007B; Thu, 12 May 2022 09:30:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 206A16B0075 for ; Thu, 12 May 2022 09:30:05 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id EB32F8060D for ; Thu, 12 May 2022 13:30:04 +0000 (UTC) X-FDA: 79457174328.15.92EE8CC Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by imf14.hostedemail.com (Postfix) with ESMTP id 13F261000AD for ; Thu, 12 May 2022 13:30:01 +0000 (UTC) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1652362202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jh/jFbnSUA7IWAzYFoSb7APluz7HA7PKXomh5dcaByM=; b=fRsO4W3fk23MjEkXh9sd7FucAJARujP68JUxGf5kyNywn9isXTs1vCI2Pt+Y2qJXhLfogX 13saEhEqvFrc2p0vyyWeHR0SlG8Vfx0RB193zm/hYKO2N8PZMhJ1iSV3Ep2Ff7HB3AsMHo 5mtOlYk2NMXWa5dO735mzynKgt8LKxjl+eleD/ARq8oYA+/+BU6grmoJ6sD0YOLg20YgjX f9i854gOWGfg7ptRZFVfHbedvvEVkNN3606k1PE/5xUT9rnD691gQVNKtrKF+PHTfBY6vH 6yQUdhd7QZ735zuBncutoBUasZ01LhB7xEsOgOE34G5Dya8ipXEIxo+2LsL27Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1652362202; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jh/jFbnSUA7IWAzYFoSb7APluz7HA7PKXomh5dcaByM=; b=T12LhOIo4k09ChfV17VLUuuDL26U+nTtwCeDRbjCSbJBnnw37TCkkByi2ts+5O89QuZmVD 4XmUByyA7rdd+fAQ== To: "Kirill A. Shutemov" , Dave Hansen , Andy Lutomirski , Peter Zijlstra Cc: x86@kernel.org, Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , "H . J . Lu" , Andi Kleen , Rick Edgecombe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: Re: [RFCv2 07/10] x86/mm: Handle tagged memory accesses from kernel threads In-Reply-To: <20220511022751.65540-9-kirill.shutemov@linux.intel.com> References: <20220511022751.65540-1-kirill.shutemov@linux.intel.com> <20220511022751.65540-9-kirill.shutemov@linux.intel.com> Date: Thu, 12 May 2022 15:30:01 +0200 Message-ID: <878rr6x4iu.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain X-Stat-Signature: 4sce4i6d99gzf6r1mtnyuwaxet5nk19t X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 13F261000AD X-Rspam-User: Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linutronix.de header.s=2020 header.b=fRsO4W3f; dkim=pass header.d=linutronix.de header.s=2020e header.b=T12LhOIo; dmarc=pass (policy=none) header.from=linutronix.de; spf=pass (imf14.hostedemail.com: domain of tglx@linutronix.de designates 193.142.43.55 as permitted sender) smtp.mailfrom=tglx@linutronix.de X-HE-Tag: 1652362201-582124 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 Wed, May 11 2022 at 05:27, Kirill A. Shutemov wrote: > diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c > index f9fe71d1f42c..b320556e1c22 100644 > --- a/arch/x86/mm/tlb.c > +++ b/arch/x86/mm/tlb.c > @@ -185,6 +185,34 @@ static u8 gen_lam(struct task_struct *tsk, struct mm_struct *mm) > if (!tsk) > return LAM_NONE; > > + if (tsk->flags & PF_KTHREAD) { > + /* > + * For kernel thread use the most permissive LAM > + * used by the mm. It's required to handle kernel thread > + * memory accesses on behalf of a process. > + * > + * Adjust thread flags accodringly, so untagged_addr() would > + * work correctly. > + */ > + > + tsk->thread.features &= ~(X86_THREAD_LAM_U48 | > + X86_THREAD_LAM_U57); > + > + switch (mm->context.lam) { > + case LAM_NONE: > + return LAM_NONE; > + case LAM_U57: > + tsk->thread.features |= X86_THREAD_LAM_U57; > + return LAM_U57; > + case LAM_U48: > + tsk->thread.features |= X86_THREAD_LAM_U48; > + return LAM_U48; Pretending that LAM is configurable per thread and then having a magic override in the per process mm when accessing that process' memory from a kernel thread is inconsistent, a horrible hack and a recipe for hard to diagnose problems. LAM has to be enabled by the process _before_ creating threads and then stay enabled until the whole thing dies. That's the only sensible use case. I understand that tsk->thread.features is conveniant for the untagging mechanism, but the whole setup should be: prctl(ENABLE, which) if (can_enable_lam(which)) { mm->lam.c3_mask = CR3_LAM(which); mm->lam.untag_mask = UNTAG_LAM(which); current->thread.lam_untag_mask = mm->lam.untag_mask; } and can_enable_lam(which) if (current_is_multithreaded()) return -ETOOLATE; if (current->mm->lam_cr3_mask) return -EBUSY; .... Now vs. kernel threads. Doing this like the above is just the wrong place. If a kernel thread accesses user space memory of a process then it has to invoke kthread_use_mm(), right? So the obvious point to cache that setting is in kthread_use_mm() and kthread_unuse_mm() clears it: kthread_use_mm() current->thread.lam_untag_mask = mm->lam.untag_mask; kthread_unuse_mm() current->thread.lam_untag_mask = 0; This makes all of the mechanics trivial because CR3 switch then simply does: new_cr3 |= mm->lam.c3_mask; No conditionals and evaluations, nothing. Just straight forward and comprehensible code. Thanks, tglx