Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp2275472rdb; Fri, 8 Dec 2023 03:43:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGKD5vPWLNYjJI8R+TxYu4WH6yBkhdG5hG1xCmAOf1YtWGKA5q2sWHbrUeucM/2IJid4bnQ X-Received: by 2002:a9d:68c6:0:b0:6d9:f57a:1dc5 with SMTP id i6-20020a9d68c6000000b006d9f57a1dc5mr125906oto.64.1702035829739; Fri, 08 Dec 2023 03:43:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702035829; cv=none; d=google.com; s=arc-20160816; b=TtJAzPrbWz9q6WvFQX5TfcpLtQDWcMJ4ONqjiXZkPxePjHRUPcjnbT+ccCaSSNQfSf OKgTqfwQZDigcR0Fgk7SXZNLyNrNM6lyglMGsDz8s4cGW46c5qQszlkzFcRBqxSS6ODd +/7jOf0n1sCXtNa8YqAGRlUgIG6fUG+CQ+VMuU6ZibsyQf7c/IQa3I1e2TE+tmgyyksF kYyz4b0XOUyxYyoZbdPb+PDgKhzbjIBjrbs9z9E8MyE+1rkrJsuLc0Zd1YuWqrKriZkY UyNYjDe02s2iMxhCoVei7j7JeqDsl9pUMhqBuBWhDyuyr0p0fzS6lAdIvZAQj1+OOpjm cZDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :content-language:references:to:subject:cc:user-agent:mime-version :date:message-id:dkim-signature; bh=yYC9Bgx+DjEW1QQVVdIPzuqfa3I840VUMH1cfyvmuFA=; fh=CHF6icUB8BkFux9PDqkpqFGX84EaHzyqwt6q0oBm+fk=; b=YUJKWMUCrqUjdbQRI2XZmKDcVVtfDjpxTM7cvBOeGCw7hbncKZOESZIgR1m76Nisfn MdtGfQitHltjUOygQCOyWPYT+XUqFPnQsMr2x4Njtp56jy7dot99ENgwXfqlu8ZuN7Ts QCL39q4msqZbOsKf9cI6yrcGjW3mPEVXG6K2gQGZWzW21WUxrLcidU1/wqnmv9zjsDzv og/R1wlSeDWf4QFzeqWDHCaHBcIV08BczH3DcKwckc6qHndkVDsIHagh0OUMx+mlrvpn D2yP06exc0GRO2vHc/HzW7BeCAlGb5J1LwuiT6NBRXWqdll2tR5vfvpnpZwQiGlQBxh5 fScA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GOZ1N66u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id z1-20020a656101000000b005be1e555473si1434726pgu.574.2023.12.08.03.43.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 03:43:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GOZ1N66u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id D520681972C3; Fri, 8 Dec 2023 03:43:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233381AbjLHLnZ (ORCPT + 99 others); Fri, 8 Dec 2023 06:43:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232504AbjLHLnY (ORCPT ); Fri, 8 Dec 2023 06:43:24 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2839AD59; Fri, 8 Dec 2023 03:43:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1702035811; x=1733571811; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=ZmkAP0N/gOOmFK2HS73Ar6ckEAFg8Q3aZC+p9EeSO1Y=; b=GOZ1N66uIDbEVMD4+BhgoazD/AoEsfQ94BjrcIkQdHTbZUOB5cnjx47X b64apZQ1fMDcaDzkh8O4CJIpwkdxJr/vhPfEChmsi75RVxt1aXVXXQzxB W+H9MQYM2Ahv6KqGPo4K8vabl7GWHNn3liYDbGfm7tgdjIPL0X8VyGGEw 8/vyGDS5TGKwe0JC95SLyOCcTg5zDK1Ezx/wL0qSmaqTuvP9hsK6irLog 7MjevueSoiPOw9liSjmTcgQuNXxUFdyT/X+/+S93SEGX8yAN7YdXWojhm WmVN0GCDJ930q5xO0c5jYG+ESr0EJL4l6R23fPTx8oc/zIikDVW4qai2i g==; X-IronPort-AV: E=McAfee;i="6600,9927,10917"; a="394134276" X-IronPort-AV: E=Sophos;i="6.04,260,1695711600"; d="scan'208";a="394134276" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2023 03:43:30 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10917"; a="765465888" X-IronPort-AV: E=Sophos;i="6.04,260,1695711600"; d="scan'208";a="765465888" Received: from blu2-mobl.ccr.corp.intel.com (HELO [10.255.31.238]) ([10.255.31.238]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2023 03:43:27 -0800 Message-ID: Date: Fri, 8 Dec 2023 19:43:12 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: baolu.lu@linux.intel.com, Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 4/6] iommufd: Deliver fault messages to user space To: Jason Gunthorpe References: <20231026024930.382898-1-baolu.lu@linux.intel.com> <20231026024930.382898-5-baolu.lu@linux.intel.com> <20231201152459.GB1489931@ziepe.ca> Content-Language: en-US From: Baolu Lu In-Reply-To: <20231201152459.GB1489931@ziepe.ca> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 08 Dec 2023 03:43:47 -0800 (PST) On 2023/12/1 23:24, Jason Gunthorpe wrote: > On Thu, Oct 26, 2023 at 10:49:28AM +0800, Lu Baolu wrote: > >> +static ssize_t hwpt_fault_fops_write(struct file *filep, >> + const char __user *buf, >> + size_t count, loff_t *ppos) >> +{ >> + size_t response_size = sizeof(struct iommu_hwpt_page_response); >> + struct hw_pgtable_fault *fault = filep->private_data; >> + struct iommu_hwpt_page_response response; >> + struct iommufd_hw_pagetable *hwpt; >> + struct iopf_group *iter, *group; >> + struct iommufd_device *idev; >> + size_t done = 0; >> + int rc = 0; >> + >> + if (*ppos || count % response_size) >> + return -ESPIPE; >> + >> + mutex_lock(&fault->mutex); >> + while (!list_empty(&fault->response) && count > done) { >> + rc = copy_from_user(&response, buf + done, response_size); >> + if (rc) >> + break; >> + >> + /* Get the device that this response targets at. */ >> + idev = container_of(iommufd_get_object(fault->ictx, >> + response.dev_id, >> + IOMMUFD_OBJ_DEVICE), >> + struct iommufd_device, obj); >> + if (IS_ERR(idev)) { >> + rc = PTR_ERR(idev); >> + break; >> + } > > See here it might be better to have a per-fd list of outstanding > faults per-fd and then the cookie would just index that list, then you > get everything in one shot instead of having to do a xarray looking > and then a linear list search Yours is more efficient. I will do it that way in the next version. > >> +static const struct file_operations hwpt_fault_fops = { >> + .owner = THIS_MODULE, >> + .read = hwpt_fault_fops_read, >> + .write = hwpt_fault_fops_write, >> +}; > > nonseekable_open() behavior should be integrated into this Sure. > >> +static int hw_pagetable_get_fault_fd(struct hw_pgtable_fault *fault) >> +{ >> + struct file *filep; >> + int fdno; >> + >> + fdno = get_unused_fd_flags(O_CLOEXEC); >> + if (fdno < 0) >> + return fdno; >> + >> + filep = anon_inode_getfile("[iommufd-pgfault]", &hwpt_fault_fops, >> + fault, O_RDWR); >> + if (IS_ERR(filep)) { >> + put_unused_fd(fdno); >> + return PTR_ERR(filep); >> + } >> + >> + fd_install(fdno, filep); >> + fault->fault_file = filep; >> + fault->fault_fd = fdno; > > fd_install must be the very last thing before returning success from a > system call because we cannot undo it. Yes. > > There are other failure paths before here and the final return > > Jason Best regards, baolu