Skip to content

Commit 79b9677

Browse files
kaysieversJames Bottomley
authored andcommitted
[SCSI] sr: check_events() ignore GET_EVENT when TUR says otherwise
Some broken devices indicates that media has changed on every GET_EVENT_STATUS_NOTIFICATION. This translates into MEDIA_CHANGE uevent on every open() which lets udev run into a loop. Verify GET_EVENT result against TUR and if it generates spurious events for several times in a row, ignore the GET_EVENT events, and trust only the TUR status. This is the log of a USB stick with a (broken) fake CDROM drive: scsi 5:0:0:0: Direct-Access SanDisk U3 Cruzer Micro 8.02 PQ: 0 ANSI: 0 CCS sd 5:0:0:0: Attached scsi generic sg3 type 0 scsi 5:0:0:1: CD-ROM SanDisk U3 Cruzer Micro 8.02 PQ: 0 ANSI: 0 sd 5:0:0:0: [sdb] Attached SCSI removable disk sr2: scsi3-mmc drive: 48x/48x tray sr 5:0:0:1: Attached scsi CD-ROM sr2 sr 5:0:0:1: Attached scsi generic sg4 type 5 sr2: GET_EVENT and TUR disagree continuously, suppress GET_EVENT events sd 5:0:0:0: [sdb] 31777279 512-byte logical blocks: (16.2 GB/15.1 GiB) sd 5:0:0:0: [sdb] No Caching mode page present sd 5:0:0:0: [sdb] Assuming drive cache: write through sd 5:0:0:0: [sdb] No Caching mode page present sd 5:0:0:0: [sdb] Assuming drive cache: write through sdb: sdb1 -tj: Updated to consider only spurious GET_EVENT events among different types of disagreement and allow using TUR for kernel event polling after GET_EVENT is ignored. Reported-By: Markus Rathgeb maggu2810@googlemail.com Signed-off-by: Kay Sievers <kay.sievers@vrfy.org> Signed-off-by: Tejun Heo <tj@kernel.org> Cc: stable@kernel.org # >= v2.6.38, fixes udev busy looping w/ certain devices Signed-off-by: James Bottomley <JBottomley@Parallels.com>
1 parent 57a6fa9 commit 79b9677

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎drivers/scsi/sr.c‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,33 @@ static unsigned int sr_check_events(struct cdrom_device_info *cdi,
221221
return 0;
222222

223223
events = sr_get_events(cd->device);
224+
cd->get_event_changed |= events & DISK_EVENT_MEDIA_CHANGE;
225+
226+
/*
227+
* If earlier GET_EVENT_STATUS_NOTIFICATION and TUR did not agree
228+
* for several times in a row. We rely on TUR only for this likely
229+
* broken device, to prevent generating incorrect media changed
230+
* events for every open().
231+
*/
232+
if (cd->ignore_get_event) {
233+
events &= ~DISK_EVENT_MEDIA_CHANGE;
234+
goto do_tur;
235+
}
236+
224237
/*
225238
* GET_EVENT_STATUS_NOTIFICATION is enough unless MEDIA_CHANGE
226239
* is being cleared. Note that there are devices which hang
227240
* if asked to execute TUR repeatedly.
228241
*/
229-
if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
230-
goto skip_tur;
242+
if (cd->device->changed) {
243+
events |= DISK_EVENT_MEDIA_CHANGE;
244+
cd->device->changed = 0;
245+
cd->tur_changed = true;
246+
}
231247

248+
if (!(clearing & DISK_EVENT_MEDIA_CHANGE))
249+
return events;
250+
do_tur:
232251
/* let's see whether the media is there with TUR */
233252
last_present = cd->media_present;
234253
ret = scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
@@ -242,12 +261,31 @@ static unsigned int sr_check_events(struct cdrom_device_info *cdi,
242261
(scsi_sense_valid(&sshdr) && sshdr.asc != 0x3a);
243262

244263
if (last_present != cd->media_present)
245-
events |= DISK_EVENT_MEDIA_CHANGE;
246-
skip_tur:
264+
cd->device->changed = 1;
265+
247266
if (cd->device->changed) {
248267
events |= DISK_EVENT_MEDIA_CHANGE;
249268
cd->device->changed = 0;
269+
cd->tur_changed = true;
270+
}
271+
272+
if (cd->ignore_get_event)
273+
return events;
274+
275+
/* check whether GET_EVENT is reporting spurious MEDIA_CHANGE */
276+
if (!cd->tur_changed) {
277+
if (cd->get_event_changed) {
278+
if (cd->tur_mismatch++ > 8) {
279+
sdev_printk(KERN_WARNING, cd->device,
280+
"GET_EVENT and TUR disagree continuously, suppress GET_EVENT events\n");
281+
cd->ignore_get_event = true;
282+
}
283+
} else {
284+
cd->tur_mismatch = 0;
285+
}
250286
}
287+
cd->tur_changed = false;
288+
cd->get_event_changed = false;
251289

252290
return events;
253291
}

‎drivers/scsi/sr.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ typedef struct scsi_cd {
4141
unsigned readcd_known:1; /* drive supports READ_CD (0xbe) */
4242
unsigned readcd_cdda:1; /* reading audio data using READ_CD */
4343
unsigned media_present:1; /* media is present */
44+
45+
/* GET_EVENT spurious event handling, blk layer guarantees exclusion */
46+
int tur_mismatch; /* nr of get_event TUR mismatches */
47+
bool tur_changed:1; /* changed according to TUR */
48+
bool get_event_changed:1; /* changed according to GET_EVENT */
49+
bool ignore_get_event:1; /* GET_EVENT is unreliable, use TUR */
50+
4451
struct cdrom_device_info cdi;
4552
/* We hold gendisk and scsi_device references on probe and use
4653
* the refs on this kref to decide when to release them */

0 commit comments

Comments
 (0)