Add atomic_fcmpset_int32 and some comments.

Because `atomic_fcmpset_int` maybe run not correctly sometimes, and then loop long time, But no reason found yet.
This commit is contained in:
fengbojiang 2023-09-15 15:19:08 +08:00
parent e5e25e6af1
commit 8e683b405b
3 changed files with 48 additions and 1 deletions

View File

@ -227,6 +227,34 @@ atomic_fcmpset_##TYPE(volatile u_##TYPE *dst, u_##TYPE *expect, u_##TYPE src) \
return (res); \ return (res); \
} }
#ifdef FSTACK
/*
* The atomic_fcmpset_int above run error sometimes,
* it's return value is 0 while CAS success, and then loop long time.
* And no reason found yet.
*
* But use the function can fix it.
* This function copied from DPDK's rte_atomic.h
*/
static __inline int \
atomic_fcmpset_int32(volatile u_int *dst, u_int *expect, u_int src) \
{ \
u_char res; \
\
__asm __volatile( \
" " MPLOCKED " " \
"cmpxchgl %[src], %[dst];" \
"sete %[res];" \
: [res] "=a" (res), /* output */ \
[dst] "=m" (*dst) \
: [src] "r" (src), /* input */ \
"a" (*expect), \
"m" (*dst) \
: "memory"); /* no-clobber list */ \
return (res); \
}
#endif
ATOMIC_CMPSET(char); ATOMIC_CMPSET(char);
ATOMIC_CMPSET(short); ATOMIC_CMPSET(short);
ATOMIC_CMPSET(int); ATOMIC_CMPSET(int);

View File

@ -3136,6 +3136,16 @@ fget_unlocked_seq(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
* if anything goes wrong. In practice this only happens when userspace is * if anything goes wrong. In practice this only happens when userspace is
* racing with itself. * racing with itself.
*/ */
#ifdef FSTACK
/*
* Note: If loop dead in this function,
* Maybe CAS run not correctly in `refcount_acquire_if_not_zero`,
* You can try modify `atomic_fcmpset_int` to `atomic_fcmpset_int32`
* in function `refcount_acquire_if_gt` of `refcount.h`
*
* See also `atomic_fcmpset_int32` in `freebsd/amd64/include/atomic.h`
*/
#endif
int int
fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp, fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
struct file **fpp) struct file **fpp)
@ -5137,7 +5147,7 @@ ff_getmaxfd(void)
{ {
struct thread *td = curthread; struct thread *td = curthread;
return getmaxfd(td); return getmaxfd(td);
} }
#endif #endif

View File

@ -117,6 +117,15 @@ refcount_acquire_checked(volatile u_int *count)
* This functions returns non-zero if the refcount was * This functions returns non-zero if the refcount was
* incremented. Else zero is returned. * incremented. Else zero is returned.
*/ */
#ifdef FSTACK
/*
* Note: If loop dead in this function,
* Maybe CAS that atomic_fcmpset_int run not correctly in this function,
* You can try modify `atomic_fcmpset_int` to `atomic_fcmpset_int32`.
*
* See also `atomic_fcmpset_int32` in `freebsd/amd64/include/atomic.h`
*/
#endif
static __inline __result_use_check bool static __inline __result_use_check bool
refcount_acquire_if_gt(volatile u_int *count, u_int n) refcount_acquire_if_gt(volatile u_int *count, u_int n)
{ {