Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1972097ybe; Tue, 3 Sep 2019 06:18:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqxU6YEVXxTiZqPrOnt5mWtfNNwJbNF6ehW2RJq3UsG9sd4lu9vbRxCpBY+0tA7V2Fxb+b4p X-Received: by 2002:a63:f857:: with SMTP id v23mr30113705pgj.228.1567516680747; Tue, 03 Sep 2019 06:18:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567516680; cv=none; d=google.com; s=arc-20160816; b=SjdNHsr8pELyxuDlV/MlLjfwg2/1R4P8hWP+5RjTtDQghgqJdjsOCDamScb8+IPbWv mV4WcgVLDqqY+RS9xUJgCTATWdO86r3RkayX+6bHAVMNG26LpsPRgiPfFcQ7r9JcmB8A IAuSqCMoqN2kswUh76iDv2yJVev5Ezw3+HdG0t4afGp570RpXSc6rF3p7PEx0p4VRrHR MEmctsocCFa6h6ICmNTTDyPYuyEaYUgvNreZA0kld8yPF5zqttUxTqPthtlbaaAe1I2o U8mpYvWmFAojpldv1S0aKVgq0Gvjk3J+EQMnqmP6PG5uWFxGI1qRJpTLTyvA9PN7Oosj wEyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=UfH/G7LF0jgKvkK9Lbk94LwKyN+gktuW5T+nfM2fabs=; b=l4559wpGrsudSf0MqDWlBQusMpi3We+V3NaPRmsHKazfUQMK28Oa2xjOS/mWHb+mDH EEwn1RS97gh9GWT9w9q+3VuprdMC2SuKGln4fu93n4C7IuHGCL9Wu7nvgNOTvCTSL8V3 A34TY6GMjQEtgnjqvqzjXZtnw+DDCNu99xHxBDLWFjdbVT4hHAEW3SmbNbR8CZ17Kw8B 6a3utTmSy4HKDuBGIFTCgbNlbvRLTpdqU/7FLA+sM4cXvPxsaZaSzn0yu/hPznnJvIbP H3NDUHw5ePTbJrD/jrkLq0wydwjSy+tJx66RX9Bw1iw0b3W9C48ArHkCzcPUw9Z9invi ifJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@shipmail.org header.s=mail header.b=kRDnlGIx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v31si14790781plg.339.2019.09.03.06.17.44; Tue, 03 Sep 2019 06:18:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail (test mode) header.i=@shipmail.org header.s=mail header.b=kRDnlGIx; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729348AbfICNP3 (ORCPT + 99 others); Tue, 3 Sep 2019 09:15:29 -0400 Received: from pio-pvt-msa2.bahnhof.se ([79.136.2.41]:39744 "EHLO pio-pvt-msa2.bahnhof.se" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729332AbfICNP3 (ORCPT ); Tue, 3 Sep 2019 09:15:29 -0400 Received: from localhost (localhost [127.0.0.1]) by pio-pvt-msa2.bahnhof.se (Postfix) with ESMTP id 5CD594041E; Tue, 3 Sep 2019 15:15:21 +0200 (CEST) Authentication-Results: pio-pvt-msa2.bahnhof.se; dkim=pass (1024-bit key; unprotected) header.d=shipmail.org header.i=@shipmail.org header.b=kRDnlGIx; dkim-atps=neutral X-Virus-Scanned: Debian amavisd-new at bahnhof.se X-Spam-Flag: NO X-Spam-Score: -2.099 X-Spam-Level: X-Spam-Status: No, score=-2.099 tagged_above=-999 required=6.31 tests=[BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Received: from pio-pvt-msa2.bahnhof.se ([127.0.0.1]) by localhost (pio-pvt-msa2.bahnhof.se [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HEPT9RSmogm2; Tue, 3 Sep 2019 15:15:19 +0200 (CEST) Received: from mail1.shipmail.org (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) (Authenticated sender: mb878879) by pio-pvt-msa2.bahnhof.se (Postfix) with ESMTPA id 3AAC94047D; Tue, 3 Sep 2019 15:15:18 +0200 (CEST) Received: from localhost.localdomain.localdomain (h-205-35.A357.priv.bahnhof.se [155.4.205.35]) by mail1.shipmail.org (Postfix) with ESMTPSA id E453F360330; Tue, 3 Sep 2019 15:15:17 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=shipmail.org; s=mail; t=1567516518; bh=1DziKwTjiIMj17OZYZOeUEJ6CP5BMAzv4f1IdbkdopM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kRDnlGIxm2xAiKU2h+6QgzVP4jgLGi7H7/e3P/PcpWk2QfzaUVu6YHSkh4zYUw4AB cUsOOvXQOE0f5q8skRRj2Z6FvIhsATajksRYEOj/SPQmulnssLTAa5DJniDGmjbcbG t0Z4hJkPl5N9cWf9aRIhktUPL0iSoqtRb76d20xc= From: =?UTF-8?q?Thomas=20Hellstr=C3=B6m=20=28VMware=29?= To: dri-devel@lists.freedesktop.org, pv-drivers@vmware.com, linux-graphics-maintainer@vmware.com, linux-kernel@vger.kernel.org Cc: Thomas Hellstrom , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Heiko Carstens , Christian Borntraeger , Tom Lendacky , =?UTF-8?q?Christian=20K=C3=B6nig?= Subject: [PATCH v2 4/4] drm/ttm: Cache dma pool decrypted pages when AMD SEV is active Date: Tue, 3 Sep 2019 15:15:04 +0200 Message-Id: <20190903131504.18935-5-thomas_os@shipmail.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190903131504.18935-1-thomas_os@shipmail.org> References: <20190903131504.18935-1-thomas_os@shipmail.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Hellstrom The TTM dma pool allocates coherent pages for use with TTM. When forcing unencrypted DMA, such allocations become very expensive since the linear kernel map has to be changed to mark the pages decrypted. To avoid too many such allocations and frees, cache the decrypted pages even if they are in the normal cpu caching state, where otherwise the pool frees them immediately when unused. Tested with vmwgfx on SEV-ES. Cc: Dave Hansen Cc: Andy Lutomirski Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Heiko Carstens Cc: Christian Borntraeger Cc: Tom Lendacky Cc: Christian König Signed-off-by: Thomas Hellstrom --- drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c index 9b15df8ecd49..a3247f24e106 100644 --- a/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +++ b/drivers/gpu/drm/ttm/ttm_page_alloc_dma.c @@ -1000,7 +1000,7 @@ void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev) struct dma_pool *pool; struct dma_page *d_page, *next; enum pool_type type; - bool is_cached = false; + bool immediate_free = false; unsigned count, i, npages = 0; unsigned long irq_flags; @@ -1035,8 +1035,17 @@ void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev) if (!pool) return; - is_cached = (ttm_dma_find_pool(pool->dev, - ttm_to_type(ttm->page_flags, tt_cached)) == pool); + /* + * If memory is cached and sev encryption is not active, allocating + * and freeing coherent memory is relatively cheap, so we can free + * it immediately. If sev encryption is active, allocating coherent + * memory involves a call to set_memory_decrypted() which is very + * expensive, so cache coherent pages is sev is active. + */ + immediate_free = (ttm_dma_find_pool + (pool->dev, + ttm_to_type(ttm->page_flags, tt_cached)) == pool && + !force_dma_unencrypted(dev)); /* make sure pages array match list and count number of pages */ count = 0; @@ -1051,13 +1060,13 @@ void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev) d_page->vaddr &= ~VADDR_FLAG_UPDATED_COUNT; } - if (is_cached) + if (immediate_free) ttm_dma_page_put(pool, d_page); } spin_lock_irqsave(&pool->lock, irq_flags); pool->npages_in_use -= count; - if (is_cached) { + if (immediate_free) { pool->nfrees += count; } else { pool->npages_free += count; -- 2.20.1