Skip to contentSkip to conversation

USB: cdc-acm: fix racy TIOCMIWAIT implementation

PATCH Superseded Johan Hovold 2 participants 14 messages Sep 7 – Sep 11

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: cdc-acm: fix racy TIOCMIWAIT implementation

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

Fixes 5a6a62bdb925 ("cdc-acm: add TIOCMIWAIT")
Cc Oliver Neukum <[email protected]>
---
 drivers/usb/class/cdc-acm.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
drivers/usb/class/cdc-acm.c +15 -14
index 7bc5329fa3ed..00c250515e83 100644
@@ -1027,7 +1027,10 @@ static int wait_serial_change(struct acm *acm, unsigned long arg)
 	DECLARE_WAITQUEUE(wait, current);
 	struct async_icount old, new;
 
-	do {
+	add_wait_queue(&acm->wioctl, &wait);
+	for (;;) {
+		set_current_state(TASK_INTERRUPTIBLE);
+
 		spin_lock_irq(&acm->read_lock);
 		old = acm->oldcount;
 		new = acm->iocount;
@@ -1044,22 +1047,20 @@ static int wait_serial_change(struct acm *acm, unsigned long arg)
 			old.rng != new.rng)
 			break;
 
-		add_wait_queue(&acm->wioctl, &wait);
-		set_current_state(TASK_INTERRUPTIBLE);
-		schedule();
-		remove_wait_queue(&acm->wioctl, &wait);
 		if (acm->disconnected) {
-			if (arg & TIOCM_CD)
-				break;
-			else
-				rv = -ENODEV;
-		} else {
-			if (signal_pending(current))
-				rv = -ERESTARTSYS;
+			rv = -ENODEV;
+			break;
 		}
-	} while (!rv);
 
-	
+		schedule();
+
+		if (signal_pending(current)) {
+			rv = -ERESTARTSYS;
+			break;
+		}
+	}
+	__set_current_state(TASK_RUNNING);
+	remove_wait_queue(&acm->wioctl, &wait);
 
 	return rv;
 }
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 3 lines · Johan Hovold

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

I am sorry, but in this case I have to ask:
Isn't TIOCMIWAIT inherently racy against the hardware?

Regards
Oliver

#2 lore raw
Quoted 7 lines · Oliver Neukum
Quoted 3 lines · Johan Hovold

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

I am sorry, but in this case I have to ask:
Isn't TIOCMIWAIT inherently racy against the hardware?

What do you mean?

Looking at the implementation again now, it seems further changes are needed to fix the implementation, though. The completion handler should not be updating old_count. That's for each TIOCMIWAIT to do.

Johan

#3 lore raw
Quoted 10 lines · Johan Hovold
Quoted 7 lines · Oliver Neukum
Quoted 3 lines · Johan Hovold

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

I am sorry, but in this case I have to ask:
Isn't TIOCMIWAIT inherently racy against the hardware?

What do you mean?

Ok, I think I see what you're getting at. Nothing prevents a modem status change just before calling TIOCMIWAIT from being missed. That is indeed a general problem with the interface.

Johan

maintainer
#4 lore raw
In reply to Johan Hovold
Quoted 10 lines · Johan Hovold
Quoted 7 lines · Oliver Neukum
Quoted 3 lines · Johan Hovold

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

I am sorry, but in this case I have to ask:
Isn't TIOCMIWAIT inherently racy against the hardware?

What do you mean?

The interface does not state from or to which state it changed. Now you could say that it is implicitly the last time TIOCMGET was called, but that is

1. not clearly stated
2. not enforceable

Quoted 3 lines

Looking at the implementation again now, it seems further changes are needed to fix the implementation, though. The completion handler should not be updating old_count. That's for each TIOCMIWAIT to do.

I am sorry to be obnoxious about that, but I am afraid that would break TIOCGICOUNT

Regards
Oliver

#5 lore raw
Quoted 10 lines · Oliver Neukum
Quoted 9 lines · Johan Hovold
Quoted 7 lines · Oliver Neukum
Quoted 3 lines · Johan Hovold

The wakeup condition needs to be checked after adding the task to the waitqueue and updating the task state to avoid missing a racing modem status update or disconnect.

I am sorry, but in this case I have to ask:
Isn't TIOCMIWAIT inherently racy against the hardware?

Quoted 8 lines
Quoted 1 line

What do you mean?

The interface does not state from or to which state it changed. Now you could say that it is implicitly the last time TIOCMGET was called, but that is

1. not clearly stated
2. not enforceable

Right. The original implementation (and serial core) have always stored the counters on entry and compared to that. So that is the defacto semantics.

Quoted 6 lines
Quoted 3 lines

Looking at the implementation again now, it seems further changes are needed to fix the implementation, though. The completion handler should not be updating old_count. That's for each TIOCMIWAIT to do.

I am sorry to be obnoxious about that, but I am afraid that would break TIOCGICOUNT

Why do you think so? TIOCGICOUNT just returns the current counters.

Johan

maintainer
#6 lore raw
Quoted 4 lines · Johan Hovold
Quoted 3 lines · Oliver Neukum
Quoted 2 lines · Johan Hovold
Quoted 1 line · Oliver Neukum

On 07.09.26 08:55, Johan Hovold wrote:

[..]

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Looking at the implementation again now, it seems further changes are needed to fix the implementation, though. The completion handler should not be updating old_count. That's for each TIOCMIWAIT to do.

I am sorry to be obnoxious about that, but I am afraid that would break TIOCGICOUNT

Why do you think so? TIOCGICOUNT just returns the current counters.

Yes, but user space can call TIOCGICOUNT multiple times in sequence _without_ calling TIOCMIWAIT in between.
In that case the counters have to increase if a line changes, don't they? For that to happen you need to handle a status change in the completion handler.

Regards
Oliver

#7 lore raw
Quoted 16 lines · Oliver Neukum
Quoted 4 lines · Johan Hovold
Quoted 3 lines · Oliver Neukum
Quoted 2 lines · Johan Hovold
Quoted 1 line · Oliver Neukum

On 07.09.26 08:55, Johan Hovold wrote:

[..]

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Looking at the implementation again now, it seems further changes are needed to fix the implementation, though. The completion handler should not be updating old_count. That's for each TIOCMIWAIT to do.

I am sorry to be obnoxious about that, but I am afraid that would break TIOCGICOUNT

Quoted 7 lines
Quoted 1 line

Why do you think so? TIOCGICOUNT just returns the current counters.

Yes, but user space can call TIOCGICOUNT multiple times in sequence _without_ calling TIOCMIWAIT in between.
In that case the counters have to increase if a line changes, don't they? For that to happen you need to handle a status change in the completion handler.

Yes, the completion handler needs to handle the status change (i.e. compare new and previous status and increment the counters), but it shouldn't update any old *icount* structure.

Johan

maintainer
#8 lore raw
Quoted 12 lines · Johan Hovold
Quoted 10 lines · Oliver Neukum
Quoted 1 line · Johan Hovold

On Mon, Sep 07, 2026 at 11:50:41AM +0200, Oliver Neukum wrote:

Yes, but user space can call TIOCGICOUNT multiple times in sequence _without_ calling TIOCMIWAIT in between.
In that case the counters have to increase if a line changes, don't they? For that to happen you need to handle a status change in the completion handler.

Quoted 3 lines

Yes, the completion handler needs to handle the status change (i.e. compare new and previous status and increment the counters), but it shouldn't update any old *icount* structure.

We could do that and it would indeed be simpler.

But it would change the semantics so that past status changes no longer show up in TIOCMIWAIT. Is that really a good idea?

Regards
Oliver

#9 lore raw
Quoted 15 lines · Oliver Neukum
Quoted 12 lines · Johan Hovold
Quoted 10 lines · Oliver Neukum
Quoted 1 line · Johan Hovold

On Mon, Sep 07, 2026 at 11:50:41AM +0200, Oliver Neukum wrote:

Yes, but user space can call TIOCGICOUNT multiple times in sequence _without_ calling TIOCMIWAIT in between.
In that case the counters have to increase if a line changes, don't they? For that to happen you need to handle a status change in the completion handler.

Quoted 8 lines
Quoted 3 lines

Yes, the completion handler needs to handle the status change (i.e. compare new and previous status and increment the counters), but it shouldn't update any old *icount* structure.

We could do that and it would indeed be simpler.

But it would change the semantics so that past status changes no longer show up in TIOCMIWAIT. Is that really a good idea?

Yes, as the current behaviour is both non-standard and inconsistent.

If you have multiple tasks waiting waiting on status change, then only one of them will detect it, for example.

And if there are multiple changes (before or during TIOCMIWAIT) events may be lost too.

Johan

maintainer
#10 lore raw
Quoted 8 lines · Johan Hovold
Quoted 6 lines · Oliver Neukum

We could do that and it would indeed be simpler.

But it would change the semantics so that past status changes no longer show up in TIOCMIWAIT. Is that really a good idea?

Quoted 7 lines

Yes, as the current behaviour is both non-standard and inconsistent.

If you have multiple tasks waiting waiting on status change, then only one of them will detect it, for example.

And if there are multiple changes (before or during TIOCMIWAIT) events may be lost too.

Very well. Ample reasons. Do you have a patch or do you want me to fix it?

Regards
Oliver

#11 lore raw
Quoted 13 lines · Oliver Neukum
Quoted 10 lines · Johan Hovold
Quoted 6 lines · Oliver Neukum

We could do that and it would indeed be simpler.

But it would change the semantics so that past status changes no longer show up in TIOCMIWAIT. Is that really a good idea?

Yes, as the current behaviour is both non-standard and inconsistent.

Quoted 7 lines
Quoted 5 lines

If you have multiple tasks waiting waiting on status change, then only one of them will detect it, for example.

And if there are multiple changes (before or during TIOCMIWAIT) events may be lost too.

Very well. Ample reasons. Do you have a patch or do you want me to fix it?

I sent a v2 yesterday that should take care of it all:

	https://lore.kernel.org/all/[email protected]/

Johan

maintainer
#12 lore raw
Quoted 9 lines · Johan Hovold
Quoted 4 lines · Oliver Neukum

Very well. Ample reasons. Do you have a patch or do you want me to fix it?

I sent a v2 yesterday that should take care of it all:

	https://lore.kernel.org/all/[email protected]/

Thank you. I misinterpreted your intent. Sorry.

I am afraid that patch has a few weaknesses.

1. It mixes changes to measuring the old count and to how the sleeping works
2. If you want to change the logic at all, then we have a first check for changes that is almost sure to fail.

It seems to me that if you are going to touch the logic there at all, the sensible order of actions in the loop would be

A - check for signals
B - schedule
C - check for changes

That way we

i - avoid calling into the scheduler if signals are pending
ii - avoid returning -ERESTARTSYS if we have a result we can report iii - avoid rechecking for changes after only a few dozen cycles We have an inherent race against the hardware. The chance that we don't have to sleep is essentially zero.

Regards
Oliver

#13 lore raw
Quoted 11 lines · Oliver Neukum
Quoted 3 lines · Johan Hovold

I sent a v2 yesterday that should take care of it all:

	https://lore.kernel.org/all/[email protected]/

Thank you. I misinterpreted your intent. Sorry.

I am afraid that patch has a few weaknesses.

1. It mixes changes to measuring the old count and to how the sleeping works

It could possibly be split in two (i.e. my v1 + a separate change to drop the old count), but the commit fixes the "racy tiocmiwait implementation" and all of these races were there from the start. So it does not seem necessary to break it up.

Quoted 6 lines

2. If you want to change the logic at all, then we have a first check for changes that is almost sure to fail.

It seems to me that if you are going to touch the logic there at all, the sensible order of actions in the loop would be

Quoted 8 lines

A - check for signals
B - schedule
C - check for changes

That way we

i - avoid calling into the scheduler if signals are pending
ii - avoid returning -ERESTARTSYS if we have a result we can report

This would be further changes to the current behaviour, which is also reflected in serial core. Can possibly be done on top.

Quoted 3 lines

iii - avoid rechecking for changes after only a few dozen cycles We have an inherent race against the hardware. The chance that we don't have to sleep is essentially zero.

Sure, but the code is more readable this way with a standard wait loop (and it highlights the limitations of the interface).

Johan