From owner-freebsd-bugs@freebsd.org Tue Dec 18 15:18:46 2018 Return-Path: Delivered-To: freebsd-bugs@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A27551326A38 for ; Tue, 18 Dec 2018 15:18:46 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mailman.ysv.freebsd.org (mailman.ysv.freebsd.org [IPv6:2001:1900:2254:206a::50:5]) by mx1.freebsd.org (Postfix) with ESMTP id 36F4571387 for ; Tue, 18 Dec 2018 15:18:46 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: by mailman.ysv.freebsd.org (Postfix) id E8F271326A37; Tue, 18 Dec 2018 15:18:45 +0000 (UTC) Delivered-To: bugs@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C680E1326A36 for ; Tue, 18 Dec 2018 15:18:45 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.ysv.freebsd.org (mxrelay.ysv.freebsd.org [IPv6:2001:1900:2254:206a::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.ysv.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B4E271386 for ; Tue, 18 Dec 2018 15:18:45 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2001:1900:2254:206a::16:76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.ysv.freebsd.org (Postfix) with ESMTPS id 89D52E458 for ; Tue, 18 Dec 2018 15:18:44 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.118]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id wBIFIi50053928 for ; Tue, 18 Dec 2018 15:18:44 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id wBIFIibf053918 for bugs@FreeBSD.org; Tue, 18 Dec 2018 15:18:44 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 234135] a race between ifioctl and clone_destroy in lagg and similar network drivers Date: Tue, 18 Dec 2018 15:18:44 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: avg@FreeBSD.org X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Dec 2018 15:18:46 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D234135 Bug ID: 234135 Summary: a race between ifioctl and clone_destroy in lagg and similar network drivers Product: Base System Version: CURRENT Hardware: Any OS: Any Status: New Severity: Affects Only Me Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: avg@FreeBSD.org We have got a page fault crash with the following stack trace: _sx_slock_hard lagg_media_status ifmedia_ioctl lagg_ioctl ifioctl kern_ioctl sys_ioctl It appears that the crash happened because of a destroyed sx lock. My theory is that the following race happened. lagg_clone_destroy() and lagg_ioctl() were called concurrently. lagg_clone_destroy() won a race to lock sc_sx while lagg_media_status() got blocked on it. I think that after some adaptive spinning the thread was pl= aced on a sleep queue. Then, lagg_clone_destroy() unlocked the lock and proceed= ed to destroy it. After the lagg_media_status() thread was waken up it found the lock in the destroyed state and crashed on it in a typical fashion (trying to dereference a NULL pointer as a struct thread pointer). Here is a more detailed, step by step description of the above. Thread T1 calls ifioctl with, for instance, SIOCGIFMEDIA parameter. T1 calls ifunit_ref(), finds the interface with !(if_flags & IFF_DYING). T1 increments the interface's reference count and proceeds to call ifhwioct= l() and then the interface's (driver's) if_ioctl method. Enter thread T2. T2 calls ifioctl(SIOCIFDESTROY) on the same interface. T2 invokes if_clone_destroy() that looks up the interface by name and increments its reference count. Then, T2 calls if_clone_destroyif() that calls ifc_simple_destroy(). The latter calls ifcs_destroy method on the interface. >From here on we consider driver-specific code that, obviously, varies from driver to driver. But after having reviewed a handful of drivers that use if_clone_simple I see that all of them have the same pattern. So, T2 calls ifcs_destroy. A driver's ifcs_destroy would handle its internal state. Then, it would typically call if_free() on the interface. Since the interface at this point has multiple outstanding references, including one taken by T2 itself, it is not actually freed. It's just marked as IFF_DYING. Also, its reference count is decremented by one, so that it can = be actually freed after T2 and T1 release their references. Afterwards, ifcs_destroy would typically free if_softc. At this point the driver's if_ioctl method is being executed by T1. The method can access if_softc that has been freed by now. So, that's the race. Any internal locking using a lock in the driver's softc instance does not h= elp, because the lock would be destroyed and freed together with the if_softc in ifcs_destroy. So, if_ioctl attempting to get that lock is the same kind of the problem. --=20 You are receiving this mail because: You are the assignee for the bug.=