Received: by 2002:a9a:4c47:0:b029:116:c383:538 with SMTP id u7csp806469lko; Tue, 13 Jul 2021 10:32:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyE5Sli/sCwPIcfv/6NR+zy4vFNLVGm3pAs8GVAucMw5uU5MfWSMfodDLYebpZ4tucTTr+L X-Received: by 2002:a6b:5b01:: with SMTP id v1mr4073454ioh.132.1626197553503; Tue, 13 Jul 2021 10:32:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626197553; cv=none; d=google.com; s=arc-20160816; b=l5Ra6DLwBku/PmSHGvBeE8vPA5EcU8510uTTx32e4FDiFQauUN857Rqd3WPKLhwfrr i2J4A7yixks5H9talwF0vBwWdVZbK2HFK+71qOObEwC9xwPYOrmxjAjw1MV4tGDwntWB pcfUsL/aSm69K0FJJQc6UA6snxDWZuTayyUCkkA3eC20V2Aq/QXVsiNb9PRi+4eRUHHw 6fgALffaFLFskL1MjeZKIoR95iro/6fDpk/Nah9SEstmNwu0HPO6Cqsi8szhrnC+RtD6 PmcFiXMk6heJ4SXXMW0b0FqxabdTkOc9uHgbhDS7NcRaql2iv4ooNQX3ckCRjemwQ53D TA7A== 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=btyk1P0sOO00sH3TSxvktcgXFFh7CqbVErkDv5cbTW11rT+TKyaRhcU/dhYsrJppsj L3Y4OuifC1cyzMD6QdfUppClhscPRoBK7vYoUiJpYf8SfkM0q7VqvlHPlaTl3DlWcW0u 04F1xXlgji+H7Q8pRbLmLZ332+7yeEzVmi9HKjChwNE0cDOkBeGRLtwKwJUoIQ81JqdT kYw21UwvZ+xF8R2AmlQ9napCodu3aJ/e/tnmNTnNPn0DbLEynfuIGl4PPstEI0QZN3YP L3Iy2IUKgVaA6++0lPVVzSPyKaF7cN7EFUCIRllX4V1mR7FeaaiGWi+qeKX2hYHCkbJY utYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QAav+USR; 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 d21si4948362jam.9.2021.07.13.10.32.21; Tue, 13 Jul 2021 10:32:33 -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=QAav+USR; 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 S233375AbhGMR1r (ORCPT + 99 others); Tue, 13 Jul 2021 13:27:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233299AbhGMR1o (ORCPT ); Tue, 13 Jul 2021 13:27:44 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69506C0613DD for ; Tue, 13 Jul 2021 10:24:54 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id k20so15240165pgg.7 for ; Tue, 13 Jul 2021 10:24:54 -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=QAav+USR8km1W3fwSHLhKJS1on6grs3Opl96GLHDUXTzUdbAL5VYIkzoLyBrlANCbK h7tBqzU14vWuEXJHP7LavFv46VEE9aRy2w311L4AqiEr4ztmPKwZzTPgC/vKTwAZRGKF 8xn5xqsdT8QRQbhwm/rAziNhlFvLZDompwboVOpGY7UaqSk5HfvGfnzzUNm8ol5Ikg4q In8yeOl0RGiTRrCnAM2/36GUzvCB4jpNSWwyywTmTZW1ZGpe0wUeaU6RjGWp9iTt5O4q XgskL3sxT23Ri8ryCneep2SfQAeYns4MP9AgQ1b7Lc++vG7g422y5i/EtHDzO3ZqDDhW gTTg== 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=IbGD7pVXwmguGlX1GmOrimWxCWJ/8PtkWRJsgIwji6iGuHrnKFnNe6ZmkwwScr7wSt tT4UMDLJ8voPvBuxvZQCa3CTqutOi/myE0dRkIF+7P6zMcKya4TS8Z6NtghahEuaZ/zz 9x8vuZh4+RPNE7VynAUGORI+CLI9ML5sXNm9rGuUOzGlBIan4UfRNp7CkS3W9PbYw8aP +A4MJpKubfUEkMbFiiN1kRRF4+bDf/wi3ZZmkV2xujWwQM8OmXFEu5e4l+kf+ELKudXP WvgD7d8im4JO+RZqkdL08V8dhjcN/admA2DyLFpkGnX/6qfH4kdqoOEJiXkjBW30hqJY j+Vg== X-Gm-Message-State: AOAM5301tDneOEW83NjjOmGkiuvnKor/DXzInV+ZGM0/AbeLfFU0qXSh uOMzgOzr2yK6HjzlPW5uLSo= X-Received: by 2002:a62:3045:0:b029:32b:880f:c03a with SMTP id w66-20020a6230450000b029032b880fc03amr5756377pfw.22.1626197093839; Tue, 13 Jul 2021 10:24:53 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id lt14sm18239032pjb.47.2021.07.13.10.24.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 10:24:53 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Joerg Roedel Cc: John Garry , Nadav Amit , Jiajun Cao , Robin Murphy , Lu Baolu , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Will Deacon Subject: [PATCH v5 4/7] iommu: Factor iommu_iotlb_gather_is_disjoint() out Date: Tue, 13 Jul 2021 02:41:48 -0700 Message-Id: <20210713094151.652597-5-namit@vmware.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210713094151.652597-1-namit@vmware.com> References: <20210713094151.652597-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