Skip to contentSkip to conversation

Fix r6xx/r7xx cs checker

PATCH Jerome Glisse 2 participants 3 messages Feb 11, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,

Ok never do small change which looks innocuous late at night. There was an double free. Attached is too patch, either one that apply on top of your free or a new big cs checker patch with the fix inside. I let you chose :)

Cheers,
Jerome

Quoted 1 line

From 65e68139ad182df3617ee371af3cc9bea6464cd7 Mon Sep 17 00:00:00 2001

From: Jerome Glisse <[email protected]>
Date: Thu, 11 Feb 2010 12:44:32 +0100
Subject: [PATCH] drm/radeon/kms: fix r600/r700 cs checker to avoid double kfree

radeon_cs kfree the tracker structure but for r6xx/r7xx we want to kfree it inside the parse function because we share it with the UMS code path. Set tracker to NULL after freeing it will avoid double free.

---
 drivers/gpu/drm/radeon/r600_cs.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
drivers/gpu/drm/radeon/r600_cs.c +6 -1
index ac67d64..00e69c5 100644
@@ -846,9 +846,9 @@ static inline int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx
 					"0x%04X\n", reg);
 			return -EINVAL;
 		}
+		tmp = (reg - CB_COLOR0_BASE) / 4;
 		track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx);
 		ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
-		tmp = (reg - CB_COLOR0_BASE) / 4;
 		track->cb_color_base_last[tmp] = ib[idx];
 		track->cb_color_bo[tmp] = reloc->robj;
 		break;
@@ -1324,6 +1324,8 @@ int r600_cs_parse(struct radeon_cs_parser *p)
 	do {
 		r = r600_cs_packet_parse(p, &pkt, p->idx);
 		if (r) {
+			kfree(p->track);
+			p->track = NULL;
 			return r;
 		}
 		p->idx += pkt.count + 2;
@@ -1339,10 +1341,12 @@ int r600_cs_parse(struct radeon_cs_parser *p)
 		default:
 			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
 			kfree(p->track);
+			p->track = NULL;
 			return -EINVAL;
 		}
 		if (r) {
 			kfree(p->track);
+			p->track = NULL;
 			return r;
 		}
 	} while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
@@ -1353,6 +1357,7 @@ int r600_cs_parse(struct radeon_cs_parser *p)
 	}
 #endif
 	kfree(p->track);
+	p->track = NULL;
 	return 0;
 }
 
Signature · 1 line
1.6.6
Quoted 1 line

From 0b217dd315f3cc8b14eb3cf0b1c039aa611565fc Mon Sep 17 00:00:00 2001

From: Jerome Glisse <[email protected]>
Date: Wed, 10 Feb 2010 22:30:05 +0000
Subject: [PATCH] drm/radeon/kms: r600/r700 command stream checker

This patch add cs checker to r600/r700 hw. Command stream checking will rewrite some of the cs value in order to restrict GPU access to BO size. This doesn't break old userspace but just enforce safe value. It should break any things that was using the r600/r700 cs ioctl to do forbidden things (malicious software), though we are not aware of such things.

Here is the list of thing we check :
- enforcing resource size
- enforcing color buffer slice tile max, will restrict cb access
- enforcing db buffer slice tile max, will restrict db access

We don't check for shader bigger than the BO in which they are supposed to be, such use would lead to GPU lockup and is harmless from security POV, as far as we can tell (note that even checking for this wouldn't prevent someone to write bogus shader that lead to lockup).

This patch has received as much testing as humanly possible with old userspace to check that it didn't break such configuration. However not all the applications out there were tested, thus it might broke some odd, rare applications.

[airlied: fix rules for cs checker for parallel builds]

---
 drivers/gpu/drm/radeon/Makefile      |    5 +
 drivers/gpu/drm/radeon/r600.c        |    6 +
 drivers/gpu/drm/radeon/r600_cp.c     |   31 ++
 drivers/gpu/drm/radeon/r600_cs.c     |  831 ++++++++++++++++++++++++++++------
 drivers/gpu/drm/radeon/r600d.h       |  467 +++++++++++++++++++-
 drivers/gpu/drm/radeon/radeon.h      |    6 +
 drivers/gpu/drm/radeon/radeon_drv.h  |    3 +
 drivers/gpu/drm/radeon/reg_srcs/r600 |  837 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/radeon/rv770.c       |    6 +
 9 files changed, 2063 insertions(+), 129 deletions(-)
 create mode 100644 drivers/gpu/drm/radeon/reg_srcs/r600
drivers/gpu/drm/radeon/Makefile +5 -0
index 83c5907..b46f115 100644
@@ -30,6 +30,9 @@ $(obj)/r420_reg_safe.h: $(src)/reg_srcs/r420 $(obj)/mkregtable
 $(obj)/rs600_reg_safe.h: $(src)/reg_srcs/rs600 $(obj)/mkregtable
 	$(call if_changed,mkregtable)
 
+$(obj)/r600_reg_safe.h: $(src)/reg_srcs/r600 $(obj)/mkregtable
+	$(call if_changed,mkregtable)
+
 $(obj)/r100.o: $(obj)/r100_reg_safe.h $(obj)/rn50_reg_safe.h
 
 $(obj)/r200.o: $(obj)/r200_reg_safe.h
@@ -42,6 +45,8 @@ $(obj)/r420.o: $(obj)/r420_reg_safe.h
 
 $(obj)/rs600.o: $(obj)/rs600_reg_safe.h
 
+$(obj)/r600_cs.o: $(obj)/r600_reg_safe.h
+
 radeon-y := radeon_drv.o radeon_cp.o radeon_state.o radeon_mem.o \
 	radeon_irq.o r300_cmdbuf.o r600_cp.o
 # add KMS driver
drivers/gpu/drm/radeon/r600.c +6 -0
index 05769fa..c9723da 100644
@@ -1077,21 +1077,27 @@ void r600_gpu_init(struct radeon_device *rdev)
 	switch (rdev->config.r600.max_tile_pipes) {
 	case 1:
 		tiling_config |= PIPE_TILING(0);
+		rdev->config.r600.tiling_npipes = 1;
 		break;
 	case 2:
 		tiling_config |= PIPE_TILING(1);
+		rdev->config.r600.tiling_npipes = 2;
 		break;
 	case 4:
 		tiling_config |= PIPE_TILING(2);
+		rdev->config.r600.tiling_npipes = 4;
 		break;
 	case 8:
 		tiling_config |= PIPE_TILING(3);
+		rdev->config.r600.tiling_npipes = 8;
 		break;
 	default:
 		break;
 	}
+	rdev->config.r600.tiling_nbanks = 4 << ((ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT);
 	tiling_config |= BANK_TILING((ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT);
 	tiling_config |= GROUP_SIZE(0);
+	rdev->config.r600.tiling_group_size = 256;
 	tmp = (ramcfg & NOOFROWS_MASK) >> NOOFROWS_SHIFT;
 	if (tmp > 3) {
 		tiling_config |= ROW_TILING(3);
drivers/gpu/drm/radeon/r600_cp.c +31 -0
index 6d5a711..d9712a1 100644
@@ -873,6 +873,17 @@ static void r600_gfx_init(struct drm_device *dev,
 	RADEON_WRITE(R600_GB_TILING_CONFIG,      gb_tiling_config);
 	RADEON_WRITE(R600_DCP_TILING_CONFIG,    (gb_tiling_config & 0xffff));
 	RADEON_WRITE(R600_HDP_TILING_CONFIG,    (gb_tiling_config & 0xffff));
+	if (gb_tiling_config & 0xc0) {
+		dev_priv->r600_group_size = 512;
+	} else {
+		dev_priv->r600_group_size = 256;
+	}
+	dev_priv->r600_npipes = 1 << ((gb_tiling_config >> 1) & 0x7);
+	if (gb_tiling_config & 0x30) {
+		dev_priv->r600_nbanks = 8;
+	} else {
+		dev_priv->r600_nbanks = 4;
+	}
 
 	RADEON_WRITE(R600_CC_RB_BACKEND_DISABLE,      cc_rb_backend_disable);
 	RADEON_WRITE(R600_CC_GC_SHADER_PIPE_CONFIG,   cc_gc_shader_pipe_config);
@@ -1444,6 +1455,17 @@ static void r700_gfx_init(struct drm_device *dev,
 	RADEON_WRITE(R600_GB_TILING_CONFIG,      gb_tiling_config);
 	RADEON_WRITE(R600_DCP_TILING_CONFIG,    (gb_tiling_config & 0xffff));
 	RADEON_WRITE(R600_HDP_TILING_CONFIG,    (gb_tiling_config & 0xffff));
+	if (gb_tiling_config & 0xc0) {
+		dev_priv->r600_group_size = 512;
+	} else {
+		dev_priv->r600_group_size = 256;
+	}
+	dev_priv->r600_npipes = 1 << ((gb_tiling_config >> 1) & 0x7);
+	if (gb_tiling_config & 0x30) {
+		dev_priv->r600_nbanks = 8;
+	} else {
+		dev_priv->r600_nbanks = 4;
+	}
 
 	RADEON_WRITE(R600_CC_RB_BACKEND_DISABLE,      cc_rb_backend_disable);
 	RADEON_WRITE(R600_CC_GC_SHADER_PIPE_CONFIG,   cc_gc_shader_pipe_config);
@@ -2526,3 +2548,12 @@ out:
 	mutex_unlock(&dev_priv->cs_mutex);
 	return r;
 }
+
+void r600_cs_legacy_get_tiling_conf(struct drm_device *dev, u32 *npipes, u32 *nbanks, u32 *group_size)
+{
+	struct drm_radeon_private *dev_priv = dev->dev_private;
+
+	*npipes = dev_priv->r600_npipes;
+	*nbanks = dev_priv->r600_nbanks;
+	*group_size = dev_priv->r600_group_size;
+}
drivers/gpu/drm/radeon/r600_cs.c +703 -128

945 lines not on this page · raw message

drivers/gpu/drm/radeon/r600d.h +466 -1

478 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon.h +6 -0
index a7a96a2..9eeca6f 100644
@@ -808,6 +808,9 @@ struct r600_asic {
 	unsigned sx_max_export_pos_size;
 	unsigned sx_max_export_smx_size;
 	unsigned sq_num_cf_insts;
+	unsigned tiling_nbanks;
+	unsigned tiling_npipes;
+	unsigned tiling_group_size;
 };
 
 struct rv770_asic {
@@ -828,6 +831,9 @@ struct rv770_asic {
 	unsigned sc_prim_fifo_size;
 	unsigned sc_hiz_tile_fifo_size;
 	unsigned sc_earlyz_tile_fifo_fize;
+	unsigned tiling_nbanks;
+	unsigned tiling_npipes;
+	unsigned tiling_group_size;
 };
 
 union radeon_asic_config {
drivers/gpu/drm/radeon/radeon_drv.h +3 -0
index e137852..b058316 100644
@@ -294,6 +294,9 @@ typedef struct drm_radeon_private {
 	int r700_sc_prim_fifo_size;
 	int r700_sc_hiz_tile_fifo_size;
 	int r700_sc_earlyz_tile_fifo_fize;
+	int r600_group_size;
+	int r600_npipes;
+	int r600_nbanks;
 
 	struct mutex cs_mutex;
 	u32 cs_id_scnt;
drivers/gpu/drm/radeon/reg_srcs/r600 new file +837 -0

837 lines not on this page · raw message

drivers/gpu/drm/radeon/rv770.c +6 -0
index c9320e7..3ecd138 100644
@@ -516,15 +516,19 @@ static void rv770_gpu_init(struct radeon_device *rdev)
 	switch (rdev->config.rv770.max_tile_pipes) {
 	case 1:
 		gb_tiling_config |= PIPE_TILING(0);
+		rdev->config.rv770.tiling_npipes = 1;
 		break;
 	case 2:
 		gb_tiling_config |= PIPE_TILING(1);
+		rdev->config.rv770.tiling_npipes = 2;
 		break;
 	case 4:
 		gb_tiling_config |= PIPE_TILING(2);
+		rdev->config.rv770.tiling_npipes = 4;
 		break;
 	case 8:
 		gb_tiling_config |= PIPE_TILING(3);
+		rdev->config.rv770.tiling_npipes = 8;
 		break;
 	default:
 		break;
@@ -534,8 +538,10 @@ static void rv770_gpu_init(struct radeon_device *rdev)
 		gb_tiling_config |= BANK_TILING(1);
 	else
 		gb_tiling_config |= BANK_TILING((mc_arb_ramcfg & NOOFBANK_MASK) >> NOOFBANK_SHIFT);
+	rdev->config.rv770.tiling_nbanks = 4 << ((gb_tiling_config >> 4) & 0x3);
 
 	gb_tiling_config |= GROUP_SIZE(0);
+	rdev->config.rv770.tiling_group_size = 256;
 
 	if (((mc_arb_ramcfg & NOOFROWS_MASK) >> NOOFROWS_SHIFT) > 3) {
 		gb_tiling_config |= ROW_TILING(3);
Signature · 1 line
1.6.6

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev

--
_______________________________________________
Dri-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dri-devel

#1 lore raw
Quoted 7 lines · Jerome Glisse

Hi,

Ok never do small change which looks innocuous late at night. There was an double free. Attached is too patch, either one that apply on top of your free or a new big cs checker patch with the fix inside. I let you chose :)

Cubemaps require 8 face alignment on r6xx+

Alex

Quoted 14 lines

Cheers,
Jerome

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
--
_______________________________________________
Dri-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dri-devel

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
--

#2 lore raw
Quoted 11 lines · Alex Deucher
Quoted 7 lines · Jerome Glisse

Hi,

Ok never do small change which looks innocuous late at night. There was an double free. Attached is too patch, either one that apply on top of your free or a new big cs checker patch with the fix inside. I let you chose :)

Cubemaps require 8 face alignment on r6xx+

Also some warnings:
drivers/gpu/drm/radeon/r600_cs.c: In function ‘r600_cs_track_check’:
drivers/gpu/drm/radeon/r600_cs.c:166: warning: ‘bpe’ may be used uninitialized in this function drivers/gpu/drm/radeon/r600_cs.c:166: note: ‘bpe’ was declared here drivers/gpu/drm/radeon/r600_cs.c: In function ‘r600_cs_parse’:
drivers/gpu/drm/radeon/r600_cs.c:938: warning: ‘bpe’ may be used uninitialized in this function drivers/gpu/drm/radeon/r600_cs.c:938: note: ‘bpe’ was declared here

Alex

Quoted 18 lines

Alex

Quoted 14 lines

Cheers,
Jerome

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
--
_______________________________________________
Dri-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dri-devel

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
--