Received: by 10.223.176.5 with SMTP id f5csp869804wra; Fri, 2 Feb 2018 07:25:53 -0800 (PST) X-Google-Smtp-Source: AH8x224ZcpzO9uzhWkL+NKseoBipR7RCqcCUSwjtj+bMbjYIvUhgd640cmfs1ejNks2QYspNyGUQ X-Received: by 10.101.67.193 with SMTP id n1mr31728690pgp.116.1517585153166; Fri, 02 Feb 2018 07:25:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517585153; cv=none; d=google.com; s=arc-20160816; b=bmco2m6+Bu6YqkdPQ9Vn9CEOIhv3199H1PyOpNWhmuri5CO/nzoksM9UDXtWp58ggC EqqouNipQ9CFzHgVPDorEMUlQQkYJMzUviZ571Hynr6t3xzbXFqKaoW5ocA/epiOqB17 cgcOVEZUC+lAuXLTIovCxsE8OV2FU6CGpK4NV2DW1qohIPRrSIf2L6zvA4LreIyRXv05 k5IjW3dIi4H9TosD6pyobGJXoS66FNE4FPRFWNhWaL5pbBDFlfj94RRhZyTKaEDoGxPf ZLhOUMhsvSl8m1fXGybp6DDtlCmppIYfdixsdPdgzAAToZE1AVXGH2f8w2IL4l0DroGy D1zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date:arc-authentication-results; bh=D/JV+kUzax3Atc9CpgyhJglQ53Ofh1nvymFyAtbXu8o=; b=H9WgZ4OZH88tG+FnX/rOUzBhmg/HiiQzDOWGOpCH/HGEG1MlnHFul9vPTU1kzlzOcO HMTKqCt+8TVGrtvuRrhc0t2+FY0t9DYdGZQoQdj2DsK8hggahiTotHjQAcr/kJ4eNO7S S9Qo4JbcqEidqOobwdRqtlSKx8HMOH93M99UmynAdIlAjRHhfo4LqGNvsUkHVJif/lUY yNcsuZR32bQFxoq3TX/dkePRE/65MvV85PpZsHXEreK0R2iF5FJu8NfGC6eSy46AC8wz BQjn+oFNInXTmJMU6dS91kbsIIdc/wLIurz8uxUmuPAcel4OC1QsAJeMEu0Efe0g+GSl by9w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b24-v6si1994889pls.617.2018.02.02.07.25.38; Fri, 02 Feb 2018 07:25:53 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752003AbeBBPZH (ORCPT + 99 others); Fri, 2 Feb 2018 10:25:07 -0500 Received: from iolanthe.rowland.org ([192.131.102.54]:41116 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751907AbeBBPZB (ORCPT ); Fri, 2 Feb 2018 10:25:01 -0500 Received: (qmail 2053 invoked by uid 2102); 2 Feb 2018 10:25:00 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 2 Feb 2018 10:25:00 -0500 Date: Fri, 2 Feb 2018 10:25:00 -0500 (EST) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Haiqing Bai cc: gregkh@linuxfoundation.org, , , Subject: Re: [V2][PATCH] ohci-hcd: Fix race condition caused by ohci_urb_enqueue() and io_watchdog_func() In-Reply-To: <1517550699-7879-1-git-send-email-Haiqing.Bai@windriver.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2 Feb 2018, Haiqing Bai wrote: > From: Shigeru Yoshida > > Running io_watchdog_func() while ohci_urb_enqueue() is running can > cause a race condition where ohci->prev_frame_no is corrupted and the > watchdog can mis-detect following error: > > ohci-platform 664a0800.usb: frame counter not updating; disabled > ohci-platform 664a0800.usb: HC died; cleaning up > > Specifically, following scenario causes a race condition: > > 1. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags) > and enters the critical section > 2. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it > returns false > 3. ohci_urb_enqueue() sets ohci->prev_frame_no to a frame number > read by ohci_frame_no(ohci) > 4. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer() > 5. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock, > flags) and exits the critical section > 6. Later, ohci_urb_enqueue() is called > 7. ohci_urb_enqueue() calls spin_lock_irqsave(&ohci->lock, flags) > and enters the critical section > 8. The timer scheduled on step 4 expires and io_watchdog_func() runs > 9. io_watchdog_func() calls spin_lock_irqsave(&ohci->lock, flags) > and waits on it because ohci_urb_enqueue() is already in the > critical section on step 7 > 10. ohci_urb_enqueue() calls timer_pending(&ohci->io_watchdog) and it > returns false > 11. ohci_urb_enqueue() sets ohci->prev_frame_no to new frame number > read by ohci_frame_no(ohci) because the frame number proceeded > between step 3 and 6 > 12. ohci_urb_enqueue() schedules io_watchdog_func() with mod_timer() > 13. ohci_urb_enqueue() calls spin_unlock_irqrestore(&ohci->lock, > flags) and exits the critical section, then wake up > io_watchdog_func() which is waiting on step 9 > 14. io_watchdog_func() enters the critical section > 15. io_watchdog_func() calls ohci_frame_no(ohci) and set frame_no > variable to the frame number > 16. io_watchdog_func() compares frame_no and ohci->prev_frame_no > > On step 16, because this calling of io_watchdog_func() is scheduled on > step 4, the frame number set in ohci->prev_frame_no is expected to the > number set on step 3. However, ohci->prev_frame_no is overwritten on > step 11. Because step 16 is executed soon after step 11, the frame > number might not proceed, so ohci->prev_frame_no must equals to > frame_no. > > To address above scenario, this patch introduces a special sentinel > value IO_WATCHDOG_OFF and set this value to ohci->prev_frame_no when > the watchdog is not pending or running. When ohci_urb_enqueue() > schedules the watchdog (step 4 and 12 above), it compares > ohci->prev_frame_no to IO_WATCHDOG_OFF so that ohci->prev_frame_no is > not overwritten while io_watchdog_func() is running. > > v2: Instead of adding an extra flag variable, defining IO_WATCHDOG_OFF > as a special sentinel value for prev_frame_no. > > Signed-off-by: Shigeru Yoshida > Signed-off-by: Haiqing Bai The "v2" explanation is supposed to go below the "---" tear line, but that doesn't matter much. In any case, Acked-by: Alan Stern You know, this could have been fixed a lot more easily if the core kernel had a timer_pending_or_executing() routine. I bet there are other places in the kernel that suffer from the same bug and need the same fix. Alan Stern