Skip to content

Commit 712f314

Browse files
committed
fbmem: fix remove_conflicting_framebuffers races
When a register_framebuffer() call results in us removing old conflicting framebuffers, the new registration_lock doesn't protect that situation. And we can't just add the same locking to the function, because these functions call each other: register_framebuffer() calls remove_conflicting_framebuffers, which in turn calls unregister_framebuffer for any conflicting entry. In order to fix it, this just creates wrapper functions around all three functions and makes the versions that actually do the work be called "do_xxx()", leaving just the wrapper that gets the lock and calls the worker function. So the rule becomes simply that "do_xxxx()" has to be called with the lock held, and now do_register_framebuffer() can just call do_remove_conflicting_framebuffers(), and that in turn can call _do_unregister_framebuffer(), and there is no deadlock, and we can hold the registration lock over the whole sequence, fixing the races. It also makes error cases simpler, and fixes one situation where we would return from unregister_framebuffer() without releasing the lock, pointed out by Bruno Prémont. Tested-by: Bruno Prémont <bonbons@linux-vserver.org> Tested-by: Anca Emanuel <anca.emanuel@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent c47747f commit 712f314

1 file changed

Lines changed: 68 additions & 49 deletions

File tree

‎drivers/video/fbmem.c‎

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,8 +1537,10 @@ static bool fb_do_apertures_overlap(struct apertures_struct *gena,
15371537
return false;
15381538
}
15391539

1540+
static int do_unregister_framebuffer(struct fb_info *fb_info);
1541+
15401542
#define VGA_FB_PHYS 0xA0000
1541-
void remove_conflicting_framebuffers(struct apertures_struct *a,
1543+
static void do_remove_conflicting_framebuffers(struct apertures_struct *a,
15421544
const char *name, bool primary)
15431545
{
15441546
int i;
@@ -1560,24 +1562,12 @@ void remove_conflicting_framebuffers(struct apertures_struct *a,
15601562
printk(KERN_INFO "fb: conflicting fb hw usage "
15611563
"%s vs %s - removing generic driver\n",
15621564
name, registered_fb[i]->fix.id);
1563-
unregister_framebuffer(registered_fb[i]);
1565+
do_unregister_framebuffer(registered_fb[i]);
15641566
}
15651567
}
15661568
}
1567-
EXPORT_SYMBOL(remove_conflicting_framebuffers);
1568-
1569-
/**
1570-
* register_framebuffer - registers a frame buffer device
1571-
* @fb_info: frame buffer info structure
1572-
*
1573-
* Registers a frame buffer device @fb_info.
1574-
*
1575-
* Returns negative errno on error, or zero for success.
1576-
*
1577-
*/
15781569

1579-
int
1580-
register_framebuffer(struct fb_info *fb_info)
1570+
static int do_register_framebuffer(struct fb_info *fb_info)
15811571
{
15821572
int i;
15831573
struct fb_event event;
@@ -1589,10 +1579,9 @@ register_framebuffer(struct fb_info *fb_info)
15891579
if (fb_check_foreignness(fb_info))
15901580
return -ENOSYS;
15911581

1592-
remove_conflicting_framebuffers(fb_info->apertures, fb_info->fix.id,
1582+
do_remove_conflicting_framebuffers(fb_info->apertures, fb_info->fix.id,
15931583
fb_is_primary_device(fb_info));
15941584

1595-
mutex_lock(&registration_lock);
15961585
num_registered_fb++;
15971586
for (i = 0 ; i < FB_MAX; i++)
15981587
if (!registered_fb[i])
@@ -1635,7 +1624,6 @@ register_framebuffer(struct fb_info *fb_info)
16351624
fb_var_to_videomode(&mode, &fb_info->var);
16361625
fb_add_videomode(&mode, &fb_info->modelist);
16371626
registered_fb[i] = fb_info;
1638-
mutex_unlock(&registration_lock);
16391627

16401628
event.info = fb_info;
16411629
if (!lock_fb_info(fb_info))
@@ -1645,48 +1633,23 @@ register_framebuffer(struct fb_info *fb_info)
16451633
return 0;
16461634
}
16471635

1648-
1649-
/**
1650-
* unregister_framebuffer - releases a frame buffer device
1651-
* @fb_info: frame buffer info structure
1652-
*
1653-
* Unregisters a frame buffer device @fb_info.
1654-
*
1655-
* Returns negative errno on error, or zero for success.
1656-
*
1657-
* This function will also notify the framebuffer console
1658-
* to release the driver.
1659-
*
1660-
* This is meant to be called within a driver's module_exit()
1661-
* function. If this is called outside module_exit(), ensure
1662-
* that the driver implements fb_open() and fb_release() to
1663-
* check that no processes are using the device.
1664-
*/
1665-
1666-
int
1667-
unregister_framebuffer(struct fb_info *fb_info)
1636+
static int do_unregister_framebuffer(struct fb_info *fb_info)
16681637
{
16691638
struct fb_event event;
16701639
int i, ret = 0;
16711640

1672-
mutex_lock(&registration_lock);
16731641
i = fb_info->node;
1674-
if (!registered_fb[i]) {
1675-
ret = -EINVAL;
1676-
goto done;
1677-
}
1678-
1642+
if (!registered_fb[i])
1643+
return -EINVAL;
16791644

16801645
if (!lock_fb_info(fb_info))
16811646
return -ENODEV;
16821647
event.info = fb_info;
16831648
ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
16841649
unlock_fb_info(fb_info);
16851650

1686-
if (ret) {
1687-
ret = -EINVAL;
1688-
goto done;
1689-
}
1651+
if (ret)
1652+
return -EINVAL;
16901653

16911654
if (fb_info->pixmap.addr &&
16921655
(fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
@@ -1701,8 +1664,64 @@ unregister_framebuffer(struct fb_info *fb_info)
17011664

17021665
/* this may free fb info */
17031666
put_fb_info(fb_info);
1704-
done:
1667+
return 0;
1668+
}
1669+
1670+
void remove_conflicting_framebuffers(struct apertures_struct *a,
1671+
const char *name, bool primary)
1672+
{
1673+
mutex_lock(&registration_lock);
1674+
do_remove_conflicting_framebuffers(a, name, primary);
17051675
mutex_unlock(&registration_lock);
1676+
}
1677+
EXPORT_SYMBOL(remove_conflicting_framebuffers);
1678+
1679+
/**
1680+
* register_framebuffer - registers a frame buffer device
1681+
* @fb_info: frame buffer info structure
1682+
*
1683+
* Registers a frame buffer device @fb_info.
1684+
*
1685+
* Returns negative errno on error, or zero for success.
1686+
*
1687+
*/
1688+
int
1689+
register_framebuffer(struct fb_info *fb_info)
1690+
{
1691+
int ret;
1692+
1693+
mutex_lock(&registration_lock);
1694+
ret = do_register_framebuffer(fb_info);
1695+
mutex_unlock(&registration_lock);
1696+
1697+
return ret;
1698+
}
1699+
1700+
/**
1701+
* unregister_framebuffer - releases a frame buffer device
1702+
* @fb_info: frame buffer info structure
1703+
*
1704+
* Unregisters a frame buffer device @fb_info.
1705+
*
1706+
* Returns negative errno on error, or zero for success.
1707+
*
1708+
* This function will also notify the framebuffer console
1709+
* to release the driver.
1710+
*
1711+
* This is meant to be called within a driver's module_exit()
1712+
* function. If this is called outside module_exit(), ensure
1713+
* that the driver implements fb_open() and fb_release() to
1714+
* check that no processes are using the device.
1715+
*/
1716+
int
1717+
unregister_framebuffer(struct fb_info *fb_info)
1718+
{
1719+
int ret;
1720+
1721+
mutex_lock(&registration_lock);
1722+
ret = do_unregister_framebuffer(fb_info);
1723+
mutex_unlock(&registration_lock);
1724+
17061725
return ret;
17071726
}
17081727

0 commit comments

Comments
 (0)