Skip to content

core: rework device state serialization/enumeration - #9200

Merged
poettering merged 22 commits into
systemd:masterfrom
poettering:device-state-fix
Jun 7, 2018
Merged

poettering merged 22 commits into
systemd:masterfrom
poettering:device-state-fix

Conversation

@poettering

Copy link
Copy Markdown
Member

This is a fix for #8832. It's a bit larger than I hoped for. But this was quite broken before...

@fbuihuu could you give this a whirl? does this fix things for you? If not, could you provide me with a reproducer?

Comment thread TODO Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right I noticed that too. But I wouldn't call that a feature ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's buggy behaviour that we should fix. Hence I added it to the TODO list.

Comment thread src/core/mount.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"(void)" can be dropped I think as device_found_node() doesn't return any value now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, will fix

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the use of "else" is really needed here ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's not, but it has the benefit that it naturally provides us with a new {} environment, which we can then use to scope the udev device object with, so that the scope isn't longer than necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative would be to give up on the strict adherence to C89 and allow (judicious) use of declarations of variables in the right place. Forcing the use of fake scopes just to do that doesn't really give us anything, because it's still possible to define a variable in the middle of a function, it's just more annoying to do so. Allowing declarations in the "natural" place has another big advantage: it's much easier to do "declaration is initialization", also reducing the potential for bugs. So all in all, I think we should just allow this, I'll prep a PR.

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't look right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch, it does not. I wonder why I didn't notice this. Yikes.

@poettering

Copy link
Copy Markdown
Member Author

OK, I force pushed a new version now, fixing the rather glaring typo I made, that @fbuihuu found. I also dropped the pointless (void) casts, as suggested.

PTAL!

poettering added 13 commits June 7, 2018 13:36
Also, proceed, as there's little we can do in this case.
let's drop the "now" argument, it's exactly what MANAGER_IS_RUNNING()
returns, hence let's use that instead to simplify things.

Moreover, let's change the add/found argument pair to become found/mask,
which allows us to change multiple flags at the same time into opposing
directions, which will be useful later on.

Also, let's change the return type to void. It's a notifier call where
callers will ignore the return value anyway as it is nothing actionable.

Should not change behaviour.
Let's separate the validate step out. Also, let's update some comments
which have long ceased to be true.

No change in behaviour.
This is a simple wrapper around udev_device_new_from_devnum(), and uses
the data from a struct stat's .st_rdev field to derive the udev_device
object.

@keszybz keszybz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good.

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment explaining what found and mask mean would be very nice, because the naming of those variables is not helpful.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative would be to give up on the strict adherence to C89 and allow (judicious) use of declarations of variables in the right place. Forcing the use of fake scopes just to do that doesn't really give us anything, because it's still possible to define a variable in the middle of a function, it's just more annoying to do so. Allowing declarations in the "natural" place has another big advantage: it's much easier to do "declaration is initialization", also reducing the potential for bugs. So all in all, I think we should just allow this, I'll prep a PR.

Comment thread src/core/unit.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth it to define this a separate function when there's just on user?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mostly capt it so that it matches nicely with unit_coldplug() which kinda does the same stuff...

Comment thread src/core/device.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type of rvalue here has no effect on the enum type (unless the value is large enough to force a certain width, but that's not the case here), so "U" is just misleading.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, you are right, this won't have any effect on sizeof(DEVICE_FOUND_UDEV). However, it does have an effect on the shift-left sooner or later. "1 << 31" is undefined in C on 32bit systems, while "1U << 31" is well defined. Now, you might say that "that's a synthetic case, and we never have so many flags defined" and as soon as you shift left by 32 you need "1ULL << 32" anyway, but I think in general, signed-extended shift-left is really really weird, and I'd rather not do that if we are talking about a flags sets...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the lesson to learn is to maybe add a macro for this...

#define ENUM_BIT(x) (1LLU << (x))

enum {
        X = ENUM_BIT(0),
        Y = ENUM_BIT(1),
        Z = ENUM_BIT(2),
}

does that make sense, or does this become too magic?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's too much magic. Not in a case like this, but we often define masks and "_MAX" and such, and it'd be awkward to combine with a macro. So I think it's fine as is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, by the time I read this i already had done #9224... :-(

PTAL

(I'd be fine if this isn't merged, but I kinda like the unifying work this does, after all we currently use 1 <<, 1U <<, 1UL <<, 1ULL <<, and UINT64_C(1) << at different places all over the place...

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!FLAGS_SET(previous, DEVICE_FOUND_UDEV) ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will fix

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This state was added in v238-539-g918e6f1c01 so it's OK to remove it without backwards compat.

Comment thread src/core/device.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that seems unnecessarily harsh. If I'm reading the code correctly, we would accept such paths and escape them, so this change breaks backward compat. And people tend to autogenerate rules, so a non-normalized path is possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not about deps, this is about adding alias names for units. For deps defined through udev rules we are tolerant, and will mangle. But I am sure if we accept device aliases we should be be very strict, as otherwise we might get into quite some ambiguity troubles

This is very similar to the existing unit method coldplug() but is
called a bit later. The idea is that that coldplug() restores the unit
state from before any prior reload/restart, i.e. puts the deserialized
state in effect. The catchup() call is then called a bit later, to
catch up with the system state for which we missed notifications while
we were reloading. This is only really useful for mount, swap and device
mount points were we should be careful to generate all missing unit
state change events (i.e. call unit_notify() appropriately) for
everything that happened while we were reloading.
No need to maintain a NULL marker at the end of the table if we know the
size of the array anyway.
This reworks how device units are "powered on".

This makes sure that any device changes that might have happened while
we were restarting/reloading will be noticed properly. For that we'll
now properly serialize/deserialize both the device unit state and the
device "found" flags, and restore these initially in the "coldplug"
phase of the manager deserialization. While enumerating the udev devices
during startup we'll put together a new "found" flags mask, which we'll
the switch to in the "catchup" phase of the manager deserialization,
which follows the "coldplug" phase.

Note that during the "coldplug" phase no unit state change events are
generated, which is different for the "catchall" phase which will do
that. Thus we correctly make sure that the deserialized state won't pull
in new deps, but any device's change while we were reloading would.

Fixes: systemd#8832
Replaces: systemd#8675
Make sure that whenever we enter "dead" state we unset the sysfs path,
not just when we are changing to it due to "found" mask changes.
Previously the enumerate() callback defined for each unit type would do
two things:

1. It would create perpetual units (i.e. -.slice, system.slice, -.mount and
   init.scope)

2. It would enumerate units from /proc/self/mountinfo, /proc/swaps and
   the udev database

With this change these two parts are split into two seperate methods:
enumerate() now only does #2, while enumerate_perpetual() is responsible
for #1. Why make this change? Well, perpetual units should have a
slightly different effect that those found through enumeration: as
perpetual units should be up unconditionally, perpetually and thus never
change state, they should also not pull in deps by their state changing,
not even when the state is first set to active. Thus, their state is
generally initialized through the per-device coldplug() method in
similar  fashion to the deserialized state from a previous run would be
put into place. OTOH units found through regular enumeration should
result in state changes (and thus pull in deps due to state changes),
hence their state should be put in effect in the catchup() method
instead. Hence, given this difference, let's also separate the
functions, so that the rule is:

1. What is created in enumerate_perpetual() should be started in
   coldplug()

2. What is created in enumerate() should be started in catchup().
This is mostly fall-out from d1a1f0a,
however some cases are older bugs.

There might be more issues lurking, this was a simple grep for "%m"
across the tree, with all lines removed that mention "errno" at all.
@poettering

Copy link
Copy Markdown
Member Author

OK, force pushed a new version. Only changes are the suggested comment about the "DeviceFound" mask parameters, more FLAGS_SET() usage.

I left the other stuff as it is, for the mentioned reasons

@keszybz

keszybz commented Jun 7, 2018

Copy link
Copy Markdown
Member

OK, let's merge. I tested this a bit, and everything seems OK.

@keszybz keszybz added the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Jun 7, 2018
@poettering

Copy link
Copy Markdown
Member Author

All green! Yay!

@poettering
poettering merged commit 3ceca73 into systemd:master Jun 7, 2018
filbranden added a commit to filbranden/systemd that referenced this pull request Jun 7, 2018
The refactor in systemd#9200 inadvertently dropped the variable assignment to
traverse the device and its hierarchy in add_matches_for_device().

This was uncovered by Coverity (CID #1393310).

Fix that by restoring the assignment.

Tested: `journalctl /dev/sda` now filters journalctl output again.
poettering pushed a commit that referenced this pull request Jun 7, 2018
The refactor in #9200 inadvertently dropped the variable assignment to
traverse the device and its hierarchy in add_matches_for_device().

This was uncovered by Coverity (CID #1393310).

Fix that by restoring the assignment.

Tested: `journalctl /dev/sda` now filters journalctl output again.
@keszybz keszybz removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants