x86, drm, fbdev: Do not specify encrypted memory for video mappings
authorTom Lendacky <thomas.lendacky@amd.com>
Mon, 17 Jul 2017 21:10:26 +0000 (16:10 -0500)
committerIngo Molnar <mingo@kernel.org>
Tue, 18 Jul 2017 09:38:04 +0000 (11:38 +0200)
Since video memory needs to be accessed decrypted, be sure that the
memory encryption mask is not set for the video ranges.

Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Borislav Petkov <bp@suse.de>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Dave Young <dyoung@redhat.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Larry Woodman <lwoodman@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Matt Fleming <matt@codeblueprint.co.uk>
Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Toshimitsu Kani <toshi.kani@hpe.com>
Cc: kasan-dev@googlegroups.com
Cc: kvm@vger.kernel.org
Cc: linux-arch@vger.kernel.org
Cc: linux-doc@vger.kernel.org
Cc: linux-efi@vger.kernel.org
Cc: linux-mm@kvack.org
Link: http://lkml.kernel.org/r/a19436f30424402e01f63a09b32ab103272acced.1500319216.git.thomas.lendacky@amd.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
arch/x86/include/asm/vga.h
arch/x86/mm/pageattr.c
drivers/gpu/drm/drm_gem.c
drivers/gpu/drm/drm_vm.c
drivers/gpu/drm/ttm/ttm_bo_vm.c
drivers/gpu/drm/udl/udl_fb.c
drivers/video/fbdev/core/fbmem.c

index c4b9dc2f67c5f6f7a095dd7d4a3a6bb7fd7b3975..9f42beefc67a3d5d5242e054425c713ca394553e 100644 (file)
@@ -7,12 +7,24 @@
 #ifndef _ASM_X86_VGA_H
 #define _ASM_X86_VGA_H
 
+#include <asm/set_memory.h>
+
 /*
  *     On the PC, we can just recalculate addresses and then
  *     access the videoram directly without any black magic.
+ *     To support memory encryption however, we need to access
+ *     the videoram as decrypted memory.
  */
 
-#define VGA_MAP_MEM(x, s) (unsigned long)phys_to_virt(x)
+#define VGA_MAP_MEM(x, s)                                      \
+({                                                             \
+       unsigned long start = (unsigned long)phys_to_virt(x);   \
+                                                               \
+       if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))                 \
+               set_memory_decrypted(start, (s) >> PAGE_SHIFT); \
+                                                               \
+       start;                                                  \
+})
 
 #define vga_readb(x) (*(x))
 #define vga_writeb(x, y) (*(y) = (x))
index 9c8ea129ff5cd16580aa34fa055e6ddbf1e44b4d..dfb7d657cf4322b0dedcd0bb63c1058bd090b4ea 100644 (file)
@@ -1831,11 +1831,13 @@ int set_memory_encrypted(unsigned long addr, int numpages)
 {
        return __set_memory_enc_dec(addr, numpages, true);
 }
+EXPORT_SYMBOL_GPL(set_memory_encrypted);
 
 int set_memory_decrypted(unsigned long addr, int numpages)
 {
        return __set_memory_enc_dec(addr, numpages, false);
 }
+EXPORT_SYMBOL_GPL(set_memory_decrypted);
 
 int set_pages_uc(struct page *page, int numpages)
 {
index 8dc11064253d9e5ed58f8c817a471b36c25c5951..7a61a07ac4de97643199b09cbdaf185e80b9edaf 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/pagemap.h>
 #include <linux/shmem_fs.h>
 #include <linux/dma-buf.h>
+#include <linux/mem_encrypt.h>
 #include <drm/drmP.h>
 #include <drm/drm_vma_manager.h>
 #include <drm/drm_gem.h>
@@ -928,6 +929,7 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
        vma->vm_ops = dev->driver->gem_vm_ops;
        vma->vm_private_data = obj;
        vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
+       vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 
        /* Take a ref for this mapping of the object, so that the fault
         * handler can dereference the mmap offset's pointer to the object.
index 1170b3209a1269aff7c1cfa3692b0fe73b413bb8..ed4bcbfd60864ca46064620cd1c9b83e0b1c8c25 100644 (file)
@@ -40,6 +40,7 @@
 #include <linux/efi.h>
 #include <linux/slab.h>
 #endif
+#include <linux/mem_encrypt.h>
 #include <asm/pgtable.h>
 #include "drm_internal.h"
 #include "drm_legacy.h"
@@ -58,6 +59,9 @@ static pgprot_t drm_io_prot(struct drm_local_map *map,
 {
        pgprot_t tmp = vm_get_page_prot(vma->vm_flags);
 
+       /* We don't want graphics memory to be mapped encrypted */
+       tmp = pgprot_decrypted(tmp);
+
 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
        if (map->type == _DRM_REGISTERS && !(map->flags & _DRM_WRITE_COMBINING))
                tmp = pgprot_noncached(tmp);
index b442d12f2f7d64819faff9eace7bdc51fa199f89..84fb009d4eb045eeafb1e731faf418107613c69e 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/rbtree.h>
 #include <linux/module.h>
 #include <linux/uaccess.h>
+#include <linux/mem_encrypt.h>
 
 #define TTM_BO_VM_NUM_PREFAULT 16
 
@@ -230,9 +231,11 @@ static int ttm_bo_vm_fault(struct vm_fault *vmf)
         * first page.
         */
        for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
-               if (bo->mem.bus.is_iomem)
+               if (bo->mem.bus.is_iomem) {
+                       /* Iomem should not be marked encrypted */
+                       cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot);
                        pfn = bdev->driver->io_mem_pfn(bo, page_offset);
-               else {
+               else {
                        page = ttm->pages[page_offset];
                        if (unlikely(!page && i == 0)) {
                                retval = VM_FAULT_OOM;
index 4a650036256444ed84d1d2ef4e349236d5eb5f26..92e1690e28de90faf99d3fb0d2eec7308aa9d693 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/slab.h>
 #include <linux/fb.h>
 #include <linux/dma-buf.h>
+#include <linux/mem_encrypt.h>
 
 #include <drm/drmP.h>
 #include <drm/drm_crtc.h>
@@ -169,6 +170,9 @@ static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
        pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
                  pos, size);
 
+       /* We don't want the framebuffer to be mapped encrypted */
+       vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
+
        while (size > 0) {
                page = vmalloc_to_pfn((void *)pos);
                if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
index 7a42238db446b0093323505b710963ac94a53e15..25e862c487f643f97353abb607caf23ea336a757 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/device.h>
 #include <linux/efi.h>
 #include <linux/fb.h>
+#include <linux/mem_encrypt.h>
 
 #include <asm/fb.h>
 
@@ -1396,6 +1397,12 @@ fb_mmap(struct file *file, struct vm_area_struct * vma)
        mutex_lock(&info->mm_lock);
        if (fb->fb_mmap) {
                int res;
+
+               /*
+                * The framebuffer needs to be accessed decrypted, be sure
+                * SME protection is removed ahead of the call
+                */
+               vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
                res = fb->fb_mmap(info, vma);
                mutex_unlock(&info->mm_lock);
                return res;
@@ -1421,6 +1428,11 @@ fb_mmap(struct file *file, struct vm_area_struct * vma)
        mutex_unlock(&info->mm_lock);
 
        vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
+       /*
+        * The framebuffer needs to be accessed decrypted, be sure
+        * SME protection is removed
+        */
+       vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
        fb_pgprotect(file, vma, start);
 
        return vm_iomap_memory(vma, start, len);