Skip to contentSkip to conversation

tracing: Allocate mask_str buffer dynamically

PATCH RESEND Superseded [email protected] 2 participants 3 messages Nov 29, 2017 – Nov 30, 2017 Tracing

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v3 1/1 tracing: Allocate mask_str buffer dynamically

From: Changbin Du <[email protected]>

The default NR_CPUS can be very large, but actual possible nr_cpu_ids usually is very small. For my x86 distribution, the NR_CPUS is 8192 and nr_cpu_ids is 4. About 2 pages are wasted.

Most machines don't have so many CPUs, so define a array with NR_CPUS just wastes memory. So let's allocate the buffer dynamically when need.

The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

Example output:
ff,ffffffff

With this change, the mutext tracing_cpumask_update_lock also can be removed now, which was used to protect mask_str.

Cc Steven Rostedt <[email protected]>

---
v3:
- remove tracing_cpumask_update_lock which was used to protect mask_str. (Rostedt)
v2:
- remove 'static' declaration.
- fix buffer size.

---
 kernel/trace/trace.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
kernel/trace/trace.c +9 -20
index 73e67b6..6750d05 100644
@@ -4178,37 +4178,30 @@ static const struct file_operations show_traces_fops = {
 	.llseek		= seq_lseek,
 };
 
-/*
- * The tracer itself will not take this lock, but still we want
- * to provide a consistent cpumask to user-space:
- */
-static DEFINE_MUTEX(tracing_cpumask_update_lock);
-
-/*
- * Temporary storage for the character representation of the
- * CPU bitmask (and one more byte for the newline):
- */
-static char mask_str[NR_CPUS + 1];
-
 static ssize_t
 tracing_cpumask_read(struct file *filp, char __user *ubuf,
 		     size_t count, loff_t *ppos)
 {
 	struct trace_array *tr = file_inode(filp)->i_private;
+	char *mask_str;
 	int len;
 
-	mutex_lock(&tracing_cpumask_update_lock);
+	/* Bitmap, ',' and two more bytes for the newline and '\0'. */
+	len = DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;
+	mask_str = kmalloc(len, GFP_KERNEL);
+	if (!mask_str)
+		return -ENOMEM;
 
-	len = snprintf(mask_str, count, "%*pb\n",
+	len = snprintf(mask_str, len, "%*pb\n",
 		       cpumask_pr_args(tr->tracing_cpumask));
 	if (len >= count) {
 		count = -EINVAL;
 		goto out_err;
 	}
-	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
+	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
 
 out_err:
-	mutex_unlock(&tracing_cpumask_update_lock);
+	kfree(mask_str);
 
 	return count;
 }
@@ -4228,8 +4221,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	if (err)
 		goto err_unlock;
 
-	mutex_lock(&tracing_cpumask_update_lock);
-
 	local_irq_disable();
 	arch_spin_lock(&tr->max_lock);
 	for_each_tracing_cpu(cpu) {
@@ -4252,8 +4243,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	local_irq_enable();
 
 	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
-
-	mutex_unlock(&tracing_cpumask_update_lock);
 	free_cpumask_var(tracing_cpumask_new);
 
 	return count;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 6 lines · On Wed, 29 Nov 2017 12:42:45 +0800 [email protected] wrote:

From: Changbin Du <[email protected]>

The default NR_CPUS can be very large, but actual possible nr_cpu_ids usually is very small. For my x86 distribution, the NR_CPUS is 8192 and nr_cpu_ids is 4. About 2 pages are wasted.

Quoted 8 lines

Most machines don't have so many CPUs, so define a array with NR_CPUS just wastes memory. So let's allocate the buffer dynamically when need.

The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

Example output:
ff,ffffffff

Um, what if there's more than 64 CPUs, where I have booted several boxes that have more. There's going to be more than 1 comma.

Quoted 38 lines

With this change, the mutext tracing_cpumask_update_lock also can be removed now, which was used to protect mask_str.

Cc Steven Rostedt <[email protected]>

---
v3:
- remove tracing_cpumask_update_lock which was used to protect mask_str. (Rostedt)
v2:
- remove 'static' declaration.
- fix buffer size.

---
 kernel/trace/trace.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
kernel/trace/trace.c +0 -12
index 73e67b6..6750d05 100644
@@ -4178,37 +4178,30 @@ static const struct file_operations show_traces_fops = {
 	.llseek		= seq_lseek,
 };
 
-/*
- * The tracer itself will not take this lock, but still we want
- * to provide a consistent cpumask to user-space:
- */
-static DEFINE_MUTEX(tracing_cpumask_update_lock);
-
-/*
- * Temporary storage for the character representation of the
- * CPU bitmask (and one more byte for the newline):
- */
-static char mask_str[NR_CPUS + 1];
-
 static ssize_t

kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4190

Quoted 10 lines
kernel/trace/trace.c +3 -1
 tracing_cpumask_read(struct file *filp, char __user *ubuf,
 		     size_t count, loff_t *ppos)
 {
 	struct trace_array *tr = file_inode(filp)->i_private;
+	char *mask_str;
 	int len;
 
-	mutex_lock(&tracing_cpumask_update_lock);
+	/* Bitmap, ',' and two more bytes for the newline and '\0'. */
+	len = DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

This is broken. Instead do:

	len = snprintf(NULL, 0, "%*pb\n",
		       cpumask_pr_args(tr->tracing_cpumask)) + 1;

	mask_str = kmalloc(len, GFP_KERNEL);
	[..]
	len = snprintf(mask_str, len, "%*pb\n",
		       cpumask_pr_args(tr->tracing_cpumask));

-- Steve

Quoted 38 lines
kernel/trace/trace.c +6 -7
+	mask_str = kmalloc(len, GFP_KERNEL);
+	if (!mask_str)
+		return -ENOMEM;
 
-	len = snprintf(mask_str, count, "%*pb\n",
+	len = snprintf(mask_str, len, "%*pb\n",
 		       cpumask_pr_args(tr->tracing_cpumask));
 	if (len >= count) {
 		count = -EINVAL;
 		goto out_err;
 	}
-	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
+	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
 
 out_err:
-	mutex_unlock(&tracing_cpumask_update_lock);
+	kfree(mask_str);
 
 	return count;
 }
@@ -4228,8 +4221,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	if (err)
 		goto err_unlock;
 
-	mutex_lock(&tracing_cpumask_update_lock);
-
 	local_irq_disable();
 	arch_spin_lock(&tr->max_lock);
 	for_each_tracing_cpu(cpu) {
@@ -4252,8 +4243,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	local_irq_enable();
 
 	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
-
-	mutex_unlock(&tracing_cpumask_update_lock);
 	free_cpumask_var(tracing_cpumask_new);
 
 	return count;
#2 lore raw
Quoted 12 lines · Steven Rostedt
Quoted 9 lines · On Wed, 29 Nov 2017 12:42:45 +0800 [email protected] wrote:

From: Changbin Du <[email protected]>

The default NR_CPUS can be very large, but actual possible nr_cpu_ids usually is very small. For my x86 distribution, the NR_CPUS is 8192 and nr_cpu_ids is 4. About 2 pages are wasted.

Most machines don't have so many CPUs, so define a array with NR_CPUS just wastes memory. So let's allocate the buffer dynamically when need.

Quoted 9 lines
Quoted 5 lines

The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

Example output:
ff,ffffffff

Um, what if there's more than 64 CPUs, where I have booted several boxes that have more. There's going to be more than 1 comma.

The commas are calculated by formula. (DIV_ROUND_UP(nr_cpu_ids, 4))

Quoted 59 lines
Quoted 38 lines

With this change, the mutext tracing_cpumask_update_lock also can be removed now, which was used to protect mask_str.

Cc Steven Rostedt <[email protected]>

---
v3:
- remove tracing_cpumask_update_lock which was used to protect mask_str. (Rostedt)
v2:
- remove 'static' declaration.
- fix buffer size.

---
 kernel/trace/trace.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
kernel/trace/trace.c +0 -12
index 73e67b6..6750d05 100644
@@ -4178,37 +4178,30 @@ static const struct file_operations show_traces_fops = {
 	.llseek		= seq_lseek,
 };
 
-/*
- * The tracer itself will not take this lock, but still we want
- * to provide a consistent cpumask to user-space:
- */
-static DEFINE_MUTEX(tracing_cpumask_update_lock);
-
-/*
- * Temporary storage for the character representation of the
- * CPU bitmask (and one more byte for the newline):
- */
-static char mask_str[NR_CPUS + 1];
-
 static ssize_t

kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4190

Quoted 10 lines
kernel/trace/trace.c +3 -1
 tracing_cpumask_read(struct file *filp, char __user *ubuf,
 		     size_t count, loff_t *ppos)
 {
 	struct trace_array *tr = file_inode(filp)->i_private;
+	char *mask_str;
 	int len;
 
-	mutex_lock(&tracing_cpumask_update_lock);
+	/* Bitmap, ',' and two more bytes for the newline and '\0'. */
+	len = DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

This is broken. Instead do:

	len = snprintf(NULL, 0, "%*pb\n",
		       cpumask_pr_args(tr->tracing_cpumask)) + 1;

	mask_str = kmalloc(len, GFP_KERNEL);
	[..]
	len = snprintf(mask_str, len, "%*pb\n",
		       cpumask_pr_args(tr->tracing_cpumask));
Quoted 2 lines

-- Steve

hmm. I never know that snprintf has such usage. This is much better than calculating it by a formula.

Quoted 39 lines
Quoted 38 lines
kernel/trace/trace.c +6 -7
+	mask_str = kmalloc(len, GFP_KERNEL);
+	if (!mask_str)
+		return -ENOMEM;
 
-	len = snprintf(mask_str, count, "%*pb\n",
+	len = snprintf(mask_str, len, "%*pb\n",
 		       cpumask_pr_args(tr->tracing_cpumask));
 	if (len >= count) {
 		count = -EINVAL;
 		goto out_err;
 	}
-	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
+	count = simple_read_from_buffer(ubuf, count, ppos, mask_str, len);
 
 out_err:
-	mutex_unlock(&tracing_cpumask_update_lock);
+	kfree(mask_str);
 
 	return count;
 }
@@ -4228,8 +4221,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	if (err)
 		goto err_unlock;
 
-	mutex_lock(&tracing_cpumask_update_lock);
-
 	local_irq_disable();
 	arch_spin_lock(&tr->max_lock);
 	for_each_tracing_cpu(cpu) {
@@ -4252,8 +4243,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	local_irq_enable();
 
 	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
-
-	mutex_unlock(&tracing_cpumask_update_lock);
 	free_cpumask_var(tracing_cpumask_new);
 
 	return count;
Signature · 2 lines
Thanks,
Changbin Du