diff mbox series

[4/4] libtracefs: Do not count CPUs beyond set size in get_affinity

Message ID 20220119155741.1766541-5-rostedt@goodmis.org (mailing list archive)
State Accepted
Commit 915e7f58eb59530412595b69e9511c5933014c0c
Headers show
Series libtracefs: Address the comments that Tzvetomir had for the get affinity functions | expand

Commit Message

Steven Rostedt Jan. 19, 2022, 3:57 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

If the string of CPUs has bits set that are beyond the size that the
passed in cpu_set_t size can hold, do not set them.

This relies on the _S version of the CPU_SET macros from not crashing if
the size if too big. One would think that the _S versions would be made
specifically to protect against being too big.

If a CPU is set in the string but is outside the size limit of the
cpu_set_t passed in, then do not add it to the count that is returned by
tracefs_instance_get_affinity_set().

Link: https://lore.kernel.org/all/CAPpZLN4n=L-ZHCXM+LDRiQu0XwR4iCnGeCKJOuOWenkz2EhESA@mail.gmail.com/

Reported-by: Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 src/tracefs-instance.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/src/tracefs-instance.c b/src/tracefs-instance.c
index db51af3818f0..1493938fa445 100644
--- a/src/tracefs-instance.c
+++ b/src/tracefs-instance.c
@@ -1000,7 +1000,16 @@  static inline int update_cpu_set(int cpus, int cpu_set, int cpu,
 		return 0;
 
 	CPU_SET_S(cpu_set + cpu, set_size, set);
-	return 1;
+
+	/*
+	 * It is possible that the passed in set_size is not big enough
+	 * to hold the cpu we just set. If that's the case, do not report
+	 * it as being set.
+	 *
+	 * The CPU_ISSET_S() should return false if the CPU given to it
+	 * is bigger than the set itself.
+	 */
+	return CPU_ISSET_S(cpu_set + cpu, set_size, set) ? 1 : 0;
 }
 
 /**