Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

UBI: fix resource de-allocation

GregKH asked to fix UBI which has fake device release method. Indeed,
we have to free UBI device description object from the release method,
because otherwise we'll oops is someone opens a UBI device sysfs file,
then the device is removed, and he reads the file. With this fix, he
will get -ENODEV instead of an oops.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

+13 -3
+13 -3
drivers/mtd/ubi/build.c
··· 263 263 return ret; 264 264 } 265 265 266 - /* Fake "release" method for UBI devices */ 267 - static void dev_release(struct device *dev) { } 266 + static void dev_release(struct device *dev) 267 + { 268 + struct ubi_device *ubi = container_of(dev, struct ubi_device, dev); 269 + 270 + kfree(ubi); 271 + } 268 272 269 273 /** 270 274 * ubi_sysfs_init - initialize sysfs for an UBI device. ··· 948 944 if (ubi->bgt_thread) 949 945 kthread_stop(ubi->bgt_thread); 950 946 947 + /* 948 + * Get a reference to the device in order to prevent 'dev_release()' 949 + * from freeing @ubi object. 950 + */ 951 + get_device(&ubi->dev); 952 + 951 953 uif_close(ubi); 952 954 ubi_wl_close(ubi); 953 955 free_internal_volumes(ubi); ··· 965 955 vfree(ubi->dbg_peb_buf); 966 956 #endif 967 957 ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num); 968 - kfree(ubi); 958 + put_device(&ubi->dev); 969 959 return 0; 970 960 } 971 961