From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2F232CC6B1D for ; Wed, 14 Oct 2026 09:46:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=XH1DvjtQ4kSOTYVjVn8rMjarIlp6Ws3f/rSFMl2hKxs=; b=AVce5+sNU+rbnRZx6clJEMwqSS WfdkAkeLXH+YovINNjFrElgwL+brooayDYjxh2fOl5niLsrjC2DhDaVfE7qUImVQXlfmwb4DeGkTH PiDeSxJOoPYOfsroXlk4D67EhJvFAsaWnI0bYFA17LbZjlwKvpxg391QdsfdQNh0Fwzu2c21hMR7p R5/pVMGO3KRyp4cGf38pdDWQzjUAFLRNXPoU6OyqHKVCTdkKLqrUlrtBK4ce7hRFK1YWzbX42sSVl t5eZEc2DEYA6+PNIdxFs9DLSZU9IAVJuS4J6jiAFLyXZabhKAmM1yVLa7U17KRx105qrIX0h2JebF moJoUq1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1xGvR7-0000000F40e-1kQB; Wed, 14 Oct 2026 09:38:13 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1xGvQl-0000000F3gD-3QQB for linux-arm-kernel@bombadil.infradead.org; Wed, 14 Oct 2026 09:37:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:MIME-Version :References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=XH1DvjtQ4kSOTYVjVn8rMjarIlp6Ws3f/rSFMl2hKxs=; b=ZTCclAOjKNQN0Um1QXhVeGcLIt Xd/D53ZYwvsXMejoD7Du4NjSnW/dmjyG6sgwO31uyqnIqsmP55K4uyWYAXK+AmSNWaSDwN6u0pYDS g5pwJSC/5rqJUjGVTam46erzP/tMHGWMAtlVYF/JIP/D7BDrkLFJYW9iNRlUNFFJIKV92Q5ugFypG UZJ3M7K/f3WCiilXQEVWrpQyllYZnlVtw/VIgKknIWP0jH2M9DGUYiX+uSTChzoVmTjl87xLbcbjr kPlSW3DstyJku76QkvT2IWQKrp+SpgSidLnOtoLYV9PrxdOYcLe4BhoshPFFQLY3usPNi4y5MnB7Y T4ezgBXQ==; Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by desiato.infradead.org with esmtps (Exim 4.99.5 #2 (Red Hat Linux)) id 1xGvQh-0000000GZZh-26oY for linux-arm-kernel@lists.infradead.org; Wed, 14 Oct 2026 09:37:50 +0000 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-48c4d870d54so2415601f8f.2 for ; Wed, 14 Oct 2026 02:37:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791970666; x=1792575466; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=XH1DvjtQ4kSOTYVjVn8rMjarIlp6Ws3f/rSFMl2hKxs=; b=dK6n/O7gbRZhfu0he99ueptXPtqX+4w+4vIFeTbA2/HoTUBOGDJX+/FJ3Og5C601b7 vZpK7TemVW8puxbK4NDXL8hTsdFH9JVe4znQqjFftJULqLQ4+XA60SVk7hBPzOWM5evu oxqyb5O12lB2Jl7bQnMMB4BbqZ4JuI2O/XkYhj2qb8db+2Aj1NbWKeBxStmcBGjKX0f9 +hdbEWCj45XdocSzqNbLbsEqx6sOShOFIfKMRrm7BE3NK/8khf6HEgi2zbdbnnlKUoyW sTc4D2lyr625wlVbxHxzRlsVcISv1ZDSrTRse9hSdZn5MHpaSfYnFGYhOnr/bZapw7su PkVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791970666; x=1792575466; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=XH1DvjtQ4kSOTYVjVn8rMjarIlp6Ws3f/rSFMl2hKxs=; b=vWU0V5RttKic/BcludMbotJSPBg1m0gYoa8MxY1w1+07Dcp6+nzfNxvcaD7HH6rzW7 /9WEQ3m9Y1H5wP2pBAboYumuZCSQDYbe46YebaoGYS3ellppQ/lAxVMDlyzM91ptzffQ qgIg3i09kG3YZ1uW23GAh/5M1CwrAQYC62VECOFrnydO0Qr497u0iHs1ZXwytyFrGxIJ dCZWANbrGvRqo15/7QWPfq9zWXVCRbpEHqOy48aMxq8SwuoT9YnwR1L+YLKFCW6Ngo88 SiBvtlLonSDxNtGd9NNjH7yHt/70hxQ72DrYh0xeXZCq//S48G+xlxgWozKsQ3dCFNXK 2DoA== X-Forwarded-Encrypted: i=1; AKwUvBy/Up8v/dH+KksdYRlalCacdlCvQdQQlKceeoYYxnM3U8R5QXou0IIzOfHyYyyJkQy8bpceI90CFcywYZbp05N9@lists.infradead.org X-Gm-Message-State: AFq9FYIQh01sFRZWKbbwhASDaipx5BEnc+XZSp65YXF7/TpowRQdslCo cwGa8MVd5CT0hGucK3HLd9hccysrtGV3ebOjZXNMOtUUKW5r/DnBdZ5Y X-Gm-Gg: AYBFou3IDR6xgYXfYlh435wdiB6fRvny1px7yrEOCUASr63RhwBBTXiF0UKaGfZfaxF rulNMlGVZ8UkPzryib6WCkSuEHIp0e8+CmkWlOOxW0HuwkKCuOrh92I0+8cRbAVKlhhQLGUg1WD IFJHUTFHFPBBDLqpHKd9LiRJGj36kCeF19Xq2VSYe+okCLK3283ok5/JrzdH1t2IBQn3MRh5hAq sZjnZfKjdSlzUh7Pml4qBdVSQNF9aEZIxd11HfohOVCr59x1myM3eI8ez+eZAFIKY9sEeNBNd3x mDDtF6yBrfX7cp+++iJO5QANwcTt4zRbiIOr4usC/RQIYiFSE5FPWPGBl8Bh2PKs09+iAdi2icU EhYVnyUaNhfM5q+GUH+sV2hARMy/ve+jvM8t7ccFB2g4O4A9XdjTJw88ojTEI7U6jxwt+Xq4bnm hRnAz2VXro4fIHyBzQI8ca+2L0t2L1IOV7z9uMY5Id4aGlZu8ceW8IDqVREuwHi65Nhas+CIVTo Uj4bluw66ug22Mo3atMed5LRnLKsxTFhRfX/L64TwwEVhhMmih/ERCahyFnotO1Ls+OkiP1TgcL 7n7nmLsQs6B/Tn9xrbPWSfxLbxF6tuPKIcv1 X-Received: by 2002:adf:e001:0:20b0:48b:efb:4a66 with SMTP id ffacd0b85a97d-48dba7b405bmr21828342f8f.6.1791970666161; Wed, 14 Oct 2026 02:37:46 -0700 (PDT) Received: from dev-dsk-fgriffo-1c-93421965.eu-west-1.amazon.com (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48de3f7f8b8sm9365477f8f.35.2026.10.14.02.37.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Oct 2026 02:37:45 -0700 (PDT) From: Fred Griffoul To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton , Catalin Marinas , Andrew Morton , David Hildenbrand , Alexander Viro , Christian Brauner , Jan Kara , Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Jonathan Corbet , Shuah Khan , Shuah Khan Cc: David Woodhouse , Ackerley Tng , "Aneesh Kumar K . V" , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Steffen Eiden , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org Subject: [RFC PATCH v2 08/11] iommufd/selftest: Add mock-domain IOVA queries Date: Wed, 14 Oct 2026 09:37:30 +0000 Message-ID: <20261014093733.90312-9-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261014093733.90312-1-griffoul@gmail.com> References: <20261014093733.90312-1-griffoul@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261014_103747_778926_30ABECE6 X-CRM114-Status: GOOD ( 25.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Fred Griffoul A test of memory that can be taken back or replaced does not know in advance which frame a device reaches. IOMMU_TEST_OP_MD_CHECK_MAP only compares against a known buffer. Add MD_CHECK_MAPPED, which checks that a range is fully mapped or fully unmapped, and MD_IOVA_TO_PHYS, which returns the frame behind an IOVA, or 0 if it is unmapped. Both refuse an IOVA outside the domain's aperture. Both hold domains_rwsem for writing so that a userspace unmap cannot free a page-table level during the walk. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Fred Griffoul --- drivers/iommu/iommufd/iommufd_test.h | 16 ++++ drivers/iommu/iommufd/selftest.c | 109 +++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 52b78cbcc920..28fd9c43edc4 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -31,6 +31,8 @@ enum { IOMMU_TEST_OP_PASID_CHECK_HWPT, IOMMU_TEST_OP_DMABUF_GET, IOMMU_TEST_OP_DMABUF_REVOKE, + IOMMU_TEST_OP_MD_CHECK_MAPPED, + IOMMU_TEST_OP_MD_IOVA_TO_PHYS, }; enum { @@ -193,6 +195,20 @@ struct iommu_test_cmd { __s32 dmabuf_fd; __u32 revoked; } dmabuf_revoke; + struct { + /* + * 1: every page in [iova, iova+length) must be mapped; + * 0: none of them may be. Mixed is an error. + */ + __u32 mapped; + __u32 __reserved; + __aligned_u64 iova; + __aligned_u64 length; + } check_mapped; + struct { + __aligned_u64 iova; + __aligned_u64 out_phys; /* 0 if unmapped */ + } iova_to_phys; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index af07c642a526..1f5cd2d00fda 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -2031,6 +2031,107 @@ static int iommufd_test_dmabuf_get(struct iommufd_ucmd *ucmd, return rc; } +/* + * True if [iova, iova + length) lies inside the domain's aperture. Outside + * it the page table returns an error code from iova_to_phys(), not 0. + */ +static bool mock_domain_covers(struct mock_iommu_domain *mock, + unsigned long iova, size_t length) +{ + struct iommu_domain_geometry *geo = &mock->domain.geometry; + unsigned long last; + + if (check_add_overflow(iova, length - 1, &last)) + return false; + return iova >= geo->aperture_start && last <= geo->aperture_end; +} + +/* + * iova_to_phys() walks the page table, so it must not run while an unmap + * frees a level of it. A userspace unmap holds the IOAS domains_rwsem for + * reading while it unmaps the domains, so holding it for writing keeps such + * unmaps away. A memory provider or dma-buf revoke unmaps under its pages + * mutex only, so tests must not run these queries while a revoke of the + * range is in progress. + */ +static struct rw_semaphore * +mock_domain_unmap_lock(struct iommufd_hw_pagetable *hwpt) +{ + return &to_hwpt_paging(hwpt)->ioas->iopt.domains_rwsem; +} + +/* + * Report the physical address the mock domain resolves @iova to, or 0 if + * it is unmapped. Lets a test check that two IOVAs share one frame, or that + * an IOVA moved to another frame, without knowing the frames in advance. + */ +static int iommufd_test_md_iova_to_phys(struct iommufd_ucmd *ucmd, + unsigned int mockpt_id, + unsigned long iova) +{ + struct iommu_test_cmd *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + struct mock_iommu_domain *mock; + unsigned int page_size; + int rc; + + hwpt = get_md_pagetable(ucmd, mockpt_id, &mock); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + page_size = 1 << __ffs(mock->domain.pgsize_bitmap); + if (iova % page_size || !mock_domain_covers(mock, iova, page_size)) { + rc = -EINVAL; + goto out_put; + } + down_write(mock_domain_unmap_lock(hwpt)); + cmd->iova_to_phys.out_phys = + mock->domain.ops->iova_to_phys(&mock->domain, iova); + up_write(mock_domain_unmap_lock(hwpt)); + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); +out_put: + iommufd_put_object(ucmd->ictx, &hwpt->obj); + return rc; +} + +static int iommufd_test_md_check_mapped(struct iommufd_ucmd *ucmd, + unsigned int mockpt_id, + unsigned long iova, size_t length, + bool mapped) +{ + struct iommufd_hw_pagetable *hwpt; + struct mock_iommu_domain *mock; + unsigned int page_size; + int rc = 0; + + hwpt = get_md_pagetable(ucmd, mockpt_id, &mock); + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); + + page_size = 1 << __ffs(mock->domain.pgsize_bitmap); + if (iova % page_size || length % page_size || !length || + !mock_domain_covers(mock, iova, length)) { + rc = -EINVAL; + goto out_put; + } + + down_write(mock_domain_unmap_lock(hwpt)); + for (; length; length -= page_size, iova += page_size) { + bool is_mapped = + mock->domain.ops->iova_to_phys(&mock->domain, iova) != 0; + + if (is_mapped != mapped) { + rc = -ENOENT; + break; + } + } + up_write(mock_domain_unmap_lock(hwpt)); + +out_put: + iommufd_put_object(ucmd->ictx, &hwpt->obj); + return rc; +} + static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd, bool revoked) { @@ -2143,6 +2244,14 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_dmabuf_revoke(ucmd, cmd->dmabuf_revoke.dmabuf_fd, cmd->dmabuf_revoke.revoked); + case IOMMU_TEST_OP_MD_CHECK_MAPPED: + return iommufd_test_md_check_mapped(ucmd, cmd->id, + cmd->check_mapped.iova, + cmd->check_mapped.length, + cmd->check_mapped.mapped); + case IOMMU_TEST_OP_MD_IOVA_TO_PHYS: + return iommufd_test_md_iova_to_phys(ucmd, cmd->id, + cmd->iova_to_phys.iova); default: return -EOPNOTSUPP; } -- 2.47.3