Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp926460pxj; Wed, 16 Jun 2021 17:26:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEdMscBcJ2O+XIXg1hUxQVTre1M+juVycJsvFrjJs+649RepGTPqoHPepHU3QqZY3meMN0 X-Received: by 2002:a05:6402:3553:: with SMTP id f19mr2919031edd.0.1623889619094; Wed, 16 Jun 2021 17:26:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623889619; cv=none; d=google.com; s=arc-20160816; b=TSBHGBjcAXR9JXkHTZqgdQ9Tsf0hPpiwNza1EmzcfN5vIlsEKIuiJGBpcQ4WmP+3vU RvrD5kcpuZkG9n8cjghT2cA7ip8XfIHxd0msCpIKAuBzhWkHi9+Rr2UhMBF+Gw+Aqu6W cgu6fPtqyBT5J0s2FQkLEj5Yi/Ua4Y7+qxQjjVaQ2MHqAipcXPKqKKoI5dj66tUebMhf wzp9B4kWQyGO17bDWOAikAZmZc/wWR26PSyWQ3NPEzMpRu84uAFRnsO3EbgtGrGMSBZJ Xu3sll8MDZ47EilPyQovjsecfvUuQJ13GmvUSkMYONM3XhhJgFnM+KNUfxnVnlZyuPeU kbjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hs2JqvHLc2VhwLkCLKEeX1pzwKqE7ns6gX3Zd+1Br1s=; b=SkvS5bdsP6C41WtBEyJ74Pok06Z271GWjvXFTJbMWCIsuyODnfgi/ZkQQMtEoZQVbd 56xCbRtFVfS8VqTLF3g+DVylkEJIqQGF1oe1lKIUAef/Y8o7ga8sHKgkuawEMmkXqClR jlA/KQGxx08dJwQ8qe54sCrAmE0KacBuDVfGTV+4VGSXdPlw0Dk/D37gBQBrmdwNlFRL KYttqDBJSJcbXLM7rcTlBPdM2VxtKvrEByjp8tx8XUSVwj6CAcL4Sr5hbaitH2sfyiIM 6ep+EP4NQBI57xB6ZwmG53ZIdAsUwVxffzFeMr3O9sK0e4BplH1j6Cj7SHssJ7HRo4hd JmuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AuFzhHmQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i6si3613045edr.379.2021.06.16.17.26.36; Wed, 16 Jun 2021 17:26:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=AuFzhHmQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231486AbhFPRgo (ORCPT + 99 others); Wed, 16 Jun 2021 13:36:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231442AbhFPRgi (ORCPT ); Wed, 16 Jun 2021 13:36:38 -0400 Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6BA6C061760 for ; Wed, 16 Jun 2021 10:34:29 -0700 (PDT) Received: by mail-pg1-x52d.google.com with SMTP id v7so2590463pgl.2 for ; Wed, 16 Jun 2021 10:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=hs2JqvHLc2VhwLkCLKEeX1pzwKqE7ns6gX3Zd+1Br1s=; b=AuFzhHmQOykzLH+MM2+snE+jgetovtrcAA9tToZ4Hwqpc4ggWwisMmLP8LmdDI1DZP M9wsgjXbeOs/7OrkHYKEI/LYtx9RaTtI6hSxFJVvoKvjlrGCCd+USinhf+sGBgqCzk/9 l5scqtjteV8HcG2vA1J63HQUPGEYU6E02fahsVg2hAVBJwA5BtD40lQeGzaEor8q5cX9 j2+J8F5sEaaM0/eSedOq2RCBdUUxUgkHGhxWjiUq2v5qR2leo73iWaAAMRQrAaZKEXQp OleZbYNR4kdcdGupYsUdUeRjxVv2R4Wxfd/aA1kE006DimXpl+tShRgh5fbZ1LXzmmqD 7hJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hs2JqvHLc2VhwLkCLKEeX1pzwKqE7ns6gX3Zd+1Br1s=; b=NoaWwsxxUhWHLgiR/xY1tY6yuyprV6VSU/DXcU/1MxqsR/jaJgiGPjXP3XKm8R+yDO SrgXqhz8sfri4HPSdF/vn09jxo6pUaAgSa/OasjiqUBPjISVkV7C5kfZVBBfRNfm3Q+3 TP0oeMMQRMJQkRh4Nv7L5xlgIm4uXnbd6fWq6memSDYilI9LRqlfPzo3ib9GQRQ7UaGe soVJ7pD6LeZyEabj3zGwUmSpKRHTCOz09BKilRS52Ge0b4ZS+UdycgTtcwM12x1hIsvI tfELVqv0rOBxNl9t53Fgx5qdWtHduln+bZkZbguSquPsyfm9B2RDvVy/qwTlOZT4ncvj Zorg== X-Gm-Message-State: AOAM530683GNIrLQt1Y3b+B5tc0GfZGbIIimS/1aNM5J1v915qA/WL9J bILV+BJHpCyiD5BSTYyV6jw= X-Received: by 2002:aa7:92c6:0:b029:2f1:3fbb:3171 with SMTP id k6-20020aa792c60000b02902f13fbb3171mr1043892pfa.0.1623864869005; Wed, 16 Jun 2021 10:34:29 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id m3sm2933901pfh.174.2021.06.16.10.34.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Jun 2021 10:34:28 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Joerg Roedel Cc: Nadav Amit , Jiajun Cao , Robin Murphy , Lu Baolu , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Will Deacon Subject: [PATCH v4 4/7] iommu: Factor iommu_iotlb_gather_is_disjoint() out Date: Wed, 16 Jun 2021 03:04:57 -0700 Message-Id: <20210616100500.174507-5-namit@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210616100500.174507-1-namit@vmware.com> References: <20210616100500.174507-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nadav Amit Refactor iommu_iotlb_gather_add_page() and factor out the logic that detects whether IOTLB gather range and a new range are disjoint. To be used by the next patch that implements different gathering logic for AMD. Note that updating gather->pgsize unconditionally does not affect correctness as the function had (and has) an invariant, in which gather->pgsize always represents the flushing granularity of its range. Arguably, “size" should never be zero, but lets assume for the matter of discussion that it might. If "size" equals to "gather->pgsize", then the assignment in question has no impact. Otherwise, if "size" is non-zero, then iommu_iotlb_sync() would initialize the size and range (see iommu_iotlb_gather_init()), and the invariant is kept. Otherwise, "size" is zero, and "gather" already holds a range, so gather->pgsize is non-zero and (gather->pgsize && gather->pgsize != size) is true. Therefore, again, iommu_iotlb_sync() would be called and initialize the size. Cc: Joerg Roedel Cc: Jiajun Cao Cc: Robin Murphy Cc: Lu Baolu Cc: iommu@lists.linux-foundation.org Cc: linux-kernel@vger.kernel.org> Acked-by: Will Deacon Signed-off-by: Nadav Amit --- include/linux/iommu.h | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index e554871db46f..979a5ceeea55 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -497,6 +497,28 @@ static inline void iommu_iotlb_sync(struct iommu_domain *domain, iommu_iotlb_gather_init(iotlb_gather); } +/** + * iommu_iotlb_gather_is_disjoint - Checks whether a new range is disjoint + * + * @gather: TLB gather data + * @iova: start of page to invalidate + * @size: size of page to invalidate + * + * Helper for IOMMU drivers to check whether a new range and the gathered range + * are disjoint. For many IOMMUs, flushing the IOMMU in this case is better + * than merging the two, which might lead to unnecessary invalidations. + */ +static inline +bool iommu_iotlb_gather_is_disjoint(struct iommu_iotlb_gather *gather, + unsigned long iova, size_t size) +{ + unsigned long start = iova, end = start + size - 1; + + return gather->end != 0 && + (end + 1 < gather->start || start > gather->end + 1); +} + + /** * iommu_iotlb_gather_add_range - Gather for address-based TLB invalidation * @gather: TLB gather data @@ -533,20 +555,16 @@ static inline void iommu_iotlb_gather_add_page(struct iommu_domain *domain, struct iommu_iotlb_gather *gather, unsigned long iova, size_t size) { - unsigned long start = iova, end = start + size - 1; - /* * If the new page is disjoint from the current range or is mapped at * a different granularity, then sync the TLB so that the gather * structure can be rewritten. */ - if (gather->pgsize != size || - end + 1 < gather->start || start > gather->end + 1) { - if (gather->pgsize) - iommu_iotlb_sync(domain, gather); - gather->pgsize = size; - } + if ((gather->pgsize && gather->pgsize != size) || + iommu_iotlb_gather_is_disjoint(gather, iova, size)) + iommu_iotlb_sync(domain, gather); + gather->pgsize = size; iommu_iotlb_gather_add_range(gather, iova, size); } -- 2.25.1