cpu-set-util: rename cpu_set_reset() -> cpu_set_done()

This also introduces cpu_set_done_and_replace().

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe
2025-06-24 00:20:20 +09:00
parent f4093f43fb
commit 296fe3d511
21 changed files with 80 additions and 77 deletions
+4 -4
View File
@@ -267,10 +267,10 @@ void cgroup_context_done(CGroupContext *c) {
c->restrict_network_interfaces = set_free(c->restrict_network_interfaces);
cpu_set_reset(&c->cpuset_cpus);
cpu_set_reset(&c->startup_cpuset_cpus);
cpu_set_reset(&c->cpuset_mems);
cpu_set_reset(&c->startup_cpuset_mems);
cpu_set_done(&c->cpuset_cpus);
cpu_set_done(&c->startup_cpuset_cpus);
cpu_set_done(&c->cpuset_mems);
cpu_set_done(&c->startup_cpuset_mems);
c->delegate_subgroup = mfree(c->delegate_subgroup);
+4 -6
View File
@@ -1241,7 +1241,7 @@ int bus_cgroup_set_property(
} else if (STR_IN_SET(name, "AllowedCPUs", "StartupAllowedCPUs", "AllowedMemoryNodes", "StartupAllowedMemoryNodes")) {
const void *a;
size_t n;
_cleanup_(cpu_set_reset) CPUSet new_set = {};
_cleanup_(cpu_set_done) CPUSet new_set = {};
r = sd_bus_message_read_array(message, 'y', &a, &n);
if (r < 0)
@@ -1267,12 +1267,10 @@ int bus_cgroup_set_property(
set = &c->cpuset_mems;
else if (streq(name, "StartupAllowedMemoryNodes"))
set = &c->startup_cpuset_mems;
else
assert_not_reached();
assert(set);
cpu_set_reset(set);
*set = new_set;
new_set = (CPUSet) {};
cpu_set_done_and_replace(*set, new_set);
unit_invalidate_cgroup(u, CGROUP_MASK_CPUSET);
unit_write_settingf(u, flags, name, "%s=\n%s=%s", name, name, setstr);
+6 -5
View File
@@ -115,7 +115,7 @@ static int property_get_cpu_affinity(
sd_bus_error *error) {
ExecContext *c = ASSERT_PTR(userdata);
_cleanup_(cpu_set_reset) CPUSet s = {};
_cleanup_(cpu_set_done) CPUSet s = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
@@ -2876,10 +2876,9 @@ int bus_exec_context_set_transient_property(
}
#endif
if (STR_IN_SET(name, "CPUAffinity", "NUMAMask")) {
_cleanup_(cpu_set_done) CPUSet set = {};
const void *a;
size_t n;
bool affinity = streq(name, "CPUAffinity");
_cleanup_(cpu_set_reset) CPUSet set = {};
r = sd_bus_message_read_array(message, 'y', &a, &n);
if (r < 0)
@@ -2890,8 +2889,10 @@ int bus_exec_context_set_transient_property(
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
CPUSet *cpuset = streq(name, "CPUAffinity") ? &c->cpu_set : &c->numa_policy.nodes;
if (n == 0) {
cpu_set_reset(affinity ? &c->cpu_set : &c->numa_policy.nodes);
cpu_set_done(cpuset);
unit_write_settingf(u, flags, name, "%s=", name);
} else {
_cleanup_free_ char *str = NULL;
@@ -2903,7 +2904,7 @@ int bus_exec_context_set_transient_property(
/* We forego any optimizations here, and always create the structure using
* cpu_set_add_set(), because we don't want to care if the existing size we
* got over dbus is appropriate. */
r = cpu_set_add_set(affinity ? &c->cpu_set : &c->numa_policy.nodes, &set);
r = cpu_set_add_set(cpuset, &set);
if (r < 0)
return r;
+2 -2
View File
@@ -1154,7 +1154,7 @@ static int property_get_cpuset_cpus(
sd_bus_error *error) {
Unit *u = ASSERT_PTR(userdata);
_cleanup_(cpu_set_reset) CPUSet cpus = {};
_cleanup_(cpu_set_done) CPUSet cpus = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
@@ -1176,7 +1176,7 @@ static int property_get_cpuset_mems(
sd_bus_error *error) {
Unit *u = ASSERT_PTR(userdata);
_cleanup_(cpu_set_reset) CPUSet mems = {};
_cleanup_(cpu_set_done) CPUSet mems = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
+2 -2
View File
@@ -3990,7 +3990,7 @@ static int exec_context_cpu_affinity_from_numa(const ExecContext *c, CPUSet *ret
return 0;
}
_cleanup_(cpu_set_reset) CPUSet s = {};
_cleanup_(cpu_set_done) CPUSet s = {};
r = numa_to_cpu_set(&c->numa_policy, &s);
if (r < 0)
return r;
@@ -5107,7 +5107,7 @@ int exec_invoke(
}
if (context->cpu_affinity_from_numa || context->cpu_set.set) {
_cleanup_(cpu_set_reset) CPUSet converted_cpu_set = {};
_cleanup_(cpu_set_done) CPUSet converted_cpu_set = {};
const CPUSet *cpu_set;
if (context->cpu_affinity_from_numa) {
+1 -1
View File
@@ -720,7 +720,7 @@ void exec_context_done(ExecContext *c) {
c->n_temporary_filesystems = 0;
c->mount_images = mount_image_free_many(c->mount_images, &c->n_mount_images);
cpu_set_reset(&c->cpu_set);
cpu_set_done(&c->cpu_set);
numa_policy_reset(&c->numa_policy);
c->utmp_id = mfree(c->utmp_id);
+1 -1
View File
@@ -1834,7 +1834,7 @@ int config_parse_exec_cpu_affinity(
if (streq(rvalue, "numa")) {
c->cpu_affinity_from_numa = true;
cpu_set_reset(&c->cpu_set);
cpu_set_done(&c->cpu_set);
return 0;
}
+1 -1
View File
@@ -2787,7 +2787,7 @@ static void reset_arguments(void) {
arg_machine_id = (sd_id128_t) {};
arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE;
cpu_set_reset(&arg_cpu_affinity);
cpu_set_done(&arg_cpu_affinity);
numa_policy_reset(&arg_numa_policy);
arg_random_seed = mfree(arg_random_seed);
+1 -1
View File
@@ -455,7 +455,7 @@ empty:
static int effective_cpuset_build_json(sd_json_variant **ret, const char *name, void *userdata, const char *cpuset_name) {
Unit *u = ASSERT_PTR(userdata);
_cleanup_(cpu_set_reset) CPUSet cpus = {};
_cleanup_(cpu_set_done) CPUSet cpus = {};
int r;
assert(ret);
+3 -7
View File
@@ -1240,10 +1240,7 @@ static int oci_cgroup_cpu_cpus(const char *name, sd_json_variant *v, sd_json_dis
if (r < 0)
return json_log(v, flags, r, "Failed to parse CPU set specification: %s", n);
cpu_set_reset(&data->cpu_set);
data->cpu_set = set;
return 0;
return cpu_set_done_and_replace(data->cpu_set, set);
}
static int oci_cgroup_cpu(const char *name, sd_json_variant *v, sd_json_dispatch_flags_t flags, void *userdata) {
@@ -1270,12 +1267,11 @@ static int oci_cgroup_cpu(const char *name, sd_json_variant *v, sd_json_dispatch
r = oci_dispatch(v, table, flags, &data);
if (r < 0) {
cpu_set_reset(&data.cpu_set);
cpu_set_done(&data.cpu_set);
return r;
}
cpu_set_reset(&s->cpu_set);
s->cpu_set = data.cpu_set;
cpu_set_done_and_replace(s->cpu_set, data.cpu_set);
if (data.weight != UINT64_MAX) {
r = settings_allocate_properties(s);
+1 -1
View File
@@ -136,7 +136,7 @@ Settings* settings_free(Settings *s) {
strv_free(s->syscall_deny_list);
rlimit_free_all(s->rlimit);
free(s->hostname);
cpu_set_reset(&s->cpu_set);
cpu_set_done(&s->cpu_set);
strv_free(s->bind_user);
strv_free(s->network_interfaces);
+4 -7
View File
@@ -279,7 +279,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_syscall_deny_list, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_seccomp, seccomp_releasep);
#endif
STATIC_DESTRUCTOR_REGISTER(arg_credentials, machine_credential_context_done);
STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_reset);
STATIC_DESTRUCTOR_REGISTER(arg_cpu_set, cpu_set_done);
STATIC_DESTRUCTOR_REGISTER(arg_sysctl, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_bind_user, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_settings_filename, freep);
@@ -1460,8 +1460,7 @@ static int parse_argv(int argc, char *argv[]) {
if (r < 0)
return log_error_errno(r, "Failed to parse CPU affinity mask %s: %m", optarg);
cpu_set_reset(&arg_cpu_set);
arg_cpu_set = cpuset;
cpu_set_done_and_replace(arg_cpu_set, cpuset);
arg_settings_mask |= SETTING_CPU_AFFINITY;
break;
}
@@ -4908,10 +4907,8 @@ static int merge_settings(Settings *settings, const char *path) {
if (!arg_settings_trusted)
log_warning("Ignoring CPUAffinity= setting, file '%s' is not trusted.", path);
else {
cpu_set_reset(&arg_cpu_set);
arg_cpu_set = TAKE_STRUCT(settings->cpu_set);
}
else
cpu_set_done_and_replace(arg_cpu_set, settings->cpu_set);
}
if ((arg_settings_mask & SETTING_RESOLV_CONF) == 0 &&
+4 -3
View File
@@ -615,7 +615,8 @@ static int bus_append_cgroup_property(sd_bus_message *m, const char *field, cons
"StartupAllowedCPUs",
"AllowedMemoryNodes",
"StartupAllowedMemoryNodes")) {
_cleanup_(cpu_set_reset) CPUSet cpuset = {};
_cleanup_(cpu_set_done) CPUSet cpuset = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
@@ -1564,7 +1565,7 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
}
if (streq(field, "CPUAffinity")) {
_cleanup_(cpu_set_reset) CPUSet cpuset = {};
_cleanup_(cpu_set_done) CPUSet cpuset = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
@@ -1599,7 +1600,7 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
}
if (streq(field, "NUMAMask")) {
_cleanup_(cpu_set_reset) CPUSet nodes = {};
_cleanup_(cpu_set_done) CPUSet nodes = {};
_cleanup_free_ uint8_t *array = NULL;
size_t allocated;
+15 -6
View File
@@ -117,11 +117,20 @@ char* cpu_set_to_mask_string(const CPUSet *c) {
return TAKE_PTR(str) ?: strdup("0");
}
void cpu_set_done(CPUSet *c) {
assert(c);
if (c->set)
CPU_FREE(c->set);
*c = (CPUSet) {};
}
CPUSet* cpu_set_free(CPUSet *c) {
if (!c)
return c;
cpu_set_reset(c);
cpu_set_done(c);
return mfree(c);
}
@@ -217,7 +226,7 @@ int parse_cpu_set_full(
unsigned line,
const char *lvalue) {
_cleanup_(cpu_set_reset) CPUSet c = {};
_cleanup_(cpu_set_done) CPUSet c = {};
const char *p = ASSERT_PTR(rvalue);
assert(cpu_set);
@@ -272,7 +281,7 @@ int parse_cpu_set_extend(
unsigned line,
const char *lvalue) {
_cleanup_(cpu_set_reset) CPUSet cpuset = {};
_cleanup_(cpu_set_done) CPUSet cpuset = {};
int r;
assert(old);
@@ -282,8 +291,8 @@ int parse_cpu_set_extend(
return r;
if (!cpuset.set) {
/* An empty assignment resets the CPU list */
cpu_set_reset(old);
/* An empty assignment clears the CPU list */
cpu_set_done(old);
return 0;
}
@@ -348,7 +357,7 @@ int cpu_set_to_dbus(const CPUSet *c, uint8_t **ret, size_t *ret_size) {
}
int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *ret) {
_cleanup_(cpu_set_reset) CPUSet c = {};
_cleanup_(cpu_set_done) CPUSet c = {};
int r;
assert(bits || size == 0);
+8 -6
View File
@@ -11,12 +11,14 @@ typedef struct CPUSet {
size_t allocated; /* in bytes */
} CPUSet;
static inline void cpu_set_reset(CPUSet *a) {
assert((a->allocated > 0) == !!a->set);
if (a->set)
CPU_FREE(a->set);
*a = (CPUSet) {};
}
void cpu_set_done(CPUSet *c);
#define cpu_set_done_and_replace(a, b) \
({ \
CPUSet *_a = &(a), *_b = &(b); \
cpu_set_done(_a); \
*_a = TAKE_STRUCT(*_b); \
0; \
})
CPUSet* cpu_set_free(CPUSet *c);
DEFINE_TRIVIAL_CLEANUP_FUNC(CPUSet*, cpu_set_free);
+3 -4
View File
@@ -91,17 +91,15 @@ int apply_numa_policy(const NUMAPolicy *policy) {
}
int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
_cleanup_(cpu_set_done) CPUSet s = {};
int r;
size_t i;
_cleanup_(cpu_set_reset) CPUSet s = {};
assert(policy);
assert(ret);
for (i = 0; i < policy->nodes.allocated * 8; i++) {
for (size_t i = 0; i < policy->nodes.allocated * 8; i++) {
_cleanup_free_ char *l = NULL;
char p[STRLEN("/sys/devices/system/node/node//cpulist") + DECIMAL_STR_MAX(size_t) + 1];
_cleanup_(cpu_set_reset) CPUSet part = {};
if (!CPU_ISSET_S(i, policy->nodes.allocated, policy->nodes.set))
continue;
@@ -112,6 +110,7 @@ int numa_to_cpu_set(const NUMAPolicy *policy, CPUSet *ret) {
if (r < 0)
return r;
_cleanup_(cpu_set_done) CPUSet part = {};
r = parse_cpu_set(l, &part);
if (r < 0)
return r;
+1 -1
View File
@@ -23,7 +23,7 @@ static inline int numa_policy_get_type(const NUMAPolicy *p) {
static inline void numa_policy_reset(NUMAPolicy *p) {
assert(p);
cpu_set_reset(&p->nodes);
cpu_set_done(&p->nodes);
p->type = -1;
}
+1 -1
View File
@@ -1775,7 +1775,7 @@ static int print_property(const char *name, const char *expected_value, sd_bus_m
"EffectiveCPUs", "EffectiveMemoryNodes")) {
_cleanup_free_ char *affinity = NULL;
_cleanup_(cpu_set_reset) CPUSet set = {};
_cleanup_(cpu_set_done) CPUSet set = {};
const void *a;
size_t n;
+16 -16
View File
@@ -41,14 +41,14 @@ TEST(parse_cpu_set) {
/* empty */
ASSERT_CPUSET_EMPTY(c);
ASSERT_CPUSET_STRING(c, "", "", "0");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Single value */
ASSERT_OK(parse_cpu_set("0", &c));
ASSERT_CPUSET_COUNT(c, 1);
ASSERT_CPUSET_ISSET(c, 0);
ASSERT_CPUSET_STRING(c, "0", "0", "1");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Simple range (from CPUAffinity example) */
ASSERT_OK(parse_cpu_set("1 2 4", &c));
@@ -57,7 +57,7 @@ TEST(parse_cpu_set) {
ASSERT_CPUSET_ISSET(c, 2);
ASSERT_CPUSET_ISSET(c, 4);
ASSERT_CPUSET_STRING(c, "1 2 4", "1-2 4", "16");
cpu_set_reset(&c);
cpu_set_done(&c);
/* A more interesting range */
ASSERT_OK(parse_cpu_set("0 1 2 3 8 9 10 11", &c));
@@ -67,7 +67,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Quoted strings */
ASSERT_OK(parse_cpu_set("8 '9' 10 \"11\"", &c));
@@ -75,7 +75,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "8 9 10 11", "8-11", "f00");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Use commas as separators */
ASSERT_OK(parse_cpu_set("0,1,2,3 8,9,10,11", &c));
@@ -85,7 +85,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Commas with spaces (and trailing comma, space) */
ASSERT_OK(parse_cpu_set("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c));
@@ -94,7 +94,7 @@ TEST(parse_cpu_set) {
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_ISSET(c, 63);
ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 63", "0-7 63", "80000000,000000ff");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Ranges */
ASSERT_OK(parse_cpu_set("0-3,8-11", &c));
@@ -104,7 +104,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
cpu_set_reset(&c);
cpu_set_done(&c);
ASSERT_OK(parse_cpu_set("36-39,44-47", &c));
ASSERT_CPUSET_COUNT(c, 8);
@@ -113,14 +113,14 @@ TEST(parse_cpu_set) {
for (unsigned i = 44; i < 48; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "36 37 38 39 44 45 46 47", "36-39 44-47", "f0f0,00000000");
cpu_set_reset(&c);
cpu_set_done(&c);
ASSERT_OK(parse_cpu_set("64-71", &c));
ASSERT_CPUSET_COUNT(c, 8);
for (unsigned i = 64; i < 72; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "64 65 66 67 68 69 70 71", "64-71", "ff,00000000,00000000");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Ranges with trailing comma, space */
ASSERT_OK(parse_cpu_set("0-3 8-11, ", &c));
@@ -130,13 +130,13 @@ TEST(parse_cpu_set) {
for (unsigned i = 8; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Negative range (returns empty cpu_set) */
ASSERT_OK(parse_cpu_set("3-0", &c));
ASSERT_CPUSET_COUNT(c, 0);
ASSERT_CPUSET_STRING(c, "", "", "0");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Overlapping ranges */
ASSERT_OK(parse_cpu_set("0-7 4-11", &c));
@@ -144,7 +144,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 0; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 8 9 10 11", "0-11", "fff");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Mix ranges and individual CPUs */
ASSERT_OK(parse_cpu_set("0,2 4-11", &c));
@@ -154,7 +154,7 @@ TEST(parse_cpu_set) {
for (unsigned i = 4; i < 12; i++)
ASSERT_CPUSET_ISSET(c, i);
ASSERT_CPUSET_STRING(c, "0 2 4 5 6 7 8 9 10 11", "0 2 4-11", "ff5");
cpu_set_reset(&c);
cpu_set_done(&c);
/* Garbage */
ASSERT_ERROR(parse_cpu_set("0 1 2 3 garbage", &c), EINVAL);
@@ -185,7 +185,7 @@ TEST(parse_cpu_set) {
ASSERT_NOT_NULL(strextend_with_separator(&expected_mask, ",", i < 6 ? "ffffffff" : "00000000"));
ASSERT_CPUSET_STRING(c, expected_str, "8000-8191", expected_mask);
cpu_set_reset(&c);
cpu_set_done(&c);
}
TEST(parse_cpu_set_extend) {
@@ -204,7 +204,7 @@ TEST(parse_cpu_set_extend) {
}
TEST(cpu_set_to_from_dbus) {
_cleanup_(cpu_set_reset) CPUSet c = {}, c2 = {};
_cleanup_(cpu_set_done) CPUSet c = {}, c2 = {};
ASSERT_OK(parse_cpu_set("1 3 8 100-200", &c));
ASSERT_CPUSET_COUNT(c, 104);
+1 -1
View File
@@ -331,7 +331,7 @@ static void test_exec_bindpaths(Manager *m) {
}
static void test_exec_cpuaffinity(Manager *m) {
_cleanup_(cpu_set_reset) CPUSet c = {};
_cleanup_(cpu_set_done) CPUSet c = {};
ASSERT_OK(cpu_set_realloc(&c, 8192)); /* just allocate the maximum possible size */
ASSERT_OK_ERRNO(sched_getaffinity(0, c.allocated, c.set));
+1 -1
View File
@@ -1393,7 +1393,7 @@ int config_parse_rps_cpu_mask(
}
if (streq(rvalue, "disable"))
cpu_set_reset(mask);
cpu_set_done(mask);
else if (streq(rvalue, "all")) {
r = cpu_set_add_all(mask);