From patchwork Fri Apr 26 03:43:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 13644105 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48DBDC4345F for ; Fri, 26 Apr 2024 03:43:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 77A956B0098; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 68EA46B0099; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52F416B009A; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1ED2B6B0099 for ; Thu, 25 Apr 2024 23:43:30 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C87941C0C09 for ; Fri, 26 Apr 2024 03:43:29 +0000 (UTC) X-FDA: 82050288138.13.C1DDC93 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) by imf29.hostedemail.com (Postfix) with ESMTP id 436F7120003 for ; Fri, 26 Apr 2024 03:43:28 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=RA1Fgqa7; dmarc=pass (policy=none) header.from=soleen.com; spf=pass (imf29.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.177 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714103008; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GURCfQOZVuUrKhF3OYuy9zEmklzBEXgHfus1Ew76lR0=; b=RBh+6nS0FmfgtrRbzxAlIBveeRu6qDgxQyBixlEePN/FAG9OaAVHyRO57IZt/g0Rmtc/5o dywtrQhHl4olrX0Zj/trDaZ48zWpVanujMnp0NRXuhrT0E2xhSU99oVWiJaf4uD0VZQo02 QrdWhcHlFdsgL9KlVZEqK8y6yf5kynI= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=RA1Fgqa7; dmarc=pass (policy=none) header.from=soleen.com; spf=pass (imf29.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.160.177 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714103008; a=rsa-sha256; cv=none; b=sWvQF2v5jRozRbsNsKC4/Cqjj0Idv/Vn879BlP55jxmcecw+ynAkp1K84JkRotngzmvCGF heyX3hw81rO0v6T5Lpm5Mbwl7K6CvAo4RO+OBrtgeZ8GqIesc7MxhVhzxeqAuuRYfoLiPn 2bdU+3qF8xHXnEooUSOFDJpe6yxEEnA= Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-4375ddb9eaeso10634801cf.3 for ; Thu, 25 Apr 2024 20:43:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1714103007; x=1714707807; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=GURCfQOZVuUrKhF3OYuy9zEmklzBEXgHfus1Ew76lR0=; b=RA1Fgqa7QmwEIKUz8VcrtUvSGp77QW26muTz/qypMstvtuXVpWgi3TU6ed+CJLxUSb dWtSZld6IMhGwUY+rBtDKrKVyYI1U+eDFu8v5KVDgEhil73kr2EzPRr9d0RFCFCYWSKl tDlUW3iL+qFRbPm70DStBYBulSfz37difYXmDgmEFFlYNAf1jgU9Na8RD3UGwYs6uEIC EEEtM/2EjiwJFMmdM9E153GcxYrefqhwqfs3UgJjaE7OJfNUnlMoreawbYssZDBQHqBA G0b29ak/d1CuSAOKb4OKMA40hs28QFKKTREkA/BCXQn0CtUR6sg2FjNlECcgIs5BUd0u 07zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714103007; x=1714707807; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GURCfQOZVuUrKhF3OYuy9zEmklzBEXgHfus1Ew76lR0=; b=gQwvCgj2gNPN7VAN6CZ/n4XiE+Z7+b2v1Q8gtd9nd48AjG4OsTg8h1Sv0Lpgo25f4Z Qu5wUB5OAIL8iE71IItbtp7Bab+KH548GpcmTW+e2lbQeb9gSp9mEldD567h6bQ2ffBm yEvxT+yBNZhjFyusg+P2dU/PgI+dpk47yOLjinFVDJFlfVZVexR4DnYSPZ94XLcatAtm w0waD6IOlKB3iOSGSQhNGj0NhTLuK1n3t8T5JpJDGnmtZTBFiQTPDU6SkyYn2y75DGTr 9YdUykmujA3CidtvkgXterQpKsLKsOX1xIhpQ57N/bkhVoVhCIL9m7uftNneSmbxCMgz RVTQ== X-Forwarded-Encrypted: i=1; AJvYcCXSEzejXU1sF2zt9KNqa+KVdu2kc4WuKMBoc+se+kynBu0a+svbKRPMIBMOvIOBd+LHvl8SYA69gcgx7RbSLHvf8us= X-Gm-Message-State: AOJu0YwBqe3B/ebLyyh7nVldAwhXvSCjkQ7jgLoa/LsbIcbu4XQcLKAO nENMsSFnEVgSrB6e4OW2Viqk3UmMC8/tfTBnPlq+At8mT+v4wLg9YppGIQOYANs= X-Google-Smtp-Source: AGHT+IHx2jRF9sHZe0ji37o0f9d/WxOmNqlXZKb8rptZFsjIiM8LnbaBigJJCVNFjwy+WUFUifGdEw== X-Received: by 2002:ac8:7f0e:0:b0:437:a1a2:f832 with SMTP id f14-20020ac87f0e000000b00437a1a2f832mr2149126qtk.11.1714103007410; Thu, 25 Apr 2024 20:43:27 -0700 (PDT) Received: from soleen.c.googlers.com.com (129.177.85.34.bc.googleusercontent.com. [34.85.177.129]) by smtp.gmail.com with ESMTPSA id c6-20020ac80546000000b00436bb57faddsm7540815qth.25.2024.04.25.20.43.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 20:43:27 -0700 (PDT) From: Pasha Tatashin To: akpm@linux-foundation.org, linux-mm@kvack.org, pasha.tatashin@soleen.com, linux-kernel@vger.kernel.org, rientjes@google.com, dwmw2@infradead.org, baolu.lu@linux.intel.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, iommu@lists.linux.dev Subject: [RFC v2 2/3] iommu/intel: synchronize page table map and unmap operations Date: Fri, 26 Apr 2024 03:43:22 +0000 Message-ID: <20240426034323.417219-3-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog In-Reply-To: <20240426034323.417219-1-pasha.tatashin@soleen.com> References: <20240426034323.417219-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 436F7120003 X-Stat-Signature: hfhgocstrd5qbgut4zysnf1qz147ujq9 X-HE-Tag: 1714103008-82148 X-HE-Meta: U2FsdGVkX19k2HQlN22Oe7wj305a/EfR1T8cET1InQNQgPRACwk+sQ+pOisiV6Um6hJWr8RFilR/juNP4IYQK+jLlWqL3xGKO8O+hBbKwAkOlPTd6WlRzqoWOUPi7HGoGuIP+DytJ+gydno+g36Adw0qtPOAeiuszexAxu902gUSrhewK+IKqVtFhNI4vaAWLcku7gY4LpPM6xOYPEAxLvSa+povEQRWbWi0TtJOvgl2av9TjYBt1FXWw9klX4rQS2C7H/kV23n3ZIuxOTo+0LiBzXHrcQF8d829Lc1jWzsYWczSksVBGgDYGbHiOoiF6uqUZKaL73mb35Y4lpK0h0uHvBBhATYef5Q6U9pZOiUmQFIwFvpSC4GUGlWIUG1CgC6I7FNYGanjrBv2FvNxVX998QTzk7ZJneX6INFJi1wfeaw27UQ3RK9o+Cvq8CxhdmuOtSbbQNKq1W9wGG2IMGKhAwzOdGLT047N6zXNgBN916+vEwofjngVVDxLXESzVXtcp02OIZjTyeejGjRvuRao7+KTmDHHVNCoyV9zwAIjgjTTtcOqZZbPEgholDmEQ6OA97vMtwwVMvlJyWU0pnFO0Ddd/cjfefS5Qt75C4R6ITxPl1970U80USRCpk9n4NVsOHzC9Sh+cCqe3ek4jh6pS1uuSVuzcZt2zF9l8NfFakwN+O++QvOVQNBbZMhZlKzg+eXJeNO86dsBM5GK4bv+MtZE9qk8K1uRBqsmyEeuJsnJf/fZ/HlDh7qD9BEhNaH2Czul9JsEtJyjGAbI4EY799IEiDvf/660BlKSn2JukLKqBeAwnDruUznhon8IQns69HBhZqOa/K8B+Xky1hIk/1hzwhSxYKdWh+hL925+Cf/IZ1dmbTngEpEQmN66iIAvqWmq4A++uEldSxzTtN7h9g3/ZKHuJ8W4itP3FCkTe/fcnJi4o3V6Um/N5jdBvgW/i8YAD4hXPxNnw8l kLRRs6mw tyuFByeZarWSuxo4VCzYt43Y+ZTvE+khasiviPSs+kCIhOk6kScIcewPr6CXixsfHzqOBysIrVpHyvt5bL34t7BPNTmxpk0AXRhogPyagLruXq1SRtb5ghgnVlJAT5DNH0k1PiVKC/mx8R7diKycmhZnDRbxDLH6TJoGovrBAv7aBTxw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since, we are going to update parent page table entries when lower level page tables become emtpy and we add them to the free list. We need a way to synchronize the operation. Use domain->pgd_lock to protect all map and unmap operations. This is reader/writer lock. At the beginning everything is going to be read only mode, however, later, when free page table on unmap is added we will add a writer section as well. Signed-off-by: Pasha Tatashin --- drivers/iommu/intel/iommu.c | 21 +++++++++++++++++++-- drivers/iommu/intel/iommu.h | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 1bfb6eccad05..8c7e596728b5 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -995,11 +995,13 @@ static void dma_pte_free_pagetable(struct dmar_domain *domain, unsigned long last_pfn, int retain_level) { + read_lock(&domain->pgd_lock); dma_pte_clear_range(domain, start_pfn, last_pfn); /* We don't need lock here; nobody else touches the iova range */ dma_pte_free_level(domain, agaw_to_level(domain->agaw), retain_level, domain->pgd, 0, start_pfn, last_pfn); + read_unlock(&domain->pgd_lock); /* free pgd */ if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { @@ -1093,9 +1095,11 @@ static void domain_unmap(struct dmar_domain *domain, unsigned long start_pfn, WARN_ON(start_pfn > last_pfn)) return; + read_lock(&domain->pgd_lock); /* we don't need lock here; nobody else touches the iova range */ dma_pte_clear_level(domain, agaw_to_level(domain->agaw), domain->pgd, 0, start_pfn, last_pfn, freelist); + read_unlock(&domain->pgd_lock); /* free pgd */ if (start_pfn == 0 && last_pfn == DOMAIN_MAX_PFN(domain->gaw)) { @@ -2088,6 +2092,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, pteval = ((phys_addr_t)phys_pfn << VTD_PAGE_SHIFT) | attr; + read_lock(&domain->pgd_lock); while (nr_pages > 0) { uint64_t tmp; @@ -2097,8 +2102,10 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, pte = pfn_to_dma_pte(domain, iov_pfn, &largepage_lvl, gfp); - if (!pte) + if (!pte) { + read_unlock(&domain->pgd_lock); return -ENOMEM; + } first_pte = pte; lvl_pages = lvl_to_nr_pages(largepage_lvl); @@ -2158,6 +2165,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, pte = NULL; } } + read_unlock(&domain->pgd_lock); return 0; } @@ -3829,6 +3837,7 @@ static int md_domain_init(struct dmar_domain *domain, int guest_width) domain->pgd = iommu_alloc_page_node(domain->nid, GFP_ATOMIC); if (!domain->pgd) return -ENOMEM; + rwlock_init(&domain->pgd_lock); domain_flush_cache(domain, domain->pgd, PAGE_SIZE); return 0; } @@ -4074,11 +4083,15 @@ static size_t intel_iommu_unmap(struct iommu_domain *domain, unsigned long start_pfn, last_pfn; int level = 0; + read_lock(&dmar_domain->pgd_lock); /* Cope with horrid API which requires us to unmap more than the size argument if it happens to be a large-page mapping. */ if (unlikely(!pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, - &level, GFP_ATOMIC))) + &level, GFP_ATOMIC))) { + read_unlock(&dmar_domain->pgd_lock); return 0; + } + read_unlock(&dmar_domain->pgd_lock); if (size < VTD_PAGE_SIZE << level_to_offset_bits(level)) size = VTD_PAGE_SIZE << level_to_offset_bits(level); @@ -4145,8 +4158,10 @@ static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain, int level = 0; u64 phys = 0; + read_lock(&dmar_domain->pgd_lock); pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &level, GFP_ATOMIC); + read_unlock(&dmar_domain->pgd_lock); if (pte && dma_pte_present(pte)) phys = dma_pte_addr(pte) + (iova & (BIT_MASK(level_to_offset_bits(level) + @@ -4801,8 +4816,10 @@ static int intel_iommu_read_and_clear_dirty(struct iommu_domain *domain, struct dma_pte *pte; int lvl = 0; + read_lock(&dmar_domain->pgd_lock); pte = pfn_to_dma_pte(dmar_domain, iova >> VTD_PAGE_SHIFT, &lvl, GFP_ATOMIC); + read_unlock(&dmar_domain->pgd_lock); pgsize = level_size(lvl) << VTD_PAGE_SHIFT; if (!pte || !dma_pte_present(pte)) { iova += pgsize; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index e5c1eb23897f..2f38b087ea4f 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -615,6 +615,9 @@ struct dmar_domain { struct { /* virtual address */ struct dma_pte *pgd; + + /* Synchronizes pgd map/unmap operations */ + rwlock_t pgd_lock; /* max guest address width */ int gaw; /*