From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 166CD29D29E for ; Fri, 27 Mar 2026 18:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774636328; cv=none; b=rjtfRehWbiEy6qthxFl+FawEzwU0BS9PSwNLY8WWbVLZh1CA6Nocm98uLUAWUF1kBIrKixtJj7sts+Vt0cxhPIYj8U36xP3AqApIyvQzXChwLKv2EQ8+9FOQiNQUxgksxQi4w64nl7ZAYgO1mYrGgEhh4Kt2yGcu2GMRu8Vq2jA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774636328; c=relaxed/simple; bh=I8pCU0iLD4KpeNGY/h/1gemUn3rTUbWvhlBneg4jlks=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mGsdI64eK0c8FIAL1MdmarVKCJbfW+CxUpAgTY3zZs1bnf6MJ1LTcYrVuurGl8nTgly20sTQwKIJuHk6GQtdZ5ScxuNe1rfw2Aqwno8ndNwpdRQIcnI3EZrWR9+ojAlp6d6SVYQq0O+paZk2Pyic0ZOEuEf5zRK4YNqy6tsaF+Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Zk6KGSm0; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Zk6KGSm0" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2b0b260d309so10205ad.1 for ; Fri, 27 Mar 2026 11:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774636326; x=1775241126; 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=6LpDt37HhnOjDLZnhrjfc9Is2eaIGxJ/qygK6ijfc2M=; b=Zk6KGSm0Cgc2GnT9Er8A9b5BKr1rzqlOk+dIUrHvHjoSMCiOdd4yVV+0jC86y5Dbsp sz4JWRa23hic8Np7cPBf2KcqbDlu2C8YvGE/Gw2CwEfUTECJiCgMnNwwcXvmjYnwbEBv U0LPW5nWn0SCdvcczipLkIjYNERVwBKo3sgufD1bjMcCBQZaijdSl9WJCzUXXbprxOqE KSvPPCzVD4DcOpU1CsZaqx41/mlTWvMDPgI1dySuJ7zeucdW0vUmPziO/NKQwSrLvEJo NPVBbo4aXZ0FGjpFG8jwu+rFAZhMdDmyG+V5cQJ1Jy9S/wg4kl2UJt3vYqkQRSnLEe+h AFnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774636326; x=1775241126; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6LpDt37HhnOjDLZnhrjfc9Is2eaIGxJ/qygK6ijfc2M=; b=OQkDU5Xh4+aaP5JGhnI/CIA7j/PwJqGPCN1kEZDgwgzbxhXoNv0I1jCmI2MF7PzArB irm8PDb1VjznUPi4LaG/1C/HlSbGBN1ry2sMbgl3mAQRJdCIiOyUax2f59J8B/SxbSzO +sTt5sxdL2VrxU2ps0DoDiQ2LyNO8vHnklTDoW5O6QlsIY8E2A78juXEQTQDO4nWRRx6 msTk5xnMwz78Xw6UvdOFkdbRKX89KAeJcBWRq1SzJEHP3vW6I1Co0YjJzevBR49dMOIc N4DxUKfl5Ut+QVZxYMUoIsf6bnSwRltryfqbcAxjcWafvgsm0e9oYLQAPJ13yfOuvFuV fRVg== X-Forwarded-Encrypted: i=1; AJvYcCXd2AOg8aMoSbncgOuNoAgUILnF6n+OElf3WhcnJUwOqCk9B66GNOZQRDTTxAPR+R9OdOAH6f69BO08ojI=@vger.kernel.org X-Gm-Message-State: AOJu0YwBHLF5en+oxvPRTMu17ZSuBPL4KUU33j2Mr8YnxLm9Cf0YkKaL K+9TCopProb7cRylzUKBIqviQnxpGXmfKmVSNnAYsBlDpsfuce6ez57znW8pqI+n7w== X-Gm-Gg: ATEYQzwgxPQH/C+P7O5h1fMVUwAl2dqvoRH7EBheN/9oS+ttQQmqxMDsObSawD74yMo UbwiVIV5TjW8L1qGOEFbhV/x8umO0eHcMHPdhJyP5tYOszWjRZMAiWKwkF4sUAOX7pHdgn+/cLL itcaD23cuxMXcPMwAqjHmfpKgQtQF+wG6LzK/RF5oguOSvLjJD9KuK/Ep/POFfjNGCYpVVXACcl EVKvYAyvUIzCM1KL9nduOLMJcCbov7dmJsLBGgX475IGgwP6TciyuufuwwbX9xDlOQV6B6UAVMc K19IuC/z5OaC4M7QnRqpptU5T6+79C9FRvZH/p2OAxu+U+kJ/P7H8VGWL9GcjMCZ7EwYKPmMJt1 jOEvH9NPMz81eVZgl2ehcTWyXezKbW4nGIlAQoSV7vgFlmIwrQ4/1HlkCEfWQZmLJXsr7FQa0uG NjqGuCtgv8ieEr8HfHuBZ7Txqd+2rXbXEa9ecmvFIqe3/3yJbiLYHFEqWf9wDnFQ== X-Received: by 2002:a17:903:1b6c:b0:271:9873:80d9 with SMTP id d9443c01a7336-2b241e132a0mr249515ad.7.1774636325437; Fri, 27 Mar 2026 11:32:05 -0700 (PDT) Received: from google.com (168.136.83.34.bc.googleusercontent.com. [34.83.136.168]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82ca76e8478sm28351b3a.41.2026.03.27.11.32.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Mar 2026 11:32:04 -0700 (PDT) Date: Fri, 27 Mar 2026 18:32:00 +0000 From: Samiullah Khawaja To: Vipin Sharma Cc: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Jason Gunthorpe , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu , Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Chris Li , Pranjal Shrivastava , YiFei Zhu Subject: Re: [PATCH 14/14] iommufd/selftest: Add test to verify iommufd preservation Message-ID: References: <20260203220948.2176157-1-skhawaja@google.com> <20260203220948.2176157-15-skhawaja@google.com> <20260323212009.GC2571566.vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20260323212009.GC2571566.vipinsh@google.com> On Mon, Mar 23, 2026 at 03:18:39PM -0700, Vipin Sharma wrote: >On Tue, Feb 03, 2026 at 10:09:48PM +0000, Samiullah Khawaja wrote: >> Test iommufd preservation by setting up an iommufd and vfio cdev and >> preserve it across live update. Test takes VFIO cdev path of a device >> bound to vfio-pci driver and binds it to an iommufd being preserved. It >> also preserves the vfio cdev so the iommufd state associated with it is >> also preserved. >> >> The restore path is tested by restoring the preserved vfio cdev only. >> Test tries to finish the session without restoring iommufd and confirms >> that it fails. > >May be alos add more details that VFIO bind will also fail, it will return EPERM >due to ... Agreed will add more detail. > >> diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftests/iommu/Makefile >> +$(TEST_GEN_PROGS_EXTENDED): %: %.o $(LIBLIVEUPDATE_O) >> + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -static -o $@ > >Why static? User can pass static flag through EXTRA_CFLAGS. I will remove this. > >> diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate.c b/tools/testing/selftests/iommu/iommufd_liveupdate.c >> + >> +#define ksft_assert(condition) \ >> + do { if (!(condition)) \ >> + ksft_exit_fail_msg("Failed: %s at %s %d: %s\n", \ >> + #condition, __FILE__, __LINE__, strerror(errno)); } while (0) > >Please add indentation to the code. Will do. > >> + >> +int setup_cdev(const char *vfio_cdev_path) > >Nit: s/setup_cdev/open_cdev Will change this to open_cdev. > >Since you are using open_iommufd() name below, this will make it >consistent as there not setup here apart from opening. > >> +int setup_iommufd(int iommufd, int memfd, int cdev_fd, int hwpt_token) >> + bind.iommufd = iommufd; >> + ret = ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind); >> + ksft_assert(!ret); >> + >> + ret = ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data); >> + ksft_assert(!ret); >> + >> + hwpt_alloc.dev_id = bind.out_devid; >> + hwpt_alloc.pt_id = alloc_data.out_ioas_id; >> + ret = ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc); >> + ksft_assert(!ret); >> + >> + attach_data.pt_id = hwpt_alloc.out_hwpt_id; >> + ret = ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data); >> + ksft_assert(!ret); >> + >> + map_file.ioas_id = alloc_data.out_ioas_id; >> + ret = ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file); >> + ksft_assert(!ret); >> + >> + set_preserve.hwpt_id = attach_data.pt_id; >> + ret = ioctl(iommufd, IOMMU_HWPT_LU_SET_PRESERVE, &set_preserve); >> + ksft_assert(!ret); >> + >> + return ret; >> +} > >iommufd_utils.h has functions defined for iommufd ioctls. I think we >should use those functions here and if related to iommufd are not >present we should add there. iommufd_utils.h has utilities to use with mock testing, so those cannot be used here. I will see if I can make separate helper functions for these and use those here. > >> +int main(int argc, char *argv[]) >> +{ >> + int iommufd, cdev_fd, memfd, luo, session, ret; >> + const int token = 0x123456; >> + const int cdev_token = 0x654321; >> + const int hwpt_token = 0x789012; >> + const int memfd_token = 0x890123; >> + >> + if (argc < 2) { >> + printf("Usage: ./iommufd_liveupdate \n"); >> + return 1; >> + } >> + >> + luo = luo_open_device(); >> + ksft_assert(luo > 0); >> + >> + session = luo_retrieve_session(luo, "iommufd-test"); >> + if (session == -ENOENT) { >> + session = luo_create_session(luo, "iommufd-test"); >> + >> + iommufd = open_iommufd(); >> + memfd = create_sealed_memfd(SZ_1M); >> + cdev_fd = setup_cdev(argv[1]); >> + >> + ret = setup_iommufd(iommufd, memfd, cdev_fd, hwpt_token); >> + ksft_assert(!ret); >> + >> + /* Cannot preserve cdev without iommufd */ >> + ret = luo_session_preserve_fd(session, cdev_fd, cdev_token); >> + ksft_assert(ret); >> + >> + /* Cannot preserve iommufd without preserving memfd. */ >> + ret = luo_session_preserve_fd(session, iommufd, token); >> + ksft_assert(ret); >> + >> + ret = luo_session_preserve_fd(session, memfd, memfd_token); >> + ksft_assert(!ret); >> + >> + ret = luo_session_preserve_fd(session, iommufd, token); >> + ksft_assert(!ret); >> + >> + ret = luo_session_preserve_fd(session, cdev_fd, cdev_token); >> + ksft_assert(!ret); >> + > >All of these ksft_assert are hurting my eyes:) I like the approach in >VFIO where library APIs does validation and test code only check actual >things needed. > >Should we at least create a common function to combine both >luo_session_preserve() and ksft_assert()? Agreed. Will rework these. > Thanks, Sami