mailbox: txdone_method shouldn't always be reset
PATCH 1/1 mailbox: txdone_method shouldn't always be reset
A client that knows how to drive txdone would temporarily "upgrade" the method to TXDONE_BY_ACK. But with the introduction of commit 33cd7123ac0ba ("mailbox: reset txdone_method TXDONE_BY_POLL if client knows_txdone") there is no longer a distinction between a channel in "upgraded" state or a channel for a controller that only supports TXDONE_BY_ACK. So upon freeing the channel it will be "downgraded" to TXDONE_BY_POLL.
But a channel that operates with the txdone method of TXDONE_BY_POLL requires that the controller implements the last_tx_done callback and that the associated hrtimer was initialized when the controller was registered.
So the core now relies on the fact that subsequent calls to mbox_request_channel() "upgrades" the channel to TXDONE_BY_ACK or it will dereference the non-initialized hrtimer.
The intention of commit 33cd7123ac0ba ("mailbox: reset txdone_method TXDONE_BY_POLL if client knows_txdone") is to not restart the hrtimer when the channel is in an "upgraded" state. So this patch reverts the commit, in order to never leave the channel is a invalid state, and instead only start the timer when we're in the "non-upgraded" POLL state.
--- drivers/mailbox/mailbox.c | 6 +++--- drivers/mailbox/pcc.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/mailbox/mailbox.c +3 -3
@@ -85,7 +85,7 @@ static void msg_submit(struct mbox_chan *chan) exit:spin_unlock_irqrestore (&chan->lock, flags); -if (!err && (chan->txdone_method &TXDONE_BY_POLL )) +if (!err && chan->txdone_method ==TXDONE_BY_POLL )/* kick start the timer immediately to avoid delays */ hrtimer_start (&chan->mbox->poll_hrt,0 ,HRTIMER_MODE_REL ); }
@@ -351,7 +351,7 @@ struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)init_completion (&chan->tx_complete);if (chan->txdone_method ==TXDONE_BY_POLL && cl->knows_txdone) - chan->txdone_method =TXDONE_BY_ACK ; + chan->txdone_method |=TXDONE_BY_ACK ;spin_unlock_irqrestore (&chan->lock, flags);
@@ -418,7 +418,7 @@ void mbox_free_channel(struct mbox_chan *chan)spin_lock_irqsave (&chan->lock, flags); chan->cl =NULL ; chan->active_req =NULL ; -if (chan->txdone_method ==TXDONE_BY_ACK ) +if (chan->txdone_method == (TXDONE_BY_POLL |TXDONE_BY_ACK )) chan->txdone_method =TXDONE_BY_POLL ;module_put (chan->mbox->dev->driver->owner);
drivers/mailbox/pcc.c +2 -2
@@ -265,7 +265,7 @@ struct mbox_chan *pcc_mbox_request_channel(struct mbox_client *cl,init_completion (&chan->tx_complete);if (chan->txdone_method ==TXDONE_BY_POLL && cl->knows_txdone) - chan->txdone_method =TXDONE_BY_ACK ; + chan->txdone_method |=TXDONE_BY_ACK ;spin_unlock_irqrestore (&chan->lock, flags);
@@ -311,7 +311,7 @@ void pcc_mbox_free_channel(struct mbox_chan *chan)spin_lock_irqsave (&chan->lock, flags); chan->cl =NULL ; chan->active_req =NULL ; -if (chan->txdone_method ==TXDONE_BY_ACK ) +if (chan->txdone_method == (TXDONE_BY_POLL |TXDONE_BY_ACK )) chan->txdone_method =TXDONE_BY_POLL ;spin_unlock_irqrestore (&chan->lock, flags);
Signature · 1 line
2.15.0
Jassi Brar
maintainerQuoted 7 lines · Bjorn Andersson
A client that knows how to drive txdone would temporarily "upgrade" the method to
TXDONE_BY_ACK. But with the introduction of commit33cd7123ac0ba("mailbox: resettxdone_methodTXDONE_BY_POLLif clientknows_txdone") there is no longer a distinction between a channel in "upgraded" state or a channel for a controller that only supportsTXDONE_BY_ACK. So upon freeing the channel it will be "downgraded" toTXDONE_BY_POLL.
Quoted 9 lines
But a channel that operates with the txdone method of
TXDONE_BY_POLLrequires that the controller implements thelast_tx_donecallback and that the associated hrtimer was initialized when the controller was registered.So the core now relies on the fact that subsequent calls to
mbox_request_channel()"upgrades" the channel toTXDONE_BY_ACKor it will dereference the non-initialized hrtimer.
I think we just need avoid the channel not running under POLL
Does this fix your issue?
drivers/mailbox/mailbox.c +0 -0
@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct
hrtimer *hrtimer)
for (i = 0; i < mbox->num_chans; i++) {
struct mbox_chan *chan = &mbox->chans[i];- if (chan->active_req && chan->cl) {
+ if (chan->active_req && chan->cl &&
+ chan->txdone_method == TXDONE_BY_POLL) {
txdone = chan->mbox->ops->last_tx_done(chan);
if (txdone)
tx_tick(chan, 0);Thanks.
Jassi Brar
maintainerQuoted 14 lines · Jassi Brar
Quoted 12 lines · Bjorn Andersson
A client that knows how to drive txdone would temporarily "upgrade" the method to
TXDONE_BY_ACK. But with the introduction of commit33cd7123ac0ba("mailbox: resettxdone_methodTXDONE_BY_POLLif clientknows_txdone") there is no longer a distinction between a channel in "upgraded" state or a channel for a controller that only supportsTXDONE_BY_ACK. So upon freeing the channel it will be "downgraded" toTXDONE_BY_POLL.But a channel that operates with the txdone method of
TXDONE_BY_POLLrequires that the controller implements thelast_tx_donecallback and that the associated hrtimer was initialized when the controller was registered.
Quoted 6 lines
Quoted 4 lines
So the core now relies on the fact that subsequent calls to
mbox_request_channel()"upgrades" the channel toTXDONE_BY_ACKor it will dereference the non-initialized hrtimer.I think we just need avoid the channel not running under POLL
And also detect if the channel was originally driven under POLL before mbox_request_channel.
So this is finally what I think we need.
drivers/mailbox/mailbox.c +0 -0
@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct
hrtimer *hrtimer)
for (i = 0; i < mbox->num_chans; i++) {
struct mbox_chan *chan = &mbox->chans[i];- if (chan->active_req && chan->cl) {
+ if (chan->active_req && chan->cl &&
+ chan->txdone_method == TXDONE_BY_POLL) {
txdone = chan->mbox->ops->last_tx_done(chan);
if (txdone)
tx_tick(chan, 0);
@@ -418,7 +419,7 @@ void mbox_free_channel(struct mbox_chan *chan)
spin_lock_irqsave(&chan->lock, flags);
chan->cl = NULL;
chan->active_req = NULL;
- if (chan->txdone_method == TXDONE_BY_ACK)
+ if (chan->txdone_method == TXDONE_BY_ACK && chan->mbox->txdone_poll)
chan->txdone_method = TXDONE_BY_POLL;
module_put(chan->mbox->dev->driver->owner);Quoted 30 lines · Jassi Brar
Quoted 14 lines · Jassi Brar
Quoted 12 lines · Bjorn Andersson
A client that knows how to drive txdone would temporarily "upgrade" the method to
TXDONE_BY_ACK. But with the introduction of commit33cd7123ac0ba("mailbox: resettxdone_methodTXDONE_BY_POLLif clientknows_txdone") there is no longer a distinction between a channel in "upgraded" state or a channel for a controller that only supportsTXDONE_BY_ACK. So upon freeing the channel it will be "downgraded" toTXDONE_BY_POLL.But a channel that operates with the txdone method of
TXDONE_BY_POLLrequires that the controller implements thelast_tx_donecallback and that the associated hrtimer was initialized when the controller was registered.Quoted 6 lines
Quoted 4 lines
So the core now relies on the fact that subsequent calls to
mbox_request_channel()"upgrades" the channel toTXDONE_BY_ACKor it will dereference the non-initialized hrtimer.I think we just need avoid the channel not running under POLL
And also detect if the channel was originally driven under POLL before
mbox_request_channel.
So this is finally what I think we need.drivers/mailbox/mailbox.c +0
-0
drivers/mailbox/mailbox.c static enum hrtimer_restart txdone_hrtimer(struct · line 129
Quoted 7 lines
drivers/mailbox/mailbox.c +2
-1@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)for (i =0 ; i < mbox->num_chans; i++) {struct mbox_chan *chan = &mbox->chans[i]; -if (chan->active_req && chan->cl) { +if (chan->active_req && chan->cl && + chan->txdone_method ==TXDONE_BY_POLL ) {
The hrtimer code will crash before reaching this point if the channel wasn't TXDONE_BY_POLL when it was created, so this part is not needed.
drivers/mailbox/mailbox.c void mbox_free_channel(struct mbox_chan *chan) · line 422
Quoted 9 lines
drivers/mailbox/mailbox.c +0
-0txdone = chan->mbox->ops->last_tx_done (chan);if (txdone)tx_tick(chan, 0);drivers/mailbox/mailbox.c +1
-1@@ -418,7 +419,7 @@ void mbox_free_channel(struct mbox_chan *chan)spin_lock_irqsave (&chan->lock, flags); chan->cl =NULL ; chan->active_req =NULL ; -if (chan->txdone_method ==TXDONE_BY_ACK ) +if (chan->txdone_method ==TXDONE_BY_ACK && chan->mbox->txdone_poll)
This should be enough, but the logic here is not obvious. This relies on the fact that a mbox with txdone_poll would have been initialized as txdone = POLL and that txdone now being ACK would imply that it was "upgraded" in the request path.
So at least this would have to be captured in a comment.
(Using a bitmask with both POLL | ACK set did capture this in a more direct way, but obviously wasn't clear to Sudeep)
drivers/mailbox/mailbox.c void mbox_free_channel(struct mbox_chan *chan) · line 425
Quoted 3 lines
drivers/mailbox/mailbox.c +0
-0chan->txdone_method =TXDONE_BY_POLL ;module_put (chan->mbox->dev->driver->owner);
Regards,
Bjorn
A message that is not in the archive (sent off-list or not yet received): CABb+yY0cwDCG=zKBT1ckoLSi+cHnmxcNbi2BXz2EstVTAo169w@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CABb+yY0SmdLyo7g46VqznHZravBv7qUTZcuQ5o4v+4QGMw-pyQ@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CABb+yY0EkKUMfLrQs4OjHkVPKghKND6hChfzmjS60asTrAcYyg@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CABb+yY0GkRpPgAgAFOMxLJ_7qh4wV7R5At8VFOxepuEN76K1QQ@mail.gmail.com
A message that is not in the archive (sent off-list or not yet received): CABb+yY0nSnkYQtL28UwsqRCRkaZHSU2O-RM9r=50n0n7Owkf+A@mail.gmail.com
Quoted 19 lines · Jassi Brar
On 16 Nov 2017 23:12, "Bjorn Andersson" <[email protected]> wrote:
drivers/mailbox/mailbox.c static enum hrtimer_restart txdone_hrtimer(struct · line 129
Quoted 12 lines · Jassi Brar
drivers/mailbox/mailbox.c +2
-1@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)for (i =0 ; i < mbox->num_chans; i++) {struct mbox_chan *chan = &mbox->chans[i]; -if (chan->active_req && chan->cl) { +if (chan->active_req && chan->cl && + chan->txdone_method ==TXDONE_BY_POLL ) {The hrtimer code will crash before reaching this point if the channel wasn't
TXDONE_BY_POLLwhen it was created, so this part is not needed.
Quoted 4 lines
We have one timer for all channels of a controller. While this channel may be run by ACK, some other might need to be POLLed. And we want to avoid polling this channel.
Oh, you're right.
But the fact that the timer function will poll channels that are "upgraded" to ACK is a separate issue.
Quoted 17 lines
drivers/mailbox/mailbox.c void mbox_free_channel(struct mbox_chan *chan) · line 422
Quoted 10 lines
drivers/mailbox/mailbox.c +0
-0txdone = chan->mbox->ops->last_tx_done (chan);if (txdone)tx_tick(chan, 0);drivers/mailbox/mailbox.c +1
-1@@ -418,7 +419,7 @@ void mbox_free_channel(struct mbox_chan *chan)spin_lock_irqsave (&chan->lock, flags); chan->cl =NULL ; chan->active_req =NULL ; -if (chan->txdone_method ==TXDONE_BY_ACK ) + if (chan->txdone_method == TXDONE_BY_ACK &&
chan->mbox->txdone_poll)This should be enough, but the logic here is not obvious. This relies on the fact that a mbox with
txdone_pollwould have been initialized as txdone = POLL and that txdone now being ACK would imply that it was "upgraded" in the request path.
Quoted 5 lines
So at least this would have to be captured in a comment.
Sure I'll add a comment.
Thanks
Regards,
Bjorn
Quoted 25 lines · Bjorn Andersson
Quoted 19 lines · Jassi Brar
On 16 Nov 2017 23:12, "Bjorn Andersson" <[email protected]> wrote:
drivers/mailbox/mailbox.c static enum hrtimer_restart txdone_hrtimer(struct · line 129
Quoted 12 lines · Jassi Brar
drivers/mailbox/mailbox.c +2
-1@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)for (i =0 ; i < mbox->num_chans; i++) {struct mbox_chan *chan = &mbox->chans[i]; -if (chan->active_req && chan->cl) { +if (chan->active_req && chan->cl && + chan->txdone_method ==TXDONE_BY_POLL ) {The hrtimer code will crash before reaching this point if the channel wasn't
TXDONE_BY_POLLwhen it was created, so this part is not needed.Quoted 4 lines
We have one timer for all channels of a controller. While this channel may be run by ACK, some other might need to be POLLed. And we want to avoid polling this channel.
Quoted 5 lines
Oh, you're right.
But the fact that the timer function will poll channels that are "upgraded" to ACK is a separate issue.
Ah right, I recall now that's the reason Alexey had patch introducing timer per channel.
Signature · 2 lines
Regards, Sudeep
Jassi Brar
maintainerQuoted 25 lines · Bjorn Andersson
Quoted 19 lines · Jassi Brar
On 16 Nov 2017 23:12, "Bjorn Andersson" <[email protected]> wrote:
drivers/mailbox/mailbox.c static enum hrtimer_restart txdone_hrtimer(struct · line 129
Quoted 12 lines · Jassi Brar
drivers/mailbox/mailbox.c +2
-1@@ -124,7 +124,8 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer)for (i =0 ; i < mbox->num_chans; i++) {struct mbox_chan *chan = &mbox->chans[i]; -if (chan->active_req && chan->cl) { +if (chan->active_req && chan->cl && + chan->txdone_method ==TXDONE_BY_POLL ) {The hrtimer code will crash before reaching this point if the channel wasn't
TXDONE_BY_POLLwhen it was created, so this part is not needed.Quoted 4 lines
We have one timer for all channels of a controller. While this channel may be run by ACK, some other might need to be POLLed. And we want to avoid polling this channel.
Quoted 5 lines
Oh, you're right.
But the fact that the timer function will poll channels that are "upgraded" to ACK is a separate issue.
After adding that extra check, the timer function never polls such a channel.
thnx
(- Remove Alexey Klimov's ARM id as he has left the company, adding his personal id instead)
Quoted 14 lines · Jassi Brar
Quoted 12 lines · Bjorn Andersson
A client that knows how to drive txdone would temporarily "upgrade" the method to
TXDONE_BY_ACK. But with the introduction of commit33cd7123ac0ba("mailbox: resettxdone_methodTXDONE_BY_POLLif clientknows_txdone") there is no longer a distinction between a channel in "upgraded" state or a channel for a controller that only supportsTXDONE_BY_ACK. So upon freeing the channel it will be "downgraded" toTXDONE_BY_POLL.But a channel that operates with the txdone method of
TXDONE_BY_POLLrequires that the controller implements thelast_tx_donecallback and that the associated hrtimer was initialized when the controller was registered.
Quoted 8 lines
Quoted 4 lines
So the core now relies on the fact that subsequent calls to
mbox_request_channel()"upgrades" the channel toTXDONE_BY_ACKor it will dereference the non-initialized hrtimer.I think we just need avoid the channel not running under POLL
Does this fix your issue?
Unfortunately only Alexey had fast reproducer of the issue reported with original commit. I will try to run on my setup, but it was hard to reproduce(mainly lack of firmware support for multiple virtual channels). I will try my best to test this.
Sorry for not considering temporary "upgrade" use case.
Signature · 2 lines
Regards, Sudeep