LWN.net Logo

Freeing network devices safely

Recent development kernels include a great deal of networking information under /sys/class. For the moment, it is mostly physical layer stuff, but one should expect more information to show up there over time, as it migrates out of /proc/sys. The current networking sysfs files draw their information from the interface's associated net_device structure. That scheme works nicely, in that network drivers need not concern themselves with providing the sysfs infrastructure; it just sort of happens. But consider what happens if a suitably privileged user executes something like:

	rmmod e100 < /sys/class/net/eth0/statistics/tx_bytes

This command will keep the indicated sysfs file open past the time when the module containing the net_device structure behind that file is removed from the system. Unless special care is taken, the open file will be left pointing to structures which no longer exist, leading to all kinds of potential trouble. Most drivers do not take that care.

Until 2.6.0-test4, that is. After a series of patches by Stephen Hemminger, drivers are expected to use kmalloc() to create net_device structures dynamicly. Most drivers already worked that way; the difference now is that drivers can no longer just return those structures with kfree() when they are no longer needed. Instead, there is a new function which is used to get rid of a net_device structure:

    void free_netdev (struct net_device *dev);

This function, of course, helps the networking system maintain reference counts for net_device structures, and avoid freeing them until they are truly unused. This whole structure is relatively simple, but it demonstrates, again, the higher level of care required to avoid creating race conditions in the 2.6 kernel.


(Log in to post comments)

Copyright © 2003, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds