Skip to contentSkip to conversation

x86/spec_ctrl: Add sysctl knobs to enable/disable SPEC_CTRL feature

PATCH Alexey Dobriyan 2 participants 2 messages Jan 8, 2018

lore
Share & tools
lore
#0 lore raw

Review

Quoted 1 line
(quoted diff) +1 -0
+	len = sprintf(buf, "%d\n", READ_ONCE(*field));

READ_ONCE isn't necessary as there is only one read being made.

Review

Quoted 7 lines
(quoted diff) +7 -0
+	len = min(count, sizeof(buf) - 1);
+	if (copy_from_user(buf, user_buf, len))
+		return -EFAULT;
+
+	buf[len] = '\0';
+	if (kstrtouint(buf, 0, &enable))
+		return -EINVAL;

There is kstrto*_from_user().

#1 lore raw
Quoted 3 lines · Alexey Dobriyan

Review

Quoted 1 line
(quoted diff) +1 -0
+	len = sprintf(buf, "%d\n", READ_ONCE(*field));

READ_ONCE isn't necessary as there is only one read being made.

Others might disagree but you shouldn't ever let gcc touch any memory that can change under gcc without READ_ONCE or volatile.

Ages ago I was told in a switch() statement gcc could decide to use an hash and re-read the value and crash.

Not for the simple case above, and we often don't use READ_ONCE and we might user barrier() instead, but it would be better to use READ_ONCE.

READ_ONCE is more correct and there's no point to try to optimize it especially if there's only one read being made in that function.

Either version in practice will work, but READ_ONCE is preferable IMHO.

Thanks,
Andrea