Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2733930pxb; Mon, 25 Apr 2022 00:50:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyt4aDhElJ9mXRPiGgRCwKynaBHVzBYj5SkxyZICDUrR2IslBAr66nmGi6ftgJEi2m3CGP X-Received: by 2002:a17:907:168a:b0:6df:ad44:3009 with SMTP id hc10-20020a170907168a00b006dfad443009mr15513300ejc.176.1650873058748; Mon, 25 Apr 2022 00:50:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650873058; cv=none; d=google.com; s=arc-20160816; b=CfTL9MBd0W9MoZ56Aef7In7fYxvfw2ou5TIsUkV89fEO/uvi9YGmD7eUSqgNHjJ/35 ntxYi6w8vpKrEHQyLcyWFTAXBdpcPsL1OsKsRd9Gl/k5D+Kix98YnkdVf6bkLFoQN17j 5ofqrd8A0JNUEgo2gUurkYcYWqODuna9EssacRLUK3vpC5i0KzIW9tbdcVsc2pulUFjq ym0DmEwcJt065bWk6KMdCrN4/H9VnCGU5AGh9rBozAGVvw4P+NYTQVh1NaOX+1ZtnWf4 QAhZTJCMKJ3DoD0ddtZBjjtVpIuOeWlO0FQuLCPzHx6sduSd6sDSuINZHT1FH5b4g6NU VHmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:from:references:cc:to :subject:message-id:dkim-signature; bh=HucfxWg7WLzKK33ibHuCmIEtwQY3NwivHh2WljaMSPk=; b=IUii3GjCYsoD2rfkE3Wz6VjOYNMjc9HaCIGYF+T7Kq1r5AdUk0ZC+S+/X2saXxdtjA Gl395TTlcqwN/J29j/q4QaTk/bUBcTc0fq/tFZovwZ5uI70RywtxuRFrw902NlyJKkPh 3QUP/bRiSbrAGS9z0Sfd7F8ebL4KYwoGoyuVXP15ph+GWC/4C+ruIkaa2QhFkeKfsTTe aoiczs0S367GjNp9L5+20QDKUmJDM9LcaEN7ublx07I+5DBHuVoLewFrMyyaBP62KcC9 aitPnJ6qz4ngfkq9xFrFOrCuaQrL0d4wlyYsY+1kjijI0si46ee4DBP2MvocRo20wNvk Q26w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=QZdi3CdC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u7-20020a170906654700b006e7ee599274si12979528ejn.235.2022.04.25.00.50.35; Mon, 25 Apr 2022 00:50:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=QZdi3CdC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240520AbiDYDB3 (ORCPT + 99 others); Sun, 24 Apr 2022 23:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240514AbiDYDBS (ORCPT ); Sun, 24 Apr 2022 23:01:18 -0400 Received: from out203-205-221-210.mail.qq.com (out203-205-221-210.mail.qq.com [203.205.221.210]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C739B84ED6 for ; Sun, 24 Apr 2022 19:58:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1650855488; bh=HucfxWg7WLzKK33ibHuCmIEtwQY3NwivHh2WljaMSPk=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=QZdi3CdCyKj1C4fVLcPcm4rK4IBLJ0vsJp5yBIiUw9Td4uqxZdbVvLk5ANwrG7ucN 5H07mqKtK089BFQ2c+FeO1RAgoICcN5A/5yAUVZfOyeQZ6nAYO0WGrD3dtiwUqiTO3 vQup7Se/15B7oorh7+kshRjaW9lorhNF5k8AXvOw= Received: from [10.27.0.6] ([94.177.118.128]) by newxmesmtplogicsvrsza8.qq.com (NewEsmtp) with SMTP id E7715EE1; Mon, 25 Apr 2022 10:57:55 +0800 X-QQ-mid: xmsmtpt1650855475te67wfj42 Message-ID: X-QQ-XMAILINFO: MQ+wLuVvI2LQC/KJV6LaTwBj061LhYfOrByOSpey+K3S1udv28bAJjd5Bdllfd pNb7IEi0EY/cpHk10CYCm+d4cYqiCLcqqx/UcZ9XIVctITwZhcaxrq4AXXKFmiHMzFmtOaxWriNQ 4hCNKjJs4cvj9qjSwWboYISnOiamhpm3yGXZ+WAmnQqWDDu5uS8Y7eYFmxHJwuwcHHM4B6Qaz2mI wWuVLmL0i/W/vSNlo3/tQiiviDuSL2NxALnbXwUkBqHNAuxFAEjZnZZYRF5kfCz9bvnys4jlOYBs bMlE2272nyI0aVjUA4mnUxCdHyImXVnpX+fzFlSwpcGJHmuVPt4j5WEfL7Ybo1XI4WBnq0Bn3lV4 iE6jqCw3iMWqKMtshFuaHGNoUP5Uetko1WTX/8KW49AOaE9ix1IUdW2q7EP8sxF3iC+IRoCgTZ6t 3YWYr6gLxB6mwTgHj/j7aQrqNhECc1f0hGC5xfxUVPTTvnRDXgLi8Y8qE9/eIHoWC56ibR9x/W/I zTOkiWDLBTv0L5Ba+/J/ZF3GrVEuBYuoHZSsWYASi4WTqhFmT/wBikEosE422A4nweHtC7ao5Gqc 6eHmokCHPzg4hk0QMnGLXm7jD/Xj8ey9dgYk6UC8Uq3CPMxaDNl1LpTXeDVxxWsTjcILOZKuo2xp F8pvRpJDYBHcV7xhV8+OxlftqHtBYLJoGQKeFnLRufsIcTLkw/yR6LFWtLZkitCdUEF0Fa3JmEeT m6wjurCIXKFXX1wQOMsa+yb/OcBI0a+/VnCxpH0gWGLg84Aa7qAN4Y0C1CVwRIiX5CJE9Y+jHRxv h244I2/fM5O5tmDQ3142Jof8BbQa69HKjH8fmK8bdFk/g3BNNOrK54rWxGiId5ajoz4cy3UuCiqs mRjBozcKxwJifU8C3We0p/Xkouf5HGrEi4elXPN5nNbrYRzFmUkMyI2YPSE9oMzXcF5S934HkO Subject: Re: [PATCH v4 05/11] iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit To: Dave Hansen , Jean-Philippe Brucker Cc: Joerg Roedel , Fenghua Yu , Ravi V Shankar , Tony Luck , Ashok Raj , Peter Zijlstra , Dave Hansen , x86 , linux-kernel , iommu , Ingo Molnar , Borislav Petkov , Andy Lutomirski , Josh Poimboeuf , Thomas Gleixner , zhangfei References: <20220207230254.3342514-6-fenghua.yu@intel.com> <56ed509d-a7cf-1fde-676c-a28eb204989b@intel.com> <41ed3405-66d9-0cde-fc01-b3eacb85a081@intel.com> <8b1e40c9-b2e8-7b73-d9ad-2c6a5a167370@intel.com> From: "zhangfei.gao@foxmail.com" X-OQ-MSGID: <6daae664-fad2-fe1f-0c22-b8c8aceab841@foxmail.com> Date: Mon, 25 Apr 2022 10:57:52 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <8b1e40c9-b2e8-7b73-d9ad-2c6a5a167370@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_MUA_MOZILLA, FREEMAIL_FROM,HELO_DYNAMIC_IPADDR,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Dave On 2022/4/12 下午11:35, Dave Hansen wrote: > On 4/12/22 08:10, Jean-Philippe Brucker wrote: >>> I wonder if the Intel and ARM IOMMU code differ in the way they keep >>> references to the mm, or if this affects Intel as well, but we just >>> haven't tested the code enough. >> The Arm code was written expecting the PASID to be freed on unbind(), not >> mm exit. I missed the change of behavior, sorry (I thought your plan was >> to extend PASID lifetime, not shorten it?) but as is it seems very broken. >> For example in the iommu_sva_unbind_device(), we have >> arm_smmu_mmu_notifier_put() clearing the PASID table entry for >> "mm->pasid", which is going to end badly if the PASID has been cleared or >> reallocated. We can't clear the PASID entry in mm exit because at that >> point the device may still be issuing DMA for that PASID and we need to >> quiesce the entry rather than deactivate it. > I think we ended up flipping some of this around on the Intel side. > Instead of having to quiesce the device on mm exit, we don't let the mm > exit until the device is done. > > When you program the pasid into the device, it's a lot like when you > create a thread. We bump the reference count on the mm when we program > the page table pointer into a CPU. We drop the thread's reference to > the mm when the thread exits and will no longer be using the page tables. > > Same thing with pasids. We bump the refcount on the mm when the pasid > is programmed into the device. Once the device is done with the mm, we > drop the mm. > > Basically, instead of recounting the pasid itself, we just refcount the mm. This has issue, since refcount the mm will block  fops_release to be called, where unbind may really happen. For example, user driver are ended unexpectedly, usually system will end all applications via close fd -> fops_release -> unbind may happen here. Now mmget is called, fops_release -> unbind has NO chance to be called, so ioasid can NOT be freed. Thanks