Skip to content

Commit d3e709e

Browse files
author
Matthew Wilcox
committed
idr: Return the deleted entry from idr_remove
It is a relatively common idiom (8 instances) to first look up an IDR entry, and then remove it from the tree if it is found, possibly doing further operations upon the entry afterwards. If we change idr_remove() to return the removed object, all of these users can save themselves a walk of the IDR tree. Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
1 parent 8ac0486 commit d3e709e

9 files changed

Lines changed: 15 additions & 29 deletions

File tree

‎drivers/atm/nicstar.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,13 +1980,12 @@ static void dequeue_rx(ns_dev * card, ns_rsqe * rsqe)
19801980
card->lbfqc = ns_stat_lfbqc_get(stat);
19811981

19821982
id = le32_to_cpu(rsqe->buffer_handle);
1983-
skb = idr_find(&card->idr, id);
1983+
skb = idr_remove(&card->idr, id);
19841984
if (!skb) {
19851985
RXPRINTK(KERN_ERR
1986-
"nicstar%d: idr_find() failed!\n", card->index);
1986+
"nicstar%d: skb not found!\n", card->index);
19871987
return;
19881988
}
1989-
idr_remove(&card->idr, id);
19901989
dma_sync_single_for_cpu(&card->pcidev->dev,
19911990
NS_PRV_DMA(skb),
19921991
(NS_PRV_BUFTYPE(skb) == BUF_SM

‎drivers/block/drbd/drbd_main.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,11 +2915,9 @@ enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsig
29152915
idr_remove(&connection->peer_devices, vnr);
29162916
out_idr_remove_from_resource:
29172917
for_each_connection(connection, resource) {
2918-
peer_device = idr_find(&connection->peer_devices, vnr);
2919-
if (peer_device) {
2920-
idr_remove(&connection->peer_devices, vnr);
2918+
peer_device = idr_remove(&connection->peer_devices, vnr);
2919+
if (peer_device)
29212920
kref_put(&connection->kref, drbd_destroy_connection);
2922-
}
29232921
}
29242922
for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
29252923
list_del(&peer_device->peer_devices);

‎drivers/firewire/core-cdev.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,8 +1307,7 @@ static void iso_resource_work(struct work_struct *work)
13071307
*/
13081308
if (r->todo == ISO_RES_REALLOC && !success &&
13091309
!client->in_shutdown &&
1310-
idr_find(&client->resource_idr, r->resource.handle)) {
1311-
idr_remove(&client->resource_idr, r->resource.handle);
1310+
idr_remove(&client->resource_idr, r->resource.handle)) {
13121311
client_put(client);
13131312
free = true;
13141313
}

‎drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
7070
struct amdgpu_bo_list *list;
7171

7272
mutex_lock(&fpriv->bo_list_lock);
73-
list = idr_find(&fpriv->bo_list_handles, id);
73+
list = idr_remove(&fpriv->bo_list_handles, id);
7474
if (list) {
75+
/* Another user may have a reference to this list still */
7576
mutex_lock(&list->lock);
76-
idr_remove(&fpriv->bo_list_handles, id);
7777
mutex_unlock(&list->lock);
7878
amdgpu_bo_list_free(list);
7979
}

‎drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,11 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
135135
struct amdgpu_ctx *ctx;
136136

137137
mutex_lock(&mgr->lock);
138-
ctx = idr_find(&mgr->ctx_handles, id);
139-
if (ctx) {
140-
idr_remove(&mgr->ctx_handles, id);
138+
ctx = idr_remove(&mgr->ctx_handles, id);
139+
if (ctx)
141140
kref_put(&ctx->refcount, amdgpu_ctx_do_release);
142-
mutex_unlock(&mgr->lock);
143-
return 0;
144-
}
145141
mutex_unlock(&mgr->lock);
146-
return -EINVAL;
142+
return ctx ? 0 : -EINVAL;
147143
}
148144

149145
static int amdgpu_ctx_query(struct amdgpu_device *adev,

‎drivers/net/wireless/marvell/mwifiex/txrx.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,7 @@ void mwifiex_parse_tx_status_event(struct mwifiex_private *priv,
346346
return;
347347

348348
spin_lock_irqsave(&priv->ack_status_lock, flags);
349-
ack_skb = idr_find(&priv->ack_status_frames, tx_status->tx_token_id);
350-
if (ack_skb)
351-
idr_remove(&priv->ack_status_frames, tx_status->tx_token_id);
349+
ack_skb = idr_remove(&priv->ack_status_frames, tx_status->tx_token_id);
352350
spin_unlock_irqrestore(&priv->ack_status_lock, flags);
353351

354352
if (ack_skb) {

‎drivers/target/target_core_user.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,7 @@ static unsigned int tcmu_handle_completions(struct tcmu_dev *udev)
642642
WARN_ON(tcmu_hdr_get_op(entry->hdr.len_op) != TCMU_OP_CMD);
643643

644644
spin_lock(&udev->commands_lock);
645-
cmd = idr_find(&udev->commands, entry->hdr.cmd_id);
646-
if (cmd)
647-
idr_remove(&udev->commands, cmd->cmd_id);
645+
cmd = idr_remove(&udev->commands, entry->hdr.cmd_id);
648646
spin_unlock(&udev->commands_lock);
649647

650648
if (!cmd) {

‎include/linux/idr.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ void *idr_get_next(struct idr *, int *nextid);
8888
void *idr_replace(struct idr *, void *, int id);
8989
void idr_destroy(struct idr *);
9090

91-
static inline void idr_remove(struct idr *idr, int id)
91+
static inline void *idr_remove(struct idr *idr, int id)
9292
{
93-
radix_tree_delete(&idr->idr_rt, id);
93+
return radix_tree_delete_item(&idr->idr_rt, id, NULL);
9494
}
9595

9696
static inline void idr_init(struct idr *idr)

‎net/mac80211/status.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,7 @@ static void ieee80211_report_ack_skb(struct ieee80211_local *local,
462462
unsigned long flags;
463463

464464
spin_lock_irqsave(&local->ack_status_lock, flags);
465-
skb = idr_find(&local->ack_status_frames, info->ack_frame_id);
466-
if (skb)
467-
idr_remove(&local->ack_status_frames, info->ack_frame_id);
465+
skb = idr_remove(&local->ack_status_frames, info->ack_frame_id);
468466
spin_unlock_irqrestore(&local->ack_status_lock, flags);
469467

470468
if (!skb)

0 commit comments

Comments
 (0)