Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4044160rdb; Mon, 11 Dec 2023 07:25:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGWfepWREmErk7brrNgQpEcHbhfYO68kEgFaAu9Fl6TED0Kb4ta/pdyiDKFFY7wDbdKxcZs X-Received: by 2002:a05:6a20:d42f:b0:190:53c1:fee2 with SMTP id il47-20020a056a20d42f00b0019053c1fee2mr5430937pzb.19.1702308310227; Mon, 11 Dec 2023 07:25:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702308310; cv=none; d=google.com; s=arc-20160816; b=dcMpsnJabNWCN2RRj+X4ShObyRrMw6OGf4/XuInQLzneLVfLuFWFDiGRPfw4yA4bZt xDYZ+v3i5sOW4NuSmOAaSfGnouX9QxQ292gFrgpZDj9Xv4mdwAQ5TzLzaGvRYVVhmK9a T4G9c7Z2k8Ml73cN2hp6AO3XZJ7OSWRprACBxGOvE8nos+9zAnQ2YI88VRLNHfY+zj3O qLT+5+pOnPcvC+75gMr7/DDp3GqwuTf80PpccqkuI9it6jJA/GvXAdpFJpLErRasDkgf 71l/KGgPUv8GlbaSwdCMx5zPnk3LVdyA8AFyxjsSp2BEBkukAvTznJvy1FqfJl7AB0cS kh1Q== 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-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Z3xtaErerlt4nlnnOzSlabRBtDSO5G3MlJlaHfuuRQ8=; fh=LjrnQdUjVV5X3XIgJYPM443ykgMLMNshafdbFec1v5k=; b=OPAkd32fwxVy5mrDMOgba/9BjvWKrTBVpBJJUk5du5t2/EE4EUNoPIchir7VTC7CyW xnIUsYwE9rzoEkw9kCnHvljwO+xTaVnWSCdAjCqj7cdCYO5bakyR0G0H52tHrlPQZfDz 7OGnl27mYqC13UKgFnS2cKcvIWl/eqzZGvBt8+iAV1GAejF54CLfIfZKdMnxsZFeDfRT rjSFs2V1zZ5OVk2SkXFmo4kV1K5U9v0tRvVnC7BFYc0WFmkMXzOArqDsFU4EXjwphSXI ePQ9LisN8y/Qxp3XI6YfbURx+3W93iQ53aOSXV0qv5MPd3c6JAYJ/PvNGPVhLetiEjnn khnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=UohwAyJm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id m184-20020a6326c1000000b0056952b496efsi6284929pgm.366.2023.12.11.07.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:25:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@ziepe.ca header.s=google header.b=UohwAyJm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 96E628093D56; Mon, 11 Dec 2023 07:25:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343886AbjLKPYx (ORCPT + 99 others); Mon, 11 Dec 2023 10:24:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343865AbjLKPYw (ORCPT ); Mon, 11 Dec 2023 10:24:52 -0500 Received: from mail-oo1-xc2d.google.com (mail-oo1-xc2d.google.com [IPv6:2607:f8b0:4864:20::c2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E85C7DF for ; Mon, 11 Dec 2023 07:24:57 -0800 (PST) Received: by mail-oo1-xc2d.google.com with SMTP id 006d021491bc7-5906df1d2adso1807733eaf.2 for ; Mon, 11 Dec 2023 07:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1702308297; x=1702913097; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Z3xtaErerlt4nlnnOzSlabRBtDSO5G3MlJlaHfuuRQ8=; b=UohwAyJmU+Kj9oPGjLSbDNlsjNYf3kkih/ccgSBZ4ow6gwDGBIxWh00uyeG3ArqVt+ I7+7mw9JMgO3fRC6OtK9gJnheo814dvWavnX3uLyYwkeWUl8QPhM9ZBlBRZxAi0WVA6R gJGrI0HKrg/Pm88HEqUIzmXaWMXtEfv81/1jrw/GsOobuJzWRCpJP3ImEE3O687aQe9V 12vQJqPrTkUO6OUqCuFUuxJiLzNPiyzKIi7BY+frlOSLqNCvXaaF264ZhlVWrh7aD7ad I5jXz4RfhKqJHW1dwSYUgxsD9gZPynbtIOzs9CPj3zM6CyUXJhLUfD5P4k4Zp072wJ1J bWoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702308297; x=1702913097; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Z3xtaErerlt4nlnnOzSlabRBtDSO5G3MlJlaHfuuRQ8=; b=iFvu2Ly2j9o+cH8Zd55O3/ji1DnicmEqe8ohIVx9sbBr/9vQt8n2lik3nOz/IWBMeG gWMIsYwYVSdbNfGjmDsW66Phdm1yADwrYOrcuVV2cEmRvAiDXQjEMOlsFsDCHQO1j+aU FMDXMHtNrrCb8vcYacaUk9vs+FYPXn58H31YRsxa8xiKRJgbMssXaesY3/EM+yKp0axT dMyxbAE8A6TnBRm0ZesBbcJZYOuP+JLnOy3nCx9lmMAkufq23VdpTAlYeIUWipgqc4FK 0Xl0hD3e0ONIxI41xMTAquy0IP9GdLhnxsjQj4HJHgN9AWWRXHhr8zFu+Y/flYp+K3i4 WgkA== X-Gm-Message-State: AOJu0Yx2OfKvC+HiRL+4lbDL2CV0hAMFwXvCEskYOKey9PRBDPMHmI1Q rBe74aC32hiL3WnsQrpCJNjGLg== X-Received: by 2002:a05:6359:628c:b0:170:17eb:2fc6 with SMTP id se12-20020a056359628c00b0017017eb2fc6mr834223rwb.63.1702308297235; Mon, 11 Dec 2023 07:24:57 -0800 (PST) Received: from ziepe.ca (hlfxns017vw-142-134-23-187.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.134.23.187]) by smtp.gmail.com with ESMTPSA id t11-20020a056214154b00b0065b13180892sm3375747qvw.16.2023.12.11.07.24.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 07:24:56 -0800 (PST) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1rCi9Q-00CcQj-Ag; Mon, 11 Dec 2023 11:24:56 -0400 Date: Mon, 11 Dec 2023 11:24:56 -0400 From: Jason Gunthorpe To: Lu Baolu Cc: Joerg Roedel , Will Deacon , Robin Murphy , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Longfang Liu , Yan Zhao , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 12/12] iommu: Use refcount for fault data access Message-ID: <20231211152456.GB1489931@ziepe.ca> References: <20231207064308.313316-1-baolu.lu@linux.intel.com> <20231207064308.313316-13-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231207064308.313316-13-baolu.lu@linux.intel.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 11 Dec 2023 07:25:07 -0800 (PST) On Thu, Dec 07, 2023 at 02:43:08PM +0800, Lu Baolu wrote: > @@ -217,12 +250,9 @@ int iommu_page_response(struct device *dev, > if (!ops->page_response) > return -ENODEV; > > - mutex_lock(¶m->lock); > - fault_param = param->fault_param; > - if (!fault_param) { > - mutex_unlock(¶m->lock); > + fault_param = iopf_get_dev_fault_param(dev); > + if (!fault_param) > return -EINVAL; > - } The refcounting should work by passing around the fault_param object, not re-obtaining it from the dev from a work. The work should be locked to the iommu_fault_param that was active when the work was launched. When we get to iommu_page_response it does this: /* Only send response if there is a fault report pending */ mutex_lock(&fault_param->lock); if (list_empty(&fault_param->faults)) { dev_warn_ratelimited(dev, "no pending PRQ, drop response\n"); goto done_unlock; } Which determines that the iommu_fault_param is stale and pending free.. Also iopf_queue_remove_device() is messed up - it returns an error code but nothing ever does anything with it :( Remove functions like this should never fail. Removal should be like I explained earlier: - Disable new PRI reception - Ack all outstanding PRQ to the device - Disable PRI on the device - Tear down the iopf infrastructure So under this model if the iopf_queue_remove_device() has been called it should be sort of a 'disassociate' action where fault_param is still floating out there but iommu_page_response() does nothing. IOW pass the refcount from the iommu_report_device_fault() down into the fault handler, into the work and then into iommu_page_response() which will ultimately put it back. > @@ -282,22 +313,15 @@ EXPORT_SYMBOL_GPL(iommu_page_response); > */ > int iopf_queue_flush_dev(struct device *dev) > { > - int ret = 0; > - struct iommu_fault_param *iopf_param; > - struct dev_iommu *param = dev->iommu; > + struct iommu_fault_param *iopf_param = iopf_get_dev_fault_param(dev); > > - if (!param) > + if (!iopf_param) > return -ENODEV; And this also seems unnecessary, it is a bug to call this after iopf_queue_remove_device() right? Just rcu_derefernce(param->fault_param, true) and WARN_ON NULL. Jason