mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Puthukattukaran <james.puthukattukaran@oracle.com>
To: "Lendacky, Thomas" <thomas.lendacky@amd.com>,
	"Singh, Brijesh" <brijesh.singh@amd.com>
Cc: gregkh@linuxfoundation.org,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: [PATCH 1/1] drivers/char/mem.c: Disable encryption bit in page tables for, MMIO access
Date: Thu, 4 Oct 2018 13:18:58 -0400	[thread overview]
Message-ID: <f2160d42-9bbf-b7fc-315c-7db8f799cab3@oracle.com> (raw)
In-Reply-To: <671c6651-71b3-0e0d-8e87-191ca14fed50@oracle.com>

Attempting to mmap to a memory mapped IO space returns -1s because the
memory encryption bit is set for these pages. According to the AMD spec,
this bit should not be set for non-DRAM space. The patch checks if this
is an memory IO region being accessed and decrypts accordingly.

Signed-off-by: James Puthukattukaran<james.puthukattukaran@oracle.com>
---
 drivers/char/mem.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index ffeb60d..beaa374 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -355,10 +355,30 @@ static inline int private_mapping_ok(struct vm_area_struct *vma)
 #endif
 };
 
+static int check_flags(struct resource *res, void *arg)
+{
+	int *mmio = arg;
+	*mmio = (!((res->flags & IORESOURCE_SYSTEM_RAM) ==
+		IORESOURCE_SYSTEM_RAM) && (res->desc == IORES_DESC_NONE));
+	return *mmio;
+}
+
+static void check_iomem_region(phys_addr_t addr, size_t size,
+				int *mmio)
+{
+	u64 start, end;
+
+	start = (u64)addr;
+	end = start + size - 1;
+	*mmio = 0;
+	walk_mem_res(start, end, mmio, check_flags);
+}
+
 static int mmap_mem(struct file *file, struct vm_area_struct *vma)
 {
 	size_t size = vma->vm_end - vma->vm_start;
 	phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
+	int mmio;
 
 	/* Does it even fit in phys_addr_t? */
 	if (offset >> PAGE_SHIFT != vma->vm_pgoff)
@@ -387,6 +407,13 @@ static int mmap_mem(struct file *file, struct vm_area_struct *vma)
 
 	vma->vm_ops = &mmap_mem_ops;
 
+	if (mem_encrypt_active()) {
+		check_iomem_region(vma->vm_pgoff, size, &mmio);
+		if (mmio)
+			vma->vm_page_prot  =
+					pgprot_decrypted(vma->vm_page_prot);
+	}
+
 	/* Remap-pfn-range will mark the range VM_IO */
 	if (remap_pfn_range(vma,
 			    vma->vm_start,


      reply	other threads:[~2018-10-04 17:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-04 17:17 [PATCH 0/1] drivers/char/mem.c: Disable encryption bit in page tables for " James Puthukattukaran
2018-10-04 17:18 ` James Puthukattukaran [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f2160d42-9bbf-b7fc-315c-7db8f799cab3@oracle.com \
    --to=james.puthukattukaran@oracle.com \
    --cc=brijesh.singh@amd.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thomas.lendacky@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®