Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp802150pxb; Thu, 23 Sep 2021 10:50:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcah4vVGr2I5Qgai6CHnOFz3gxkiTX6Y2gGj3+7tNavu0BGt3EJgAZSKwkj7Vkb6zw3Z/e X-Received: by 2002:a92:c548:: with SMTP id a8mr4775845ilj.295.1632419452291; Thu, 23 Sep 2021 10:50:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632419452; cv=none; d=google.com; s=arc-20160816; b=r6Yy2JjhHknTPveGi2OhRML2SYsyoVp9IctnHyTgZ1KCwH7WhlhfT7fo9leSelLqba FSegPXy1+RPHnEYEXmsKPV6zTTX9i6RQftYBZsVKFSe/xjxstOEHBTeY8aNMJtcRdIg/ N0fT3DM71+vPmx2ONm+mn88hNllYBXmENX2a12RtEp8VWrELCCATZ+wiCKnKwHj+xt6r 0S7k6u5i5f8q+Ac0bg4wf5ruX3SXnPTgO/IAWUl0m8xQERF5gKVOqdNsa0QDald29hk8 ULJTRhxIQkoDz+V3BEGpV/Rig7hnM82wM3FRjTyEQ0qMBiGojCgnscAsv3yP3tAsf4pI idzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:dkim-signature:dkim-signature:from; bh=jI90BA5MPkAckY4vCCiww4UBg58P9vuxvJTxiYDoROo=; b=1FPP6DVKZbIzRPWhk/uPbxWre5raoydqgR2R+kyr4820LWAYIJf4prgMBIsJ2HPyq9 WaqPHcwwNds6XcRVp+Z7bRD/mV6Jh0Myqd6xz6fCZ2JcV5Wk61m3v/H71yUzGYWE+/6a sk3GZ3qD2AsCz+r/UJP8BXF3i0jzXOueQtWtk1CBPCDNAoKZMg5Bq172r7uwDqwdmv2v XwV+GqygRamGfLMCBIiINAf+z29ry5JnMXy5kCER0isvEHJ9ANt9Cxi4E2BovITDy21X s6HQecNGG/6nlgAf0hHOHjtJaf8B/sf7JMRKO+CDXZx+mDEOaxaMF2lsPx2whApmerlX igaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0UJ1kq8z; dkim=neutral (no key) header.i=@linutronix.de header.b="ORk/j6uy"; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c8si7544437ilu.80.2021.09.23.10.50.40; Thu, 23 Sep 2021 10:50:52 -0700 (PDT) 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=@linutronix.de header.s=2020 header.b=0UJ1kq8z; dkim=neutral (no key) header.i=@linutronix.de header.b="ORk/j6uy"; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242158AbhIWRt5 (ORCPT + 99 others); Thu, 23 Sep 2021 13:49:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242198AbhIWRtq (ORCPT ); Thu, 23 Sep 2021 13:49:46 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E26CDC061574 for ; Thu, 23 Sep 2021 10:48:13 -0700 (PDT) From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1632419292; 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=jI90BA5MPkAckY4vCCiww4UBg58P9vuxvJTxiYDoROo=; b=0UJ1kq8zXk/55UFzhAZk8Pa/XZuXjzCVfxiIxP2wPsrRq/WS8NctFN9pPIrs3nmofqPIP7 H2aPjzRh9GR+G9Cdb8xTKWQufz1VHAQ6dujOII5dU+SvvFZ6ykivpnYLA/Ah1uTOZt/ulB kNKfoZsD3Kb4/+Vx0LNVNygBQ0+ky6S5krGcltgbWTDxsI4dzNlM68MNSkDxrHF0QykUAA O9pkfxEdPXUb/iI1LEehs5tBa8v8bTd9tEmIovUEIpBseIU/ZySvqgn8125Dhvh8w0phry n1W85HXgx53+fXcgANboDgJUTbFKkNRPO1pfr3pzAHoKKz5LPPteZ7/TGQJxSw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1632419292; 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=jI90BA5MPkAckY4vCCiww4UBg58P9vuxvJTxiYDoROo=; b=ORk/j6uylsTqc5jG0yKd1pGXrqufsWVy4PgKm8KzRi0t/b5TInyrmgTwhDGlN5E+6QEECX z2wrzbL+gvYV1XCQ== To: "Luck, Tony" Cc: Fenghua Yu , Ingo Molnar , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , Dave Hansen , Lu Baolu , Joerg Roedel , Josh Poimboeuf , Dave Jiang , Jacob Jun Pan , Ashok Raj , Ravi V Shankar , iommu@lists.linux-foundation.org, x86 , linux-kernel Subject: Re: [PATCH 5/8] x86/mmu: Add mm-based PASID refcounting In-Reply-To: References: <20210920192349.2602141-1-fenghua.yu@intel.com> <20210920192349.2602141-6-fenghua.yu@intel.com> <87y27nfjel.ffs@tglx> Date: Thu, 23 Sep 2021 19:48:11 +0200 Message-ID: <87o88jfajo.ffs@tglx> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Sep 23 2021 at 09:40, Tony Luck wrote: > On Thu, Sep 23, 2021 at 04:36:50PM +0200, Thomas Gleixner wrote: >> This code is again defining that PASID is entirely restricted to >> INTEL. It's true, that no other vendor supports this, but PASID is >> a non-vendor specific concept. >> >> Sticking this into INTEL code means that any other PASID implementation >> has to rip it out again from INTEL code and make it a run time property. >> >> The refcounting issue should be the same for all PASID mechanisms which >> attach PASID to a mm. What's INTEL specific about that? >> >> So can we pretty please do that correct right away? > > It's a bit messy (surprise). > > There are two reasons to hold a refcount on a PASID > > 1) The process has done a bind on a device that uses PASIDs > > This one isn't dependent on Intel. Yep. > 2) A task within a process is using ENQCMD (and thus holds > a reference on the PASID because IA32_PASID MSR for this > task has the PASID value loaded with the enable bit set). > > This is (currently) Intel specific (until others > implement an ENQCMD-like feature to allow apps to > access PASID enabled devices without going through > the OS). Right, but all it does is to grab another reference on the PASID and if the task exits it needs to be dropped, right? So you already added 'has_valid_pasid' to task_struct, which is a misnomer btw. The meaning of this flag is that the task is actually using the PASID (in the ENQCMD case written to the MSR) beyond the purposes of the PASID which is attached to current->mm. But the information which is important from a pasid resource management POV is whether the task actually holds a seperate refcount on the PASID or not. That's completely generic. It does not matter whether the task uses it to populate the IA32_PASID_MSR or to just keeps it in memory just because it can. The point is that is holds a refcount. So we can have a generic interface: int iommu_pasid_get_task_ref() { if (current->holds_pasid_ref) return -EYOUGOTONEALREADY; if (!has_pasid(current->mm) return -EWHATAREYOUASKINGFOR; if (!iommu->pasid_get_ref) return -EHOWDIDYOUMAKEUPAPASID; if (iommu->pasid_get_ref()) return -ETHEIOMMUDOESNOTLIKEYOU; current->holds_pasid_ref = true; return 0; } Actually letting this return a bool should be good enough, but you get the idea. void iommu_pasid_put_task_ref() { if (!current->holds_pasid_ref) return; current->holds_pasid_ref = false; iommu->pasid_put_ref(); } Which makes the exception handling in traps.c the real x86/intel specific muck: bool fixup_pasid_exception(...) { /* ENCQMD and future muck */ if (!per_task_pasid_usage_enabled()) return false; if (iommu_pasid_get_ref()) return false; fpu_write_task_pasid(); return true; } fpu_write_task_pasid() can just grab the pasid from current->mm->pasid and be done with it. The task exit code can just call iommu_pasid_put_task_ref() from the generic code and not from within x86. That means you want in Kconfig: PASID_TASK_REFS bool and select that when a IOMMU supporting it is enabled and provide either the proper protypes or stub functions depending on that. Hmm? Thanks, tglx