linux - container_of isn't returning expected address -
i'm not sure i'm doing incorrectly it's time eyes. make device device_create()
providing "extra data" follows:
pdevice = device_create(ahcip_class, null, /*no parent*/ mkdev(ahcip_major, ahcip_minor + i), &mydevs[i], driver_name "%d", ahcip_minor + i);
expecting sysfs attribute function going take pointer struct kobject
member of struct device
following attribute function
static ahcip_dev *get_ahcip_dev(struct kobject *ko) { ahcip_dev *adev = null; struct device *pdev = container_of(ko, struct device, kobj); if (!pdev) { pr_err("%s:%d unable find device struct in kobject\n", __func__, __line__); return null; } /* debugging stuff */ pr_info("%s:%d mydevs[0] %p\n", __func__, __line__, mydevs); pr_info("%s:%d mydevs[1] %p\n", __func__, __line__, mydevs+1); pr_info("%s:%d mydevs[0].psysfs_dev %p\n", __func__, __line__, mydevs->psysfs_dev); pr_info("%s:%d mydevs[1].psysfs_dev %p\n", __func__, __line__, (mydevs + 1)->psysfs_dev); pr_info("%s:%d pdev %p\n", __func__, __line__, pdev); adev = (ahcip_dev*)dev_get_drvdata(pdev); /* return pointer anyway, if it's null, print klog */ if (!adev) pr_err("%s:%d no ahcip_dev, private driver data null\n", __func__, __line__); return adev; } static ssize_t pxis_show(struct kobject *kobj, struct kobj_attribute *attr, char *buff) { u32 pi = 0; ahcip_dev *adev = get_ahcip_dev(kobj); /* get_ahcip_dev() print happened, needs return * error code */ if (!adev) return -eio; pi = adev->port_index; return sprintf(buff, "%08x\n", get_port_reg(adev->hba->ports[pi], 0x10)); }
the output (condensed) above function shows:
get_ahcip_dev:175 mydevs[1].psysfs_dev ffff88011b2b4800 get_ahcip_dev:176 pdev ffff88011b2b47f0
pdev in case should point same memory location mydevs[1].psysfs_dev it's 16 bytes "earlier". doing wrong?
i don't answer own questions seems appropriate in case. root of problem faulty assumption of attribute function needed process. attribute functions have prototype you can view in context here
ssize_t (*show)(struct kobject *, struct attribute *,char *); ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
since device_create()
function returns struct device
object defined follows (excerpt only, see full def here)
struct device { struct device *parent; struct device_private *p; struct kobject kobj; ... }
i assumed pointer attribute function must process. reading through description of problem shows when used container_of
macro thought address of containing struct device
16 bytes "to early." notice, first 2 fields of structure pointers. on 64 bit system, mine is, 16 bytes.
because function prototypes defined shown above, assumed getting reference kobj
field. instead, getting actual struct device
object. in other words, getting address wanted upon function entry , still trying find it.
something may documented in labyrinth of linux kernel documentation. if knows of it, please put link here. read didn't see 1 coming. hope question , answer helps other kernel newbie.
Comments
Post a Comment