diff options
author | Richard Yao <[email protected]> | 2023-01-17 12:57:12 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2023-01-17 09:57:12 -0800 |
commit | 2e7f664f04bae4375ab6bebf232983b2326c9370 (patch) | |
tree | 89b60cbee1de54b1683e989dca12186dc3c39a8c /module/os | |
parent | 60f86a2bfaddd42d75af775503c40943e471bc53 (diff) |
Cleanup of dead code suggested by Clang Static Analyzer (#14380)
I recently gained the ability to run Clang's static analyzer on the
linux kernel modules via a few hacks. This extended coverage to code
that was previously missed since Clang's static analyzer only looked at
code that we built in userspace. Running it against the Linux kernel
modules built from my local branch produced a total of 72 reports
against my local branch. Of those, 50 were reports of logic errors and
22 were reports of dead code. Since we already had cleaned up all of
the previous dead code reports, I felt it would be a good next step to
clean up these dead code reports. Clang did a further breakdown of the
dead code reports into:
Dead assignment 15
Dead increment 2
Dead nested assignment 5
The benefit of cleaning these up, especially in the case of dead nested
assignment, is that they can expose places where our error handling is
incorrect. A number of them were fairly straight forward. However
several were not:
In vdev_disk_physio_completion(), not only were we not using the return
value from the static function vdev_disk_dio_put(), but nothing used it,
so I changed it to return void and removed the existing (void) cast in
the other area where we call it in addition to no longer storing it to a
stack value.
In FSE_createDTable(), the function is dead code. Its helper function
FSE_freeDTable() is also dead code, as are the CPP definitions in
`module/zstd/include/zstd_compat_wrapper.h`. We just delete it all.
In zfs_zevent_wait(), we have an optimization opportunity. cv_wait_sig()
returns 0 if there are waiting signals and 1 if there are none. The
Linux SPL version literally returns `signal_pending(current) ? 0 : 1)`
and FreeBSD implements the same semantics, we can just do
`!cv_wait_sig()` in place of `signal_pending(current)` to avoid
unnecessarily calling it again.
zfs_setattr() on FreeBSD version did not have error handling issue
because the code was removed entirely from FreeBSD version. The error is
from updating the attribute directory's files. After some thought, I
decided to propapage errors on it to userspace.
In zfs_secpolicy_tmp_snapshot(), we ignore a lack of permission from the
first check in favor of checking three other permissions. I assume this
is intentional.
In zfs_create_fs(), the return value of zap_update() was not checked
despite setting an important version number. I see no backward
compatibility reason to permit failures, so we add an assertion to catch
failures. Interestingly, Linux is still using ASSERT(error == 0) from
OpenSolaris while FreeBSD has switched to the improved ASSERT0(error)
from illumos, although illumos has yet to adopt it here. ASSERT(error ==
0) was used on Linux while ASSERT0(error) was used on FreeBSD since the
entire file needs conversion and that should be the subject of
another patch.
dnode_move()'s issue was caused by us not having implemented
POINTER_IS_VALID() on Linux. We have a stub in
`include/os/linux/spl/sys/kmem_cache.h` for it, when it really should be
in `include/os/linux/spl/sys/kmem.h` to be consistent with
Illumos/OpenSolaris. FreeBSD put both `POINTER_IS_VALID()` and
`POINTER_INVALIDATE()` in `include/os/freebsd/spl/sys/kmem.h`, so we
copy what it did.
Whenever a report was in platform-specific code, I checked the FreeBSD
version to see if it also applied to FreeBSD, but it was only relevant a
few times.
Lastly, the patch that enabled Clang's static analyzer to be run on the
Linux kernel modules needs more work before it can be put into a PR. I
plan to do that in the future as part of the on-going static analysis
work that I am doing.
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Richard Yao <[email protected]>
Closes #14380
Diffstat (limited to 'module/os')
-rw-r--r-- | module/os/freebsd/zfs/zfs_acl.c | 1 | ||||
-rw-r--r-- | module/os/freebsd/zfs/zfs_znode.c | 1 | ||||
-rw-r--r-- | module/os/linux/spl/spl-kmem-cache.c | 8 | ||||
-rw-r--r-- | module/os/linux/spl/spl-thread.c | 5 | ||||
-rw-r--r-- | module/os/linux/zfs/vdev_disk.c | 9 | ||||
-rw-r--r-- | module/os/linux/zfs/zfs_acl.c | 3 | ||||
-rw-r--r-- | module/os/linux/zfs/zfs_vnops_os.c | 3 | ||||
-rw-r--r-- | module/os/linux/zfs/zfs_znode.c | 3 |
8 files changed, 10 insertions, 23 deletions
diff --git a/module/os/freebsd/zfs/zfs_acl.c b/module/os/freebsd/zfs/zfs_acl.c index a9318f7a0..9f735dbb5 100644 --- a/module/os/freebsd/zfs/zfs_acl.c +++ b/module/os/freebsd/zfs/zfs_acl.c @@ -2414,7 +2414,6 @@ zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr, * read_acl/read_attributes */ - error = 0; ASSERT3U(working_mode, !=, 0); if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) && diff --git a/module/os/freebsd/zfs/zfs_znode.c b/module/os/freebsd/zfs/zfs_znode.c index 957530243..e691e60a8 100644 --- a/module/os/freebsd/zfs/zfs_znode.c +++ b/module/os/freebsd/zfs/zfs_znode.c @@ -1708,6 +1708,7 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) } ASSERT3U(version, !=, 0); error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); + ASSERT0(error); /* * Create zap object used for SA attribute registration diff --git a/module/os/linux/spl/spl-kmem-cache.c b/module/os/linux/spl/spl-kmem-cache.c index 4cceeb8bd..963e7a1ec 100644 --- a/module/os/linux/spl/spl-kmem-cache.c +++ b/module/os/linux/spl/spl-kmem-cache.c @@ -791,10 +791,8 @@ spl_kmem_cache_create(const char *name, size_t size, size_t align, } else { unsigned long slabflags = 0; - if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) { - rc = EINVAL; + if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) goto out; - } #if defined(SLAB_USERCOPY) /* @@ -815,10 +813,8 @@ spl_kmem_cache_create(const char *name, size_t size, size_t align, skc->skc_linux_cache = kmem_cache_create( skc->skc_name, size, align, slabflags, NULL); #endif - if (skc->skc_linux_cache == NULL) { - rc = ENOMEM; + if (skc->skc_linux_cache == NULL) goto out; - } } down_write(&spl_kmem_cache_sem); diff --git a/module/os/linux/spl/spl-thread.c b/module/os/linux/spl/spl-thread.c index b863945a1..b4ef86a5e 100644 --- a/module/os/linux/spl/spl-thread.c +++ b/module/os/linux/spl/spl-thread.c @@ -178,12 +178,11 @@ issig(int why) sigorsets(&set, &task->blocked, &set); spin_lock_irq(&task->sighand->siglock); - int ret; #ifdef HAVE_DEQUEUE_SIGNAL_4ARG enum pid_type __type; - if ((ret = dequeue_signal(task, &set, &__info, &__type)) != 0) { + if (dequeue_signal(task, &set, &__info, &__type) != 0) { #else - if ((ret = dequeue_signal(task, &set, &__info)) != 0) { + if (dequeue_signal(task, &set, &__info) != 0) { #endif #ifdef HAVE_SIGNAL_STOP spin_unlock_irq(&task->sighand->siglock); diff --git a/module/os/linux/zfs/vdev_disk.c b/module/os/linux/zfs/vdev_disk.c index 2b5c4956d..925ee9d9f 100644 --- a/module/os/linux/zfs/vdev_disk.c +++ b/module/os/linux/zfs/vdev_disk.c @@ -425,7 +425,7 @@ vdev_disk_dio_get(dio_request_t *dr) atomic_inc(&dr->dr_ref); } -static int +static void vdev_disk_dio_put(dio_request_t *dr) { int rc = atomic_dec_return(&dr->dr_ref); @@ -449,14 +449,11 @@ vdev_disk_dio_put(dio_request_t *dr) zio_delay_interrupt(zio); } } - - return (rc); } BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error) { dio_request_t *dr = bio->bi_private; - int rc; if (dr->dr_error == 0) { #ifdef HAVE_1ARG_BIO_END_IO_T @@ -470,7 +467,7 @@ BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error) } /* Drop reference acquired by __vdev_disk_physio */ - rc = vdev_disk_dio_put(dr); + vdev_disk_dio_put(dr); } static inline void @@ -742,7 +739,7 @@ retry: if (dr->dr_bio_count > 1) blk_finish_plug(&plug); - (void) vdev_disk_dio_put(dr); + vdev_disk_dio_put(dr); return (error); } diff --git a/module/os/linux/zfs/zfs_acl.c b/module/os/linux/zfs/zfs_acl.c index 75638399e..db1bb9577 100644 --- a/module/os/linux/zfs/zfs_acl.c +++ b/module/os/linux/zfs/zfs_acl.c @@ -2581,7 +2581,6 @@ zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr) } if (uid == KUID_TO_SUID(ZTOI(zdp)->i_uid)) { - owner = B_TRUE; if (zdp->z_mode & S_IXUSR) { mutex_exit(&zdp->z_acl_lock); return (0); @@ -2591,7 +2590,6 @@ zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr) } } if (groupmember(KGID_TO_SGID(ZTOI(zdp)->i_gid), cr)) { - groupmbr = B_TRUE; if (zdp->z_mode & S_IXGRP) { mutex_exit(&zdp->z_acl_lock); return (0); @@ -2720,7 +2718,6 @@ zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr, * read_acl/read_attributes */ - error = 0; ASSERT(working_mode != 0); if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) && diff --git a/module/os/linux/zfs/zfs_vnops_os.c b/module/os/linux/zfs/zfs_vnops_os.c index a94af0ea3..47f132a38 100644 --- a/module/os/linux/zfs/zfs_vnops_os.c +++ b/module/os/linux/zfs/zfs_vnops_os.c @@ -721,7 +721,6 @@ top: if (have_acl) zfs_acl_ids_free(&acl_ids); - have_acl = B_FALSE; /* * A directory entry already exists for this name. @@ -2532,7 +2531,7 @@ out: dmu_tx_commit(tx); if (attrzp) { if (err2 == 0 && handle_eadir) - err2 = zfs_setattr_dir(attrzp); + err = zfs_setattr_dir(attrzp); zrele(attrzp); } zfs_znode_update_vfs(zp); diff --git a/module/os/linux/zfs/zfs_znode.c b/module/os/linux/zfs/zfs_znode.c index d673da384..395e2cf57 100644 --- a/module/os/linux/zfs/zfs_znode.c +++ b/module/os/linux/zfs/zfs_znode.c @@ -495,13 +495,11 @@ zfs_set_inode_flags(znode_t *zp, struct inode *ip) void zfs_znode_update_vfs(znode_t *zp) { - zfsvfs_t *zfsvfs; struct inode *ip; uint32_t blksize; u_longlong_t i_blocks; ASSERT(zp != NULL); - zfsvfs = ZTOZSB(zp); ip = ZTOI(zp); /* Skip .zfs control nodes which do not exist on disk. */ @@ -1885,6 +1883,7 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) } ASSERT(version != 0); error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); + ASSERT(error == 0); /* * Create zap object used for SA attribute registration |