ktypes vs. devices classes (struct class)
Greg KH
greg at kroah.com
Sun Oct 1 05:50:46 EDT 2023
On Sat, Sep 30, 2023 at 08:17:26PM +0200, Richard wrote:
> Hi,
>
> I appreciate your answer, thank you for your time.
>
> >
> > Look closer. Tell me what "struct class" is for vs. what "struct
> > kobj_type" is for and see if they both could be used for the same thing?
>
> I've looked at the definitions in include/linux and thought a bit about the
> semantics.
>
> struct kobj_type defines behaviour (sysfs_ops and default_attrs) that is
> common for multiple kobjects. But those kobjects could be embedded in
> drivers, devices or a bus.
>
> I think one core difference is that "struct class" is strictly for devices
> so it's more specific and contains a lot of members that are only relevant
> for devices.
Close.
"struct class" represents how userspace interacts with a device (tty /
input / block / etc.)
"struct kobj_type" is needed to describe what "type" of struct kobject a
specific kobject is. It defines a number of operations that handle the
lifespan of the kobject.
> My digging however has brought me to a few new questions:
>
> Do all devices (their kobjects to be more precise) in one class (e.g.
> /sys/class/net ) belong to the same ktype?
ktype is "lower" than classes. ktype is not used for things in the
driver model, it's used for things "lower" than the driver model (and to
implement the driver model itself.)
So no driver should ever be messing with a ktype. If you want to have a
different "type" of device on the same bus or class, use "struct
device_type" as that's what that is for.
> Is it possible that one device belongs to several classes?
No.
> I've seen struct class defines **class_groups, but (contrary to struct
> kobj_type) not the corresponding struct sysfs_ops, why? Where is it then?
groups are used to define attributes (i.e. sysfs files). sysfs_ops is
much "lower" in the stack.
I think the description of how the driver model works in the book, Linux
Device Drivers, 3rd edition, free online, should still represent how
things work on this layer pretty well, although we have changed things
in places over time since the book was written. Try looking that first.
> > When we implemented them, we didn't think so but maybe something has
> > changed to now allow this? If so, great, please send us patches to do
> > so!
>
> Oh please don't misunderstand me, even if we had come to an agreement that
> this architecture was unelegent (which it isn't I see the point now), I
> don't think that would have been reason enough to change it.
Change is good if it is needed, that's how code evolves, based on new
ideas and use cases. So that's fine if needed.
hope this helps,
greg k-h
More information about the Kernelnewbies
mailing list