]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Replace P2ALIGN with P2ALIGN_TYPED and delete P2ALIGN.
authorchenqiuhao1997 <haohao0924@126.com>
Fri, 10 May 2024 15:47:21 +0000 (23:47 +0800)
committerGitHub <noreply@github.com>
Fri, 10 May 2024 15:47:21 +0000 (08:47 -0700)
In P2ALIGN, the result would be incorrect when align is unsigned
integer and x is larger than max value of the type of align.
In that case, -(align) would be a positive integer, which means
high bits would be zero and finally stay zero after '&' when
align is converted to a larger integer type.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Youzhong Yang <yyang@mathworks.com>
Signed-off-by: Qiuhao Chen <chenqiuhao1997@gmail.com>
Closes #15940

17 files changed:
cmd/zdb/zdb.c
cmd/ztest.c
include/os/freebsd/spl/sys/ccompile.h
include/os/freebsd/spl/sys/sysmacros.h
include/os/linux/spl/sys/sysmacros.h
lib/libefi/rdwr_efi.c
lib/libspl/include/os/linux/sys/sysmacros.h
lib/libzfs/os/linux/libzfs_pool_os.c
module/os/freebsd/zfs/vdev_geom.c
module/os/linux/zfs/zvol_os.c
module/zcommon/zfs_fletcher.c
module/zfs/btree.c
module/zfs/dmu.c
module/zfs/dmu_object.c
module/zfs/metaslab.c
module/zfs/vdev.c
module/zfs/vdev_raidz.c

index ce80c0aa59066ae9d8d614f1248c5d6cccbca939..797ae34b6e5357605eab21edd70d617f2a11cf88 100644 (file)
@@ -5143,7 +5143,7 @@ dump_label(const char *dev)
            sizeof (cksum_record_t), offsetof(cksum_record_t, link));
 
        psize = statbuf.st_size;
-       psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
+       psize = P2ALIGN_TYPED(psize, sizeof (vdev_label_t), uint64_t);
        ashift = SPA_MINBLOCKSHIFT;
 
        /*
index b0fea8b3cfb4a3929835ce6e598213d6f0bd64b9..56eb01618c2e64913fd8392809cf5c8d96c4b443 100644 (file)
@@ -2529,7 +2529,7 @@ ztest_get_data(void *arg, uint64_t arg2, lr_write_t *lr, char *buf,
                ASSERT3P(zio, !=, NULL);
                size = doi.doi_data_block_size;
                if (ISP2(size)) {
-                       offset = P2ALIGN(offset, size);
+                       offset = P2ALIGN_TYPED(offset, size, uint64_t);
                } else {
                        ASSERT3U(offset, <, size);
                        offset = 0;
@@ -3978,7 +3978,8 @@ raidz_scratch_verify(void)
        raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
        offset = RRSS_GET_OFFSET(&spa->spa_uberblock);
        state = RRSS_GET_STATE(&spa->spa_uberblock);
-       write_size = P2ALIGN(VDEV_BOOT_SIZE, 1 << raidvd->vdev_ashift);
+       write_size = P2ALIGN_TYPED(VDEV_BOOT_SIZE, 1 << raidvd->vdev_ashift,
+           uint64_t);
        logical_size = write_size * raidvd->vdev_children;
 
        switch (state) {
@@ -5016,7 +5017,8 @@ ztest_dmu_object_next_chunk(ztest_ds_t *zd, uint64_t id)
         */
        mutex_enter(&os->os_obj_lock);
        object = ztest_random(os->os_obj_next_chunk);
-       os->os_obj_next_chunk = P2ALIGN(object, dnodes_per_chunk);
+       os->os_obj_next_chunk = P2ALIGN_TYPED(object, dnodes_per_chunk,
+           uint64_t);
        mutex_exit(&os->os_obj_lock);
 }
 
@@ -6638,7 +6640,8 @@ ztest_fault_inject(ztest_ds_t *zd, uint64_t id)
                 * the end of the disk (vdev_psize) is aligned to
                 * sizeof (vdev_label_t).
                 */
-               uint64_t psize = P2ALIGN(fsize, sizeof (vdev_label_t));
+               uint64_t psize = P2ALIGN_TYPED(fsize, sizeof (vdev_label_t),
+                   uint64_t);
                if ((leaf & 1) == 1 &&
                    offset + sizeof (bad) > psize - VDEV_LABEL_END_SIZE)
                        continue;
@@ -6962,8 +6965,8 @@ ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
                                size_t inc = 64 * ztest_random(size / 67);
                                /* sometimes add few bytes to test non-simd */
                                if (ztest_random(100) < 10)
-                                       inc += P2ALIGN(ztest_random(64),
-                                           sizeof (uint32_t));
+                                       inc += P2ALIGN_TYPED(ztest_random(64),
+                                           sizeof (uint32_t), uint64_t);
 
                                if (inc > (size - pos))
                                        inc = size - pos;
index 26cf4db87aea8fe3f6b867ddedf5aa9ee978db87..bebeb0db24526159325e4811503d643cc7efb820 100644 (file)
@@ -138,7 +138,8 @@ typedef int enum_t;
 #define        readdir64 readdir
 #define        dirent64 dirent
 #endif
-#define        P2ALIGN(x, align)               ((x) & -(align))
+// Deprecated. Use P2ALIGN_TYPED instead.
+// #define     P2ALIGN(x, align)               ((x) & -(align))
 #define        P2CROSS(x, y, align)    (((x) ^ (y)) > (align) - 1)
 #define        P2ROUNDUP(x, align)             ((((x) - 1) | ((align) - 1)) + 1)
 #define        P2PHASE(x, align)               ((x) & ((align) - 1))
index 3e8841ae66bd9498b15b99da959071b52fee030b..2c9f4438d769bef172b68d331136dda127672509 100644 (file)
@@ -191,7 +191,8 @@ extern unsigned char bcd_to_byte[256];
  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
  */
-#define        P2ALIGN(x, align)               ((x) & -(align))
+// Deprecated. Use P2ALIGN_TYPED instead.
+// #define     P2ALIGN(x, align)               ((x) & -(align))
 
 /*
  * return x % (mod) align
index 99e3a6fb41c634f018c90404f310a9eb8c68e2f0..0e8390736309379d9c1e53dc363c73b389782c58 100644 (file)
@@ -159,7 +159,8 @@ makedev(unsigned int major, unsigned int minor)
 /*
  * Compatibility macros/typedefs needed for Solaris -> Linux port
  */
-#define        P2ALIGN(x, align)       ((x) & -(align))
+// Deprecated. Use P2ALIGN_TYPED instead.
+// #define     P2ALIGN(x, align)       ((x) & -(align))
 #define        P2CROSS(x, y, align)    (((x) ^ (y)) > (align) - 1)
 #define        P2ROUNDUP(x, align)     ((((x) - 1) | ((align) - 1)) + 1)
 #define        P2PHASE(x, align)       ((x) & ((align) - 1))
index 739219e0410fcbbb65f73f0403d5afa283716fae..63c91059ae5fc37ad0c7bac307c83a297ed317b5 100644 (file)
@@ -1175,8 +1175,8 @@ efi_use_whole_disk(int fd)
         * (for performance reasons). The alignment should match the
         * alignment used by the "zpool_label_disk" function.
         */
-       limit = P2ALIGN(efi_label->efi_last_lba - nblocks - EFI_MIN_RESV_SIZE,
-           PARTITION_END_ALIGNMENT);
+       limit = P2ALIGN_TYPED(efi_label->efi_last_lba - nblocks -
+           EFI_MIN_RESV_SIZE, PARTITION_END_ALIGNMENT, diskaddr_t);
        if (data_start + data_size != limit || resv_start != limit)
                sync_needed = B_TRUE;
 
index 5765ee25c6cb99c569ca57de6d52c4ac9fc31d33..26e1c87a35af169ffc8d37e09e5dbb0678a8bc11 100644 (file)
@@ -52,7 +52,8 @@
 /*
  * Compatibility macros/typedefs needed for Solaris -> Linux port
  */
-#define        P2ALIGN(x, align)       ((x) & -(align))
+// Deprecated. Use P2ALIGN_TYPED instead.
+// #define     P2ALIGN(x, align)       ((x) & -(align))
 #define        P2CROSS(x, y, align)    (((x) ^ (y)) > (align) - 1)
 #define        P2ROUNDUP(x, align)     ((((x) - 1) | ((align) - 1)) + 1)
 #define        P2BOUNDARY(off, len, align) \
index 86eef3255bc2f1723f8c61e60ed4764b5ea41e8a..7b18e31c8674db885a91e1f12ec213d76f56151d 100644 (file)
@@ -268,7 +268,8 @@ zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
        if (start_block == MAXOFFSET_T)
                start_block = NEW_START_BLOCK;
        slice_size -= start_block;
-       slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
+       slice_size = P2ALIGN_TYPED(slice_size, PARTITION_END_ALIGNMENT,
+           uint64_t);
 
        vtoc->efi_parts[0].p_start = start_block;
        vtoc->efi_parts[0].p_size = slice_size;
index 9d88971919db148015af6282d39b2634573693a0..38c1d8e9e4641842f784ac16d018358ad0323faa 100644 (file)
@@ -457,7 +457,7 @@ vdev_geom_read_config(struct g_consumer *cp, nvlist_t **configp)
        ZFS_LOG(1, "Reading config from %s...", pp->name);
 
        psize = pp->mediasize;
-       psize = P2ALIGN(psize, (uint64_t)sizeof (vdev_label_t));
+       psize = P2ALIGN_TYPED(psize, sizeof (vdev_label_t), uint64_t);
 
        size = sizeof (*vdev_lists[0]) + pp->sectorsize -
            ((sizeof (*vdev_lists[0]) - 1) % pp->sectorsize) - 1;
index 3012423e9f2ac94c3fee9be39cc96fb383cf1960..3e020e532263799fcb0beb3ba89b2331ab81b96b 100644 (file)
@@ -384,7 +384,7 @@ zvol_discard(zv_request_t *zvr)
         */
        if (!io_is_secure_erase(bio, rq)) {
                start = P2ROUNDUP(start, zv->zv_volblocksize);
-               end = P2ALIGN(end, zv->zv_volblocksize);
+               end = P2ALIGN_TYPED(end, zv->zv_volblocksize, uint64_t);
                size = end - start;
        }
 
index 619ddef0243ad51df7d3c35e3995205b0aec2f7d..74b8c2a475a12c7a931f71f956a66df8fd48cef0 100644 (file)
@@ -471,7 +471,8 @@ fletcher_4_native(const void *buf, uint64_t size,
     const void *ctx_template, zio_cksum_t *zcp)
 {
        (void) ctx_template;
-       const uint64_t p2size = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
+       const uint64_t p2size = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE,
+           uint64_t);
 
        ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
 
@@ -519,7 +520,8 @@ fletcher_4_byteswap(const void *buf, uint64_t size,
     const void *ctx_template, zio_cksum_t *zcp)
 {
        (void) ctx_template;
-       const uint64_t p2size = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
+       const uint64_t p2size = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE,
+           uint64_t);
 
        ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
 
@@ -878,7 +880,7 @@ abd_fletcher_4_iter(void *data, size_t size, void *private)
        fletcher_4_ctx_t *ctx = cdp->acd_ctx;
        fletcher_4_ops_t *ops = (fletcher_4_ops_t *)cdp->acd_private;
        boolean_t native = cdp->acd_byteorder == ZIO_CHECKSUM_NATIVE;
-       uint64_t asize = P2ALIGN(size, FLETCHER_MIN_SIMD_SIZE);
+       uint64_t asize = P2ALIGN_TYPED(size, FLETCHER_MIN_SIMD_SIZE, uint64_t);
 
        ASSERT(IS_P2ALIGNED(size, sizeof (uint32_t)));
 
index af2b94a850be063de9c8452dd32d0cac8e05c250..9c52083603f1c60532f7a69a806e4251a0a51a23 100644 (file)
@@ -218,7 +218,7 @@ zfs_btree_create_custom(zfs_btree_t *tree,
            zfs_btree_find_in_buf : bt_find_in_buf;
        tree->bt_elem_size = size;
        tree->bt_leaf_size = lsize;
-       tree->bt_leaf_cap = P2ALIGN(esize / size, 2);
+       tree->bt_leaf_cap = P2ALIGN_TYPED(esize / size, 2, size_t);
        tree->bt_height = -1;
        tree->bt_bulk = NULL;
 }
index 6ef149aab9a67364a4a5f4f9b92cd33ea89d8775..8b440aafba43b11ab4908da9fdc3f50da249b07f 100644 (file)
@@ -537,7 +537,8 @@ dmu_buf_hold_array_by_dnode(dnode_t *dn, uint64_t offset, uint64_t length,
        if (dn->dn_datablkshift) {
                int blkshift = dn->dn_datablkshift;
                nblks = (P2ROUNDUP(offset + length, 1ULL << blkshift) -
-                   P2ALIGN(offset, 1ULL << blkshift)) >> blkshift;
+                   P2ALIGN_TYPED(offset, 1ULL << blkshift, uint64_t))
+                   >> blkshift;
        } else {
                if (offset + length > dn->dn_datablksz) {
                        zfs_panic_recover("zfs: accessing past end of object "
@@ -854,7 +855,7 @@ get_next_chunk(dnode_t *dn, uint64_t *start, uint64_t minimum, uint64_t *l1blks)
                }
 
                /* set start to the beginning of this L1 indirect */
-               *start = P2ALIGN(*start, iblkrange);
+               *start = P2ALIGN_TYPED(*start, iblkrange, uint64_t);
        }
        if (*start < minimum)
                *start = minimum;
index d0e39a423bb0c2335f955ac3ed134b6cf0ee2a8d..56986ea4344629c8f85768e2d002028644f3f418 100644 (file)
@@ -160,7 +160,7 @@ dmu_object_alloc_impl(objset_t *os, dmu_object_type_t ot, int blocksize,
                         * is not suitably aligned.
                         */
                        os->os_obj_next_chunk =
-                           P2ALIGN(object, dnodes_per_chunk) +
+                           P2ALIGN_TYPED(object, dnodes_per_chunk, uint64_t) +
                            dnodes_per_chunk;
                        (void) atomic_swap_64(cpuobj, object);
                        mutex_exit(&os->os_obj_lock);
index 9e762357b7270775564762ea117e75791cd1f986..cb004930d287a7e4075f203ba37806ea6ace2907 100644 (file)
@@ -627,8 +627,8 @@ metaslab_class_expandable_space(metaslab_class_t *mc)
                 * metaslabs. We report the expandable space in terms
                 * of the metaslab size since that's the unit of expansion.
                 */
-               space += P2ALIGN(tvd->vdev_max_asize - tvd->vdev_asize,
-                   1ULL << tvd->vdev_ms_shift);
+               space += P2ALIGN_TYPED(tvd->vdev_max_asize - tvd->vdev_asize,
+                   1ULL << tvd->vdev_ms_shift, uint64_t);
        }
        spa_config_exit(mc->mc_spa, SCL_VDEV, FTAG);
        return (space);
index c5551eb6cf6e6d189083e29eed52432e56907324..414bf84f6f7ae83e4651e4f9bff81d58264d26a4 100644 (file)
@@ -348,7 +348,8 @@ vdev_get_min_asize(vdev_t *vd)
         * to the nearest metaslab.
         */
        if (vd == vd->vdev_top)
-               return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
+               return (P2ALIGN_TYPED(vd->vdev_asize, 1ULL << vd->vdev_ms_shift,
+                   uint64_t));
 
        return (pvd->vdev_ops->vdev_op_min_asize(pvd));
 }
@@ -2115,8 +2116,8 @@ vdev_open(vdev_t *vd)
                }
        }
 
-       osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
-       max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
+       osize = P2ALIGN_TYPED(osize, sizeof (vdev_label_t), uint64_t);
+       max_osize = P2ALIGN_TYPED(max_osize, sizeof (vdev_label_t), uint64_t);
 
        if (vd->vdev_children == 0) {
                if (osize < SPA_MINDEVSIZE) {
@@ -4764,9 +4765,9 @@ vdev_get_stats_ex(vdev_t *vd, vdev_stat_t *vs, vdev_stat_ex_t *vsx)
                 * can expand.
                 */
                if (vd->vdev_aux == NULL && tvd != NULL) {
-                       vs->vs_esize = P2ALIGN(
+                       vs->vs_esize = P2ALIGN_TYPED(
                            vd->vdev_max_asize - vd->vdev_asize,
-                           1ULL << tvd->vdev_ms_shift);
+                           1ULL << tvd->vdev_ms_shift, uint64_t);
                }
 
                vs->vs_configured_ashift = vd->vdev_top != NULL
index de7d0fa794780d9628184dfc032a943a8be8918d..15c8b8ca6016f5bfd3f40342e6ec8809f5e119a2 100644 (file)
@@ -4039,7 +4039,8 @@ raidz_reflow_scratch_sync(void *arg, dmu_tx_t *tx)
        spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
        vdev_t *raidvd = vdev_lookup_top(spa, vre->vre_vdev_id);
        int ashift = raidvd->vdev_ashift;
-       uint64_t write_size = P2ALIGN(VDEV_BOOT_SIZE, 1 << ashift);
+       uint64_t write_size = P2ALIGN_TYPED(VDEV_BOOT_SIZE, 1 << ashift,
+           uint64_t);
        uint64_t logical_size = write_size * raidvd->vdev_children;
        uint64_t read_size =
            P2ROUNDUP(DIV_ROUND_UP(logical_size, (raidvd->vdev_children - 1)),