From b2abe33d23cfaea6cd3f8335a1ee08c480512c6f Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:13 -0500 Subject: [PATCH 1/6] net: ipa: rework last transaction determination When quiescing a channel, we find the "last" transaction, which is the latest one to have been allocated. (New transaction allocation will have been prevented by the time this is called.) Currently we do this by looking for the first non-empty transaction list in each state, then return the last entry from that last. Instead, determine the last entry in each list (if any) and return that entry if found. Temporarily (locally) introduce list_last_entry_or_null() as a helper for this, mirroring list_first_entry_or_null(). This macro definition will be removed by an upcoming patch. Remove the temporary warnings added by the previous commit. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi.c | 28 ++++++++++++++-------------- drivers/net/ipa/gsi_private.h | 14 ++++++++++++++ drivers/net/ipa/gsi_trans.c | 22 ---------------------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/drivers/net/ipa/gsi.c b/drivers/net/ipa/gsi.c index 9e307eebd33f..0ea98fa5dee5 100644 --- a/drivers/net/ipa/gsi.c +++ b/drivers/net/ipa/gsi.c @@ -710,7 +710,6 @@ static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id) static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) { struct gsi_trans_info *trans_info = &channel->trans_info; - const struct list_head *list; struct gsi_trans *trans; spin_lock_bh(&trans_info->spinlock); @@ -719,29 +718,30 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) * before we disabled transmits, so check for that. */ if (channel->toward_ipa) { - list = &trans_info->alloc; - if (!list_empty(list)) + trans = list_last_entry_or_null(&trans_info->alloc, + struct gsi_trans, links); + if (trans) goto done; - list = &trans_info->committed; - if (!list_empty(list)) + trans = list_last_entry_or_null(&trans_info->committed, + struct gsi_trans, links); + if (trans) goto done; - list = &trans_info->pending; - if (!list_empty(list)) + trans = list_last_entry_or_null(&trans_info->pending, + struct gsi_trans, links); + if (trans) goto done; } /* Otherwise (TX or RX) we want to wait for anything that * has completed, or has been polled but not released yet. */ - list = &trans_info->complete; - if (!list_empty(list)) + trans = list_last_entry_or_null(&trans_info->complete, + struct gsi_trans, links); + if (trans) goto done; - list = &trans_info->polled; - if (list_empty(list)) - list = NULL; + trans = list_last_entry_or_null(&trans_info->polled, + struct gsi_trans, links); done: - trans = list ? list_last_entry(list, struct gsi_trans, links) : NULL; - /* Caller will wait for this, so take a reference */ if (trans) refcount_inc(&trans->refcount); diff --git a/drivers/net/ipa/gsi_private.h b/drivers/net/ipa/gsi_private.h index 0b2516fa21b5..51bbc7a40dc2 100644 --- a/drivers/net/ipa/gsi_private.h +++ b/drivers/net/ipa/gsi_private.h @@ -16,6 +16,20 @@ struct gsi_channel; #define GSI_RING_ELEMENT_SIZE 16 /* bytes; must be a power of 2 */ +/** + * list_last_entry_or_null - get the last element from a list + * @ptr: the list head to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_head within the struct. + * + * Note that if the list is empty, it returns NULL. + */ +#define list_last_entry_or_null(ptr, type, member) ({ \ + struct list_head *head__ = (ptr); \ + struct list_head *pos__ = READ_ONCE(head__->prev); \ + pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ +}) + /** * gsi_trans_move_complete() - Mark a GSI transaction completed * @trans: Transaction to commit diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c index 4eef1480c200..b4a6f2b56356 100644 --- a/drivers/net/ipa/gsi_trans.c +++ b/drivers/net/ipa/gsi_trans.c @@ -309,23 +309,15 @@ void gsi_trans_move_polled(struct gsi_trans *trans) { struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; struct gsi_trans_info *trans_info = &channel->trans_info; - u16 trans_index; spin_lock_bh(&trans_info->spinlock); list_move_tail(&trans->links, &trans_info->polled); - trans = list_first_entry(&trans_info->polled, - struct gsi_trans, links); - spin_unlock_bh(&trans_info->spinlock); /* This completed transaction is now polled */ trans_info->completed_id++; - - WARN_ON(trans_info->polled_id == trans_info->completed_id); - trans_index = trans_info->polled_id % channel->tre_count; - WARN_ON(trans != &trans_info->trans[trans_index]); } /* Reserve some number of TREs on a channel. Returns true if successful */ @@ -413,11 +405,8 @@ struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id, /* Free a previously-allocated transaction */ void gsi_trans_free(struct gsi_trans *trans) { - struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id]; refcount_t *refcount = &trans->refcount; struct gsi_trans_info *trans_info; - struct gsi_trans *polled; - u16 trans_index; bool last; /* We must hold the lock to release the last reference */ @@ -433,9 +422,6 @@ void gsi_trans_free(struct gsi_trans *trans) if (last) list_del(&trans->links); - polled = list_first_entry_or_null(&trans_info->polled, - struct gsi_trans, links); - spin_unlock_bh(&trans_info->spinlock); if (!last) @@ -456,14 +442,6 @@ void gsi_trans_free(struct gsi_trans *trans) /* This transaction is now free */ trans_info->polled_id++; - if (polled) { - trans_index = trans_info->polled_id % channel->tre_count; - WARN_ON(polled != &trans_info->trans[trans_index]); - } else { - WARN_ON(trans_info->polled_id != - trans_info->completed_id); - } - /* Releasing the reserved TREs implicitly frees the sgl[] and * (if present) info[] arrays, plus the transaction itself. */ From c30623ea0b3a9d766f34f75a326b8c610ca3105e Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:14 -0500 Subject: [PATCH 2/6] net: ipa: use IDs for last allocated transaction Use the allocated and free transaction IDs to determine whether the "last" transaction used for quiescing a channel is in allocated state. The last allocated transaction that has not been committed (if any) immediately precedes the first free transaction in the transaction array. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/net/ipa/gsi.c b/drivers/net/ipa/gsi.c index 0ea98fa5dee5..135e51980d79 100644 --- a/drivers/net/ipa/gsi.c +++ b/drivers/net/ipa/gsi.c @@ -711,6 +711,8 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) { struct gsi_trans_info *trans_info = &channel->trans_info; struct gsi_trans *trans; + u16 trans_index; + u16 trans_id; spin_lock_bh(&trans_info->spinlock); @@ -718,10 +720,14 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) * before we disabled transmits, so check for that. */ if (channel->toward_ipa) { - trans = list_last_entry_or_null(&trans_info->alloc, - struct gsi_trans, links); - if (trans) + /* The last allocated transaction precedes the first free */ + if (trans_info->allocated_id != trans_info->free_id) { + trans_id = trans_info->free_id - 1; + trans_index = trans_id % channel->tre_count; + trans = &trans_info->trans[trans_index]; goto done; + } + trans = list_last_entry_or_null(&trans_info->committed, struct gsi_trans, links); if (trans) From 897c0ce665d619227e19f59934115c1b7719621f Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:15 -0500 Subject: [PATCH 3/6] net: ipa: use IDs exclusively for last transaction Always use transaction IDs when finding the "last" transaction to await when quiescing a channel. This basically extends what was done in the previous patch to all other transaction state IDs. As a result we are no longer updating any transaction lists inside gsi_channel_trans_last(), so there's no need to take the spinlock. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/drivers/net/ipa/gsi.c b/drivers/net/ipa/gsi.c index 135e51980d79..0983a11409f2 100644 --- a/drivers/net/ipa/gsi.c +++ b/drivers/net/ipa/gsi.c @@ -714,8 +714,6 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) u16 trans_index; u16 trans_id; - spin_lock_bh(&trans_info->spinlock); - /* There is a small chance a TX transaction got allocated just * before we disabled transmits, so check for that. */ @@ -728,32 +726,46 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) goto done; } - trans = list_last_entry_or_null(&trans_info->committed, - struct gsi_trans, links); - if (trans) + /* Last committed transaction precedes the first allocated */ + if (trans_info->committed_id != trans_info->allocated_id) { + trans_id = trans_info->allocated_id - 1; + trans_index = trans_id % channel->tre_count; + trans = &trans_info->trans[trans_index]; goto done; - trans = list_last_entry_or_null(&trans_info->pending, - struct gsi_trans, links); - if (trans) + } + + /* Last pending transaction precedes the first committed */ + if (trans_info->pending_id != trans_info->committed_id) { + trans_id = trans_info->committed_id - 1; + trans_index = trans_id % channel->tre_count; + trans = &trans_info->trans[trans_index]; goto done; + } } /* Otherwise (TX or RX) we want to wait for anything that * has completed, or has been polled but not released yet. + * + * The last pending transaction precedes the first committed. */ - trans = list_last_entry_or_null(&trans_info->complete, - struct gsi_trans, links); - if (trans) + if (trans_info->completed_id != trans_info->pending_id) { + trans_id = trans_info->pending_id - 1; + trans_index = trans_id % channel->tre_count; + trans = &trans_info->trans[trans_index]; goto done; - trans = list_last_entry_or_null(&trans_info->polled, - struct gsi_trans, links); + } + if (trans_info->polled_id != trans_info->completed_id) { + trans_id = trans_info->completed_id - 1; + trans_index = trans_id % channel->tre_count; + trans = &trans_info->trans[trans_index]; + } else { + trans = NULL; + } done: /* Caller will wait for this, so take a reference */ if (trans) refcount_inc(&trans->refcount); - spin_unlock_bh(&trans_info->spinlock); - return trans; } From e68d1d1591fd70de0651e1af66db69540f556e73 Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:16 -0500 Subject: [PATCH 4/6] net: ipa: simplify gsi_channel_trans_last() Using a little logic we can simplify gsi_channel_trans_last(). The first condition in that function looks like this: if (trans_info->allocated_id != trans_info->free_id) And if that's false, we proceed to the next one: if (trans_info->committed_id != trans_info->allocated_id) Failure of the first test implies: trans_info->allocated_id == trans_info->free_id And therefore, the second one can be rewritten this way: if (trans_info->committed_id != trans_info->free_id) Substituting free_id for allocated_id and committed_id can also be done in the code blocks executed when these conditions yield true. The net result is that all three blocks for TX endpoints can be consolidated into just one. The two blocks of code at the end of that function (used for both TX and RX channels) can be similarly consolidated into a single block. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi.c | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/drivers/net/ipa/gsi.c b/drivers/net/ipa/gsi.c index 0983a11409f2..841a946bc286 100644 --- a/drivers/net/ipa/gsi.c +++ b/drivers/net/ipa/gsi.c @@ -718,46 +718,27 @@ static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) * before we disabled transmits, so check for that. */ if (channel->toward_ipa) { - /* The last allocated transaction precedes the first free */ - if (trans_info->allocated_id != trans_info->free_id) { + /* The last allocated, committed, or pending transaction + * precedes the first free transaction. + */ + if (trans_info->pending_id != trans_info->free_id) { trans_id = trans_info->free_id - 1; trans_index = trans_id % channel->tre_count; trans = &trans_info->trans[trans_index]; goto done; } - - /* Last committed transaction precedes the first allocated */ - if (trans_info->committed_id != trans_info->allocated_id) { - trans_id = trans_info->allocated_id - 1; - trans_index = trans_id % channel->tre_count; - trans = &trans_info->trans[trans_index]; - goto done; - } - - /* Last pending transaction precedes the first committed */ - if (trans_info->pending_id != trans_info->committed_id) { - trans_id = trans_info->committed_id - 1; - trans_index = trans_id % channel->tre_count; - trans = &trans_info->trans[trans_index]; - goto done; - } } /* Otherwise (TX or RX) we want to wait for anything that * has completed, or has been polled but not released yet. * - * The last pending transaction precedes the first committed. + * The last completed or polled transaction precedes the + * first pending transaction. */ - if (trans_info->completed_id != trans_info->pending_id) { + if (trans_info->polled_id != trans_info->pending_id) { trans_id = trans_info->pending_id - 1; trans_index = trans_id % channel->tre_count; trans = &trans_info->trans[trans_index]; - goto done; - } - if (trans_info->polled_id != trans_info->completed_id) { - trans_id = trans_info->completed_id - 1; - trans_index = trans_id % channel->tre_count; - trans = &trans_info->trans[trans_index]; } else { trans = NULL; } From 4601e75596cb7a4d538e7b9cf0b599b364acbae8 Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:17 -0500 Subject: [PATCH 5/6] net: ipa: further simplify gsi_channel_trans_last() Do a little more refactoring in gsi_channel_trans_last() to simplify it further. The resulting code should behave exactly as before. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi.c | 46 +++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/drivers/net/ipa/gsi.c b/drivers/net/ipa/gsi.c index 841a946bc286..16df699009a8 100644 --- a/drivers/net/ipa/gsi.c +++ b/drivers/net/ipa/gsi.c @@ -710,42 +710,32 @@ static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id) static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel) { struct gsi_trans_info *trans_info = &channel->trans_info; + u32 pending_id = trans_info->pending_id; struct gsi_trans *trans; - u16 trans_index; u16 trans_id; - /* There is a small chance a TX transaction got allocated just - * before we disabled transmits, so check for that. - */ - if (channel->toward_ipa) { - /* The last allocated, committed, or pending transaction + if (channel->toward_ipa && pending_id != trans_info->free_id) { + /* There is a small chance a TX transaction got allocated + * just before we disabled transmits, so check for that. + * The last allocated, committed, or pending transaction * precedes the first free transaction. */ - if (trans_info->pending_id != trans_info->free_id) { - trans_id = trans_info->free_id - 1; - trans_index = trans_id % channel->tre_count; - trans = &trans_info->trans[trans_index]; - goto done; - } + trans_id = trans_info->free_id - 1; + } else if (trans_info->polled_id != pending_id) { + /* Otherwise (TX or RX) we want to wait for anything that + * has completed, or has been polled but not released yet. + * + * The last completed or polled transaction precedes the + * first pending transaction. + */ + trans_id = pending_id - 1; + } else { + return NULL; } - /* Otherwise (TX or RX) we want to wait for anything that - * has completed, or has been polled but not released yet. - * - * The last completed or polled transaction precedes the - * first pending transaction. - */ - if (trans_info->polled_id != trans_info->pending_id) { - trans_id = trans_info->pending_id - 1; - trans_index = trans_id % channel->tre_count; - trans = &trans_info->trans[trans_index]; - } else { - trans = NULL; - } -done: /* Caller will wait for this, so take a reference */ - if (trans) - refcount_inc(&trans->refcount); + trans = &trans_info->trans[trans_id % channel->tre_count]; + refcount_inc(&trans->refcount); return trans; } From 8672bab7eb947222609bec8ed8a423bc72bccdbe Mon Sep 17 00:00:00 2001 From: Alex Elder Date: Fri, 2 Sep 2022 16:02:18 -0500 Subject: [PATCH 6/6] net: ipa: verify a few more IDs The completed transaction list is used in gsi_channel_trans_complete() to return the next transaction in completed state. Add some temporary checks to verify the transaction indicated by the completed ID matches the one first in this list. Similarly, we use the pending and completed transaction lists when cancelling pending transactions in gsi_channel_trans_cancel_pending(). Add temporary checks there to verify the transactions indicated by IDs match those tracked by these lists. Signed-off-by: Alex Elder Signed-off-by: David S. Miller --- drivers/net/ipa/gsi_trans.c | 46 ++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/drivers/net/ipa/gsi_trans.c b/drivers/net/ipa/gsi_trans.c index b4a6f2b56356..05ab4d052c68 100644 --- a/drivers/net/ipa/gsi_trans.c +++ b/drivers/net/ipa/gsi_trans.c @@ -237,8 +237,24 @@ gsi_channel_trans_mapped(struct gsi_channel *channel, u32 index) /* Return the oldest completed transaction for a channel (or null) */ struct gsi_trans *gsi_channel_trans_complete(struct gsi_channel *channel) { - return list_first_entry_or_null(&channel->trans_info.complete, - struct gsi_trans, links); + struct gsi_trans_info *trans_info = &channel->trans_info; + u16 trans_id = trans_info->completed_id; + struct gsi_trans *trans; + + trans = list_first_entry_or_null(&trans_info->complete, + struct gsi_trans, links); + + if (!trans) { + WARN_ON(trans_id != trans_info->pending_id); + return NULL; + } + + if (!WARN_ON(trans_id == trans_info->pending_id)) { + trans_id %= channel->tre_count; + WARN_ON(trans != &trans_info->trans[trans_id]); + } + + return trans; } /* Move a transaction from the allocated list to the committed list */ @@ -690,6 +706,8 @@ void gsi_channel_trans_cancel_pending(struct gsi_channel *channel) { struct gsi_trans_info *trans_info = &channel->trans_info; struct gsi_trans *trans; + struct gsi_trans *first; + struct gsi_trans *last; bool cancelled; /* channel->gsi->mutex is held by caller */ @@ -701,11 +719,33 @@ void gsi_channel_trans_cancel_pending(struct gsi_channel *channel) list_splice_tail_init(&trans_info->pending, &trans_info->complete); + first = list_first_entry_or_null(&trans_info->complete, + struct gsi_trans, links); + last = list_last_entry_or_null(&trans_info->complete, + struct gsi_trans, links); + spin_unlock_bh(&trans_info->spinlock); + /* All pending transactions are now completed */ + WARN_ON(cancelled != (trans_info->pending_id != + trans_info->committed_id)); + + trans_info->pending_id = trans_info->committed_id; + /* Schedule NAPI polling to complete the cancelled transactions */ - if (cancelled) + if (cancelled) { + u16 trans_id; + napi_schedule(&channel->napi); + + trans_id = trans_info->completed_id; + trans = &trans_info->trans[trans_id % channel->tre_count]; + WARN_ON(trans != first); + + trans_id = trans_info->pending_id - 1; + trans = &trans_info->trans[trans_id % channel->tre_count]; + WARN_ON(trans != last); + } } /* Issue a command to read a single byte from a channel */