Skip to contentSkip to conversation

drm/imagination: simplify pvr_fw_trace_seq_data

PATCH Applied Rosen Penev 3 participants 3 messages Oct 5 – Oct 28

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

PATCH v2 1/1 drm/imagination: simplify pvr_fw_trace_seq_data

The trace buffer copy always has ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS entries, so embed it in struct pvr_fw_trace_seq_data as a fixed-size array instead of allocating it separately. This removes one allocation and its error handling, and lets FORTIFY_SOURCE and UBSAN_BOUNDS see the real size of the buffer.

At about 48KB, the struct needs an order-4 page allocation, which can fail under memory fragmentation. Allocate it with kvzalloc_obj() so it falls back to vmalloc in that case, and free it with kvfree().

Assisted-by LLM

---
v2: use kvzalloc_obj()

 drivers/gpu/drm/imagination/pvr_fw_trace.c | 25 ++++++----------------
 1 file changed, 7 insertions(+), 18 deletions(-)
drivers/gpu/drm/imagination/pvr_fw_trace.c +7 -18
index 6fe478f64f02..27bbc62114b1 100644
@@ -240,9 +240,6 @@ update_logtype(struct pvr_device *pvr_dev, u32 group_mask)
 }
 
 struct pvr_fw_trace_seq_data {
-	/** @buffer: Pointer to copy of trace data. */
-	u32 *buffer;
-
 	/** @start_offset: Starting offset in trace data, as reported by FW. */
 	u32 start_offset;
 
@@ -251,6 +248,9 @@ struct pvr_fw_trace_seq_data {
 
 	/** @assert_buf: Trace assert buffer, as reported by FW. */
 	struct rogue_fwif_file_info_buf assert_buf;
+
+	/** @buffer: Copy of trace data. */
+	u32 buffer[ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS];
 };
 
 static u32 find_sfid(u32 id)
@@ -454,17 +454,10 @@ static int fw_trace_open(struct inode *inode, struct file *file)
 	struct pvr_fw_trace_seq_data *trace_seq_data;
 	int err;
 
-	trace_seq_data = kzalloc_obj(*trace_seq_data);
+	trace_seq_data = kvzalloc_obj(*trace_seq_data);
 	if (!trace_seq_data)
 		return -ENOMEM;
 
-	trace_seq_data->buffer = kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,
-					 sizeof(*trace_seq_data->buffer), GFP_KERNEL);
-	if (!trace_seq_data->buffer) {
-		err = -ENOMEM;
-		goto err_free_data;
-	}
-
 	/*
 	 * Take a local copy of the trace buffer, as firmware may still be
 	 * writing to it. This will exist as long as this file is open.
@@ -477,17 +470,14 @@ static int fw_trace_open(struct inode *inode, struct file *file)
 
 	err = seq_open(file, &pvr_fw_trace_seq_ops);
 	if (err)
-		goto err_free_buffer;
+		goto err_free_data;
 
 	((struct seq_file *)file->private_data)->private = trace_seq_data;
 
 	return 0;
 
-err_free_buffer:
-	kfree(trace_seq_data->buffer);
-
 err_free_data:
-	kfree(trace_seq_data);
+	kvfree(trace_seq_data);
 
 	return err;
 }
@@ -498,8 +488,7 @@ static int fw_trace_release(struct inode *inode, struct file *file)
 		((struct seq_file *)file->private_data)->private;
 
 	seq_release(inode, file);
-	kfree(trace_seq_data->buffer);
-	kfree(trace_seq_data);
+	kvfree(trace_seq_data);
 
 	return 0;
 }
Signature · 1 line
2.56.0
Reviewed-by on v2 1/1
#1 lore raw

Hi Rosen,

Quoted 9 lines · Rosen Penev

The trace buffer copy always has ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS
entries, so embed it in struct pvr_fw_trace_seq_data as a fixed-size array instead of allocating it separately. This removes one allocation
and its error handling, and lets FORTIFY_SOURCE and UBSAN_BOUNDS see the
real size of the buffer.

Quoted 7 lines

At about 48KB, the struct needs an order-4 page allocation, which can fail under memory fragmentation. Allocate it with kvzalloc_obj() so it
falls back to vmalloc in that case, and free it with kvfree().

Assisted-by LLM

Thanks,
Alexandru Dadu

Quoted 90 lines

---
 v2: use kvzalloc_obj() drivers/gpu/drm/imagination/pvr_fw_trace.c | 25 ++++++-------------- --

 1 file changed, 7 insertions(+), 18 deletions(-)
drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -0
index 6fe478f64f02..27bbc62114b1 100644
@@ -240,9 +240,6 @@ update_logtype(struct pvr_device *pvr_dev, u32
 group_mask)
  }


 struct pvr_fw_trace_seq_data {

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -3
-	/** @buffer: Pointer to copy of trace data. */
-	u32 *buffer;
-

  /** @start_offset: Starting offset in trace data, as reported by FW. */
  u32 start_offset;

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -0
@@ -251,6 +248,9 @@ struct pvr_fw_trace_seq_data {


  /** @assert_buf: Trace assert buffer, as reported by FW. */ struct rogue_fwif_file_info_buf assert_buf;

drivers/gpu/drm/imagination/pvr_fw_trace.c +3 -0
+
+	/** @buffer: Copy of trace data. */
+	u32 buffer[ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS];

 };

 static u32 find_sfid(u32 id)

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -0
@@ -454,17 +454,10 @@ static int fw_trace_open(struct inode *inode,
 struct file *file)
  	struct pvr_fw_trace_seq_data *trace_seq_data;
  	int err;

drivers/gpu/drm/imagination/pvr_fw_trace.c +1 -1
-	trace_seq_data = kzalloc_obj(*trace_seq_data);
+	trace_seq_data = kvzalloc_obj(*trace_seq_data);

  if (!trace_seq_data)
  return -ENOMEM;

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -1
-	trace_seq_data->buffer =

kcalloc(ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS,

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -1
-					 sizeof(*trace_seq_data-
Quoted 1 line

buffer), GFP_KERNEL);

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -5
-	if (!trace_seq_data->buffer) {
-		err = -ENOMEM;
-		goto err_free_data;
-	}
-

  /*
  * Take a local copy of the trace buffer, as firmware may still be
  * writing to it. This will exist as long as this file is open.

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -0
@@ -477,17 +470,14 @@ static int fw_trace_open(struct inode *inode,
 struct file *file)


  err = seq_open(file, &pvr_fw_trace_seq_ops); if (err)

drivers/gpu/drm/imagination/pvr_fw_trace.c +1 -1
-		goto err_free_buffer;
+		goto err_free_data;


  ((struct seq_file *)file->private_data)->private = trace_seq_data;

  return 0;

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -3
-err_free_buffer:
-	kfree(trace_seq_data->buffer);
-

 err_free_data:

drivers/gpu/drm/imagination/pvr_fw_trace.c +1 -1
-	kfree(trace_seq_data);
+	kvfree(trace_seq_data);


  return err;
 }

drivers/gpu/drm/imagination/pvr_fw_trace.c +0 -0
@@ -498,8 +488,7 @@ static int fw_trace_release(struct inode *inode,
 struct file *file)
  		((struct seq_file *)file->private_data)->private;


  seq_release(inode, file);

drivers/gpu/drm/imagination/pvr_fw_trace.c +1 -2
-	kfree(trace_seq_data->buffer);
-	kfree(trace_seq_data);
+	kvfree(trace_seq_data);


  return 0;
 }

maintainer Applied
#2 lore raw
In reply to Rosen Penev
Quoted 11 lines · Rosen Penev

The trace buffer copy always has ROGUE_FW_TRACE_BUF_DEFAULT_SIZE_IN_DWORDS entries, so embed it in struct pvr_fw_trace_seq_data as a fixed-size array instead of allocating it separately. This removes one allocation and its error handling, and lets FORTIFY_SOURCE and UBSAN_BOUNDS see the real size of the buffer.

At about 48KB, the struct needs an order-4 page allocation, which can fail under memory fragmentation. Allocate it with kvzalloc_obj() so it falls back to vmalloc in that case, and free it with kvfree().

[...]

Applied to drm-misc-next, thanks!

[1/1] drm/imagination: simplify pvr_fw_trace_seq_data

      commit: feaa32dc98090b2f45f7b6f179711a6640169c1a

Best regards,

Signature · 1 line
Alessio Belle <[email protected]>