linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running
@ 2020-12-28  2:53 Xie He
       [not found] ` <20201228091654.1963-1-hdanton@sina.com>
  2020-12-28 23:11 ` David Miller
  0 siblings, 2 replies; 4+ messages in thread
From: Xie He @ 2020-12-28  2:53 UTC (permalink / raw)
  To: David S. Miller, Jakub Kicinski, netdev, linux-kernel, Krzysztof Halasa
  Cc: Xie He

ppp_cp_event is called directly or indirectly by ppp_rx with "ppp->lock"
held. It may call mod_timer to add a new timer. However, at the same time
ppp_timer may be already running and waiting for "ppp->lock". In this
case, there's no need for ppp_timer to continue running and it can just
exit.

If we let ppp_timer continue running, it may call add_timer. This causes
kernel panic because add_timer can't be called with a timer pending.
This patch fixes this problem.

Fixes: e022c2f07ae5 ("WAN: new synchronous PPP implementation for generic HDLC.")
Cc: Krzysztof Halasa <khc@pm.waw.pl>
Signed-off-by: Xie He <xie.he.0141@gmail.com>
---
 drivers/net/wan/hdlc_ppp.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/net/wan/hdlc_ppp.c b/drivers/net/wan/hdlc_ppp.c
index 64f855651336..261b53fc8e04 100644
--- a/drivers/net/wan/hdlc_ppp.c
+++ b/drivers/net/wan/hdlc_ppp.c
@@ -569,6 +569,13 @@ static void ppp_timer(struct timer_list *t)
 	unsigned long flags;
 
 	spin_lock_irqsave(&ppp->lock, flags);
+	/* mod_timer could be called after we entered this function but
+	 * before we got the lock.
+	 */
+	if (timer_pending(&proto->timer)) {
+		spin_unlock_irqrestore(&ppp->lock, flags);
+		return;
+	}
 	switch (proto->state) {
 	case STOPPING:
 	case REQ_SENT:
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running
       [not found] ` <20201228091654.1963-1-hdanton@sina.com>
@ 2020-12-28 10:22   ` Xie He
       [not found]     ` <20201229081034.2026-1-hdanton@sina.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Xie He @ 2020-12-28 10:22 UTC (permalink / raw)
  To: Hillf Danton; +Cc: LKML, Krzysztof Halasa

On Mon, Dec 28, 2020 at 1:17 AM Hillf Danton <hdanton@sina.com> wrote:
>
> On Sun, 27 Dec 2020 18:53:39 -0800 Xie He wrote:
> > ppp_cp_event is called directly or indirectly by ppp_rx with "ppp->lock"
> > held. It may call mod_timer to add a new timer. However, at the same time
> > ppp_timer may be already running and waiting for "ppp->lock". In this
> > case, there's no need for ppp_timer to continue running and it can just
> > exit.
>
> Because the timer callback loses the race in acquiring the ppp->lock
> does not mean it should abort.

I think aborting ppp_timer is the correct solution. When mod_timer is
called by ppp_cp_event, which is (directly or indirectly) called by
ppp_rx, this means we received something on the line that makes the
original timer no longer necessary. If the timer is pending, mod_timer
will delete it. If the timer is running and waiting for the lock, I
think it should abort. This way it would appear that the timer hasn't
fired and is deleted before it fires.

> > If we let ppp_timer continue running, it may call add_timer. This causes
> > kernel panic because add_timer can't be called with a timer pending.
>
> Meanwhie we can defuse the peril following add_timer() added in
> e022c2f07ae5, say by replacing add_timer() with mod_timer().

The code path that calls add_timer is for sending keep-alive packets
when operating in the OPENED state. If we have just changed to the
OPENED state in ppp_cp_event, we should wait for the amount of time
set by the (2nd) mod_timer call in ppp_cp_event, before firing the
timer. We shouldn't fire the timer immediately after we change to the
OPENED state. This is not the intention of the (2nd) mod_timer call in
ppp_cp_event. Therefore aborting ppp_timer is the correct solution.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running
  2020-12-28  2:53 [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running Xie He
       [not found] ` <20201228091654.1963-1-hdanton@sina.com>
@ 2020-12-28 23:11 ` David Miller
  1 sibling, 0 replies; 4+ messages in thread
From: David Miller @ 2020-12-28 23:11 UTC (permalink / raw)
  To: xie.he.0141; +Cc: kuba, netdev, linux-kernel, khc

From: Xie He <xie.he.0141@gmail.com>
Date: Sun, 27 Dec 2020 18:53:39 -0800

> ppp_cp_event is called directly or indirectly by ppp_rx with "ppp->lock"
> held. It may call mod_timer to add a new timer. However, at the same time
> ppp_timer may be already running and waiting for "ppp->lock". In this
> case, there's no need for ppp_timer to continue running and it can just
> exit.
> 
> If we let ppp_timer continue running, it may call add_timer. This causes
> kernel panic because add_timer can't be called with a timer pending.
> This patch fixes this problem.
> 
> Fixes: e022c2f07ae5 ("WAN: new synchronous PPP implementation for generic HDLC.")
> Cc: Krzysztof Halasa <khc@pm.waw.pl>
> Signed-off-by: Xie He <xie.he.0141@gmail.com>

Applied and queued up for -stable, thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running
       [not found]     ` <20201229081034.2026-1-hdanton@sina.com>
@ 2020-12-29 18:20       ` Xie He
  0 siblings, 0 replies; 4+ messages in thread
From: Xie He @ 2020-12-29 18:20 UTC (permalink / raw)
  To: Hillf Danton; +Cc: LKML, Krzysztof Halasa

On Tue, Dec 29, 2020 at 12:10 AM Hillf Danton <hdanton@sina.com> wrote:
>
> >The code path that calls add_timer is for sending keep-alive packets
> >when operating in the OPENED state. If we have just changed to the
> >OPENED state in ppp_cp_event, we should wait for the amount of time
> >set by the (2nd) mod_timer call in ppp_cp_event, before firing the
>
> What if your change also covers the first case of mod_timer() in
> ppp_cp_event()?

Yes, for the 1st mod_timer call in ppp_cp_event, the situation is the
same. If it is called, it means we are sending out a Configure Request
or Terminate Request. In this case, we should wait for the amount of
time set by this mod_timer call, before firing the timer. We shouldn't
fire the timer immediately because this is not the intention of this
mod_timer call.

> >timer. We shouldn't fire the timer immediately after we change to the
> >OPENED state. This is not the intention of the (2nd) mod_timer call in
> >ppp_cp_event. Therefore aborting ppp_timer is the correct solution.
> >
> Given an expiring timer, is it the right time to call ppp_tx_flush() in
> addition to add/mod_timer()?

Do you mean when we are aborting ppp_timer, whether we need to call
ppp_tx_flush in the aborted ppp_timer? I don't think so. Because when
ppp_rx (which directly or indirectly calls ppp_cp_event) releases the
lock, it will call ppp_tx_flush. So we don't need to call it again.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-12-29 18:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-28  2:53 [PATCH net v2] net: hdlc_ppp: Fix issues when mod_timer is called while timer is running Xie He
     [not found] ` <20201228091654.1963-1-hdanton@sina.com>
2020-12-28 10:22   ` Xie He
     [not found]     ` <20201229081034.2026-1-hdanton@sina.com>
2020-12-29 18:20       ` Xie He
2020-12-28 23:11 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).