Blame SOURCES/CVE-2022-0330.patch

23221e
From e314ba42cb4dccd4d9edb2ffcb2295b4c4e2d00d Mon Sep 17 00:00:00 2001
23221e
From: Yannick Cote <ycote@redhat.com>
23221e
Date: Tue, 1 Mar 2022 19:54:52 -0500
23221e
Subject: [KPATCH CVE-2022-0330] drm/i915: kpatch fixes for CVE-2022-0330
23221e
23221e
Kernels:
23221e
4.18.0-348.el8
23221e
4.18.0-348.2.1.el8_5
23221e
4.18.0-348.7.1.el8_5
23221e
4.18.0-348.12.2.el8_5
23221e
23221e
Changes since last build:
23221e
arches: x86_64
23221e
i915_drv.o: changed function: i915_driver_release
23221e
i915_vma.o: changed function: i915_vma_bind
23221e
intel_gt.o: new function: intel_gt_invalidate_tlbs
23221e
intel_gt.o: new function: tlb_invalidate_lock_ctor
23221e
intel_uncore.o: changed function: __intel_uncore_forcewake_put
23221e
intel_uncore.o: changed function: __intel_wait_for_register
23221e
intel_uncore.o: changed function: i915_pmic_bus_access_notifier
23221e
intel_uncore.o: changed function: intel_uncore_forcewake_put
23221e
intel_uncore.o: changed function: intel_uncore_forcewake_put__locked
23221e
intel_uncore.o: changed function: intel_uncore_forcewake_user_put
23221e
intel_uncore.o: new function: intel_uncore_forcewake_put_delayed
23221e
---------------------------
23221e
23221e
Kpatch-MR: https://gitlab.com/redhat/prdsc/rhel/src/kpatch/rhel-8/-/merge_requests/33
23221e
Approved-by: Joe Lawrence (@joe.lawrence)
23221e
Kernels:
23221e
4.18.0-348.el8
23221e
4.18.0-348.2.1.el8_5
23221e
4.18.0-348.7.1.el8_5
23221e
4.18.0-348.12.2.el8_5
23221e
23221e
Modifications:
23221e
- Move new bit definition to .c files avoiding changes to .h files.
23221e
- Redefine tlb_invalidate_lock as a klp shadow variable and avoid
23221e
changes to global structure definition (struct intel_gt).
23221e
23221e
commit 01dfa79afb751b4fec242c7d05ee2e0f78fe9a78
23221e
Author: Patrick Talbert <ptalbert@redhat.com>
23221e
Date:   Mon Jan 31 10:33:24 2022 +0100
23221e
23221e
    drm/i915: Flush TLBs before releasing backing store
23221e
23221e
    Bugzilla: https://bugzilla.redhat.com/2044328
23221e
    CVE: CVE-2022-0330
23221e
    Y-Commit: 5dfb7de610e0b38a03d4d71bdc6cb23a8af0161d
23221e
23221e
    commit 7938d61591d33394a21bdd7797a245b65428f44c
23221e
    Author: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
23221e
    Date:   Tue Oct 19 13:27:10 2021 +0100
23221e
23221e
        drm/i915: Flush TLBs before releasing backing store
23221e
23221e
        We need to flush TLBs before releasing backing store otherwise userspace
23221e
        is able to encounter stale entries if a) it is not declaring GPU access to
23221e
        certain buffers and b) this GPU execution then races with the backing
23221e
        store release getting triggered asynchronously.
23221e
23221e
        Approach taken is to mark any buffer objects which were ever bound to the
23221e
        GPU and triggering a serialized TLB flush when their backing store is
23221e
        released.
23221e
23221e
        Alternatively the flushing could be done on VMA unbind, at which point we
23221e
        would be able to ascertain whether there is potential parallel GPU
23221e
        execution (which could race), but choice essentially boils down to paying
23221e
        the cost of TLB flushes maybe needlessly at VMA unbind time (when the
23221e
        backing store is not known to be definitely going away, so flushing not
23221e
        always required for safety), versus potentially needlessly at backing
23221e
        store relase time since at that point cannot tell whether there is a
23221e
        parallel GPU execution happening.
23221e
23221e
        Therefore simplicity of implementation has been chosen for now, with scope
23221e
        to benchmark and refine later as required.
23221e
23221e
        Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
23221e
        Reported-by: Sushma Venkatesh Reddy <sushma.venkatesh.reddy@intel.com>
23221e
        Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
23221e
        Cc: Jon Bloomfield <jon.bloomfield@intel.com>
23221e
        Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
23221e
        Cc: Jani Nikula <jani.nikula@intel.com>
23221e
        Cc: stable@vger.kernel.org
23221e
23221e
    Signed-off-by: Patrick Talbert <ptalbert@redhat.com>
23221e
23221e
Signed-off-by: Yannick Cote <ycote@redhat.com>
23221e
---
23221e
 drivers/gpu/drm/i915/gem/i915_gem_pages.c |  13 +++
23221e
 drivers/gpu/drm/i915/gt/intel_gt.c        | 130 ++++++++++++++++++++++
23221e
 drivers/gpu/drm/i915/i915_drv.c           |   5 +
23221e
 drivers/gpu/drm/i915/i915_vma.c           |   6 +
23221e
 drivers/gpu/drm/i915/intel_uncore.c       |  26 ++++-
23221e
 5 files changed, 176 insertions(+), 4 deletions(-)
23221e
23221e
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_pages.c b/drivers/gpu/drm/i915/gem/i915_gem_pages.c
23221e
index 76574e245916..ba7fce675ee7 100644
23221e
--- a/drivers/gpu/drm/i915/gem/i915_gem_pages.c
23221e
+++ b/drivers/gpu/drm/i915/gem/i915_gem_pages.c
23221e
@@ -173,6 +173,11 @@ static void unmap_object(struct drm_i915_gem_object *obj, void *ptr)
23221e
 		vunmap(ptr);
23221e
 }
23221e
 
23221e
+/* CVE-2022-0330 - kpatch gathered definitions */
23221e
+#define I915_BO_WAS_BOUND_BIT	4
23221e
+
23221e
+void intel_gt_invalidate_tlbs(struct intel_gt *gt);
23221e
+
23221e
 struct sg_table *
23221e
 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
23221e
 {
23221e
@@ -195,6 +200,14 @@ __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj)
23221e
 	__i915_gem_object_reset_page_iter(obj);
23221e
 	obj->mm.page_sizes.phys = obj->mm.page_sizes.sg = 0;
23221e
 
23221e
+	if (test_and_clear_bit(I915_BO_WAS_BOUND_BIT, &obj->flags)) {
23221e
+		struct drm_i915_private *i915 = to_i915(obj->base.dev);
23221e
+		intel_wakeref_t wakeref;
23221e
+
23221e
+		with_intel_runtime_pm_if_active(&i915->runtime_pm, wakeref)
23221e
+			intel_gt_invalidate_tlbs(&i915->gt);
23221e
+	}
23221e
+
23221e
 	return pages;
23221e
 }
23221e
 
23221e
diff --git a/drivers/gpu/drm/i915/gt/intel_gt.c b/drivers/gpu/drm/i915/gt/intel_gt.c
23221e
index d8e1ab412634..da0b144ea418 100644
23221e
--- a/drivers/gpu/drm/i915/gt/intel_gt.c
23221e
+++ b/drivers/gpu/drm/i915/gt/intel_gt.c
23221e
@@ -662,3 +662,133 @@ void intel_gt_info_print(const struct intel_gt_info *info,
23221e
 
23221e
 	intel_sseu_dump(&info->sseu, p);
23221e
 }
23221e
+
23221e
+struct reg_and_bit {
23221e
+	i915_reg_t reg;
23221e
+	u32 bit;
23221e
+};
23221e
+
23221e
+static struct reg_and_bit
23221e
+get_reg_and_bit(const struct intel_engine_cs *engine, const bool gen8,
23221e
+		const i915_reg_t *regs, const unsigned int num)
23221e
+{
23221e
+	const unsigned int class = engine->class;
23221e
+	struct reg_and_bit rb = { };
23221e
+
23221e
+	if (drm_WARN_ON_ONCE(&engine->i915->drm,
23221e
+			     class >= num || !regs[class].reg))
23221e
+		return rb;
23221e
+
23221e
+	rb.reg = regs[class];
23221e
+	if (gen8 && class == VIDEO_DECODE_CLASS)
23221e
+		rb.reg.reg += 4 * engine->instance; /* GEN8_M2TCR */
23221e
+	else
23221e
+		rb.bit = engine->instance;
23221e
+
23221e
+	rb.bit = BIT(rb.bit);
23221e
+
23221e
+	return rb;
23221e
+}
23221e
+
23221e
+/* CVE-2022-0330 - kpatch gathered definitions */
23221e
+#include <linux/livepatch.h>
23221e
+#define KLP_CVE_2022_0330_MUTEX	0x2022033000000001
23221e
+#define GEN8_RTCR		_MMIO(0x4260)
23221e
+#define GEN8_M1TCR		_MMIO(0x4264)
23221e
+#define GEN8_M2TCR		_MMIO(0x4268)
23221e
+#define GEN8_BTCR		_MMIO(0x426c)
23221e
+#define GEN8_VTCR		_MMIO(0x4270)
23221e
+#define GEN12_GFX_TLB_INV_CR	_MMIO(0xced8)
23221e
+#define GEN12_VD_TLB_INV_CR	_MMIO(0xcedc)
23221e
+#define GEN12_VE_TLB_INV_CR	_MMIO(0xcee0)
23221e
+#define GEN12_BLT_TLB_INV_CR	_MMIO(0xcee4)
23221e
+
23221e
+void intel_uncore_forcewake_put_delayed(struct intel_uncore *uncore,
23221e
+					enum forcewake_domains domains);
23221e
+
23221e
+static int tlb_invalidate_lock_ctor(void *obj, void *shadow_data, void *ctor_data)
23221e
+{
23221e
+	struct mutex *m = shadow_data;
23221e
+	mutex_init(m);
23221e
+
23221e
+	return 0;
23221e
+}
23221e
+
23221e
+void intel_gt_invalidate_tlbs(struct intel_gt *gt)
23221e
+{
23221e
+	static const i915_reg_t gen8_regs[] = {
23221e
+		[RENDER_CLASS]			= GEN8_RTCR,
23221e
+		[VIDEO_DECODE_CLASS]		= GEN8_M1TCR, /* , GEN8_M2TCR */
23221e
+		[VIDEO_ENHANCEMENT_CLASS]	= GEN8_VTCR,
23221e
+		[COPY_ENGINE_CLASS]		= GEN8_BTCR,
23221e
+	};
23221e
+	static const i915_reg_t gen12_regs[] = {
23221e
+		[RENDER_CLASS]			= GEN12_GFX_TLB_INV_CR,
23221e
+		[VIDEO_DECODE_CLASS]		= GEN12_VD_TLB_INV_CR,
23221e
+		[VIDEO_ENHANCEMENT_CLASS]	= GEN12_VE_TLB_INV_CR,
23221e
+		[COPY_ENGINE_CLASS]		= GEN12_BLT_TLB_INV_CR,
23221e
+	};
23221e
+	struct drm_i915_private *i915 = gt->i915;
23221e
+	struct intel_uncore *uncore = gt->uncore;
23221e
+	struct intel_engine_cs *engine;
23221e
+	enum intel_engine_id id;
23221e
+	const i915_reg_t *regs;
23221e
+	unsigned int num = 0;
23221e
+	struct mutex *tlb_invalidate_lock;
23221e
+
23221e
+	if (I915_SELFTEST_ONLY(gt->awake == -ENODEV))
23221e
+		return;
23221e
+
23221e
+	if (INTEL_GEN(i915) == 12) {
23221e
+		regs = gen12_regs;
23221e
+		num = ARRAY_SIZE(gen12_regs);
23221e
+	} else if (INTEL_GEN(i915) >= 8 && INTEL_GEN(i915) <= 11) {
23221e
+		regs = gen8_regs;
23221e
+		num = ARRAY_SIZE(gen8_regs);
23221e
+	} else if (INTEL_GEN(i915) < 8) {
23221e
+		return;
23221e
+	}
23221e
+
23221e
+	if (drm_WARN_ONCE(&i915->drm, !num,
23221e
+			  "Platform does not implement TLB invalidation!"))
23221e
+		return;
23221e
+
23221e
+	GEM_TRACE("\n");
23221e
+
23221e
+	assert_rpm_wakelock_held(&i915->runtime_pm);
23221e
+
23221e
+	tlb_invalidate_lock = klp_shadow_get_or_alloc(i915, KLP_CVE_2022_0330_MUTEX,
23221e
+						      sizeof(*tlb_invalidate_lock), GFP_KERNEL,
23221e
+						      tlb_invalidate_lock_ctor, NULL);
23221e
+	if (tlb_invalidate_lock) {
23221e
+		mutex_lock(tlb_invalidate_lock);
23221e
+		intel_uncore_forcewake_get(uncore, FORCEWAKE_ALL);
23221e
+
23221e
+		for_each_engine(engine, gt, id) {
23221e
+			/*
23221e
+			 * HW architecture suggest typical invalidation time at 40us,
23221e
+			 * with pessimistic cases up to 100us and a recommendation to
23221e
+			 * cap at 1ms. We go a bit higher just in case.
23221e
+			 */
23221e
+			const unsigned int timeout_us = 100;
23221e
+			const unsigned int timeout_ms = 4;
23221e
+			struct reg_and_bit rb;
23221e
+
23221e
+			rb = get_reg_and_bit(engine, regs == gen8_regs, regs, num);
23221e
+			if (!i915_mmio_reg_offset(rb.reg))
23221e
+				continue;
23221e
+
23221e
+			intel_uncore_write_fw(uncore, rb.reg, rb.bit);
23221e
+			if (__intel_wait_for_register_fw(uncore,
23221e
+							 rb.reg, rb.bit, 0,
23221e
+							 timeout_us, timeout_ms,
23221e
+							 NULL))
23221e
+				drm_err_ratelimited(&gt->i915->drm,
23221e
+						    "%s TLB invalidation did not complete in %ums!\n",
23221e
+						    engine->name, timeout_ms);
23221e
+		}
23221e
+
23221e
+		intel_uncore_forcewake_put_delayed(uncore, FORCEWAKE_ALL);
23221e
+		mutex_unlock(tlb_invalidate_lock);
23221e
+	}
23221e
+}
23221e
diff --git a/drivers/gpu/drm/i915/i915_drv.c b/drivers/gpu/drm/i915/i915_drv.c
23221e
index 92668bcbece0..31b298618e7a 100644
23221e
--- a/drivers/gpu/drm/i915/i915_drv.c
23221e
+++ b/drivers/gpu/drm/i915/i915_drv.c
23221e
@@ -957,6 +957,10 @@ void i915_driver_remove(struct drm_i915_private *i915)
23221e
 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
23221e
 }
23221e
 
23221e
+/* CVE-2022-0330 - kpatch gathered definitions */
23221e
+#include <linux/livepatch.h>
23221e
+#define KLP_CVE_2022_0330_MUTEX 0x2022033000000001
23221e
+
23221e
 static void i915_driver_release(struct drm_device *dev)
23221e
 {
23221e
 	struct drm_i915_private *dev_priv = to_i915(dev);
23221e
@@ -979,6 +983,7 @@ static void i915_driver_release(struct drm_device *dev)
23221e
 	intel_runtime_pm_driver_release(rpm);
23221e
 
23221e
 	i915_driver_late_release(dev_priv);
23221e
+	klp_shadow_free(dev_priv, KLP_CVE_2022_0330_MUTEX, NULL);
23221e
 }
23221e
 
23221e
 static int i915_driver_open(struct drm_device *dev, struct drm_file *file)
23221e
diff --git a/drivers/gpu/drm/i915/i915_vma.c b/drivers/gpu/drm/i915/i915_vma.c
23221e
index caa9b041616b..8b2f1c8b2170 100644
23221e
--- a/drivers/gpu/drm/i915/i915_vma.c
23221e
+++ b/drivers/gpu/drm/i915/i915_vma.c
23221e
@@ -362,6 +362,9 @@ int i915_vma_wait_for_bind(struct i915_vma *vma)
23221e
 	return err;
23221e
 }
23221e
 
23221e
+/* CVE-2022-0330 - kpatch gathered definitions */
23221e
+#define I915_BO_WAS_BOUND_BIT	4
23221e
+
23221e
 /**
23221e
  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
23221e
  * @vma: VMA to map
23221e
@@ -439,6 +442,9 @@ int i915_vma_bind(struct i915_vma *vma,
23221e
 		vma->ops->bind_vma(vma->vm, NULL, vma, cache_level, bind_flags);
23221e
 	}
23221e
 
23221e
+	if (vma->obj)
23221e
+		set_bit(I915_BO_WAS_BOUND_BIT, &vma->obj->flags);
23221e
+
23221e
 	atomic_or(bind_flags, &vma->flags);
23221e
 	return 0;
23221e
 }
23221e
diff --git a/drivers/gpu/drm/i915/intel_uncore.c b/drivers/gpu/drm/i915/intel_uncore.c
23221e
index 9ac501bcfdad..9eb5d9e8e5a8 100644
23221e
--- a/drivers/gpu/drm/i915/intel_uncore.c
23221e
+++ b/drivers/gpu/drm/i915/intel_uncore.c
23221e
@@ -694,7 +694,8 @@ void intel_uncore_forcewake_get__locked(struct intel_uncore *uncore,
23221e
 }
23221e
 
23221e
 static void __intel_uncore_forcewake_put(struct intel_uncore *uncore,
23221e
-					 enum forcewake_domains fw_domains)
23221e
+					 enum forcewake_domains fw_domains,
23221e
+					 bool delayed)
23221e
 {
23221e
 	struct intel_uncore_forcewake_domain *domain;
23221e
 	unsigned int tmp;
23221e
@@ -709,7 +710,11 @@ static void __intel_uncore_forcewake_put(struct intel_uncore *uncore,
23221e
 			continue;
23221e
 		}
23221e
 
23221e
-		uncore->funcs.force_wake_put(uncore, domain->mask);
23221e
+		if (delayed &&
23221e
+		    !(domain->uncore->fw_domains_timer & domain->mask))
23221e
+			fw_domain_arm_timer(domain);
23221e
+		else
23221e
+			uncore->funcs.force_wake_put(uncore, domain->mask);
23221e
 	}
23221e
 }
23221e
 
23221e
@@ -730,7 +735,20 @@ void intel_uncore_forcewake_put(struct intel_uncore *uncore,
23221e
 		return;
23221e
 
23221e
 	spin_lock_irqsave(&uncore->lock, irqflags);
23221e
-	__intel_uncore_forcewake_put(uncore, fw_domains);
23221e
+	__intel_uncore_forcewake_put(uncore, fw_domains, false);
23221e
+	spin_unlock_irqrestore(&uncore->lock, irqflags);
23221e
+}
23221e
+
23221e
+void intel_uncore_forcewake_put_delayed(struct intel_uncore *uncore,
23221e
+					enum forcewake_domains fw_domains)
23221e
+{
23221e
+	unsigned long irqflags;
23221e
+
23221e
+	if (!uncore->funcs.force_wake_put)
23221e
+		return;
23221e
+
23221e
+	spin_lock_irqsave(&uncore->lock, irqflags);
23221e
+	__intel_uncore_forcewake_put(uncore, fw_domains, true);
23221e
 	spin_unlock_irqrestore(&uncore->lock, irqflags);
23221e
 }
23221e
 
23221e
@@ -772,7 +790,7 @@ void intel_uncore_forcewake_put__locked(struct intel_uncore *uncore,
23221e
 	if (!uncore->funcs.force_wake_put)
23221e
 		return;
23221e
 
23221e
-	__intel_uncore_forcewake_put(uncore, fw_domains);
23221e
+	__intel_uncore_forcewake_put(uncore, fw_domains, false);
23221e
 }
23221e
 
23221e
 void assert_forcewakes_inactive(struct intel_uncore *uncore)
23221e
-- 
23221e
2.34.1
23221e
23221e