Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1817676rdb; Tue, 3 Oct 2023 01:39:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF0WsP3UL8FHGhfonp9ddyBXcCbRfM+JsWtxVy7mPt4FfuyVWSUFOsxNZF64powhGsag7mb X-Received: by 2002:a05:6808:1287:b0:3a8:4e27:3af3 with SMTP id a7-20020a056808128700b003a84e273af3mr18352370oiw.48.1696322385647; Tue, 03 Oct 2023 01:39:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696322385; cv=none; d=google.com; s=arc-20160816; b=fUIebjmIxpc+nistU+PIueXf1UIz+Cv95MRX9X0NPhnEUIyIlGRZ611LqB8NVMLTEc b+1CeOttXkpfdZsfU2+oMjiCNmIr8c5Kj4fw+PfegfLx3Ev4cBNpTLF8+h2iJ7SoO4Cl 53H/7OnDHoGXhw7m+RnZ1F4DXQ1/yrbDV06dX1XYk4U/HwWzKTyspW/LufKy3bmxyVIE aH7oW8siMQ9m1hVGgFRAYs0COnn+ATIENXzj8CvTu6GFTt3M/jFEQCHOG1pL01lLiP0C Ri//Vd8qRmKfzHOMATMVmQuUMwQI8pf4w0wilgtWs9PG/62WXcGIAnHQoAOMhE54W0ju GwXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=17dL3Lk/6ufAsrBjaUmOsgyZX70WACaioehDx9d6CA0=; fh=uxffmhWKhCjma8X3H67WgiW2b9VTtScQuNTGEvOV+Qg=; b=Qa8Hrmd31BVDynB7HWPeIOAXogxtTH9OG20fXxdcyfMjo2psqw/iB5g21UbPiF5b1X SeIFbg+zdid2+hyyiXe7rJCcq7hxX4fz/MTVbSV5QkowQVadEiK1rbK5UMK9Tj8kZpSe VGrnXzvRFGjMPvBrFnPLlwFLr4gtyOVhErAYOyiEWjRamN8wbleRVciZ5cVves2xLcT3 lsajd/S2yk/9sK4J4yTpKwW0uJho3vtBZ4Xu+bbKwlrxSkXa6rZhW7ocDwvOSwnCxuAp LAanyO5dQh1PuXw7tX3sBOKt6ruNmpJ07mQWjLOtNK7BkWpOM0Vm1gk/ZNsJrGQ6dBt4 Zffw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I3G6yuEN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id o24-20020a639218000000b0057c3aaec1c8si1011155pgd.52.2023.10.03.01.39.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 01:39:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I3G6yuEN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 9C304813CDB3; Tue, 3 Oct 2023 01:39:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239346AbjJCIjm (ORCPT + 99 others); Tue, 3 Oct 2023 04:39:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230435AbjJCIjl (ORCPT ); Tue, 3 Oct 2023 04:39:41 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E527C97; Tue, 3 Oct 2023 01:39:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696322377; x=1727858377; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=SnpNVBUF66nW+RhzxAtvjdqNqvomxWSemJuUTLpDNSM=; b=I3G6yuENMyy4A6kphHzWByIw3e5OVoOJPP+vqcFS/T4lArDY3nrLn/dX Y6crIgThvdNdY/bQA3vIDD70cG0AmMZy7cxTdO1piQ/8+SPytZxy+naLN Q+zRlqxYHJ6M/4zAvFkEcGO68V7SBzZRlBOvkw+l6pgBDBsYCOw865DXZ lItKMeoXrSfgxGl92mMxlLJti9Gkf3ioZDA4gSJNJvk4iJuUYT5GfP93X 4kIYsurzZLQL9r0fxV6zGTX3Wd//TIJdNNO4aWWo64KCXcPC0UwswaVY5 0uFIf14FHtR5LmG1ttW1yPLa3/I/al4J/RrTTjxN58ugTgUF2Mg+ugpVn A==; X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="362193503" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="362193503" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:39:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10851"; a="816605201" X-IronPort-AV: E=Sophos;i="6.03,196,1694761200"; d="scan'208";a="816605201" Received: from fhoeg-mobl1.ger.corp.intel.com (HELO [10.249.254.234]) ([10.249.254.234]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Oct 2023 01:39:23 -0700 Message-ID: <3ab13ec8-40ec-2952-5b1c-0d20d99c512a@linux.intel.com> Date: Tue, 3 Oct 2023 10:39:20 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v2] drm/ttm: Make sure the mapped tt pages are decrypted when needed Content-Language: en-US To: Zack Rusin , "dri-devel@lists.freedesktop.org" Cc: "christian.koenig@amd.com" , "ray.huang@amd.com" , "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" References: <20230926040359.3040017-1-zack@kde.org> <20230926175113.679880-1-zack@kde.org> <2b5648aa-f83d-d8f7-b0fd-39c859f32f33@linux.intel.com> <09fedce26475023fb1089f8b0b77801e1d2363c0.camel@linux.intel.com> <1679743e607097b967a932d2aefacbbc7913e5cb.camel@vmware.com> From: =?UTF-8?Q?Thomas_Hellstr=c3=b6m?= In-Reply-To: <1679743e607097b967a932d2aefacbbc7913e5cb.camel@vmware.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 03 Oct 2023 01:39:44 -0700 (PDT) On 10/3/23 06:13, Zack Rusin wrote: > On Mon, 2023-10-02 at 16:27 +0200, Thomas Hellström wrote: >> !! External Email >> >> On Mon, 2023-10-02 at 10:16 +0200, Thomas Hellström wrote: >>> Hi, Zack >>> >>> On 9/26/23 19:51, Zack Rusin wrote: >>>> From: Zack Rusin >>>> >>>> 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 >>>> >>>> Signed-off-by: Zack Rusin >>>> Fixes: 3bf3710e3718 ("drm/ttm: Add a generic TTM memcpy move for >>>> page-based iomem") >>>> Cc: Christian König >>>> Cc: Thomas Hellström >>>> Cc: Huang Rui >>>> Cc: dri-devel@lists.freedesktop.org >>>> Cc: linux-kernel@vger.kernel.org >>>> Cc: # v5.14+ >>>> --- >>>>   drivers/gpu/drm/ttm/ttm_bo_util.c | 13 +++++++++++-- >>>>   drivers/gpu/drm/ttm/ttm_tt.c      |  8 ++++++++ >>>>   include/drm/ttm/ttm_tt.h          |  9 ++++++++- >>>>   3 files changed, 27 insertions(+), 3 deletions(-) >>>> >>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c >>>> b/drivers/gpu/drm/ttm/ttm_bo_util.c >>>> index fd9fd3d15101..0b3f4267130c 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 e0a77671edd6..e4966e2c988d 100644 >>>> --- a/drivers/gpu/drm/ttm/ttm_tt.c >>>> +++ b/drivers/gpu/drm/ttm/ttm_tt.c >>>> @@ -31,6 +31,7 @@ >>>> >>>>   #define pr_fmt(fmt) "[TTM] " fmt >>>> >>>> +#include >>>>   #include >>>>   #include >>>>   #include >>>> @@ -81,6 +82,13 @@ 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_MEM_ENCRYPT)) >>> You need to use CC_ATTR_GUEST_MEM_ENCRYPT here rather than >>> CC_ATTR_MEM_ENCRYPT to avoid touching and breaking the SME case and >>> only >>> fix the SEV / SEV-ES case. I'd also hold off the stable inclusion >>> until >>> it's completely verified that this doesn't break anything because if >>> it >>> does, I suspect all hell will break loose. >>> >>> With that said, for the functionality >>> >>> Reviewed-by: Thomas Hellström >>> >>> But I think this needs a wider Ack at the ttm / drm level for the >>> approach taken. >>> >>> /Thomas. >> FWIW, I think that if TTM_TT_FLAG_DECRYPTED is set, it should be >> possible to add a debug WARN_ON_ONCE() if the first PTE of the dma >> page's kernel virtual address does not use a decrypted pgprot_t. One >> way of accessing the PTEs in a platform-generic fashion is >> apply_to_page_range(). > Good point. > > Another, trivial solution to that problem of possible regression would simply be > introducing: > > #define TTM_DEVICE_USE_DMA_ALLOC BIT(0) > #define TTM_DEVICE_USE_GFP_DMA32 BIT(1) > #define TTM_DEVICE_USE_DECRYPTED_SYS_MEM BIT(2) > > and changing ttm_device_init from: > > int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs, > struct device *dev, struct address_space *mapping, > struct drm_vma_offset_manager *vma_manager, > bool use_dma_alloc, bool use_dma32); > to: > int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs, > struct device *dev, struct address_space *mapping, > struct drm_vma_offset_manager *vma_manager, > u32 use_flags); > > The driver should have a lot clearer picture of whether > TTM_DEVICE_USE_DECRYPTED_SYS_MEM should be used. That change requires porting the > drivers to the new ttm_device_init (which is trivial) but guarantees no regressions > simply by virture of having vmwgfx use TTM_DEVICE_USE_DECRYPTED_SYS_MEM only (at > least initially, I imagine at least qxl would need it as well). I've been thinking along those lines as well. But the current direction appears to be to hide all of the encryption interaction in the dma layer, so when / once we do it right, those driver overrides will probably cause even more grief. /Thomas > > Christian, any thoughts? > > z