Skip to content

Commit ab6bc44

Browse files
Xu Yilunjgunthorpe
authored andcommitted
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/[email protected] Suggested-by: Kevin Tian <[email protected]> Suggested-by: Jason Gunthorpe <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Nicolin Chen <[email protected]> Tested-by: Nicolin Chen <[email protected]> Signed-off-by: Xu Yilun <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 39a369c commit ab6bc44

File tree

5 files changed

+41
-34
lines changed

5 files changed

+41
-34
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ static void iommufd_device_remove_vdev(struct iommufd_device *idev)
152152
/*
153153
* An ongoing vdev destroy ioctl has removed the vdev from the object
154154
* 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.
157157
*/
158158
if (IS_ERR(vdev))
159159
goto out_unlock;
@@ -184,7 +184,7 @@ void iommufd_device_pre_destroy(struct iommufd_object *obj)
184184
struct iommufd_device *idev =
185185
container_of(obj, struct iommufd_device, obj);
186186

187-
/* Release the short term users on this */
187+
/* Release the wait_cnt reference on this */
188188
iommufd_device_remove_vdev(idev);
189189
}
190190

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static inline bool iommufd_lock_obj(struct iommufd_object *obj)
169169
{
170170
if (!refcount_inc_not_zero(&obj->users))
171171
return false;
172-
if (!refcount_inc_not_zero(&obj->shortterm_users)) {
172+
if (!refcount_inc_not_zero(&obj->wait_cnt)) {
173173
/*
174174
* If the caller doesn't already have a ref on obj this must be
175175
* called under the xa_lock. Otherwise the caller is holding a
@@ -187,11 +187,11 @@ static inline void iommufd_put_object(struct iommufd_ctx *ictx,
187187
struct iommufd_object *obj)
188188
{
189189
/*
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.
192192
*/
193193
refcount_dec(&obj->users);
194-
if (refcount_dec_and_test(&obj->shortterm_users))
194+
if (refcount_dec_and_test(&obj->wait_cnt))
195195
wake_up_interruptible_all(&ictx->destroy_wait);
196196
}
197197

@@ -202,7 +202,7 @@ void iommufd_object_finalize(struct iommufd_ctx *ictx,
202202
struct iommufd_object *obj);
203203

204204
enum {
205-
REMOVE_WAIT_SHORTTERM = BIT(0),
205+
REMOVE_WAIT = BIT(0),
206206
REMOVE_OBJ_TOMBSTONE = BIT(1),
207207
};
208208
int iommufd_object_remove(struct iommufd_ctx *ictx,
@@ -211,15 +211,15 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
211211

212212
/*
213213
* 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.
216216
*/
217217
static inline void iommufd_object_destroy_user(struct iommufd_ctx *ictx,
218218
struct iommufd_object *obj)
219219
{
220220
int ret;
221221

222-
ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM);
222+
ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT);
223223

224224
/*
225225
* If there is a bug and we couldn't destroy the object then we did put
@@ -239,7 +239,7 @@ static inline void iommufd_object_tombstone_user(struct iommufd_ctx *ictx,
239239
int ret;
240240

241241
ret = iommufd_object_remove(ictx, obj, obj->id,
242-
REMOVE_WAIT_SHORTTERM | REMOVE_OBJ_TOMBSTONE);
242+
REMOVE_WAIT | REMOVE_OBJ_TOMBSTONE);
243243

244244
/*
245245
* If there is a bug and we couldn't destroy the object then we did put

drivers/iommu/iommufd/main.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,
4242
return ERR_PTR(-ENOMEM);
4343
obj->type = type;
4444
/* 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);
4646
refcount_set(&obj->users, 1);
4747

4848
/*
@@ -155,22 +155,22 @@ struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,
155155
return obj;
156156
}
157157

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)
160160
{
161-
if (refcount_dec_and_test(&to_destroy->shortterm_users))
161+
if (refcount_dec_and_test(&to_destroy->wait_cnt))
162162
return 0;
163163

164164
if (iommufd_object_ops[to_destroy->type].pre_destroy)
165165
iommufd_object_ops[to_destroy->type].pre_destroy(to_destroy);
166166

167167
if (wait_event_timeout(ictx->destroy_wait,
168-
refcount_read(&to_destroy->shortterm_users) == 0,
168+
refcount_read(&to_destroy->wait_cnt) == 0,
169169
msecs_to_jiffies(60000)))
170170
return 0;
171171

172172
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);
174174
return -EBUSY;
175175
}
176176

@@ -184,17 +184,18 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
184184
{
185185
struct iommufd_object *obj;
186186
XA_STATE(xas, &ictx->objects, id);
187-
bool zerod_shortterm = false;
187+
bool zerod_wait_cnt = false;
188188
int ret;
189189

190190
/*
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.
195196
*/
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);
198199
if (ret) {
199200
/*
200201
* We have a bug. Put back the callers reference and
@@ -203,7 +204,7 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
203204
refcount_dec(&to_destroy->users);
204205
return ret;
205206
}
206-
zerod_shortterm = true;
207+
zerod_wait_cnt = true;
207208
}
208209

209210
xa_lock(&ictx->objects);
@@ -235,11 +236,11 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
235236
xa_unlock(&ictx->objects);
236237

237238
/*
238-
* Since users is zero any positive users_shortterm must be racing
239+
* Since users is zero any positive wait_cnt must be racing
239240
* iommufd_put_object(), or we have a bug.
240241
*/
241-
if (!zerod_shortterm) {
242-
ret = iommufd_object_dec_wait_shortterm(ictx, obj);
242+
if (!zerod_wait_cnt) {
243+
ret = iommufd_object_dec_wait(ictx, obj);
243244
if (WARN_ON(ret))
244245
return ret;
245246
}
@@ -249,9 +250,9 @@ int iommufd_object_remove(struct iommufd_ctx *ictx,
249250
return 0;
250251

251252
err_xa:
252-
if (zerod_shortterm) {
253+
if (zerod_wait_cnt) {
253254
/* Restore the xarray owned reference */
254-
refcount_set(&obj->shortterm_users, 1);
255+
refcount_set(&obj->wait_cnt, 1);
255256
}
256257
xa_unlock(&ictx->objects);
257258

drivers/iommu/iommufd/viommu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd)
205205
vdev->viommu = viommu;
206206
refcount_inc(&viommu->obj.users);
207207
/*
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
210210
* idev real destruction.
211211
*/
212212
vdev->idev = idev;

include/linux/iommufd.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ enum iommufd_object_type {
4646

4747
/* Base struct for all objects with a userspace ID handle. */
4848
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;
5056
refcount_t users;
5157
enum iommufd_object_type type;
5258
unsigned int id;

0 commit comments

Comments
 (0)