All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/1] drivers/char/mem.c: Disable encryption bit in page tables for MMIO access
@ 2018-10-04 17:17 James Puthukattukaran
  2018-10-04 17:18 ` [PATCH 1/1] drivers/char/mem.c: Disable encryption bit in page tables for, " James Puthukattukaran
  0 siblings, 1 reply; 2+ messages in thread
From: James Puthukattukaran @ 2018-10-04 17:17 UTC (permalink / raw)
  To: Lendacky, Thomas, Singh, Brijesh; +Cc: gregkh, Linux Kernel Mailing List

On AMD based systems, mmap'ing a PCI MMIO region does not return proper
values. This is because the mmap_mem function does not consider the fact
that IO regions are not to be encrypted. 

In the failing kernel, here's the output  --

[root@foo]# ./memaccess 0xd0000000 -t pmem -l 32
0    (0   ) : ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 
10   (16  ) : ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 


I instrumented the kernel to print out the PTE value --

Jun 20 11:46:35 bur-e1-2l-303 kernel: pfn = 0xfffffff80f0866b2, vma->pgoff = 0xd
0000, flags = 0x5044471,  prot = 0x8000800000000025
Jun 20 11:46:35 bur-e1-2l-303 kernel: pte = 0x80008000d0000235, pfn  = 0xd0000

Note that 0x8000800000000025 -- bit 47 is set. It should not be set for a 
MMIO region. 

When I disable memory encryption (mem_encrypt=off command line), things work
as they should.



[root@foo]# ./memaccess 0xd0000000 -t pmem -l 32
0    (0   ) : 20 00 00 01 40 08 00 04 f1 00 00 14 0a 00 ff 07 
10   (16  ) : 65 f6 70 02 c0 05 00 00 a0 04 00 00 0b 00 00 00 



James Puthukattukaran(1):
  drivers/char/mem.c: Disable encryption bit in page tables for MMIO
    access

 drivers/char/mem.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 1/1] drivers/char/mem.c: Disable encryption bit in page tables for, MMIO access
  2018-10-04 17:17 [PATCH 0/1] drivers/char/mem.c: Disable encryption bit in page tables for MMIO access James Puthukattukaran
@ 2018-10-04 17:18 ` James Puthukattukaran
  0 siblings, 0 replies; 2+ messages in thread
From: James Puthukattukaran @ 2018-10-04 17:18 UTC (permalink / raw)
  To: Lendacky, Thomas, Singh, Brijesh; +Cc: gregkh, Linux Kernel Mailing List

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,


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-04 17:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-04 17:17 [PATCH 0/1] drivers/char/mem.c: Disable encryption bit in page tables for MMIO access James Puthukattukaran
2018-10-04 17:18 ` [PATCH 1/1] drivers/char/mem.c: Disable encryption bit in page tables for, " James Puthukattukaran

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.