Skip to contentSkip to conversation

drm/radeon: check for allocation failure in radeon_ring_backup()

PATCH In review Dan Carpenter 2 participants 3 messages Jul 20, 2012

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

PATCH 1/1 drm/radeon: check for allocation failure in radeon_ring_backup()

Static checkers complain if this we don't check for allocation failure. Also we can use the new kmalloc_array() function here as a cleanup.

drivers/gpu/drm/radeon/radeon_ring.c +5 -1
index 75cbe46..5a0ef24 100644
@@ -402,7 +402,11 @@ unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring
 	}
 
 	/* and then save the content of the ring */
-	*data = kmalloc(size * 4, GFP_KERNEL);
+	*data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
+	if (!*data) {
+		mutex_unlock(&rdev->ring_lock);
+		return 0;
+	}
 	for (i = 0; i < size; ++i) {
 		(*data)[i] = ring->ring[ptr++];
 		ptr &= ring->ptr_mask;
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Dan Carpenter

Static checkers complain if this we don't check for allocation failure. Also we can use the new kmalloc_array() function here as a cleanup.

What's the benefit of using kmalloc_array instead of just kmalloc?

Anyway it's:

Quoted 9 lines
drivers/gpu/drm/radeon/radeon_ring.c +0 -0
index 75cbe46..5a0ef24 100644
@@ -402,7 +402,11 @@ unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring
  	}
 

drivers/gpu/drm/radeon/radeon_ring.c unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring · line 412

Quoted 10 lines
drivers/gpu/drm/radeon/radeon_ring.c +5 -1
  	/* and then save the content of the ring */
-	*data = kmalloc(size * 4, GFP_KERNEL);
+	*data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
+	if (!*data) {
+		mutex_unlock(&rdev->ring_lock);
+		return 0;
+	}
  	for (i = 0; i < size; ++i) {
  		(*data)[i] = ring->ring[ptr++];
  		ptr &= ring->ptr_mask;

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 7 lines · Christian König
Quoted 4 lines · Dan Carpenter

Static checkers complain if this we don't check for allocation failure. Also we can use the new kmalloc_array() function here as a cleanup.

What's the benefit of using kmalloc_array instead of just kmalloc?

It has built in integer overflow protection. Whenever I see multiplication in kmalloc(), I automatically start to audit for overflows, but now, ha ha, just change it to kmalloc_array() and forget about it. (There weren't any integer overflow problems in the original code, btw).

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html