Skip to content

Commit 2e7f664

Browse files
authored
Cleanup of dead code suggested by Clang Static Analyzer (openzfs#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 openzfs#14380
1 parent 60f86a2 commit 2e7f664

File tree

16 files changed

+22
-51
lines changed

16 files changed

+22
-51
lines changed

include/os/linux/spl/sys/kmem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ extern void kmem_strfree(char *str);
4040

4141
#define kmem_scnprintf scnprintf
4242

43+
#define POINTER_IS_VALID(p) (!((uintptr_t)(p) & 0x3))
44+
#define POINTER_INVALIDATE(pp) (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
45+
4346
/*
4447
* Memory allocation interfaces
4548
*/

include/os/linux/spl/sys/kmem_cache.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ extern struct rw_semaphore spl_kmem_cache_sem;
9898
#define SPL_MAX_KMEM_ORDER_NR_PAGES (KMALLOC_MAX_SIZE >> PAGE_SHIFT)
9999
#endif
100100

101-
#define POINTER_IS_VALID(p) 0 /* Unimplemented */
102-
#define POINTER_INVALIDATE(pp) /* Unimplemented */
103-
104101
typedef int (*spl_kmem_ctor_t)(void *, void *, int);
105102
typedef void (*spl_kmem_dtor_t)(void *, void *);
106103

module/os/freebsd/zfs/zfs_acl.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,6 @@ zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr,
24142414
* read_acl/read_attributes
24152415
*/
24162416

2417-
error = 0;
24182417
ASSERT3U(working_mode, !=, 0);
24192418

24202419
if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&

module/os/freebsd/zfs/zfs_znode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,7 @@ zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
17081708
}
17091709
ASSERT3U(version, !=, 0);
17101710
error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1711+
ASSERT0(error);
17111712

17121713
/*
17131714
* Create zap object used for SA attribute registration

module/os/linux/spl/spl-kmem-cache.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,8 @@ spl_kmem_cache_create(const char *name, size_t size, size_t align,
791791
} else {
792792
unsigned long slabflags = 0;
793793

794-
if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE)) {
795-
rc = EINVAL;
794+
if (size > (SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE))
796795
goto out;
797-
}
798796

799797
#if defined(SLAB_USERCOPY)
800798
/*
@@ -815,10 +813,8 @@ spl_kmem_cache_create(const char *name, size_t size, size_t align,
815813
skc->skc_linux_cache = kmem_cache_create(
816814
skc->skc_name, size, align, slabflags, NULL);
817815
#endif
818-
if (skc->skc_linux_cache == NULL) {
819-
rc = ENOMEM;
816+
if (skc->skc_linux_cache == NULL)
820817
goto out;
821-
}
822818
}
823819

824820
down_write(&spl_kmem_cache_sem);

module/os/linux/spl/spl-thread.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,11 @@ issig(int why)
178178
sigorsets(&set, &task->blocked, &set);
179179

180180
spin_lock_irq(&task->sighand->siglock);
181-
int ret;
182181
#ifdef HAVE_DEQUEUE_SIGNAL_4ARG
183182
enum pid_type __type;
184-
if ((ret = dequeue_signal(task, &set, &__info, &__type)) != 0) {
183+
if (dequeue_signal(task, &set, &__info, &__type) != 0) {
185184
#else
186-
if ((ret = dequeue_signal(task, &set, &__info)) != 0) {
185+
if (dequeue_signal(task, &set, &__info) != 0) {
187186
#endif
188187
#ifdef HAVE_SIGNAL_STOP
189188
spin_unlock_irq(&task->sighand->siglock);

module/os/linux/zfs/vdev_disk.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ vdev_disk_dio_get(dio_request_t *dr)
425425
atomic_inc(&dr->dr_ref);
426426
}
427427

428-
static int
428+
static void
429429
vdev_disk_dio_put(dio_request_t *dr)
430430
{
431431
int rc = atomic_dec_return(&dr->dr_ref);
@@ -449,14 +449,11 @@ vdev_disk_dio_put(dio_request_t *dr)
449449
zio_delay_interrupt(zio);
450450
}
451451
}
452-
453-
return (rc);
454452
}
455453

456454
BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error)
457455
{
458456
dio_request_t *dr = bio->bi_private;
459-
int rc;
460457

461458
if (dr->dr_error == 0) {
462459
#ifdef HAVE_1ARG_BIO_END_IO_T
@@ -470,7 +467,7 @@ BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error)
470467
}
471468

472469
/* Drop reference acquired by __vdev_disk_physio */
473-
rc = vdev_disk_dio_put(dr);
470+
vdev_disk_dio_put(dr);
474471
}
475472

476473
static inline void
@@ -742,7 +739,7 @@ __vdev_disk_physio(struct block_device *bdev, zio_t *zio,
742739
if (dr->dr_bio_count > 1)
743740
blk_finish_plug(&plug);
744741

745-
(void) vdev_disk_dio_put(dr);
742+
vdev_disk_dio_put(dr);
746743

747744
return (error);
748745
}

module/os/linux/zfs/zfs_acl.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,6 @@ zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr)
25812581
}
25822582

25832583
if (uid == KUID_TO_SUID(ZTOI(zdp)->i_uid)) {
2584-
owner = B_TRUE;
25852584
if (zdp->z_mode & S_IXUSR) {
25862585
mutex_exit(&zdp->z_acl_lock);
25872586
return (0);
@@ -2591,7 +2590,6 @@ zfs_fastaccesschk_execute(znode_t *zdp, cred_t *cr)
25912590
}
25922591
}
25932592
if (groupmember(KGID_TO_SGID(ZTOI(zdp)->i_gid), cr)) {
2594-
groupmbr = B_TRUE;
25952593
if (zdp->z_mode & S_IXGRP) {
25962594
mutex_exit(&zdp->z_acl_lock);
25972595
return (0);
@@ -2720,7 +2718,6 @@ zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr,
27202718
* read_acl/read_attributes
27212719
*/
27222720

2723-
error = 0;
27242721
ASSERT(working_mode != 0);
27252722

27262723
if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) &&

module/os/linux/zfs/zfs_vnops_os.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ zfs_create(znode_t *dzp, char *name, vattr_t *vap, int excl,
721721

722722
if (have_acl)
723723
zfs_acl_ids_free(&acl_ids);
724-
have_acl = B_FALSE;
725724

726725
/*
727726
* A directory entry already exists for this name.
@@ -2532,7 +2531,7 @@ zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr, zuserns_t *mnt_ns)
25322531
dmu_tx_commit(tx);
25332532
if (attrzp) {
25342533
if (err2 == 0 && handle_eadir)
2535-
err2 = zfs_setattr_dir(attrzp);
2534+
err = zfs_setattr_dir(attrzp);
25362535
zrele(attrzp);
25372536
}
25382537
zfs_znode_update_vfs(zp);

module/os/linux/zfs/zfs_znode.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,11 @@ zfs_set_inode_flags(znode_t *zp, struct inode *ip)
495495
void
496496
zfs_znode_update_vfs(znode_t *zp)
497497
{
498-
zfsvfs_t *zfsvfs;
499498
struct inode *ip;
500499
uint32_t blksize;
501500
u_longlong_t i_blocks;
502501

503502
ASSERT(zp != NULL);
504-
zfsvfs = ZTOZSB(zp);
505503
ip = ZTOI(zp);
506504

507505
/* 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)
18851883
}
18861884
ASSERT(version != 0);
18871885
error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1886+
ASSERT(error == 0);
18881887

18891888
/*
18901889
* Create zap object used for SA attribute registration

module/zcommon/zfs_fletcher.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ fletcher_4_kstat_data(char *buf, size_t size, void *data)
628628
off += snprintf(buf + off, size - off, "%-17s", "fastest");
629629
off += snprintf(buf + off, size - off, "%-15s",
630630
fletcher_4_supp_impls[fastest_stat->native]->name);
631-
off += snprintf(buf + off, size - off, "%-15s\n",
631+
(void) snprintf(buf + off, size - off, "%-15s\n",
632632
fletcher_4_supp_impls[fastest_stat->byteswap]->name);
633633
} else {
634634
ptrdiff_t id = curr_stat - fletcher_4_stat_data;
@@ -637,7 +637,7 @@ fletcher_4_kstat_data(char *buf, size_t size, void *data)
637637
fletcher_4_supp_impls[id]->name);
638638
off += snprintf(buf + off, size - off, "%-15llu",
639639
(u_longlong_t)curr_stat->native);
640-
off += snprintf(buf + off, size - off, "%-15llu\n",
640+
(void) snprintf(buf + off, size - off, "%-15llu\n",
641641
(u_longlong_t)curr_stat->byteswap);
642642
}
643643

module/zfs/fm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ zfs_zevent_wait(zfs_zevent_t *ze)
380380
break;
381381
}
382382

383-
error = cv_wait_sig(&zevent_cv, &zevent_lock);
384-
if (signal_pending(current)) {
383+
if (cv_wait_sig(&zevent_cv, &zevent_lock) == 0) {
385384
error = SET_ERROR(EINTR);
386385
break;
387386
} else if (!list_is_empty(&zevent_list)) {

module/zfs/zfs_ioctl.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
10811081
(void) innvl;
10821082
int error;
10831083

1084-
if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0)
1084+
if (secpolicy_sys_config(cr, B_FALSE) == 0)
10851085
return (0);
10861086

10871087
error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr);
@@ -1230,8 +1230,8 @@ zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
12301230
*/
12311231
int error;
12321232

1233-
if ((error = zfs_secpolicy_write_perms(zc->zc_name,
1234-
ZFS_DELEG_PERM_DIFF, cr)) == 0)
1233+
if (zfs_secpolicy_write_perms(zc->zc_name,
1234+
ZFS_DELEG_PERM_DIFF, cr) == 0)
12351235
return (0);
12361236

12371237
error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr);
@@ -1279,8 +1279,7 @@ get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp)
12791279

12801280
packed = vmem_alloc(size, KM_SLEEP);
12811281

1282-
if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size,
1283-
iflag)) != 0) {
1282+
if (ddi_copyin((void *)(uintptr_t)nvl, packed, size, iflag) != 0) {
12841283
vmem_free(packed, size);
12851284
return (SET_ERROR(EFAULT));
12861285
}
@@ -2682,7 +2681,6 @@ zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
26822681
pair = NULL;
26832682
while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
26842683
const char *propname = nvpair_name(pair);
2685-
err = 0;
26862684

26872685
propval = pair;
26882686
if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
@@ -3095,7 +3093,7 @@ zfs_ioc_set_fsacl(zfs_cmd_t *zc)
30953093
/*
30963094
* Verify nvlist is constructed correctly
30973095
*/
3098-
if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) {
3096+
if (zfs_deleg_verify_nvlist(fsaclnv) != 0) {
30993097
nvlist_free(fsaclnv);
31003098
return (SET_ERROR(EINVAL));
31013099
}

module/zfs/zvol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ zvol_create_minors_cb(const char *dsname, void *arg)
10761076
* traverse snapshots only, do not traverse children,
10771077
* and skip the 'dsname'
10781078
*/
1079-
error = dmu_objset_find(dsname,
1079+
(void) dmu_objset_find(dsname,
10801080
zvol_create_snap_minor_cb, (void *)job,
10811081
DS_FIND_SNAPSHOTS);
10821082
}

module/zstd/include/zstd_compat_wrapper.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@
7373
#define FSE_buildDTable_raw zfs_FSE_buildDTable_raw
7474
#define FSE_buildDTable_rle zfs_FSE_buildDTable_rle
7575
#define FSE_buildDTable zfs_FSE_buildDTable
76-
#define FSE_createDTable zfs_FSE_createDTable
7776
#define FSE_decompress_usingDTable zfs_FSE_decompress_usingDTable
7877
#define FSE_decompress_wksp zfs_FSE_decompress_wksp
7978
#define FSE_decompress zfs_FSE_decompress
80-
#define FSE_freeDTable zfs_FSE_freeDTable
8179

8280
/* lib/common/pool.o: */
8381
#define POOL_add zfs_POOL_add

module/zstd/lib/common/fse_decompress.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@
5656

5757

5858
/* Function templates */
59-
FSE_DTable* FSE_createDTable (unsigned tableLog)
60-
{
61-
if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
62-
return (FSE_DTable*)malloc( FSE_DTABLE_SIZE_U32(tableLog) * sizeof (U32) );
63-
}
64-
65-
void FSE_freeDTable (FSE_DTable* dt)
66-
{
67-
free(dt);
68-
}
69-
7059
size_t FSE_buildDTable(FSE_DTable* dt, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
7160
{
7261
void* const tdPtr = dt+1; /* because *dt is unsigned, 32-bits aligned on 32-bits */

0 commit comments

Comments
 (0)