From patchwork Tue Oct 17 20:25:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13426026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9148CDB484 for ; Tue, 17 Oct 2023 20:25:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACEB18000C; Tue, 17 Oct 2023 16:25:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BDAA8006D; Tue, 17 Oct 2023 16:25:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 723B28006B; Tue, 17 Oct 2023 16:25:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 36DF98000C for ; Tue, 17 Oct 2023 16:25:37 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 11AE4C0EB8 for ; Tue, 17 Oct 2023 20:25:37 +0000 (UTC) X-FDA: 81356083914.09.078FC4C Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) by imf05.hostedemail.com (Postfix) with ESMTP id 209A9100003 for ; Tue, 17 Oct 2023 20:25:34 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NnrPIv5f; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697574335; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AvCyQwbXKjFfXAG0b7eB0SG9a2ztyacdff1oBiUujDQ=; b=j7D0QlEcTvb2bzl5qXCl4XSBmpQq7fnV057CxAe+soGo2ZKELVdIo+IpM5dJL3ktAk0l47 iHqRlXcTsaLQNAeQQGa86cL3hYo1NmsaAmwLixxa4mN1yRTP96hdNfXObzbfTqhw2QLW4o +4BEF+YzvXdIx0keONg/rW2Fsc1b6qA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=NnrPIv5f; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf05.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 192.198.163.7 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697574335; a=rsa-sha256; cv=none; b=aEKhKlkbNyY0jPN7nRdQzMztH9TMxxH2L14VQGHf0XRJIEJZkF1kC/uc9IxCBCe3g+/3Ct aLBKMGJjDz8+PQ85iNYdqaunXz2jxWN91CxXfJW3iOenr91C+6c3rtFiyrIK/yeLiHJyBL NwBxgl5LQEoa0LPZenWZXa2oKITojqg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697574335; x=1729110335; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1KnITalfSoEUJepIi5EFi3cXKWjkojf3kZ3un2NNboE=; b=NnrPIv5fKSyMVkk67iI5ylmjTZ9cZLCTxsyMcz7w3W+yac57UxZbCmIX li8hOnnXqElYlefL66UjMDmAL9BXz9wMbkeqtzGGC0mS7hPPvtYN4KY5N NMsnGyOFV8jqqUp2kDzD6eKu8QK2Ef3jcP4SEAdct9btON3Hx8P6OvXPE BF64XV4nvllAmMWbmOaO15wilnoCWd1F/Yh+PnUd16MUUnS0GqJtk0jPT 5b4JFnscG9p8hyD5C0XCcI55dO/piasSGritenD29uYugwEY9UvAZ8P58 N6Fb9ARnOJq8sUS1/d9C1Vn4iJ7VsaiueDjwiYSRvRdS0hDmNCOv13DCm Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="7429558" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="7429558" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2023 13:25:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10866"; a="900040460" X-IronPort-AV: E=Sophos;i="6.03,233,1694761200"; d="scan'208";a="900040460" Received: from rtdinh-mobl1.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.212.150.155]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2023 13:23:31 -0700 From: Rick Edgecombe To: x86@kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, luto@kernel.org, peterz@infradead.org, kirill.shutemov@linux.intel.com, elena.reshetova@intel.com, isaku.yamahata@intel.com, seanjc@google.com, Michael Kelley , thomas.lendacky@amd.com, decui@microsoft.com, sathyanarayanan.kuppuswamy@linux.intel.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Cc: rick.p.edgecombe@intel.com, Christoph Hellwig , Marek Szyprowski , Robin Murphy , iommu@lists.linux.dev Subject: [PATCH 06/10] dma: Use free_decrypted_pages() Date: Tue, 17 Oct 2023 13:25:01 -0700 Message-Id: <20231017202505.340906-7-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231017202505.340906-1-rick.p.edgecombe@intel.com> References: <20231017202505.340906-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 209A9100003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 4sfmrnnkm1egb1fxmc155pwnw94ez8hx X-HE-Tag: 1697574334-419881 X-HE-Meta: U2FsdGVkX1+Z9HwS8Z2pYlOHMFnco84ZKfXd/w+vi1Ja9fZ7febyWaDknn4rT2PaDsjuXJkTDghtcoYg9m7ZL0lEGTkTzeGDVC90yGfzfJ+aoiznQf6pQMH7WxdxV6BqMLnwoJx2r7tstzEfeHWr/6yZxMhGtGlItt/5ad0cft7r+rdiJirOjaaWluSqKttK5FLst4GCePlvxgmz3menqzErUan2ZH8IBE8Djl6r32OHd0HrPIG4sxIuaL4p+rSnvP8eBzjlysVU6SijxjWJEHRiApEJ9dqgmTlCFqGPM2NtlIUTy8kebi3MHr0RBtpHWFrwVXImFUfXtFW2+xfzTsCaC/84MNdwa8uJBngFqnfSqQPt88HXKfT8rHTnT7pzjase9O+mxcVb1L8cyS6u28HotH+oOJtAkcj1k0B8X+1Y9hAx9m/R7EMjYIFHZr4rQtLkI8NfMJyaCEg8em/G8yUm8nVYPacVc1ScAE7WCJe/RSGj5epSZUBNr5QnFS6UEWOIrZmRZNhLGNZTCIQ15gZ7vISCXyP/jUsORvTiP4KlWI1uXyq5ZOBtoFAIgTBTqAhHWJKQmwrk9t8pKMbcABjHVZ4RFW3FLJ4n8ug0CZFBCpC1VWePQ37EbSTP/KJmO0MS2GY/2n4ILAtdTArh5MwdO56fQ99+Jhoq0JDkEw+Vv0txaWHD8OKJ1t1Wx5w3cwsT68J/CtqRBO8zwnBoHjq9Ij6+PLug0jsmQ0Lu8uGr+zW69soT7qy48/pMLaLca+b5I5cYDqKPDx121mR2pKKm1mAR3Wc3UCDR6v9JynV6SenmSy3pJrxFvvKi1OW/TiwYQiTTIljXUE4jubrmF5gN6BS5wGbIHY3A9rSmJeHAXq1GXtuzdPJ4Iq37juB4iDVfNrmO6IyL7XTn4Rn/l7CffxQqSWXb2KtS5b/JQ4Ztfe0z8Sa4/Loa1UZz5b2Pw2pEdkad1kqUWPxj0HN TcJ7NGko KhHecELhx+lIoicFoMmL3684mxl4G1BtRqZg9QxzgNFuR7v9qTzgbF0Zog4fFy9r8cKnZ6oYL9zBOG2+8JmviphzuFfFXoaPRzIdNOhRw7ZDK5ErVX9k3yoUbgD/0ca7uH9ISnICMof/sd0itSuecIWYCoWcGZPK/X6RlmnSyBSRSIXO3WS+GNoz4OuN1k932Ebd0a0nY/9vk9xjcaSnEPd8bH/aKdSEt2FU3V0K9gpPYig699hzSo8DGVnSpdRcEgVaXmSNIzG1pFkhIqfQXNOavISA7Q7VBaS/h X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On TDX it is possible for the untrusted host to cause set_memory_encrypted() or set_memory_decrypted() to fail such that an error is returned and the resulting memory is shared. Callers need to take care to handle these errors to avoid returning decrypted (shared) memory to the page allocator, which could lead to functional or security issues. DMA could free decrypted/shared pages if set_memory_decrypted() fails. Use the recently added free_decrypted_pages() to avoid this. Several paths also result in proper encrypted pages being freed through the same freeing function. Rely on free_decrypted_pages() to not leak the memory in these cases. Cc: Christoph Hellwig Cc: Marek Szyprowski Cc: Robin Murphy Cc: iommu@lists.linux.dev Signed-off-by: Rick Edgecombe --- include/linux/dma-map-ops.h | 3 ++- kernel/dma/contiguous.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index f2fc203fb8a1..b0800cbbc357 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -9,6 +9,7 @@ #include #include #include +#include struct cma; @@ -165,7 +166,7 @@ static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size, static inline void dma_free_contiguous(struct device *dev, struct page *page, size_t size) { - __free_pages(page, get_order(size)); + free_decrypted_pages((unsigned long)page_address(page), get_order(size)); } #endif /* CONFIG_DMA_CMA*/ diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c index f005c66f378c..e962f1f6434e 100644 --- a/kernel/dma/contiguous.c +++ b/kernel/dma/contiguous.c @@ -429,7 +429,7 @@ void dma_free_contiguous(struct device *dev, struct page *page, size_t size) } /* not in any cma, free from buddy */ - __free_pages(page, get_order(size)); + free_decrypted_pages((unsigned long)page_address(page), get_order(size)); } /*