Skip to content

Commit

Permalink
Merge pull request systemd#33424 from poettering/machined-gc-rework
Browse files Browse the repository at this point in the history
machined: clean up GC logic
  • Loading branch information
yuwata authored Jun 20, 2024
2 parents b608bf5 + 1762c2c commit 2c1ada7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 25 deletions.
39 changes: 39 additions & 0 deletions src/machine/machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Machine* machine_free(Machine *m) {
m->manager->host_machine = NULL;
}

m->leader_pidfd_event_source = sd_event_source_disable_unref(m->leader_pidfd_event_source);
if (pidref_is_set(&m->leader)) {
if (m->manager)
(void) hashmap_remove_value(m->manager->machine_leaders, PID_TO_PTR(m->leader.pid), m);
Expand Down Expand Up @@ -475,6 +476,38 @@ static int machine_ensure_scope(Machine *m, sd_bus_message *properties, sd_bus_e
return 0;
}

static int machine_dispatch_leader_pidfd(sd_event_source *s, int fd, unsigned revents, void *userdata) {
Machine *m = ASSERT_PTR(userdata);

m->leader_pidfd_event_source = sd_event_source_disable_unref(m->leader_pidfd_event_source);
machine_add_to_gc_queue(m);

return 0;
}

static int machine_watch_pidfd(Machine *m) {
int r;

assert(m);
assert(m->manager);
assert(pidref_is_set(&m->leader));
assert(!m->leader_pidfd_event_source);

if (m->leader.fd < 0)
return 0;

/* If we have a pidfd for the leader, let's also track it for POLLIN, and GC the machine
* automatically if it dies */

r = sd_event_add_io(m->manager->event, &m->leader_pidfd_event_source, m->leader.fd, EPOLLIN, machine_dispatch_leader_pidfd, m);
if (r < 0)
return r;

(void) sd_event_source_set_description(m->leader_pidfd_event_source, "machine-pidfd");

return 0;
}

int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
int r;

Expand All @@ -490,6 +523,10 @@ int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) {
if (r < 0)
return r;

r = machine_watch_pidfd(m);
if (r < 0)
return r;

/* Create cgroup */
r = machine_ensure_scope(m, properties, error);
if (r < 0)
Expand Down Expand Up @@ -592,6 +629,8 @@ void machine_add_to_gc_queue(Machine *m) {

LIST_PREPEND(gc_queue, m->manager->machine_gc_queue, m);
m->in_gc_queue = true;

manager_enqueue_gc(m->manager);
}

MachineState machine_get_state(Machine *s) {
Expand Down
1 change: 1 addition & 0 deletions src/machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct Machine {
char *scope_job;

PidRef leader;
sd_event_source *leader_pidfd_event_source;

dual_timestamp timestamp;

Expand Down
52 changes: 52 additions & 0 deletions src/machine/machined-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,55 @@ int manager_find_machine_for_gid(Manager *m, gid_t gid, Machine **ret_machine, g

return false;
}

void manager_gc(Manager *m, bool drop_not_started) {
Machine *machine;

assert(m);

while ((machine = LIST_POP(gc_queue, m->machine_gc_queue))) {
machine->in_gc_queue = false;

/* First, if we are not closing yet, initiate stopping */
if (machine_may_gc(machine, drop_not_started) &&
machine_get_state(machine) != MACHINE_CLOSING)
machine_stop(machine);

/* Now, the stop probably made this referenced
* again, but if it didn't, then it's time to let it
* go entirely. */
if (machine_may_gc(machine, drop_not_started)) {
machine_finalize(machine);
machine_free(machine);
}
}
}

static int on_deferred_gc(sd_event_source *s, void *userdata) {
manager_gc(userdata, /* drop_not_started= */ true);
return 0;
}

void manager_enqueue_gc(Manager *m) {
int r;

assert(m);

if (m->deferred_gc_event_source) {
r = sd_event_source_set_enabled(m->deferred_gc_event_source, SD_EVENT_ONESHOT);
if (r < 0)
log_warning_errno(r, "Failed to enable GC event source, ignoring: %m");

return;
}

r = sd_event_add_defer(m->event, &m->deferred_gc_event_source, on_deferred_gc, m);
if (r < 0)
return (void) log_warning_errno(r, "Failed to allocate GC event source, ignoring: %m");

r = sd_event_source_set_priority(m->deferred_gc_event_source, SD_EVENT_PRIORITY_IDLE);
if (r < 0)
log_warning_errno(r, "Failed to tweak priority of event source, ignoring: %m");

(void) sd_event_source_set_description(m->deferred_gc_event_source, "deferred-gc");
}
27 changes: 2 additions & 25 deletions src/machine/machined.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static Manager* manager_unref(Manager *m) {
sd_event_source_unref(m->nscd_cache_flush_event);
#endif

sd_event_source_disable_unref(m->deferred_gc_event_source);

hashmap_free(m->polkit_registry);

manager_varlink_done(m);
Expand Down Expand Up @@ -264,29 +266,6 @@ static int manager_connect_bus(Manager *m) {
return 0;
}

static void manager_gc(Manager *m, bool drop_not_started) {
Machine *machine;

assert(m);

while ((machine = LIST_POP(gc_queue, m->machine_gc_queue))) {
machine->in_gc_queue = false;

/* First, if we are not closing yet, initiate stopping */
if (machine_may_gc(machine, drop_not_started) &&
machine_get_state(machine) != MACHINE_CLOSING)
machine_stop(machine);

/* Now, the stop probably made this referenced
* again, but if it didn't, then it's time to let it
* go entirely. */
if (machine_may_gc(machine, drop_not_started)) {
machine_finalize(machine);
machine_free(machine);
}
}
}

static int manager_startup(Manager *m) {
Machine *machine;
int r;
Expand Down Expand Up @@ -331,8 +310,6 @@ static bool check_idle(void *userdata) {
if (!hashmap_isempty(m->polkit_registry))
return false;

manager_gc(m, true);

return hashmap_isempty(m->machines);
}

Expand Down
5 changes: 5 additions & 0 deletions src/machine/machined.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Manager {
Hashmap *machine_units;
Hashmap *machine_leaders;

sd_event_source *deferred_gc_event_source;

Hashmap *polkit_registry;

Hashmap *image_cache;
Expand Down Expand Up @@ -68,3 +70,6 @@ static inline void manager_enqueue_nscd_cache_flush(Manager *m) {}

int manager_find_machine_for_uid(Manager *m, uid_t host_uid, Machine **ret_machine, uid_t *ret_internal_uid);
int manager_find_machine_for_gid(Manager *m, gid_t host_gid, Machine **ret_machine, gid_t *ret_internal_gid);

void manager_gc(Manager *m, bool drop_not_started);
void manager_enqueue_gc(Manager *m);

0 comments on commit 2c1ada7

Please sign in to comment.