diff mbox series

[1/3] tracing: Remove dependency of saved_cmdlines_buffer on PID_MAX_DEFAULT

Message ID 20240408145819.8787-2-mkoutny@suse.com (mailing list archive)
State New
Headers show
Series kernel/pid: Remove default pid_max value | expand

Commit Message

Michal Koutný April 8, 2024, 2:58 p.m. UTC
Calculations into map_pid_to_cmdline use PID_MAX_DEFAULT but they
actually depend on the size of map_pid_to_cmdline. The size of the map
may be arbitrary. First, refer to the map size where necessary, second,
pick a good value for the size of the map.
Since the buffer is allocated at boot (i.e. user cannot affect its size
later), accounting for full PID_MAX_LIMIT would inflate map's size
unnecessarily (4*4M) for all users. Stick to the original value of
4*32k, the commit 785e3c0a3a87 ("tracing: Map all PIDs to command
lines") explains why it still works for higher pids.

The point of this exercise is to remove dependency on PID_MAX_DEFAULT.

Signed-off-by: Michal Koutný <mkoutny@suse.com>
---
 kernel/trace/trace_sched_switch.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

Comments

Steven Rostedt April 9, 2024, 3:01 p.m. UTC | #1
On Mon,  8 Apr 2024 16:58:17 +0200
Michal Koutný <mkoutny@suse.com> wrote:

> @@ -294,7 +295,7 @@ static void __trace_find_cmdline(int pid, char comm[])
>  		return;
>  	}
>  
> -	tpid = pid & (PID_MAX_DEFAULT - 1);
> +	tpid = pid % PID_MAP_SIZE;

Does that compile to the same? This is a fast path.

-- Steve


>  	map = savedcmd->map_pid_to_cmdline[tpid];
>  	if (map != NO_CMDLINE_MAP) {
>  		tpid = savedcmd->map_cmdline_to_pid[map];
Michal Koutný May 13, 2024, 5:30 p.m. UTC | #2
On Tue, Apr 09, 2024 at 11:01:26AM GMT, Steven Rostedt <rostedt@goodmis.org> wrote:
> > -	tpid = pid & (PID_MAX_DEFAULT - 1);
> > +	tpid = pid % PID_MAP_SIZE;
> 
> Does that compile to the same? This is a fast path.

I didn't check.
If fast is the intetion, I would change it to something
like BUILD_BUG_ON(!(PID_MAP_SIZE % 2)) and keep the bit operation
without reliance on compiler optimizations.

Thanks for the response (I may not follow up on this single commit
though).

Michal
diff mbox series

Patch

diff --git a/kernel/trace/trace_sched_switch.c b/kernel/trace/trace_sched_switch.c
index 8a407adb0e1c..aca2dafdd97a 100644
--- a/kernel/trace/trace_sched_switch.c
+++ b/kernel/trace/trace_sched_switch.c
@@ -161,6 +161,7 @@  static size_t tgid_map_max;
 
 #define SAVED_CMDLINES_DEFAULT 128
 #define NO_CMDLINE_MAP UINT_MAX
+#define PID_MAP_SIZE (CONFIG_BASE_SMALL ? 0x1000 : 0x8000)
 /*
  * Preemption must be disabled before acquiring trace_cmdline_lock.
  * The various trace_arrays' max_lock must be acquired in a context
@@ -168,7 +169,7 @@  static size_t tgid_map_max;
  */
 static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
 struct saved_cmdlines_buffer {
-	unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
+	unsigned map_pid_to_cmdline[PID_MAP_SIZE];
 	unsigned *map_cmdline_to_pid;
 	unsigned cmdline_num;
 	int cmdline_idx;
@@ -248,7 +249,7 @@  int trace_save_cmdline(struct task_struct *tsk)
 	if (!tsk->pid)
 		return 1;
 
-	tpid = tsk->pid & (PID_MAX_DEFAULT - 1);
+	tpid = tsk->pid % PID_MAP_SIZE;
 
 	/*
 	 * It's not the end of the world if we don't get
@@ -294,7 +295,7 @@  static void __trace_find_cmdline(int pid, char comm[])
 		return;
 	}
 
-	tpid = pid & (PID_MAX_DEFAULT - 1);
+	tpid = pid % PID_MAP_SIZE;
 	map = savedcmd->map_pid_to_cmdline[tpid];
 	if (map != NO_CMDLINE_MAP) {
 		tpid = savedcmd->map_cmdline_to_pid[map];
@@ -645,8 +646,8 @@  tracing_saved_cmdlines_size_write(struct file *filp, const char __user *ubuf,
 	if (ret)
 		return ret;
 
-	/* must have at least 1 entry or less than PID_MAX_DEFAULT */
-	if (!val || val > PID_MAX_DEFAULT)
+	/* must have at least 1 entry or fit into map_pid_to_cmdline */
+	if (!val || val >= PID_MAP_SIZE)
 		return -EINVAL;
 
 	ret = tracing_resize_saved_cmdlines((unsigned int)val);