Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9836924ybi; Wed, 24 Jul 2019 10:48:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNuNEHO8sCH8j3zOuMWUYFUNO1y2a59/HXnMY1jDQ37qFahKQ/DPPzPesVuRGnRnuRzyKG X-Received: by 2002:a63:e70f:: with SMTP id b15mr82842956pgi.152.1563990502047; Wed, 24 Jul 2019 10:48:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563990502; cv=none; d=google.com; s=arc-20160816; b=KTIYc2miR8BxO0GpthjL7Tcoc62ySq35ixxQR2AnUJodO7QvMr7mE2L39se9wJ7Dyw AURwf9sP7x6GStP3srblFEYF7oalojNtOThrmPHoEJ33BbFvAkeYjuKLDxnIlw1UX1R5 FKqAtFXZdxBBxbSptmffgExsnCxY4sLIWoNRhiWjT04YJ2onDIVwDux9mTYwGN3L7Nen XUeYOnwZrIfQtMgxPnMoAK0Z1Bb/1Gg7+CbWubJbQgKAt2eN+mj06mb4sqvRXLld7m4K f75D5lwVMvpLoPbVccQHN5EBin2pRYWzhnpT63YSMEQRu6WyLzNH7+lOP6tdG+3aoL5Z cG6Q== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=nL2zdqOJ/0dBkaTCAGj6x+KVcBR2toHf1sl9Pe2SuKs=; b=U94v9xyaFYV5hFZ/yAP1IF/gwaSEZeiP0Z6dOJMgV0TWLEQtqDClu1ZXGpZKuhaZgZ N4Rp8bS6/A97JacVkcb+Hm6eS4jgvXGF+nbJiDd2B4R+Xyd9avG6LHGfw06OIv29MEJu +k9GKxjjixnPQsD5qTvei61KOOghzgw30RvdmPI3eDCJK4OVz/X/KhJUMeuE5nyU7+pY dP86uFPagNsOk6CDO9GQ3FNGOCOSzOeoshYaNj5/K56nzp36xdrxaXddN8ayVdYf7jCf MLBfc5xo60Q02ds3kvRdMcmR/D/h9+0ImxMmENvRinMnb6FK5g6ZDloyBe+LWnwPqTUx 4Ccw== ARC-Authentication-Results: i=1; mx.google.com; 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 l64si13014495pjb.93.2019.07.24.10.48.07; Wed, 24 Jul 2019 10:48:22 -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; 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 S1727802AbfGXRGP (ORCPT + 99 others); Wed, 24 Jul 2019 13:06:15 -0400 Received: from foss.arm.com ([217.140.110.172]:44042 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725882AbfGXRGP (ORCPT ); Wed, 24 Jul 2019 13:06:15 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 464CE28; Wed, 24 Jul 2019 10:06:14 -0700 (PDT) Received: from [10.1.197.57] (e110467-lin.cambridge.arm.com [10.1.197.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 769E73F71F; Wed, 24 Jul 2019 10:06:12 -0700 (PDT) Subject: Re: [PATCH] dma-direct: Force unencrypted DMA under SME for certain DMA masks To: "Lendacky, Thomas" , "Kirill A. Shutemov" Cc: "iommu@lists.linux-foundation.org" , "linux-kernel@vger.kernel.org" , "x86@kernel.org" , Christoph Hellwig , Marek Szyprowski , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Lianbo Jiang References: <10b83d9ff31bca88e94da2ff34e30619eb396078.1562785123.git.thomas.lendacky@amd.com> <20190724155530.hlingpcirjcf2ljg@box> From: Robin Murphy Message-ID: Date: Wed, 24 Jul 2019 18:06:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 24/07/2019 17:42, Lendacky, Thomas wrote: > On 7/24/19 10:55 AM, Kirill A. Shutemov wrote: >> On Wed, Jul 10, 2019 at 07:01:19PM +0000, Lendacky, Thomas wrote: >>> @@ -351,6 +355,32 @@ bool sev_active(void) >>> } >>> EXPORT_SYMBOL(sev_active); >>> >>> +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ >>> +bool force_dma_unencrypted(struct device *dev) >>> +{ >>> + /* >>> + * For SEV, all DMA must be to unencrypted addresses. >>> + */ >>> + if (sev_active()) >>> + return true; >>> + >>> + /* >>> + * For SME, all DMA must be to unencrypted addresses if the >>> + * device does not support DMA to addresses that include the >>> + * encryption mask. >>> + */ >>> + if (sme_active()) { >>> + u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask)); >>> + u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask, >>> + dev->bus_dma_mask); >>> + >>> + if (dma_dev_mask <= dma_enc_mask) >>> + return true; >> >> Hm. What is wrong with the dev mask being equal to enc mask? IIUC, it >> means that device mask is wide enough to cover encryption bit, doesn't it? > > Not really... it's the way DMA_BIT_MASK works vs bit numbering. Let's say > that sme_me_mask has bit 47 set. __ffs64 returns 47 and DMA_BIT_MASK(47) > will generate a mask without bit 47 set (0x7fffffffffff). So the check > will catch anything that does not support at least 48-bit DMA. Couldn't that be expressed as just: if (sme_me_mask & dma_dev_mask == sme_me_mask) ? Robin.