Received: by 2002:a05:7412:a9a8:b0:f9:92ae:e617 with SMTP id o40csp132498rdh; Wed, 20 Dec 2023 19:19:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IFbDmKS8zDu2QPooHa1Hi5oZnxPLUgu+mpEYwvRk0W7vM8CoGq+0Oa5J9oD1iVQN5dmxW8X X-Received: by 2002:a17:902:e74e:b0:1cc:5e1b:98b5 with SMTP id p14-20020a170902e74e00b001cc5e1b98b5mr24021400plf.66.1703128772640; Wed, 20 Dec 2023 19:19:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703128772; cv=none; d=google.com; s=arc-20160816; b=aWKQSRJ6ejK68XQEn5/hIwjEKGJ6BvJ9hRZW/biGkehY/j1xnTEouaF6UHq1kkJhcp TI6eSmg5+e8u4j7XlWwn5SFMRTxXuz3YYlIdI23qdcD5YhNQirtZO0hq5HUExm/anFwL dzbUcSn8ZXEQAZirxw7C8+BXsLWAJfLar6YCoO1eFDkiY1EncYdq0I1PTrh1IPIv8EA9 fkJYffxk+X8qm7HVEKNRpermrs9uec4Atgw5E1vmeEcPSJH+N7lsmxwi7SrMdESTlFzQ Cq3l0XQhPGJMiR5tVf9HtNu6pI1syqLISEUU21/PKkoCgjUXjZqzRPvJixo/o2EK8wOY pCCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature; bh=+Ghdfdvj5dTvmX90o5EKDMFpbNy/cwlZMLwYqTLMlBw=; fh=+EQehun3nPNEkDgvQXjOrECitLCNDBPNEEjgmoD33ag=; b=sEjMkri3x1fKFQF0UoEhPIaqx73jME0o1R9LFgu5VgF+ythWA30P1UqpdFSNuDNdBx IeYWORcghBedM7ktz6mYLN4qQdO1v3y4anjM/vZWuHQYu/yhHSM/wC9Tg41w4V5Fo1Uy MuqPOZ2Rtch4Y9i5F4IMPdYCJiJDxN6GZjR2OJ2V6nwSLsHxC+3TA5LlmRs828NnlUmF 8GPVB68hS4QKeFn7xFrrkpkUEFDaRuZGjrH/GNcvmtKW4vNJ1gIipvdpu1GrmaXrUfEU Y9O5WfmZN25VXmjpJYW8vTOjdgyGCofMmh/ZsJ0uGYSzrgy6ZlQ34VWZq2VETk+dTToT Kaqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b="CNMWDCG/"; spf=pass (google.com: domain of linux-kernel+bounces-7799-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7799-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id jw11-20020a170903278b00b001d3ad3ec267si719690plb.376.2023.12.20.19.19.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 19:19:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-7799-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b="CNMWDCG/"; spf=pass (google.com: domain of linux-kernel+bounces-7799-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-7799-linux.lists.archive=gmail.com@vger.kernel.org" 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id BC1F7283D14 for ; Thu, 21 Dec 2023 03:19:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 689EE4C8C; Thu, 21 Dec 2023 03:19:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="CNMWDCG/" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.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 C369B4416 for ; Thu, 21 Dec 2023 03:19:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7811548d0b7so16413685a.1 for ; Wed, 20 Dec 2023 19:19:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1703128758; x=1703733558; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=+Ghdfdvj5dTvmX90o5EKDMFpbNy/cwlZMLwYqTLMlBw=; b=CNMWDCG/RVrfsVZIPCm6YfJJd8W+dCZ9Sx4TF5NzBsufl3wbh+a64Wkm9tphnA5I7G CELiS80qWUlCRIcAifwKgW1J48HRq4GNS4UvBEU3reqtur9N8Y4EzYLgIGhnJR+oVf89 XI4tMSKE1yT5eZPs/LU56OVzjwoA5j5z4wjBw5XHQ0281xJaldnKZScw2MgFZ2aRdwwr G6lbG+WW/CCXo1HcgNrYnZpAsWxWN4bAK/0J7MKcIEIbB0ZJnnahWtd5Hyi0OJjpdWmw sAvVG8ebqfDAmqZv6P17ej+Hg0iGpuLu5OI8xpl6FeaHIo84ICh67joiN+cajStLbbmw trRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703128758; x=1703733558; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+Ghdfdvj5dTvmX90o5EKDMFpbNy/cwlZMLwYqTLMlBw=; b=SpNm8O91ZqH5X4+mcLpYgzazcCVvHrIAAAJEJdlTQ+MKPTRJVO2mDjKgjExest3foz /bQpfPnYgzykPAKn7nTNoOMa+IBGrPAD5W4jHHRm+4RuvKcGHwEtOdiU1qjYDAimSCsE +z2K0B52/poll9rXQoa4DXVU3EOHIk9giIludUs+m9UmfGl0QZ40ZZOy48pBeaigB08v LAcIBP/gfvFzUXShCH0xAKeBjFT/i6QQzcw6h2Dzm3cOMQpKOHWTwLla0RDOgSQaA8sd Mk+GI8sR9HbLqKRIkTBHwe1/AMJgvGMA9yKSXvaJyu3OoKVKy9Ijwd4VpfCWR6t/aGWL cW7w== X-Gm-Message-State: AOJu0YxPBPLFzL2GtV64pa87V2cYtQI8fqRTjZSYn7eNInIuyb70/QQD wKQZ74MdAs4ARhmvT6R/ftxVqg== X-Received: by 2002:a05:620a:20c3:b0:781:129e:101 with SMTP id f3-20020a05620a20c300b00781129e0101mr1960327qka.37.1703128758561; Wed, 20 Dec 2023 19:19:18 -0800 (PST) Received: from soleen.c.googlers.com.com (55.87.194.35.bc.googleusercontent.com. [35.194.87.55]) by smtp.gmail.com with ESMTPSA id m18-20020a05620a221200b0077d85695db4sm371893qkh.99.2023.12.20.19.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Dec 2023 19:19:18 -0800 (PST) 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 0/3] iommu/intel: Free empty page tables on unmaps Date: Thu, 21 Dec 2023 03:19:12 +0000 Message-ID: <20231221031915.619337-1-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This series frees empty page tables on unmaps. It intends to be a low overhead feature. The read-writer lock is used to synchronize page table, but most of time the lock is held is reader. It is held as a writer for short period of time when unmapping a page that is bigger than the current iova request. For all other cases this lock is read-only. page->refcount is used in order to track number of entries at each page table. Microbenchmark data using iova_stress[1]: Base: yqbtg12:/home# ./iova_stress -s 16 dma_size: 4K iova space: 16T iommu: ~ 32783M time: 22.297s /iova_stress -s 16 dma_size: 4K iova space: 16T iommu: ~ 0M time: 23.388s The test maps/unmaps 4K pages and cycles through the IOVA space. Base uses 32G of memory, and test completes in 22.3S. Fix uses 0G of memory, and test completes in 23.4s. I believe the proposed fix is a good compromize in terms of complexity/ scalability. A more scalable solution would be to spread read/writer lock per-page table, and user page->private field to store the lock itself. However, since iommu already has some protection: i.e. no-one touches the iova space of the request map/unmap we can avoid the extra complexity and rely on a single per page table RW lock, and be in a reader mode most of the time. [1] https://github.com/soleen/iova_stress Pasha Tatashin (3): iommu/intel: Use page->refcount to count number of entries in IOMMU iommu/intel: synchronize page table map and unmap operations iommu/intel: free empty page tables on unmaps drivers/iommu/intel/iommu.c | 153 ++++++++++++++++++++++++++++-------- drivers/iommu/intel/iommu.h | 44 +++++++++-- 2 files changed, 158 insertions(+), 39 deletions(-) -- 2.43.0.472.g3155946c3a-goog