PCI: Add support for relative addressing in quirk tables
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 22 Aug 2018 04:56:18 +0000 (21:56 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 22 Aug 2018 17:52:47 +0000 (10:52 -0700)
Allow the PCI quirk tables to be emitted in a way that avoids absolute
references to the hook functions. This reduces the size of the entries,
and, more importantly, makes them invariant under runtime relocation
(e.g., for KASLR)

Link: http://lkml.kernel.org/r/20180704083651.24360-6-ard.biesheuvel@linaro.org
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Acked-by: Michael Ellerman <mpe@ellerman.id.au>
Acked-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: James Morris <james.morris@microsoft.com>
Cc: James Morris <jmorris@namei.org>
Cc: Jessica Yu <jeyu@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Nicolas Pitre <nico@linaro.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Russell King <linux@armlinux.org.uk>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Thomas Garnier <thgarnie@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/pci/quirks.c
include/linux/pci.h

index 46f58a9..ef7143a 100644 (file)
@@ -66,9 +66,15 @@ static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f,
                     f->vendor == (u16) PCI_ANY_ID) &&
                    (f->device == dev->device ||
                     f->device == (u16) PCI_ANY_ID)) {
-                       calltime = fixup_debug_start(dev, f->hook);
-                       f->hook(dev);
-                       fixup_debug_report(dev, calltime, f->hook);
+                       void (*hook)(struct pci_dev *dev);
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+                       hook = offset_to_ptr(&f->hook_offset);
+#else
+                       hook = f->hook;
+#endif
+                       calltime = fixup_debug_start(dev, hook);
+                       hook(dev);
+                       fixup_debug_report(dev, calltime, hook);
                }
 }
 
index 9b87f19..e72ca8d 100644 (file)
@@ -1809,7 +1809,11 @@ struct pci_fixup {
        u16 device;                     /* Or PCI_ANY_ID */
        u32 class;                      /* Or PCI_ANY_ID */
        unsigned int class_shift;       /* should be 0, 8, 16 */
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+       int hook_offset;
+#else
        void (*hook)(struct pci_dev *dev);
+#endif
 };
 
 enum pci_fixup_pass {
@@ -1823,12 +1827,28 @@ enum pci_fixup_pass {
        pci_fixup_suspend_late, /* pci_device_suspend_late() */
 };
 
+#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
+#define __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class,  \
+                                   class_shift, hook)                  \
+       __ADDRESSABLE(hook)                                             \
+       asm(".section " #sec ", \"a\"                           \n"     \
+           ".balign    16                                      \n"     \
+           ".short "   #vendor ", " #device "                  \n"     \
+           ".long "    #class ", " #class_shift "              \n"     \
+           ".long "    #hook " - .                             \n"     \
+           ".previous                                          \n");
+#define DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class,    \
+                                 class_shift, hook)                    \
+       __DECLARE_PCI_FIXUP_SECTION(sec, name, vendor, device, class,   \
+                                 class_shift, hook)
+#else
 /* Anonymous variables would be nice... */
 #define DECLARE_PCI_FIXUP_SECTION(section, name, vendor, device, class,        \
                                  class_shift, hook)                    \
        static const struct pci_fixup __PASTE(__pci_fixup_##name,__LINE__) __used       \
        __attribute__((__section__(#section), aligned((sizeof(void *)))))    \
                = { vendor, device, class, class_shift, hook };
+#endif
 
 #define DECLARE_PCI_FIXUP_CLASS_EARLY(vendor, device, class,           \
                                         class_shift, hook)             \