From patchwork Tue Jan 22 21:17:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 10776279 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 078A513B4 for ; Tue, 22 Jan 2019 21:18:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E83652B7FA for ; Tue, 22 Jan 2019 21:18:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D7F5E2B9D4; Tue, 22 Jan 2019 21:18:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76B522B7FA for ; Tue, 22 Jan 2019 21:18:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726940AbfAVVSk (ORCPT ); Tue, 22 Jan 2019 16:18:40 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:39904 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726872AbfAVVSe (ORCPT ); Tue, 22 Jan 2019 16:18:34 -0500 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x0MLFlQC107135 for ; Tue, 22 Jan 2019 16:18:32 -0500 Received: from e32.co.us.ibm.com (e32.co.us.ibm.com [32.97.110.150]) by mx0b-001b2d01.pphosted.com with ESMTP id 2q68ay628p-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 22 Jan 2019 16:18:31 -0500 Received: from localhost by e32.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 22 Jan 2019 21:18:31 -0000 Received: from b03cxnp08025.gho.boulder.ibm.com (9.17.130.17) by e32.co.us.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 22 Jan 2019 21:18:27 -0000 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x0MLIQf723724032 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 22 Jan 2019 21:18:26 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6460DBE053; Tue, 22 Jan 2019 21:18:26 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 387C0BE05A; Tue, 22 Jan 2019 21:18:22 +0000 (GMT) Received: from morokweng.localdomain.com (unknown [9.85.185.73]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 22 Jan 2019 21:18:21 +0000 (GMT) From: Thiago Jung Bauermann To: x86@kernel.org Cc: kvm@vger.kernel.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Christoph Hellwig , Marek Szyprowski , Robin Murphy , Paolo Bonzini , =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Tom Lendacky , Ram Pai , Thiago Jung Bauermann Subject: [PATCH 1/2] dma-direct: set_memory_{en,de}crypted() take number of pages Date: Tue, 22 Jan 2019 19:17:57 -0200 X-Mailer: git-send-email 2.17.2 X-TM-AS-GCONF: 00 x-cbid: 19012221-0004-0000-0000-000014D6A738 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00010457; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000275; SDB=6.01150301; UDB=6.00599445; IPR=6.00930634; MB=3.00025245; MTD=3.00000008; XFM=3.00000015; UTC=2019-01-22 21:18:31 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19012221-0005-0000-0000-00008A51EFEC Message-Id: <20190122211758.8317-1-bauerman@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-01-22_11:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901220159 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Ram Pai set_memory_encrypted() and set_memory_decrypted() expect the number of PAGE_SIZE pages to encrypt or decrypt. dma_direct_alloc() and dma_direct_free() instead pass number of bytes. This encrypts/decrypts a huge number of pages resulting in data corruption. Fixed it. [ bauermann: Slightly reworded commit message and added Fixes: tag. ] Fixes: d7b417fa08d1 ("x86/mm: Add DMA support for SEV memory encryption") Signed-off-by: Ram Pai Signed-off-by: Thiago Jung Bauermann --- kernel/dma/direct.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) Notes: 1. This was tested on powerpc with patches adding support for running under the ultravisor, which are not yet upstream. 2. The lines changed in this patch were added by commit c10f07aa27da ("dma/direct: Handle force decryption for DMA coherent buffers in common code"), but it only moves the code from an x86-specific file. Therefore the Fixes tag references the commit that first introduced the code. diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 355d16acee6d..bc78c37220ba 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -166,7 +166,7 @@ void *dma_direct_alloc_pages(struct device *dev, size_t size, ret = page_address(page); if (force_dma_unencrypted()) { - set_memory_decrypted((unsigned long)ret, 1 << get_order(size)); + set_memory_decrypted((unsigned long)ret, 1); *dma_handle = __phys_to_dma(dev, page_to_phys(page)); } else { *dma_handle = phys_to_dma(dev, page_to_phys(page)); @@ -186,10 +186,8 @@ void __dma_direct_free_pages(struct device *dev, size_t size, struct page *page) void dma_direct_free_pages(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr, unsigned long attrs) { - unsigned int page_order = get_order(size); - if (force_dma_unencrypted()) - set_memory_encrypted((unsigned long)cpu_addr, 1 << page_order); + set_memory_encrypted((unsigned long)cpu_addr, 1); __dma_direct_free_pages(dev, size, virt_to_page(cpu_addr)); }