Blame SOURCES/CVE-2022-0330.patch

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