Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iommufd: Rename some shortterm-related identifiers

Rename the shortterm-related identifiers to wait-related.

The usage of shortterm_users refcount is now beyond its name. It is
also used for references which live longer than an ioctl execution.
E.g. vdev holds idev's shortterm_users refcount on vdev allocation,
releases it during idev's pre_destroy(). Rename the refcount as
wait_cnt, since it is always used to sync the referencing & the
destruction of the object by waiting for it to go to zero.

List all changed identifiers:

iommufd_object::shortterm_users -> iommufd_object::wait_cnt
REMOVE_WAIT_SHORTTERM -> REMOVE_WAIT
iommufd_object_dec_wait_shortterm() -> iommufd_object_dec_wait()
zerod_shortterm -> zerod_wait_cnt

No functional change intended.

Link: https://patch.msgid.link/r/20250716070349.1807226-9-yilun.xu@linux.intel.com
Suggested-by: Kevin Tian <kevin.tian@intel.com>
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Xu Yilun <yilun.xu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

authored by

Xu Yilun and committed by
Jason Gunthorpe
ab6bc441 39a369c3

+41 -34
+3 -3
drivers/iommu/iommufd/device.c
··· 152 152 /* 153 153 * An ongoing vdev destroy ioctl has removed the vdev from the object 154 154 * xarray, but has not finished iommufd_vdevice_destroy() yet as it 155 - * needs the same mutex. We exit the locking then wait on short term 156 - * users for the vdev destruction. 155 + * needs the same mutex. We exit the locking then wait on wait_cnt 156 + * reference for the vdev destruction. 157 157 */ 158 158 if (IS_ERR(vdev)) 159 159 goto out_unlock; ··· 184 184 struct iommufd_device *idev = 185 185 container_of(obj, struct iommufd_device, obj); 186 186 187 - /* Release the short term users on this */ 187 + /* Release the wait_cnt reference on this */ 188 188 iommufd_device_remove_vdev(idev); 189 189 } 190 190
+9 -9
drivers/iommu/iommufd/iommufd_private.h
··· 169 169 { 170 170 if (!refcount_inc_not_zero(&obj->users)) 171 171 return false; 172 - if (!refcount_inc_not_zero(&obj->shortterm_users)) { 172 + if (!refcount_inc_not_zero(&obj->wait_cnt)) { 173 173 /* 174 174 * If the caller doesn't already have a ref on obj this must be 175 175 * called under the xa_lock. Otherwise the caller is holding a ··· 187 187 struct iommufd_object *obj) 188 188 { 189 189 /* 190 - * Users first, then shortterm so that REMOVE_WAIT_SHORTTERM never sees 191 - * a spurious !0 users with a 0 shortterm_users. 190 + * Users first, then wait_cnt so that REMOVE_WAIT never sees a spurious 191 + * !0 users with a 0 wait_cnt. 192 192 */ 193 193 refcount_dec(&obj->users); 194 - if (refcount_dec_and_test(&obj->shortterm_users)) 194 + if (refcount_dec_and_test(&obj->wait_cnt)) 195 195 wake_up_interruptible_all(&ictx->destroy_wait); 196 196 } 197 197 ··· 202 202 struct iommufd_object *obj); 203 203 204 204 enum { 205 - REMOVE_WAIT_SHORTTERM = BIT(0), 205 + REMOVE_WAIT = BIT(0), 206 206 REMOVE_OBJ_TOMBSTONE = BIT(1), 207 207 }; 208 208 int iommufd_object_remove(struct iommufd_ctx *ictx, ··· 211 211 212 212 /* 213 213 * The caller holds a users refcount and wants to destroy the object. At this 214 - * point the caller has no shortterm_users reference and at least the xarray 215 - * will be holding one. 214 + * point the caller has no wait_cnt reference and at least the xarray will be 215 + * holding one. 216 216 */ 217 217 static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx, 218 218 struct iommufd_object *obj) 219 219 { 220 220 int ret; 221 221 222 - ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); 222 + ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT); 223 223 224 224 /* 225 225 * If there is a bug and we couldn't destroy the object then we did put ··· 239 239 int ret; 240 240 241 241 ret = iommufd_object_remove(ictx, obj, obj->id, 242 - REMOVE_WAIT_SHORTTERM | REMOVE_OBJ_TOMBSTONE); 242 + REMOVE_WAIT | REMOVE_OBJ_TOMBSTONE); 243 243 244 244 /* 245 245 * If there is a bug and we couldn't destroy the object then we did put
+20 -19
drivers/iommu/iommufd/main.c
··· 42 42 return ERR_PTR(-ENOMEM); 43 43 obj->type = type; 44 44 /* Starts out bias'd by 1 until it is removed from the xarray */ 45 - refcount_set(&obj->shortterm_users, 1); 45 + refcount_set(&obj->wait_cnt, 1); 46 46 refcount_set(&obj->users, 1); 47 47 48 48 /* ··· 155 155 return obj; 156 156 } 157 157 158 - static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx, 159 - struct iommufd_object *to_destroy) 158 + static int iommufd_object_dec_wait(struct iommufd_ctx *ictx, 159 + struct iommufd_object *to_destroy) 160 160 { 161 - if (refcount_dec_and_test(&to_destroy->shortterm_users)) 161 + if (refcount_dec_and_test(&to_destroy->wait_cnt)) 162 162 return 0; 163 163 164 164 if (iommufd_object_ops[to_destroy->type].pre_destroy) 165 165 iommufd_object_ops[to_destroy->type].pre_destroy(to_destroy); 166 166 167 167 if (wait_event_timeout(ictx->destroy_wait, 168 - refcount_read(&to_destroy->shortterm_users) == 0, 168 + refcount_read(&to_destroy->wait_cnt) == 0, 169 169 msecs_to_jiffies(60000))) 170 170 return 0; 171 171 172 172 pr_crit("Time out waiting for iommufd object to become free\n"); 173 - refcount_inc(&to_destroy->shortterm_users); 173 + refcount_inc(&to_destroy->wait_cnt); 174 174 return -EBUSY; 175 175 } 176 176 ··· 184 184 { 185 185 struct iommufd_object *obj; 186 186 XA_STATE(xas, &ictx->objects, id); 187 - bool zerod_shortterm = false; 187 + bool zerod_wait_cnt = false; 188 188 int ret; 189 189 190 190 /* 191 - * The purpose of the shortterm_users is to ensure deterministic 192 - * destruction of objects used by external drivers and destroyed by this 193 - * function. Any temporary increment of the refcount must increment 194 - * shortterm_users, such as during ioctl execution. 191 + * The purpose of the wait_cnt is to ensure deterministic destruction 192 + * of objects used by external drivers and destroyed by this function. 193 + * Incrementing this wait_cnt should either be short lived, such as 194 + * during ioctl execution, or be revoked and blocked during 195 + * pre_destroy(), such as vdev holding the idev's refcount. 195 196 */ 196 - if (flags & REMOVE_WAIT_SHORTTERM) { 197 - ret = iommufd_object_dec_wait_shortterm(ictx, to_destroy); 197 + if (flags & REMOVE_WAIT) { 198 + ret = iommufd_object_dec_wait(ictx, to_destroy); 198 199 if (ret) { 199 200 /* 200 201 * We have a bug. Put back the callers reference and ··· 204 203 refcount_dec(&to_destroy->users); 205 204 return ret; 206 205 } 207 - zerod_shortterm = true; 206 + zerod_wait_cnt = true; 208 207 } 209 208 210 209 xa_lock(&ictx->objects); ··· 236 235 xa_unlock(&ictx->objects); 237 236 238 237 /* 239 - * Since users is zero any positive users_shortterm must be racing 238 + * Since users is zero any positive wait_cnt must be racing 240 239 * iommufd_put_object(), or we have a bug. 241 240 */ 242 - if (!zerod_shortterm) { 243 - ret = iommufd_object_dec_wait_shortterm(ictx, obj); 241 + if (!zerod_wait_cnt) { 242 + ret = iommufd_object_dec_wait(ictx, obj); 244 243 if (WARN_ON(ret)) 245 244 return ret; 246 245 } ··· 250 249 return 0; 251 250 252 251 err_xa: 253 - if (zerod_shortterm) { 252 + if (zerod_wait_cnt) { 254 253 /* Restore the xarray owned reference */ 255 - refcount_set(&obj->shortterm_users, 1); 254 + refcount_set(&obj->wait_cnt, 1); 256 255 } 257 256 xa_unlock(&ictx->objects); 258 257
+2 -2
drivers/iommu/iommufd/viommu.c
··· 205 205 vdev->viommu = viommu; 206 206 refcount_inc(&viommu->obj.users); 207 207 /* 208 - * A short term users reference is held on the idev so long as we have 209 - * the pointer. iommufd_device_pre_destroy() will revoke it before the 208 + * A wait_cnt reference is held on the idev so long as we have the 209 + * pointer. iommufd_device_pre_destroy() will revoke it before the 210 210 * idev real destruction. 211 211 */ 212 212 vdev->idev = idev;
+7 -1
include/linux/iommufd.h
··· 46 46 47 47 /* Base struct for all objects with a userspace ID handle. */ 48 48 struct iommufd_object { 49 - refcount_t shortterm_users; 49 + /* 50 + * Destroy will sleep and wait for wait_cnt to go to zero. This allows 51 + * concurrent users of the ID to reliably avoid causing a spurious 52 + * destroy failure. Incrementing this count should either be short 53 + * lived or be revoked and blocked during pre_destroy(). 54 + */ 55 + refcount_t wait_cnt; 50 56 refcount_t users; 51 57 enum iommufd_object_type type; 52 58 unsigned int id;