Blame SOURCES/CVE-2022-0330.patch

b2a098
From c2dd834b3e366fff19a868fa446643f7f30201c7 Mon Sep 17 00:00:00 2001
b2a098
From: Yannick Cote <ycote@redhat.com>
b2a098
Date: Tue, 8 Feb 2022 17:10:45 -0500
b2a098
Subject: [KPATCH CVE-2022-0330] drm/i915: kpatch fixes for CVE-2022-0330
b2a098
b2a098
Kernels:
b2a098
3.10.0-1160.21.1.el7
b2a098
3.10.0-1160.24.1.el7
b2a098
3.10.0-1160.25.1.el7
b2a098
3.10.0-1160.31.1.el7
b2a098
3.10.0-1160.36.2.el7
b2a098
3.10.0-1160.41.1.el7
b2a098
3.10.0-1160.42.2.el7
b2a098
3.10.0-1160.45.1.el7
b2a098
3.10.0-1160.49.1.el7
b2a098
3.10.0-1160.53.1.el7
b2a098
b2a098
Changes since last build:
b2a098
arches: x86_64
b2a098
i915_drv.o: changed function: i915_driver_destroy
b2a098
i915_gem.o: changed function: __i915_gem_object_unset_pages
b2a098
i915_gem.o: changed function: i915_gem_fault
b2a098
i915_gem.o: new function: assert_rpm_wakelock_held.part.56
b2a098
i915_gem.o: new function: tlb_invalidate_lock_ctor
b2a098
i915_vma.o: changed function: i915_vma_bind
b2a098
---------------------------
b2a098
b2a098
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-7/-/merge_requests/24
b2a098
Kernels:
b2a098
3.10.0-1160.21.1.el7
b2a098
3.10.0-1160.24.1.el7
b2a098
3.10.0-1160.25.1.el7
b2a098
3.10.0-1160.31.1.el7
b2a098
3.10.0-1160.36.2.el7
b2a098
3.10.0-1160.41.1.el7
b2a098
3.10.0-1160.42.2.el7
b2a098
3.10.0-1160.45.1.el7
b2a098
3.10.0-1160.49.1.el7
b2a098
3.10.0-1160.53.1.el7
b2a098
b2a098
Modifications:
b2a098
- Move new bit definition to .c files avoiding changes to .h files.
b2a098
- Redefine tlb_invalidate_lock as a klp shadow variable and avoid
b2a098
changes to global structure definition (struct drm_i915_private).
b2a098
b2a098
commit c96aee1f92b3a81d8a36efd91cfc5ff33ca3ac80
b2a098
Author: Dave Airlie <airlied@redhat.com>
b2a098
Date:   Tue Jan 25 18:19:06 2022 -0500
b2a098
b2a098
    drm/i915: Flush TLBs before releasing backing store
b2a098
b2a098
    Bugzilla: http://bugzilla.redhat.com/2044319
b2a098
    CVE: CVE-2022-0330
b2a098
b2a098
    commit 7938d61591d33394a21bdd7797a245b65428f44c
b2a098
    Author: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
b2a098
    Date:   Tue Oct 19 13:27:10 2021 +0100
b2a098
b2a098
        drm/i915: Flush TLBs before releasing backing store
b2a098
b2a098
        We need to flush TLBs before releasing backing store otherwise userspace
b2a098
        is able to encounter stale entries if a) it is not declaring access to
b2a098
        certain buffers and b) it races with the backing store release from a
b2a098
        such undeclared execution already executing on the GPU in parallel.
b2a098
b2a098
        The approach taken is to mark any buffer objects which were ever bound
b2a098
        to the GPU and to trigger a serialized TLB flush when their backing
b2a098
        store is released.
b2a098
b2a098
        Alternatively the flushing could be done on VMA unbind, at which point
b2a098
        we would be able to ascertain whether there is potential a parallel GPU
b2a098
        execution (which could race), but essentially it boils down to paying
b2a098
        the cost of TLB flushes potentially needlessly at VMA unbind time (when
b2a098
        the backing store is not known to be going away so not needed for
b2a098
        safety), versus potentially needlessly at backing store relase time
b2a098
        (since we at that point cannot tell whether there is anything executing
b2a098
        on the GPU which uses that object).
b2a098
b2a098
        Thereforce simplicity of implementation has been chosen for now with
b2a098
        scope to benchmark and refine later as required.
b2a098
b2a098
        Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
b2a098
        Reported-by: Sushma Venkatesh Reddy <sushma.venkatesh.reddy@intel.com>
b2a098
        Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
b2a098
        Acked-by: Dave Airlie <airlied@redhat.com>
b2a098
        Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
b2a098
        Cc: Jon Bloomfield <jon.bloomfield@intel.com>
b2a098
        Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
b2a098
        Cc: Jani Nikula <jani.nikula@intel.com>
b2a098
        Cc: stable@vger.kernel.org
b2a098
        Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
b2a098
b2a098
    Signed-off-by: Dave Airlie <airlied@redhat.com>
b2a098
b2a098
Signed-off-by: Yannick Cote <ycote@redhat.com>
b2a098
---
b2a098
 drivers/gpu/drm/i915/i915_drv.c |   4 ++
b2a098
 drivers/gpu/drm/i915/i915_gem.c | 104 ++++++++++++++++++++++++++++++++
b2a098
 drivers/gpu/drm/i915/i915_vma.c |   6 ++
b2a098
 3 files changed, 114 insertions(+)
b2a098
b2a098
diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
b2a098
index db8a0e6d2f2f..9c12def30f4b 100644
b2a098
--- a/drivers/gpu/drm/i915/i915_drv.c
b2a098
+++ b/drivers/gpu/drm/i915/i915_drv.c
b2a098
@@ -1683,11 +1683,15 @@ i915_driver_create(struct pci_dev *pdev, const struct pci_device_id *ent)
b2a098
 	return i915;
b2a098
 }
b2a098
 
b2a098
+#include <linux/livepatch.h>
b2a098
+#define KLP_CVE_2022_0330_MUTEX 0x2022033000000001
b2a098
+
b2a098
 static void i915_driver_destroy(struct drm_i915_private *i915)
b2a098
 {
b2a098
 	struct pci_dev *pdev = i915->drm.pdev;
b2a098
 
b2a098
 	drm_dev_fini(&i915->drm);
b2a098
+	klp_shadow_free(i915, KLP_CVE_2022_0330_MUTEX, NULL);
b2a098
 	kfree(i915);
b2a098
 
b2a098
 	/* And make sure we never chase our dangling pointer from pci_dev */
b2a098
diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c
b2a098
index c96ccd9001bf..b882a08b32f9 100644
b2a098
--- a/drivers/gpu/drm/i915/i915_gem.c
b2a098
+++ b/drivers/gpu/drm/i915/i915_gem.c
b2a098
@@ -2464,6 +2464,101 @@ static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object *obj)
b2a098
 	rcu_read_unlock();
b2a098
 }
b2a098
 
b2a098
+struct reg_and_bit {
b2a098
+	i915_reg_t reg;
b2a098
+	u32 bit;
b2a098
+};
b2a098
+
b2a098
+static struct reg_and_bit
b2a098
+get_reg_and_bit(const struct intel_engine_cs *engine,
b2a098
+		const i915_reg_t *regs, const unsigned int num)
b2a098
+{
b2a098
+	const unsigned int class = engine->class;
b2a098
+	struct reg_and_bit rb = { .bit = 1 };
b2a098
+
b2a098
+	if (WARN_ON_ONCE(class >= num || !regs[class].reg))
b2a098
+		return rb;
b2a098
+
b2a098
+	rb.reg = regs[class];
b2a098
+	if (class == VIDEO_DECODE_CLASS)
b2a098
+		rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
b2a098
+
b2a098
+	return rb;
b2a098
+}
b2a098
+
b2a098
+#include <linux/livepatch.h>
b2a098
+#define KLP_CVE_2022_0330_MUTEX 0x2022033000000001
b2a098
+#define I915_BO_WAS_BOUND_BIT   1
b2a098
+#define GEN8_RTCR               _MMIO(0x4260)
b2a098
+#define GEN8_M1TCR              _MMIO(0x4264)
b2a098
+#define GEN8_M2TCR              _MMIO(0x4268)
b2a098
+#define GEN8_BTCR               _MMIO(0x426c)
b2a098
+#define GEN8_VTCR               _MMIO(0x4270)
b2a098
+
b2a098
+static int tlb_invalidate_lock_ctor(void *obj, void *shadow_data, void *ctor_data)
b2a098
+{
b2a098
+	struct mutex *m = shadow_data;
b2a098
+	mutex_init(m);
b2a098
+
b2a098
+	return 0;
b2a098
+}
b2a098
+
b2a098
+static void invalidate_tlbs(struct drm_i915_private *dev_priv)
b2a098
+{
b2a098
+	static const i915_reg_t gen8_regs[] = {
b2a098
+		[RENDER_CLASS]                  = GEN8_RTCR,
b2a098
+		[VIDEO_DECODE_CLASS]            = GEN8_M1TCR, /* , GEN8_M2TCR */
b2a098
+		[VIDEO_ENHANCEMENT_CLASS]       = GEN8_VTCR,
b2a098
+		[COPY_ENGINE_CLASS]             = GEN8_BTCR,
b2a098
+	};
b2a098
+	const unsigned int num = ARRAY_SIZE(gen8_regs);
b2a098
+	const i915_reg_t *regs = gen8_regs;
b2a098
+	struct intel_engine_cs *engine;
b2a098
+	enum intel_engine_id id;
b2a098
+	struct mutex *tlb_invalidate_lock;
b2a098
+
b2a098
+	if (INTEL_GEN(dev_priv) < 8)
b2a098
+		return;
b2a098
+
b2a098
+	GEM_TRACE("\n");
b2a098
+
b2a098
+	assert_rpm_wakelock_held(dev_priv);
b2a098
+
b2a098
+	tlb_invalidate_lock = klp_shadow_get_or_alloc(dev_priv, KLP_CVE_2022_0330_MUTEX,
b2a098
+						      sizeof(*tlb_invalidate_lock), GFP_KERNEL,
b2a098
+						      tlb_invalidate_lock_ctor, NULL);
b2a098
+	if (tlb_invalidate_lock) {
b2a098
+		mutex_lock(tlb_invalidate_lock);
b2a098
+		intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
b2a098
+
b2a098
+		for_each_engine(engine, dev_priv, id) {
b2a098
+			/*
b2a098
+			 * HW architecture suggest typical invalidation time at 40us,
b2a098
+			 * with pessimistic cases up to 100us and a recommendation to
b2a098
+			 * cap at 1ms. We go a bit higher just in case.
b2a098
+			 */
b2a098
+			const unsigned int timeout_us = 100;
b2a098
+			const unsigned int timeout_ms = 4;
b2a098
+			struct reg_and_bit rb;
b2a098
+
b2a098
+			rb = get_reg_and_bit(engine, regs, num);
b2a098
+			if (!i915_mmio_reg_offset(rb.reg))
b2a098
+				continue;
b2a098
+
b2a098
+			I915_WRITE_FW(rb.reg, rb.bit);
b2a098
+			if (__intel_wait_for_register_fw(dev_priv,
b2a098
+							 rb.reg, rb.bit, 0,
b2a098
+							 timeout_us, timeout_ms,
b2a098
+							 NULL))
b2a098
+				DRM_ERROR_RATELIMITED("%s TLB invalidation did not complete in %ums!\n",
b2a098
+						      engine->name, timeout_ms);
b2a098
+		}
b2a098
+
b2a098
+		intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b2a098
+		mutex_unlock(tlb_invalidate_lock);
b2a098
+	}
b2a098
+}
b2a098
+
b2a098
 static struct sg_table *
b2a098
 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
b2a098
 {
b2a098
@@ -2493,6 +2588,15 @@ __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
b2a098
 	__i915_gem_object_reset_page_iter(obj);
b2a098
 	obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
b2a098
 
b2a098
+	if (test_and_clear_bit(I915_BO_WAS_BOUND_BIT, &obj->flags)) {
b2a098
+		struct drm_i915_private *i915 = to_i915(obj->base.dev);
b2a098
+
b2a098
+		if (intel_runtime_pm_get_if_in_use(i915)) {
b2a098
+			invalidate_tlbs(i915);
b2a098
+			intel_runtime_pm_put(i915);
b2a098
+		}
b2a098
+	}
b2a098
+
b2a098
 	return pages;
b2a098
 }
b2a098
 
b2a098
diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
b2a098
index 5b4d78cdb4ca..906e6321ad77 100644
b2a098
--- a/drivers/gpu/drm/i915/i915_vma.c
b2a098
+++ b/drivers/gpu/drm/i915/i915_vma.c
b2a098
@@ -285,6 +285,8 @@ i915_vma_instance(struct drm_i915_gem_object *obj,
b2a098
 	return vma;
b2a098
 }
b2a098
 
b2a098
+#define I915_BO_WAS_BOUND_BIT    1
b2a098
+
b2a098
 /**
b2a098
  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
b2a098
  * @vma: VMA to map
b2a098
@@ -335,6 +337,10 @@ int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
b2a098
 		return ret;
b2a098
 
b2a098
 	vma->flags |= bind_flags;
b2a098
+
b2a098
+	if (vma->obj)
b2a098
+		set_bit(I915_BO_WAS_BOUND_BIT, &vma->obj->flags);
b2a098
+
b2a098
 	return 0;
b2a098
 }
b2a098
 
b2a098
-- 
b2a098
2.26.3
b2a098
b2a098