Bug 238168 - kern/kern_conf.c: two 'if' statements with identical conditional expressions
Summary: kern/kern_conf.c: two 'if' statements with identical conditional expressions
Status: Closed Not A Bug
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords: needs-patch, needs-qa
Depends on:
Blocks:
 
Reported: 2019-05-27 09:29 UTC by Alexey Dokuchaev
Modified: 2019-05-27 14:31 UTC (History)
1 user (show)

See Also:
koobs: mfc-stable12?
koobs: mfc-stable11?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey Dokuchaev freebsd_committer freebsd_triage 2019-05-27 09:29:39 UTC
PVS Studio reports: /usr/src/sys/kern/kern_conf.c:627:1: warning: V649 There are two 'if' statements with identical conditional expressions. The first 'if' statement contains function return. This means that the second 'if' statement is senseless. Check lines: 616, 627.

>        if (devsw->d_flags & D_INIT)
>                return (0);
         if (devsw->d_flags & D_NEEDGIANT) {
                 dev_unlock();
                 dsw2 = malloc(sizeof *dsw2, M_DEVT,
                      (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK);
                 dev_lock();
                 if (dsw2 == NULL && !(devsw->d_flags & D_INIT))
                         return (ENOMEM);
         } else
                 dsw2 = NULL;
>        if (devsw->d_flags & D_INIT) {
>                if (dsw2 != NULL)
>                        cdevsw_free_devlocked(dsw2);
>                return (0);
         }

This code appeared in base r177301.
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2019-05-27 11:32:43 UTC
The D_INIT flag might be set by another thread while we unlocked dev_mtx in the D_NEEDGIANT block.  This is a common kernel pattern, unlock, do sleepable resource allocation, relock, and then recheck the original condition which might be invalidated by other thread meantime.
Comment 2 Alexey Dokuchaev freebsd_committer freebsd_triage 2019-05-27 14:31:39 UTC
Thank you for very nice explanation Kostic!  I'll try to remember this pattern in the future.