Change the vm_ooffset_t type to unsigned.

The type represents byte offset in the vm_object_t data space, which
does not span negative offsets in FreeBSD VM.  The change matches byte
offset signess with the unsignedness of the vm_pindex_t which
represents the type of the page indexes in the objects.

This allows to remove the UOFF_TO_IDX() macro which was used when we
have to forcibly interpret the type as unsigned anyway.  Also it fixes
a lot of implicit bugs in the device drivers d_mmap methods.

Reviewed by:	alc, markj (previous version)
Tested by:	pho
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Konstantin Belousov 2018-12-02 13:16:46 +00:00
parent 200bf72793
commit 10d9120c44
6 changed files with 15 additions and 22 deletions

View File

@ -814,8 +814,8 @@ gntdev_gmap_pg_fault(vm_object_t object, vm_ooffset_t offset, int prot,
relative_offset = offset - gmap->file_index; relative_offset = offset - gmap->file_index;
pidx = UOFF_TO_IDX(offset); pidx = OFF_TO_IDX(offset);
ridx = UOFF_TO_IDX(relative_offset); ridx = OFF_TO_IDX(relative_offset);
if (ridx >= gmap->count || if (ridx >= gmap->count ||
gmap->grant_map_ops[ridx].status != GNTST_okay) gmap->grant_map_ops[ridx].status != GNTST_okay)
return (VM_PAGER_FAIL); return (VM_PAGER_FAIL);
@ -1085,7 +1085,7 @@ mmap_gref(struct per_user_data *priv_user, struct gntdev_gref *gref_start,
break; break;
vm_page_insert(gref->page, mem_obj, vm_page_insert(gref->page, mem_obj,
UOFF_TO_IDX(gref->file_index)); OFF_TO_IDX(gref->file_index));
count--; count--;
} }
@ -1225,7 +1225,7 @@ gntdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
if (error != 0) if (error != 0)
return (EINVAL); return (EINVAL);
count = UOFF_TO_IDX(size); count = OFF_TO_IDX(size);
gref_start = gntdev_find_grefs(priv_user, *offset, count); gref_start = gntdev_find_grefs(priv_user, *offset, count);
if (gref_start) { if (gref_start) {

View File

@ -261,7 +261,7 @@ typedef __uint64_t kvaddr_t;
typedef __uint64_t ksize_t; typedef __uint64_t ksize_t;
typedef __vm_offset_t vm_offset_t; typedef __vm_offset_t vm_offset_t;
typedef __int64_t vm_ooffset_t; typedef __uint64_t vm_ooffset_t;
typedef __vm_paddr_t vm_paddr_t; typedef __vm_paddr_t vm_paddr_t;
typedef __uint64_t vm_pindex_t; typedef __uint64_t vm_pindex_t;
typedef __vm_size_t vm_size_t; typedef __vm_size_t vm_size_t;

View File

@ -150,9 +150,9 @@ cdev_pager_allocate(void *handle, enum obj_type tp, struct cdev_pager_ops *ops,
* of the page size. Do a check to avoid wrap. * of the page size. Do a check to avoid wrap.
*/ */
size = round_page(size); size = round_page(size);
pindex = UOFF_TO_IDX(foff) + UOFF_TO_IDX(size); pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
if (pindex > OBJ_MAX_SIZE || pindex < UOFF_TO_IDX(foff) || if (pindex > OBJ_MAX_SIZE || pindex < OFF_TO_IDX(foff) ||
pindex < UOFF_TO_IDX(size)) pindex < OFF_TO_IDX(size))
return (NULL); return (NULL);
if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0) if (ops->cdev_pg_ctor(handle, size, prot, foff, cred, &color) != 0)

View File

@ -100,9 +100,9 @@ sg_pager_alloc(void *handle, vm_ooffset_t size, vm_prot_t prot,
* to map beyond that. * to map beyond that.
*/ */
size = round_page(size); size = round_page(size);
pindex = UOFF_TO_IDX(foff) + UOFF_TO_IDX(size); pindex = OFF_TO_IDX(foff) + OFF_TO_IDX(size);
if (pindex > npages || pindex < UOFF_TO_IDX(foff) || if (pindex > npages || pindex < OFF_TO_IDX(foff) ||
pindex < UOFF_TO_IDX(size)) pindex < OFF_TO_IDX(size))
return (NULL); return (NULL);
/* /*

View File

@ -4213,7 +4213,7 @@ vm_map_lookup(vm_map_t *var_map, /* IN/OUT */
* Return the object/offset from this entry. If the entry was * Return the object/offset from this entry. If the entry was
* copy-on-write or empty, it has been fixed up. * copy-on-write or empty, it has been fixed up.
*/ */
*pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset); *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
*object = entry->object.vm_object; *object = entry->object.vm_object;
*out_prot = prot; *out_prot = prot;
@ -4294,7 +4294,7 @@ vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */
* Return the object/offset from this entry. If the entry was * Return the object/offset from this entry. If the entry was
* copy-on-write or empty, it has been fixed up. * copy-on-write or empty, it has been fixed up.
*/ */
*pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset); *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
*object = entry->object.vm_object; *object = entry->object.vm_object;
*out_prot = prot; *out_prot = prot;

View File

@ -196,20 +196,13 @@ struct vm_object {
/* /*
* Helpers to perform conversion between vm_object page indexes and offsets. * Helpers to perform conversion between vm_object page indexes and offsets.
* IDX_TO_OFF() converts an index into an offset. * IDX_TO_OFF() converts an index into an offset.
* OFF_TO_IDX() converts an offset into an index. Since offsets are signed * OFF_TO_IDX() converts an offset into an index.
* by default, the sign propagation in OFF_TO_IDX(), when applied to
* negative offsets, is intentional and returns a vm_object page index
* that cannot be created by a userspace mapping.
* UOFF_TO_IDX() treats the offset as an unsigned value and converts it
* into an index accordingly. Use it only when the full range of offset
* values are allowed. Currently, this only applies to device mappings.
* OBJ_MAX_SIZE specifies the maximum page index corresponding to the * OBJ_MAX_SIZE specifies the maximum page index corresponding to the
* maximum unsigned offset. * maximum unsigned offset.
*/ */
#define IDX_TO_OFF(idx) (((vm_ooffset_t)(idx)) << PAGE_SHIFT) #define IDX_TO_OFF(idx) (((vm_ooffset_t)(idx)) << PAGE_SHIFT)
#define OFF_TO_IDX(off) ((vm_pindex_t)(((vm_ooffset_t)(off)) >> PAGE_SHIFT)) #define OFF_TO_IDX(off) ((vm_pindex_t)(((vm_ooffset_t)(off)) >> PAGE_SHIFT))
#define UOFF_TO_IDX(off) (((vm_pindex_t)(off)) >> PAGE_SHIFT) #define OBJ_MAX_SIZE (OFF_TO_IDX(UINT64_MAX) + 1)
#define OBJ_MAX_SIZE (UOFF_TO_IDX(UINT64_MAX) + 1)
#ifdef _KERNEL #ifdef _KERNEL