Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp29013pxb; Tue, 12 Apr 2022 15:54:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJydGfjHEu29Fw6pn7e2zAy61mFyZ7TO1Ovfnfh//e3RujxrDgfZHpXx2cqVUtmaNmkD7CBv X-Received: by 2002:a17:90b:164f:b0:1c7:8d20:ff6d with SMTP id il15-20020a17090b164f00b001c78d20ff6dmr7493190pjb.64.1649804080739; Tue, 12 Apr 2022 15:54:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649804080; cv=none; d=google.com; s=arc-20160816; b=T8EhNlMtB7J/JwR5QabvqZDYcDoKqY3THOJsq77TUXBtu7XaxCWiFVjbFQ9ki2s8NW lE8JVsZB180ZQCVl7+mUKLQzSS6U3PMM0HyHh0A69yHAd3RNTJ5yYRk/WE0xwmL8BMPV HGlo2zIfgEic8RFTqxLezcLBqZHoqtPONWubO2DVsL8OmJNJK5AyC7seo6OH4Zp34sBn yDpKAZhVYG9XhMvhLFu3zgsEzuW8O/7dV1n10z+kqN0m+w2VeK5XDYxtWB7qtogdRjb4 0yQP4ni9Hv0d1COQbdi4xzs5fRcjV4JAfFU96bXmPFbTYKkb6N0pIiZjzZl3j/fxwrlU JPtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=dEtehde5cjGV9pdbeDZJ5mEUZSUw1tlthJqoKML+g0Y=; b=zTCK0j9/6FhyfQCQMkLj8BHDtwpiwWhVo+4prEjq8jk+ShqsF0dNM6lUvRpoj4MdT+ 6MwV5SEj7f5+9HCUjdDzndWAKNcp479x9KnaZIcd9lHc0EDNHzlQkJMspXzC+uOijU2f 25nt21WU0kDKt1aZwHDHjb6Zodzhlr2erDndR/QCma/QEuJ3hBkU2Y9vgowAtDyUhE2f AIpqgaNISpSmqYjVoxmLEAaHJ+lXxwCaugE0W/zDNvcFzxhVfAbOuvIaIGpYw0FXSgtu wxNtAeAlX/JY7zSd/bUHGxoX1cl8vY9tfKHCZevxi6JFJM2hWfjchbDKLxiNCiTYmoEV jk5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=s8XYPFWd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id k21-20020a170902ba9500b001588178e78dsi3863309pls.173.2022.04.12.15.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 15:54:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=s8XYPFWd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CB2F41CC434; Tue, 12 Apr 2022 14:36:30 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351681AbiDLPM6 (ORCPT + 99 others); Tue, 12 Apr 2022 11:12:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241366AbiDLPM4 (ORCPT ); Tue, 12 Apr 2022 11:12:56 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00D2C5D654 for ; Tue, 12 Apr 2022 08:10:38 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id e8so11767532wra.7 for ; Tue, 12 Apr 2022 08:10:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=dEtehde5cjGV9pdbeDZJ5mEUZSUw1tlthJqoKML+g0Y=; b=s8XYPFWdR/OTPJ6iEZkbBH/vpqPUQtbWi53fsalJbS859W1hri0trBZxuGteCEYa+i yy+syBRWikaBvxxDHOjBHYOaH/4JLGAETkbrCESo9iBj3GZi64wKtbfrViJ6egTOpQJi JUB9ZzMp6x7pza38q9oZ61TQrEeiXBBXFhKrd6nitj2xpI2tOoaWqG3UnsQC0oknQmVe PUYBNANDe28bVNmPEVgLFbVxK6SuLDBwsRgtCXcJRLJJNb2biW3TG/KISgWWHLyqdEH6 oZ82VifUj+zzzeGeV+76S1jOksa4ffRMIP31I7YQzYWcffdhE/cvx6JxVqj9uN5P3a+b jsCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=dEtehde5cjGV9pdbeDZJ5mEUZSUw1tlthJqoKML+g0Y=; b=jKYADLe4RFPO0/nNCT996aLpwQo5cl6TyR8GsDxoMKp6Yugnz5cTnTMaY7SBTcm2GT /macxk7sF9Y4piOfSDfDYSP9SNY/8VG8bcyVFnKCo94/1JUc2pq4dWoXD4uFfOabLajT ghYf2zQodeS82yypU2sVaV69Fqbs8bpFRznXPEQGe0NPDXE3uVonn3MO+XSc5PJyB3tY 2lJtX6jhdv6ELAy1V3ShHw5s8n368gqkwzBBphfV8y4CNfhea/YbpIXkv/68BHIHZ+Ik bSQgPsP0dY/5hERHfz95i8n+p6DM3u36GYYolVjYqi3wZFC9MN+vOu2hBm/9Fn/SjYEq RhVw== X-Gm-Message-State: AOAM531Tr9dbK5WMpJkt5SZr638bY7tlpnDmun/azE+rdDikHlXK5Pvk 2z3rJXsPCwBKVdYsdCKwqde4Sw== X-Received: by 2002:a5d:434f:0:b0:205:f514:8955 with SMTP id u15-20020a5d434f000000b00205f5148955mr30114394wrr.73.1649776237535; Tue, 12 Apr 2022 08:10:37 -0700 (PDT) Received: from larix (2a02-8428-1671-f801-d2dc-0c7f-2134-46d0.rev.sfr.net. [2a02:8428:1671:f801:d2dc:c7f:2134:46d0]) by smtp.gmail.com with ESMTPSA id s1-20020adfb781000000b002060d4a8bd9sm24787772wre.17.2022.04.12.08.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 08:10:36 -0700 (PDT) Date: Tue, 12 Apr 2022 16:10:34 +0100 From: Jean-Philippe Brucker To: Dave Hansen Cc: "zhangfei.gao@foxmail.com" , 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 Subject: Re: [PATCH v4 05/11] iommu/sva: Assign a PASID to mm on PASID allocation and free it on mm exit Message-ID: References: <20220207230254.3342514-6-fenghua.yu@intel.com> <56ed509d-a7cf-1fde-676c-a28eb204989b@intel.com> <41ed3405-66d9-0cde-fc01-b3eacb85a081@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <41ed3405-66d9-0cde-fc01-b3eacb85a081@intel.com> X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 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, On Tue, Apr 12, 2022 at 07:36:21AM -0700, Dave Hansen wrote: > On 4/12/22 00:04, zhangfei.gao@foxmail.com wrote: > > master process quit, mmput ->  mm_pasid_drop->ioasid_free > > But this ignore driver's iommu_sva_unbind_device function, > > iommu_sva_bind_device and iommu_sva_unbind_device are not pair,  So > > driver does not know ioasid is freed. > > > > Any suggestion? > > It sounds like you're saying that the device is still abound to the > PASID even though the mm has exited and freed the PASID. This is > essentially a use-after-free for the PASID. Right? > > The right thing to do here is to have the PASID code hold a reference on > the mm. The mm "owns" the PASID for its entire lifetime and if anything > needs the PASID to live longer, its only recourse for doing that is via > an mmget(). I _think_ mmget() is the right thing as opposed to mmgrab() > because the PASID users actually need the page tables to be around. > > This would still be nice to confirm with some traces of fork()/exit() > and the iommu_sva_{bind,unbind} and ioasid_{alloc,free} functions. > > 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. We can only deactivate it once the device driver has properly stopped the device, at which point it can call unbind(). There may be other issues but I can't check it thoroughly until next week. Thanks, Jean