Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1548930lqz; Mon, 1 Apr 2024 09:25:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVqpIwGP3Wxss4UGsKo5JHV4OY9cIbiDIFHBE91Ll3ncT8jJAfTHqfJYzBnrfI78utEvVhh/7R54rCpXw/Bcu5G/7VC4k6ZNPG2rsKyEQ== X-Google-Smtp-Source: AGHT+IGc1GQN5i4nSTfE4PrLZSm+0BegayPSi1xAeWKmZJrqTxszmkxbJi5Vk2JF3d64mtLAqaT/ X-Received: by 2002:a05:620a:3883:b0:78a:5cd3:9875 with SMTP id qp3-20020a05620a388300b0078a5cd39875mr9202183qkn.3.1711988709361; Mon, 01 Apr 2024 09:25:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711988709; cv=pass; d=google.com; s=arc-20160816; b=lBW1KSq64QrpuEku42/kQaVVp5BZkruR3cZItYp3TGpnFTS/ptrCWVASBGMUSNFTJh hRdYyJnyPVZu3MAxg/0EDCDZbzrdcoDF9ZEzvMZU+jSahwtNuUf+0hOOZo/bqIcO7ftC srwenrStSmvn5P1Dl7yLurfkjHdcap8S+E9f8YxFwnV+UjLaxNWQ/se/0eSC7IVnSYAC vPlz5QyrnsF9nVK3BJOz72wX1+cPZKpb9pju/IXYyRffqxlGAqkAVIP0pAwIezVqCrhh gI1RnHTdOsHxRef2y+VU4Z2Pv/ggZz9gEMEl3FPQ2zhzaTL+Y5bo0w94pzf/L0Nj2nz9 9atA== ARC-Message-Signature: i=2; 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:user-agent:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=w4oB3y1LaifJnFZLmO7FKF5e477WItcnaeF5M9bkI6c=; fh=ToP3gqnFe2cE/W250dyVetgh2Q/Fz3jinxcdqnPekbE=; b=YqUSzXeC7EjEYd6nH26v8D+lTS0C1Mh9cB6dsuoIqfeFwoaFcjoux1vdZr3+JtKEyr hF3XFF9R7YrDs1lU6mmHOsTa9fQ3LT56mF84PwpT8UMgTFaeCN34/mZueec2KEl7b7Gq XXKd5UMmckUY3PLggpADz6iZts/ZTkkbaYoF1PLv2SNvKlFcw0/YO0/5pwILMZEe6ctE xy8wMTWxMMtqugF73WocJiF033yqUF+isf4VeYnZnrWzW/ha10qrLrkTIZbXkElMr1Rk pdOvU+0UyomvbDqq6nQMY13K7Dn3Hq+J9pe9OWfhymtBygBem+jVbAVW4QFUEFftuVv8 zLug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WWXNMNSe; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-126849-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126849-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id t4-20020a05620a0b0400b0078a15896c19si9894482qkg.213.2024.04.01.09.25.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 09:25:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-126849-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WWXNMNSe; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-126849-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126849-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id DAC871C210E1 for ; Mon, 1 Apr 2024 16:25:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AD8B64778E; Mon, 1 Apr 2024 16:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="WWXNMNSe" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B397A1CA8F; Mon, 1 Apr 2024 16:25:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711988703; cv=none; b=hYYrUOC3dQJWxmo9nvkTN9QS7PH/jt/2tj5VwcuZXzrdvNFDac0NUuUVP6qkAal7zaRt9QHKQTxayI46aBJA9sV/lmT/yz0Ls8pJ1w+EagwUT4SOz/IV8IrlBSqF1ulFPVHIDAco4u9K/ig2lKr8sVb+qTkeBqgpGwYSijfeZXA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711988703; c=relaxed/simple; bh=vh8HYeY3QEuMjDyv6XrWLcRk0hsRK2twHm/EM9Ituso=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=L5Wp5JGVbuHwfFAjvyE5vyUp7TkrfKGQJXmQGfWOVi5nCQJ2WRM6IFa+NP2GPXG8refvDsiq/3oaFVa68JwhyMu/4smFoTCz5JpYo504xCWiEvD9VOWa31b8x/+iYoRIKOP/myzxaFF4ZHbleE4mcXpF2e/wbV7nRhum/r+k+J8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=WWXNMNSe; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD7FAC433C7; Mon, 1 Apr 2024 16:25:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1711988703; bh=vh8HYeY3QEuMjDyv6XrWLcRk0hsRK2twHm/EM9Ituso=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WWXNMNSe6mmEo+SvhYIjv5qC4EGPdIOB4ca/EnxLQbLIBzSxu2kItdEx/dNpkFu6Y vnr9M7qhX7BfYXwbfKMrLnpZwBcsh8kjSA2SoFUDxo1zDxy2eOYde/wzxlGa7m6m1P Fu20azm9HaXVhlVC5tlMiQsOO64AaITGRD6FnGzI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Zack Rusin , =?UTF-8?q?Thomas=20Hellstr=C3=B6m?= , =?UTF-8?q?Christian=20K=C3=B6nig?= , Huang Rui , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Sasha Levin Subject: [PATCH 6.7 225/432] drm/ttm: Make sure the mapped tt pages are decrypted when needed Date: Mon, 1 Apr 2024 17:43:32 +0200 Message-ID: <20240401152559.846508342@linuxfoundation.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240401152553.125349965@linuxfoundation.org> References: <20240401152553.125349965@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.7-stable review patch. If anyone has any objections, please let me know. ------------------ From: Zack Rusin [ Upstream commit 71ce046327cfd3aef3f93d1c44e091395eb03f8f ] Some drivers require the mapped tt pages to be decrypted. In an ideal world this would have been handled by the dma layer, but the TTM page fault handling would have to be rewritten to able to do that. A side-effect of the TTM page fault handling is using a dma allocation per order (via ttm_pool_alloc_page) which makes it impossible to just trivially use dma_mmap_attrs. As a result ttm has to be very careful about trying to make its pgprot for the mapped tt pages match what the dma layer thinks it is. At the ttm layer it's possible to deduce the requirement to have tt pages decrypted by checking whether coherent dma allocations have been requested and the system is running with confidential computing technologies. This approach isn't ideal but keeping TTM matching DMAs expectations for the page properties is in general fragile, unfortunately proper fix would require a rewrite of TTM's page fault handling. Fixes vmwgfx with SEV enabled. v2: Explicitly include cc_platform.h v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to limit the scope to guests and log when memory decryption is enabled. Signed-off-by: Zack Rusin Fixes: 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for page-based iomem") Reviewed-by: Thomas Hellström Acked-by: Christian König Cc: Huang Rui Cc: dri-devel@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org Cc: # v5.14+ Link: https://patchwork.freedesktop.org/patch/msgid/20230926040359.3040017-1-zack@kde.org Signed-off-by: Sasha Levin --- drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++-- drivers/gpu/drm/ttm/ttm_tt.c | 13 +++++++++++++ include/drm/ttm/ttm_tt.h | 9 ++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c index fd9fd3d15101c..0b3f4267130c4 100644 --- a/drivers/gpu/drm/ttm/ttm_bo_util.c +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c @@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res, enum ttm_caching caching; man = ttm_manager_type(bo->bdev, res->mem_type); - caching = man->use_tt ? bo->ttm->caching : res->bus.caching; + if (man->use_tt) { + caching = bo->ttm->caching; + if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED) + tmp = pgprot_decrypted(tmp); + } else { + caching = res->bus.caching; + } return ttm_prot_from_caching(caching, tmp); } @@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, .no_wait_gpu = false }; struct ttm_tt *ttm = bo->ttm; + struct ttm_resource_manager *man = + ttm_manager_type(bo->bdev, bo->resource->mem_type); pgprot_t prot; int ret; @@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, if (ret) return ret; - if (num_pages == 1 && ttm->caching == ttm_cached) { + if (num_pages == 1 && ttm->caching == ttm_cached && + !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) { /* * We're mapping a single page, and the desired * page protection is consistent with the bo. diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c index e0a77671edd6c..43eaffa7faae3 100644 --- a/drivers/gpu/drm/ttm/ttm_tt.c +++ b/drivers/gpu/drm/ttm/ttm_tt.c @@ -31,11 +31,14 @@ #define pr_fmt(fmt) "[TTM] " fmt +#include #include #include #include #include #include +#include +#include #include #include @@ -60,6 +63,7 @@ static atomic_long_t ttm_dma32_pages_allocated; int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc) { struct ttm_device *bdev = bo->bdev; + struct drm_device *ddev = bo->base.dev; uint32_t page_flags = 0; dma_resv_assert_held(bo->base.resv); @@ -81,6 +85,15 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc) pr_err("Illegal buffer object type\n"); return -EINVAL; } + /* + * When using dma_alloc_coherent with memory encryption the + * mapped TT pages need to be decrypted or otherwise the drivers + * will end up sending encrypted mem to the gpu. + */ + if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { + page_flags |= TTM_TT_FLAG_DECRYPTED; + drm_info(ddev, "TT memory decryption enabled."); + } bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags); if (unlikely(bo->ttm == NULL)) diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h index a4eff85b1f449..2b9d856ff388d 100644 --- a/include/drm/ttm/ttm_tt.h +++ b/include/drm/ttm/ttm_tt.h @@ -79,6 +79,12 @@ struct ttm_tt { * page_flags = TTM_TT_FLAG_EXTERNAL | * TTM_TT_FLAG_EXTERNAL_MAPPABLE; * + * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as + * not encrypted. The framework will try to match what the dma layer + * is doing, but note that it is a little fragile because ttm page + * fault handling abuses the DMA api a bit and dma_map_attrs can't be + * used to assure pgprot always matches. + * * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is * set by TTM after ttm_tt_populate() has successfully returned, and is * then unset when TTM calls ttm_tt_unpopulate(). @@ -87,8 +93,9 @@ struct ttm_tt { #define TTM_TT_FLAG_ZERO_ALLOC BIT(1) #define TTM_TT_FLAG_EXTERNAL BIT(2) #define TTM_TT_FLAG_EXTERNAL_MAPPABLE BIT(3) +#define TTM_TT_FLAG_DECRYPTED BIT(4) -#define TTM_TT_FLAG_PRIV_POPULATED BIT(4) +#define TTM_TT_FLAG_PRIV_POPULATED BIT(5) uint32_t page_flags; /** @num_pages: Number of pages in the page array. */ uint32_t num_pages; -- 2.43.0