Received: by 2002:ab2:68c1:0:b0:1fd:9a81:d0e4 with SMTP id e1csp37941lqp; Sat, 8 Jun 2024 05:26:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVo2pVoXuQzMptwdgeYEl/HIV4eUl98xvvy0JhFSOaNn5e2VJLPE1KXp53V07een7coXJFaWnwJeMz9fqmiexXD2D4Iwmd+UbdFih3YRQ== X-Google-Smtp-Source: AGHT+IEPiVAgaNH6OoQ731cjTvzeCVicTTz8IUcK73mUz0GmfZfQf1/GAz4zB4lWeXuYfrTr/TqY X-Received: by 2002:a05:6359:5f8a:b0:19f:431b:9203 with SMTP id e5c5f4694b2df-19f431b99a2mr97488455d.4.1717849608780; Sat, 08 Jun 2024 05:26:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717849608; cv=pass; d=google.com; s=arc-20160816; b=YG1rGFCKdvCDGZbuDY6S0F1xX0QvLdit0FgenaMXYbux9lFvME74+r39uTuXJVpMEh YArduFUXQxXQ5pzbGkiTKp8mbzTrT0eCque+ijnlqu1iEuNvrLLprkTHXxivt7C2ubgI Mn3qJkiGTckGY50rGUfkzNBCP1FymaRCWmpJ+tRmb3QduqHAdAYO5QlTtrvC9WfWr3W7 sJCTsc51hYKUDDRnVm0gqSGF1Xvr3XRmxN/cvJOqdz5LaLdy4wtGXQYXpCi8/sm9XFIc UULUMl5cu0SirqAi8SGJNZEPZgWDLQfWd+VtU4zdhU/jHz4R5UhXS/bKIuJabLEWvLrJ +m7w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:cc:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=t/Zp8C5lVx4g313kUHVi4IXhwejB9DKDpQq2haZY7+o=; fh=oj0MC99jJmXr9VOxYru8PCxqsxVpffpkkk/RjcP5DFo=; b=lnlVFO7X6ztU7MleWFfPZnz3j3hRTLrV0qR0dJA/47Y50wpeyXmcM/sfHd9MxFB/6S 4mqqt0pcurM8odgacfRarqSjFbPu98NRaMtg1JUK5ncOz/j3gg/eGmQlVSQK28O11QjP w1z+c/RBuruV97FnENlfjM91u/U7ixRsDSz3SbUqyiKJh4bf/n3vkFHzVhYp/DEN0pke IHLTL/qzsCxW4YRxo63vv54nf28X7DqKI/+Ug77GpyTpWMqoamzYpXShLAYkE2YTY6ht Qcm87FBlarudumIQlOXvH19dacz9noYbe1ZUQLXKF9NEuxskLLlM0s2ShzOrXjeyE6uE wrUw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N8sHOGgm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-207007-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207007-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de2ab2fc35si4698622a12.852.2024.06.08.05.26.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 Jun 2024 05:26:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-207007-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N8sHOGgm; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-207007-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-207007-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D70E1B20C4A for ; Sat, 8 Jun 2024 10:01:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE1811779A4; Sat, 8 Jun 2024 10:00:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="N8sHOGgm" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFFCA6A33A for ; Sat, 8 Jun 2024 10:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717840854; cv=none; b=Jh9iuLd1clY8hiiviVBvatYLdx1gHDs/FlwjitNs8hJzcycoo/PunPgnefNboBGmhTCxF7lhhoZE02+toh32J16rYCMFywut9TC0f2fc3GRVzTBV0cwkLu1Iimby7uPc48FjGtv55Zpx46MS1IyAYBdVZ9TO2EcjwldO+mqfy5A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717840854; c=relaxed/simple; bh=dD/2qxPjsTYcJBiszw6+m0kwHnKdGtPseKpmC/MTrdk=; h=Message-ID:Date:MIME-Version:Cc:Subject:To:References:From: In-Reply-To:Content-Type; b=MH4pRXXMjr5Vl6JHPMong1/qJtKwDyYiO89phnGa/glV8jZKNIMW6Tbi4vEJv1FFMuPMDwAOV5zelD4op+jLCBCqcUs6Qbftfxv6hHohVapNvO80S1SU0Zi4YZiNniXjYabMQAoRmAyPib9ZzRlQbf5YfwiYuU8dBXfMdl5OjbU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=N8sHOGgm; arc=none smtp.client-ip=192.198.163.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1717840852; x=1749376852; h=message-id:date:mime-version:cc:subject:to:references: from:in-reply-to:content-transfer-encoding; bh=dD/2qxPjsTYcJBiszw6+m0kwHnKdGtPseKpmC/MTrdk=; b=N8sHOGgmeJRIE+B7puXuRVfxk3wilCAFtwlXnaqGs9uEMPcGUYI341aa 1PJ9vtHljl0spzUhrl/b4Db5G9UjsCnaZPzL4aUrEWRBJezJ9OsikRpwv vqnRFydrePRoUVFGdiDKf9E+p0e8XksbvAqicJbmTj4Rh7jp1sFLFAHjU pGo7ncdl0/gXFf/40Z3fTTQSCzg7ksRBulnCabUKJ5l2QBWsNxHGhFkPS dJg5qie9I0c6IzK1HyvPktY4q2lV45TKOP4OoDjxLZ+n8fOA9zSCXqecQ Of0OKhmdqP+H/kt0QqXHJiIb0FDm+3ZNZaNv6h4LtC05kNm7sLGIdLJSn A==; X-CSE-ConnectionGUID: QwzhpD8BQE6OkL/8TQc5IA== X-CSE-MsgGUID: o7H0hKLCQkeKamZ9m9CeWw== X-IronPort-AV: E=McAfee;i="6600,9927,11096"; a="14366492" X-IronPort-AV: E=Sophos;i="6.08,222,1712646000"; d="scan'208";a="14366492" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Jun 2024 03:00:51 -0700 X-CSE-ConnectionGUID: lcCbsdoTQd2NporVwlg6GQ== X-CSE-MsgGUID: T9NVOjipTtmZfYc9dHtNOQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,222,1712646000"; d="scan'208";a="39004890" Received: from unknown (HELO [10.239.159.127]) ([10.239.159.127]) by orviesa006.jf.intel.com with ESMTP; 08 Jun 2024 03:00:47 -0700 Message-ID: Date: Sat, 8 Jun 2024 17:58:34 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: baolu.lu@linux.intel.com, "iommu@lists.linux.dev" , "virtualization@lists.linux-foundation.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v6 06/10] iommufd: Add iommufd fault object To: "Tian, Kevin" , Jason Gunthorpe , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , "Liu, Yi L" , Jacob Pan , Joel Granados References: <20240527040517.38561-1-baolu.lu@linux.intel.com> <20240527040517.38561-7-baolu.lu@linux.intel.com> Content-Language: en-US From: Baolu Lu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 6/7/24 5:17 PM, Tian, Kevin wrote: >> From: Lu Baolu >> Sent: Monday, May 27, 2024 12:05 PM >> >> +static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf, >> + size_t count, loff_t *ppos) >> +{ >> + size_t fault_size = sizeof(struct iommu_hwpt_pgfault); >> + struct iommufd_fault *fault = filep->private_data; >> + struct iommu_hwpt_pgfault data; >> + struct iommufd_device *idev; >> + struct iopf_group *group; >> + struct iopf_fault *iopf; >> + size_t done = 0; >> + int rc = 0; >> + >> + if (*ppos || count % fault_size) >> + return -ESPIPE; > > the man page says: > > "If count is zero, read() returns zero and has no other results." My understanding is that reading zero bytes is likely to check if a file descriptor is valid and ready for reading without actually taking any data from it. In this code, it just returns 0 and it's compatible with the man page. Or, I overlooked anything? > >> + >> + mutex_lock(&fault->mutex); >> + while (!list_empty(&fault->deliver) && count > done) { >> + group = list_first_entry(&fault->deliver, >> + struct iopf_group, node); >> + >> + if (group->fault_count * fault_size > count - done) >> + break; >> + >> + rc = xa_alloc(&fault->response, &group->cookie, group, >> + xa_limit_32b, GFP_KERNEL); >> + if (rc) >> + break; >> + >> + idev = to_iommufd_handle(group->attach_handle)->idev; >> + list_for_each_entry(iopf, &group->faults, list) { >> + iommufd_compose_fault_message(&iopf->fault, >> + &data, idev, >> + group->cookie); >> + rc = copy_to_user(buf + done, &data, fault_size); >> + if (rc) { > > 'rc' should be converted to -EFAULT. Yes. I will make it like this: if (copy_to_user(buf + done, &data, fault_size)) { xa_erase(&fault->response, group->cookie); rc = -EFAULT; break; } > >> + xa_erase(&fault->response, group->cookie); >> + break; >> + } >> + done += fault_size; >> + } >> + >> + list_del(&group->node); >> + } >> + mutex_unlock(&fault->mutex); >> + >> + return done == 0 ? rc : done; > > again this doesn't match the manual: > > "On error, -1 is returned, and errno is set appropriately." > > it doesn't matter whether 'done' is 0. I don't quite follow here. The code is doing the following: - If done == 0, it means nothing has been copied to user space. This could be due to two reasons: 1) the user read with a count set to 0, or 2) a failure case. The code returns 0 for the first case and an error number for the second. - If done != 0, some data has been copied to user space. In this case, the code returns the number of data copied regardless of the value of rc. > >> + >> +static int iommufd_fault_fops_release(struct inode *inode, struct file *filep) >> +{ >> + struct iommufd_fault *fault = filep->private_data; >> + >> + iommufd_ctx_put(fault->ictx); >> + refcount_dec(&fault->obj.users); >> + return 0; >> +} > > hmm this doesn't sound correct. the context and refcount are > acquired in iommufd_fault_alloc() but here they are reverted when > the fd is closed... These two refcounts were requested when the fault object was installed to the fault FD. filep = anon_inode_getfile("[iommufd-pgfault]", &iommufd_fault_fops, fault, O_RDWR); if (IS_ERR(filep)) { rc = PTR_ERR(filep); goto out_abort; } refcount_inc(&fault->obj.users); iommufd_ctx_get(fault->ictx); fault->filep = filep; These refcounts must then be released when the FD is released. >> + >> + filep = anon_inode_getfile("[iommufd-pgfault]", >> &iommufd_fault_fops, >> + fault, O_RDWR); >> + if (IS_ERR(filep)) { >> + rc = PTR_ERR(filep); >> + goto out_abort; >> + } >> + >> + refcount_inc(&fault->obj.users); >> + iommufd_ctx_get(fault->ictx); >> + fault->filep = filep; > > those 3 lines can be moved after below fdno get. It's reads slightly > clearer to put file related work together before getting to the last piece > of intiailzation. The filep is allocated and initialized together. >> + >> + fdno = get_unused_fd_flags(O_CLOEXEC); >> + if (fdno < 0) { >> + rc = fdno; >> + goto out_fput; >> + } >> + >> @@ -332,6 +332,7 @@ union ucmd_buffer { >> struct iommu_ioas_unmap unmap; >> struct iommu_option option; >> struct iommu_vfio_ioas vfio_ioas; >> + struct iommu_fault_alloc fault; > > alphabetic > >> @@ -381,6 +382,8 @@ static const struct iommufd_ioctl_op >> iommufd_ioctl_ops[] = { >> val64), >> IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct >> iommu_vfio_ioas, >> __reserved), >> + IOCTL_OP(IOMMU_FAULT_QUEUE_ALLOC, iommufd_fault_alloc, >> struct iommu_fault_alloc, >> + out_fault_fd), > > ditto Yes, sure. I wasn't aware of the order. Best regards, baolu