Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1560371lqz; Mon, 1 Apr 2024 09:45:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV5Fk+zsJa3Gs/axOfxT+HnuKTxTi494SSttvSI4y3d+xv0HXTCAShRwO7DhTkJoyKJ8qXnVUjvQl8+WxceS1j7Qa+RDXtnQrvVJbM1fQ== X-Google-Smtp-Source: AGHT+IHtdSRWOePYE88jxF5rahnisZdE96mJ1/m5D5PwdL22VO9eRV/vmI7ownorDSE08Y0VUPHe X-Received: by 2002:a17:902:ce8c:b0:1e0:bc7e:6fba with SMTP id f12-20020a170902ce8c00b001e0bc7e6fbamr12275612plg.5.1711989913033; Mon, 01 Apr 2024 09:45:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711989913; cv=pass; d=google.com; s=arc-20160816; b=xRoJy0BdqJwWC2mHVjExHwqzLC3kHeVNmWr6Fy16Deu/0vFEM4s8e9+EukyiyqDC8C djW8goTI41yyD0ExbYydRRQmUugZ1mSWL9cttUbMWrlBEhZ2oNqfWNtX0zmOZCjG0zkf ODv51YPbJn7IYQTBJplJdzxNQOKsFGPSBW7fsgZxBH5XwQcnUSJRWTKNq5ifkAeIoDgs l580unIwi2xUmiA1hVhGPPKrl1uEwCq6BjkQPebIO7oro627PPxycuIkS27pHP1Fx1wq ANnvH0rmBBC6/Cp1e+DxMMKkEhCHbRNoaT3cVdD2KiG3tYIeJNYdBj8su9UCexwE1odw vo6Q== 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=bPZ8HI8yb2tnft68aw7E9+0tQDtGdBB8H184c2SEtm4=; fh=ToP3gqnFe2cE/W250dyVetgh2Q/Fz3jinxcdqnPekbE=; b=ONYV0zP46r3HCmApIrXo9/5rdXVSVNtxUifkPJXX4wK7YbkKQc1vTd+Qq6wcXIp4lw gpEvOvnNDy18KIgfKNwGkDKWdwTTULC9aawMGy3OCEpcTZjvC1yC5C5sEy3aTTmWiKAo zpg2gfX49PW9mm0NtAA/bCe8mIPSfJ9qwowjojzWVmxab+7u5y43DjMF6mJ4Y01YHdbh O8n1i8hJU8+FP/toPMYJL0EEt9wKKNOW/00zil0I/nXWmdjs1zacde3eIMCaRYoopm3v 8Vbsy+QEp/67MbullXmneWMCvcnta2UUpwwCM9PwfEy0BZ1XfFRgaGN7TjxIegBmnGoO HxCQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2K31v8ej; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-126864-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126864-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id w8-20020a170902e88800b001d96401fbfcsi9679634plg.521.2024.04.01.09.45.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 09:45:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-126864-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2K31v8ej; arc=pass (i=1 dkim=pass dkdomain=linuxfoundation.org); spf=pass (google.com: domain of linux-kernel+bounces-126864-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-126864-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A9A0E282FF1 for ; Mon, 1 Apr 2024 16:45:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F9ED4778C; Mon, 1 Apr 2024 16:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="2K31v8ej" 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 8B4E81C0DE7; Mon, 1 Apr 2024 16:45:07 +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=1711989907; cv=none; b=c80DtxHe4d7etVmaeqQ0w1XzkJa/j1OXLJW7ht+9T0xDpTlxhQ7KML1/TY4VKS9RzOqFG1RoOhmHpXeJx+kzZp4Gz7jM2nSk6I/cOYjdgrPuuA9WdDLTOSP5TdNNELsPHOhUURsnPqAHML+sm2fMLsw/u8e+c0GxWi6mKGXslxU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711989907; c=relaxed/simple; bh=ndOluXhy5iCnHo+fsgKaqDdkmKdF37DsDI6Xatxaba0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SKmTZNp+kS2nzQbsi6Ac+8lgcu8V/sCU/SU5uxnQM3YHftqhmbxltaEzvTOaxLUUKj11UBFLn+dwuhS0yzsqSKn3IvTIAUrN7IhC5IR0Omc2n3wExsgPJ0E16hgdVyVxK4AfZ22xfEdU9c/PdnomGBmHua89BHgmk6k2omXHcZo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=2K31v8ej; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3F75C433C7; Mon, 1 Apr 2024 16:45:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1711989907; bh=ndOluXhy5iCnHo+fsgKaqDdkmKdF37DsDI6Xatxaba0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2K31v8ejsp2P7dJ1rFMTyVfGVvLXD6jGCistNDg2KiURf3rDckOXO7nqhVTejvmcD Kgngw+ylDowjWjD/9KLGpTLuHc+6CDsmqHMJeEIlk/LKTdZfKpPg0BM1q28uQZi4cg cA1fDPrC429PWgrgQz78xHzrMlxTh5ggvmlYJLTI= 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.6 189/396] drm/ttm: Make sure the mapped tt pages are decrypted when needed Date: Mon, 1 Apr 2024 17:43:58 +0200 Message-ID: <20240401152553.574022311@linuxfoundation.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240401152547.867452742@linuxfoundation.org> References: <20240401152547.867452742@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.6-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