Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6140123iob; Tue, 10 May 2022 11:13:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFVsPcw8b4jJigSB4Ns5GRy+sAE0jLZaFjoxLoWklrR8SQPwTeCmbugnAalyXit60423zC X-Received: by 2002:a05:6402:3226:b0:427:cef3:9fb3 with SMTP id g38-20020a056402322600b00427cef39fb3mr24470618eda.20.1652206415036; Tue, 10 May 2022 11:13:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652206415; cv=none; d=google.com; s=arc-20160816; b=uWVTvQCoPXQDCn4IKVB1tvsgSiJXSKK9+PUuBrpJ8+zmHYHqbNfPZC6VKKiFKmlab+ GntS8MiPdfidAj47FzxjJ6i1jprvpvlYbC6Ifpw31Xcak3s3sXsaU4PVPzx/v09jgtGS hvRl9+INcINgguwWSFpP5e+ZvUWmHOoumMXtBQkNVOHzeYt5l3rvkJbub1MBJ6izB4Je bPC+MqecPxihS0jMcnLaZFMhOgzYra6HAivrlZDrTiWeaEJUSLsSXs+VkyR2V1gaSY0p jRA+5aF/Znw9MbGJ+3g3eHlR1FfhxOfcSR1ciSyLviicd8jhR8mO9IU/usbinuGy2PCK FS5A== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hyf2KNFKnGMOiURkYrG/Sb0qAihSeqq38l2PYzzfEq4=; b=oddXppOqx6vbYYUwT3msEKRrQ9cHJ/eAr0dvU5Jt6cOsFfGZvJBE+08HTSbcY/Ireb eNubVuIaDLPNAQlmUisdjcP+9e1qS7xoyBKBjCcZAO5hZSscNOQkOE/blbUrm/J0vRNu rPjAWHvainl16mpraP2Gykq1lhPXkED2ZNN3eeSRddUVW8/ksJ7uiaamb72GQquT7+ey zZI5G0hDCrRiPE17bnAHBLoaNXs6EenypS/6M9U4kkv+FolMCgvSheQ1aV4SSj8buggt QhtpKBovS/fEPML6NGatbInWTJUldM+X6YURXXiLfDm2WgRh5b+qjYKELzvgF5dVj4rI pceQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bbCycOoV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o23-20020a056402039700b00425c46cc98esi15931098edv.92.2022.05.10.11.13.10; Tue, 10 May 2022 11:13:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bbCycOoV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244240AbiEJNzA (ORCPT + 99 others); Tue, 10 May 2022 09:55:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244583AbiEJNhv (ORCPT ); Tue, 10 May 2022 09:37:51 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5FA9C4E38F; Tue, 10 May 2022 06:25:56 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AB746B81DA0; Tue, 10 May 2022 13:25:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 165EFC385C2; Tue, 10 May 2022 13:25:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1652189153; bh=tVb7z9u2sLIB2f6SBSfo+pEfL244KEv53cuYpUEUuUI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bbCycOoVxLFp67Y/Riay8+Rex1MrExak8+mUiDcXaoaoVnPsxX/Tb/M/d1Hj4LauI byPQrU11RfaLwqPPtuqlUZE0Svl8d0G8j7zAaBQrbaS2kjrerd579IrU+Cttrb3GLe prQ3JOqNgFJLIhjktkNFrLvWA4IwCuoH1sXpg/co= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, David Stevens , Kevin Tian , Lu Baolu , Joerg Roedel Subject: [PATCH 5.10 09/70] iommu/vt-d: Calculate mask for non-aligned flushes Date: Tue, 10 May 2022 15:07:28 +0200 Message-Id: <20220510130733.137603691@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220510130732.861729621@linuxfoundation.org> References: <20220510130732.861729621@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Stevens commit 59bf3557cf2f8a469a554aea1e3d2c8e72a579f7 upstream. Calculate the appropriate mask for non-size-aligned page selective invalidation. Since psi uses the mask value to mask out the lower order bits of the target address, properly flushing the iotlb requires using a mask value such that [pfn, pfn+pages) all lie within the flushed size-aligned region. This is not normally an issue because iova.c always allocates iovas that are aligned to their size. However, iovas which come from other sources (e.g. userspace via VFIO) may not be aligned. To properly flush the IOTLB, both the start and end pfns need to be equal after applying the mask. That means that the most efficient mask to use is the index of the lowest bit that is equal where all higher bits are also equal. For example, if pfn=0x17f and pages=3, then end_pfn=0x181, so the smallest mask we can use is 8. Any differences above the highest bit of pages are due to carrying, so by xnor'ing pfn and end_pfn and then masking out the lower order bits based on pages, we get 0xffffff00, where the first set bit is the mask we want to use. Fixes: 6fe1010d6d9c ("vfio/type1: DMA unmap chunking") Cc: stable@vger.kernel.org Signed-off-by: David Stevens Reviewed-by: Kevin Tian Link: https://lore.kernel.org/r/20220401022430.1262215-1-stevensd@google.com Signed-off-by: Lu Baolu Link: https://lore.kernel.org/r/20220410013533.3959168-2-baolu.lu@linux.intel.com Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/intel/iommu.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1626,7 +1626,8 @@ static void iommu_flush_iotlb_psi(struct unsigned long pfn, unsigned int pages, int ih, int map) { - unsigned int mask = ilog2(__roundup_pow_of_two(pages)); + unsigned int aligned_pages = __roundup_pow_of_two(pages); + unsigned int mask = ilog2(aligned_pages); uint64_t addr = (uint64_t)pfn << VTD_PAGE_SHIFT; u16 did = domain->iommu_did[iommu->seq_id]; @@ -1638,10 +1639,30 @@ static void iommu_flush_iotlb_psi(struct if (domain_use_first_level(domain)) { domain_flush_piotlb(iommu, domain, addr, pages, ih); } else { + unsigned long bitmask = aligned_pages - 1; + + /* + * PSI masks the low order bits of the base address. If the + * address isn't aligned to the mask, then compute a mask value + * needed to ensure the target range is flushed. + */ + if (unlikely(bitmask & pfn)) { + unsigned long end_pfn = pfn + pages - 1, shared_bits; + + /* + * Since end_pfn <= pfn + bitmask, the only way bits + * higher than bitmask can differ in pfn and end_pfn is + * by carrying. This means after masking out bitmask, + * high bits starting with the first set bit in + * shared_bits are all equal in both pfn and end_pfn. + */ + shared_bits = ~(pfn ^ end_pfn) & ~bitmask; + mask = shared_bits ? __ffs(shared_bits) : BITS_PER_LONG; + } + /* * Fallback to domain selective flush if no PSI support or - * the size is too big. PSI requires page size to be 2 ^ x, - * and the base address is naturally aligned to the size. + * the size is too big. */ if (!cap_pgsel_inv(iommu->cap) || mask > cap_max_amask_val(iommu->cap))