From ef038491858cb51f8aa17b1f6e50444d2e627413 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Sun, 20 Apr 2025 18:45:55 +0100 Subject: [PATCH] [uaccess] Remove redundant userptr_add() and userptr_diff() The userptr_add() and userptr_diff() functions are now just straightforward wrappers around addition and subtraction. Remove these redundant wrappers. Signed-off-by: Michael Brown --- src/arch/x86/image/bzimage.c | 12 ++--- src/arch/x86/image/initrd.c | 25 +++++---- src/arch/x86/image/nbi.c | 4 +- src/arch/x86/include/librm.h | 11 ---- .../x86/interface/pcbios/memtop_umalloc.c | 10 ++-- src/arch/x86/transitions/librm_mgmt.c | 1 - src/core/sanboot.c | 2 +- src/core/uaccess.c | 1 - src/drivers/net/gve.c | 2 +- src/image/gzip.c | 2 +- src/include/ipxe/linux/linux_uaccess.h | 11 ---- src/include/ipxe/uaccess.h | 53 ------------------- src/interface/linux/linux_uaccess.c | 1 - 13 files changed, 27 insertions(+), 108 deletions(-) diff --git a/src/arch/x86/image/bzimage.c b/src/arch/x86/image/bzimage.c index b4a78cdfc..d00b9f155 100644 --- a/src/arch/x86/image/bzimage.c +++ b/src/arch/x86/image/bzimage.c @@ -431,7 +431,7 @@ static int bzimage_check_initrds ( struct image *image, } /* Calculate lowest usable address */ - bottom = userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ); + bottom = ( bzimg->pm_kernel + bzimg->pm_sz ); /* Check that total length fits within space available for * reshuffling. This is a conservative check, since CPIO @@ -471,14 +471,12 @@ static void bzimage_load_initrds ( struct image *image, size_t len; /* Reshuffle initrds into desired order */ - initrd_reshuffle ( userptr_add ( bzimg->pm_kernel, bzimg->pm_sz ) ); + initrd_reshuffle ( bzimg->pm_kernel + bzimg->pm_sz ); /* Find highest initrd */ for_each_image ( initrd ) { - if ( ( highest == NULL ) || - ( userptr_diff ( initrd->data, highest->data ) > 0 ) ) { + if ( ( highest == NULL ) || ( initrd->data > highest->data ) ) highest = initrd; - } } /* Do nothing if there are no initrds */ @@ -486,7 +484,7 @@ static void bzimage_load_initrds ( struct image *image, return; /* Find highest usable address */ - top = userptr_add ( highest->data, bzimage_align ( highest->len ) ); + top = ( highest->data + bzimage_align ( highest->len ) ); if ( user_to_phys ( top, -1 ) > bzimg->mem_limit ) { top = phys_to_user ( ( bzimg->mem_limit + 1 ) & ~( INITRD_ALIGN - 1 ) ); @@ -509,7 +507,7 @@ static void bzimage_load_initrds ( struct image *image, } /* Load initrd at this address */ - dest = userptr_add ( top, -offset ); + dest = ( top - offset ); len = bzimage_load_initrd ( image, initrd, dest ); /* Record initrd location */ diff --git a/src/arch/x86/image/initrd.c b/src/arch/x86/image/initrd.c index c0a56b7f6..e32e40341 100644 --- a/src/arch/x86/image/initrd.c +++ b/src/arch/x86/image/initrd.c @@ -61,10 +61,9 @@ static userptr_t initrd_squash_high ( userptr_t top ) { /* Find the highest image not yet in its final position */ highest = NULL; for_each_image ( initrd ) { - if ( ( userptr_diff ( initrd->data, current ) < 0 ) && + if ( ( initrd->data < current ) && ( ( highest == NULL ) || - ( userptr_diff ( initrd->data, - highest->data ) > 0 ) ) ) { + ( initrd->data > highest->data ) ) ) { highest = initrd; } } @@ -74,7 +73,7 @@ static userptr_t initrd_squash_high ( userptr_t top ) { /* Move this image to its final position */ len = ( ( highest->len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) ); - current = userptr_add ( current, -len ); + current -= len; DBGC ( &images, "INITRD squashing %s [%#08lx,%#08lx)->" "[%#08lx,%#08lx)\n", highest->name, user_to_phys ( highest->data, 0 ), @@ -87,10 +86,10 @@ static userptr_t initrd_squash_high ( userptr_t top ) { /* Copy any remaining initrds (e.g. embedded images) to the region */ for_each_image ( initrd ) { - if ( userptr_diff ( initrd->data, top ) >= 0 ) { + if ( initrd->data >= top ) { len = ( ( initrd->len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) ); - current = userptr_add ( current, -len ); + current -= len; DBGC ( &images, "INITRD copying %s [%#08lx,%#08lx)->" "[%#08lx,%#08lx)\n", initrd->name, user_to_phys ( initrd->data, 0 ), @@ -149,7 +148,7 @@ static void initrd_swap ( struct image *low, struct image *high, /* Adjust data pointers */ high->data = low->data; - low->data = userptr_add ( low->data, len ); + low->data += len; } /** @@ -171,7 +170,7 @@ static int initrd_swap_any ( userptr_t free, size_t free_len ) { /* Calculate location of adjacent image (if any) */ padded_len = ( ( low->len + INITRD_ALIGN - 1 ) & ~( INITRD_ALIGN - 1 ) ); - adjacent = userptr_add ( low->data, padded_len ); + adjacent = ( low->data + padded_len ); /* Search for adjacent image */ for_each_image ( high ) { @@ -235,7 +234,7 @@ void initrd_reshuffle ( userptr_t bottom ) { /* Calculate limits of available space for initrds */ top = initrd_top; - if ( userptr_diff ( initrd_bottom, bottom ) > 0 ) + if ( initrd_bottom > bottom ) bottom = initrd_bottom; /* Debug */ @@ -248,7 +247,7 @@ void initrd_reshuffle ( userptr_t bottom ) { /* Calculate available free space */ free = bottom; - free_len = userptr_diff ( used, free ); + free_len = ( used - free ); /* Bubble-sort initrds into desired order */ while ( initrd_swap_any ( free, free_len ) ) {} @@ -270,9 +269,9 @@ int initrd_reshuffle_check ( size_t len, userptr_t bottom ) { /* Calculate limits of available space for initrds */ top = initrd_top; - if ( userptr_diff ( initrd_bottom, bottom ) > 0 ) + if ( initrd_bottom > bottom ) bottom = initrd_bottom; - available = userptr_diff ( top, bottom ); + available = ( top - bottom ); /* Allow for a sensible minimum amount of free space */ len += INITRD_MIN_FREE_LEN; @@ -296,7 +295,7 @@ static void initrd_startup ( void ) { * can safely reuse when rearranging). */ len = largest_memblock ( &initrd_bottom ); - initrd_top = userptr_add ( initrd_bottom, len ); + initrd_top = ( initrd_bottom + len ); } /** initrd startup function */ diff --git a/src/arch/x86/image/nbi.c b/src/arch/x86/image/nbi.c index b691bee20..2f0d3164a 100644 --- a/src/arch/x86/image/nbi.c +++ b/src/arch/x86/image/nbi.c @@ -184,10 +184,10 @@ static int nbi_process_segments ( struct image *image, dest = phys_to_user ( sh.loadaddr ); break; case NBI_LOADADDR_AFTER: - dest = userptr_add ( dest, memsz + sh.loadaddr ); + dest = ( dest + memsz + sh.loadaddr ); break; case NBI_LOADADDR_BEFORE: - dest = userptr_add ( dest, -sh.loadaddr ); + dest = ( dest - sh.loadaddr ); break; case NBI_LOADADDR_END: /* Not correct according to the spec, but diff --git a/src/arch/x86/include/librm.h b/src/arch/x86/include/librm.h index 23ca4650f..c0d910287 100644 --- a/src/arch/x86/include/librm.h +++ b/src/arch/x86/include/librm.h @@ -137,17 +137,6 @@ UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) { return trivial_user_to_virt ( userptr, offset ); } -static inline __always_inline userptr_t -UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) { - return trivial_userptr_add ( userptr, offset ); -} - -static inline __always_inline off_t -UACCESS_INLINE ( librm, userptr_diff ) ( userptr_t userptr, - userptr_t subtrahend ) { - return trivial_userptr_diff ( userptr, subtrahend ); -} - static inline __always_inline void UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, diff --git a/src/arch/x86/interface/pcbios/memtop_umalloc.c b/src/arch/x86/interface/pcbios/memtop_umalloc.c index 1cc3aff91..e76b3df93 100644 --- a/src/arch/x86/interface/pcbios/memtop_umalloc.c +++ b/src/arch/x86/interface/pcbios/memtop_umalloc.c @@ -122,7 +122,7 @@ static void init_eheap ( void ) { userptr_t base; heap_size = largest_memblock ( &base ); - bottom = top = userptr_add ( base, heap_size ); + bottom = top = ( base + heap_size ); DBG ( "External heap grows downwards from %lx (size %zx)\n", user_to_phys ( top, 0 ), heap_size ); } @@ -144,7 +144,7 @@ static void ecollect_free ( void ) { DBG ( "EXTMEM freeing [%lx,%lx)\n", user_to_phys ( bottom, 0 ), user_to_phys ( bottom, extmem.size ) ); len = ( extmem.size + sizeof ( extmem ) ); - bottom = userptr_add ( bottom, len ); + bottom += len; heap_size += len; } } @@ -179,7 +179,7 @@ static userptr_t memtop_urealloc ( userptr_t ptr, size_t new_size ) { DBG ( "EXTMEM out of space\n" ); return UNULL; } - ptr = bottom = userptr_add ( bottom, -sizeof ( extmem ) ); + ptr = bottom = ( bottom - sizeof ( extmem ) ); heap_size -= sizeof ( extmem ); DBG ( "EXTMEM allocating [%lx,%lx)\n", user_to_phys ( ptr, 0 ), user_to_phys ( ptr, 0 ) ); @@ -190,10 +190,10 @@ static userptr_t memtop_urealloc ( userptr_t ptr, size_t new_size ) { /* Expand/shrink block if possible */ if ( ptr == bottom ) { /* Update block */ - new = userptr_add ( ptr, - ( new_size - extmem.size ) ); + new = ( ptr - ( new_size - extmem.size ) ); align = ( user_to_phys ( new, 0 ) & ( EM_ALIGN - 1 ) ); new_size += align; - new = userptr_add ( new, -align ); + new -= align; if ( new_size > ( heap_size + extmem.size ) ) { DBG ( "EXTMEM out of space\n" ); return UNULL; diff --git a/src/arch/x86/transitions/librm_mgmt.c b/src/arch/x86/transitions/librm_mgmt.c index b3820589c..ec31fceb1 100644 --- a/src/arch/x86/transitions/librm_mgmt.c +++ b/src/arch/x86/transitions/librm_mgmt.c @@ -432,7 +432,6 @@ PROVIDE_UACCESS_INLINE ( librm, phys_to_user ); PROVIDE_UACCESS_INLINE ( librm, user_to_phys ); PROVIDE_UACCESS_INLINE ( librm, virt_to_user ); PROVIDE_UACCESS_INLINE ( librm, user_to_virt ); -PROVIDE_UACCESS_INLINE ( librm, userptr_add ); PROVIDE_UACCESS_INLINE ( librm, memcpy_user ); PROVIDE_UACCESS_INLINE ( librm, memmove_user ); PROVIDE_UACCESS_INLINE ( librm, memset_user ); diff --git a/src/core/sanboot.c b/src/core/sanboot.c index e49a3f92d..4facf86b8 100644 --- a/src/core/sanboot.c +++ b/src/core/sanboot.c @@ -625,7 +625,7 @@ static int sandev_rw ( struct san_device *sandev, uint64_t lba, /* Move to next fragment */ frag_len = ( sandev->capacity.blksize * params.rw.count ); - params.rw.buffer = userptr_add ( params.rw.buffer, frag_len ); + params.rw.buffer += frag_len; params.rw.lba += params.rw.count; remaining -= params.rw.count; } diff --git a/src/core/uaccess.c b/src/core/uaccess.c index d3a9ca17d..ad17a58ab 100644 --- a/src/core/uaccess.c +++ b/src/core/uaccess.c @@ -36,7 +36,6 @@ PROVIDE_UACCESS_INLINE ( flat, phys_to_user ); PROVIDE_UACCESS_INLINE ( flat, user_to_phys ); PROVIDE_UACCESS_INLINE ( flat, virt_to_user ); PROVIDE_UACCESS_INLINE ( flat, user_to_virt ); -PROVIDE_UACCESS_INLINE ( flat, userptr_add ); PROVIDE_UACCESS_INLINE ( flat, memcpy_user ); PROVIDE_UACCESS_INLINE ( flat, memmove_user ); PROVIDE_UACCESS_INLINE ( flat, memset_user ); diff --git a/src/drivers/net/gve.c b/src/drivers/net/gve.c index efc38dd21..805feee3d 100644 --- a/src/drivers/net/gve.c +++ b/src/drivers/net/gve.c @@ -807,7 +807,7 @@ static inline __attribute__ (( always_inline )) userptr_t gve_buffer ( struct gve_queue *queue, unsigned int index ) { /* Pages are currently allocated as a single contiguous block */ - return userptr_add ( queue->qpl.data, gve_address ( queue, index ) ); + return ( queue->qpl.data + gve_address ( queue, index ) ); } /** diff --git a/src/image/gzip.c b/src/image/gzip.c index 98376e113..116d912d7 100644 --- a/src/image/gzip.c +++ b/src/image/gzip.c @@ -111,7 +111,7 @@ static int gzip_extract ( struct image *image, struct image *extracted ) { } /* Initialise input chunk */ - deflate_chunk_init ( &in, userptr_add ( image->data, offset ), 0, len ); + deflate_chunk_init ( &in, ( image->data + offset ), 0, len ); /* Presize extracted image */ if ( ( rc = image_set_len ( extracted, diff --git a/src/include/ipxe/linux/linux_uaccess.h b/src/include/ipxe/linux/linux_uaccess.h index 1e31afd9c..790c75123 100644 --- a/src/include/ipxe/linux/linux_uaccess.h +++ b/src/include/ipxe/linux/linux_uaccess.h @@ -69,17 +69,6 @@ UACCESS_INLINE ( linux, user_to_virt ) ( userptr_t userptr, off_t offset ) { return trivial_user_to_virt ( userptr, offset ); } -static inline __always_inline userptr_t -UACCESS_INLINE ( linux, userptr_add ) ( userptr_t userptr, off_t offset ) { - return trivial_userptr_add ( userptr, offset ); -} - -static inline __always_inline off_t -UACCESS_INLINE ( linux, userptr_diff ) ( userptr_t userptr, - userptr_t subtrahend ) { - return trivial_userptr_diff ( userptr, subtrahend ); -} - static inline __always_inline void UACCESS_INLINE ( linux, memcpy_user ) ( userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, diff --git a/src/include/ipxe/uaccess.h b/src/include/ipxe/uaccess.h index 1790e4066..93dc60d62 100644 --- a/src/include/ipxe/uaccess.h +++ b/src/include/ipxe/uaccess.h @@ -65,30 +65,6 @@ trivial_user_to_virt ( userptr_t userptr, off_t offset ) { return ( ( void * ) userptr + offset ); } -/** - * Add offset to user pointer - * - * @v userptr User pointer - * @v offset Offset - * @ret userptr New pointer value - */ -static inline __always_inline userptr_t -trivial_userptr_add ( userptr_t userptr, off_t offset ) { - return ( userptr + offset ); -} - -/** - * Subtract user pointers - * - * @v userptr User pointer - * @v subtrahend User pointer to be subtracted - * @ret offset Offset - */ -static inline __always_inline off_t -trivial_userptr_diff ( userptr_t userptr, userptr_t subtrahend ) { - return ( userptr - subtrahend ); -} - /** * Copy data between user buffers * @@ -231,17 +207,6 @@ UACCESS_INLINE ( flat, user_to_virt ) ( userptr_t userptr, off_t offset ) { return trivial_user_to_virt ( userptr, offset ); } -static inline __always_inline userptr_t -UACCESS_INLINE ( flat, userptr_add ) ( userptr_t userptr, off_t offset ) { - return trivial_userptr_add ( userptr, offset ); -} - -static inline __always_inline off_t -UACCESS_INLINE ( flat, userptr_diff ) ( userptr_t userptr, - userptr_t subtrahend ) { - return trivial_userptr_diff ( userptr, subtrahend ); -} - static inline __always_inline void UACCESS_INLINE ( flat, memcpy_user ) ( userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, @@ -322,24 +287,6 @@ userptr_t virt_to_user ( volatile const void *addr ); */ void * user_to_virt ( userptr_t userptr, off_t offset ); -/** - * Add offset to user pointer - * - * @v userptr User pointer - * @v offset Offset - * @ret userptr New pointer value - */ -userptr_t userptr_add ( userptr_t userptr, off_t offset ); - -/** - * Subtract user pointers - * - * @v userptr User pointer - * @v subtrahend User pointer to be subtracted - * @ret offset Offset - */ -off_t userptr_diff ( userptr_t userptr, userptr_t subtrahend ); - /** * Convert virtual address to a physical address * diff --git a/src/interface/linux/linux_uaccess.c b/src/interface/linux/linux_uaccess.c index ea2d8057c..9fc99c5e2 100644 --- a/src/interface/linux/linux_uaccess.c +++ b/src/interface/linux/linux_uaccess.c @@ -30,7 +30,6 @@ FILE_LICENCE(GPL2_OR_LATER); PROVIDE_UACCESS_INLINE(linux, user_to_phys); PROVIDE_UACCESS_INLINE(linux, virt_to_user); PROVIDE_UACCESS_INLINE(linux, user_to_virt); -PROVIDE_UACCESS_INLINE(linux, userptr_add); PROVIDE_UACCESS_INLINE(linux, memcpy_user); PROVIDE_UACCESS_INLINE(linux, memmove_user); PROVIDE_UACCESS_INLINE(linux, memset_user); -- 2.47.2