Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp673057imu; Thu, 13 Dec 2018 02:27:12 -0800 (PST) X-Google-Smtp-Source: AFSGD/Ui58wO/A+PcZuPNKiK4jqiEaNEAVhsykXsx4O+z3MoRC0V4BGrGoBHdYwVajMHogNxXHue X-Received: by 2002:a62:e704:: with SMTP id s4mr23858284pfh.124.1544696832223; Thu, 13 Dec 2018 02:27:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544696832; cv=none; d=google.com; s=arc-20160816; b=wPRPKPD3CxDzDer5HBP8fAaiW+xPZ/x0fmZC7CJjSupFSqLDkNzT7IxMVSCjawmOax 0q/5PQzyG0dNSC1K6UG/4D4D45MB1IM+sx9zUvtwXjdTCjBNssj9aQ2tjKVcQ/knwFS1 TnJ9rb3lhB5uxJTB1Z1uiThsSytwl3U8vMnsoLKZxp/5sryQ/FsHIU06z3guJat+mFaR fCwAUglZWNKnd2B3VFxWrjzTmEuxbnJL5/MZxgrMZfpmJ2UtAMAxvCwbvUyGPKrF+9VF RbSsCTYvaJzvuu49/Zuoz3ry922gvloIAjPNkChU0Wm5V25DTa59vP9m+ZK63TcFgDJH s+4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=RjhXbieLnqpmo8PT47Ixix69zcjNtLTv/M0MF3sqTCs=; b=uTKAGob8DIDnp61AgiRFhTOq60WdFQsM248ToARBDocjoRdEBEg4ldcx/8mtB7xSO2 gRLhFGx8MoLMdaNLGWDEjY+zqxAn0CLU7piiLTl+fbVqo2iIIt9sE4k6cTPKhkHo6g7x xd6vKDuvNHxE/XcEMoKPWgbCmytDcGish+GmgvN5Ukb8+gMqXc4KUgNCpZOcu4GNMBqY 4WlQ9SgjluUnfRTw8uSoGX5bPeByw3huWhKY2KYw1uY8GU8/FN72hFuT5UdtlCYb5GZU rK5DULqZN5+xM4wdeQKaEgJlCMDPR6aW80sDqy4VLatwLryR8QRIunu+COh+KK6kpeA4 Dr1g== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a11si1276251pln.78.2018.12.13.02.26.56; Thu, 13 Dec 2018 02:27:12 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727528AbeLMKXy (ORCPT + 99 others); Thu, 13 Dec 2018 05:23:54 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:35135 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725949AbeLMKXy (ORCPT ); Thu, 13 Dec 2018 05:23:54 -0500 Received: by mail-ot1-f67.google.com with SMTP id 81so1428229otj.2 for ; Thu, 13 Dec 2018 02:23:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RjhXbieLnqpmo8PT47Ixix69zcjNtLTv/M0MF3sqTCs=; b=GNyBJ1UmY7IUTl/FfeXffx7YP8TaIV+cO44MvzDPjaxPa0dLIcE7LR/2JD6qdWxkHN yJVuDVcoTP7GJq8jb0w2Oxd4IRygrCQhssI6+DGXxqTkKagv5lJYilxdUXzfR+n7XSUr LDa+YHKNEay05F8Tab8Kqo2du6ghsjgzeZUS05wI3TyLkkENLGGlZf71nDk57JOaOpJj gvXXzuyZQjAAf8uOENjj/l34EY9bJ5iVOjoUou/BmPZ7YuE3juFtmCfyqVsPB7mkqFz+ MttjBFJP5Hd+eIjaouPZ/b9PRx78ZzDRTvk3+u9kr6I3oZ0vJNMM9cwg6Bv0zHAWKYYE F3gw== X-Gm-Message-State: AA+aEWbWdMosnBnKsMbqDUrwuDsniZfg1Uqvsi0Rq6Ri9b7z/mz01V39 Eb6GVaHNANNImzOku9osijREiWgjY2BqdNb/jYk= X-Received: by 2002:a9d:2062:: with SMTP id n89mr15627725ota.244.1544696632885; Thu, 13 Dec 2018 02:23:52 -0800 (PST) MIME-Version: 1.0 References: <20181210084653.7268-1-daniel.vetter@ffwll.ch> <20181213095814.GC21184@phenom.ffwll.local> In-Reply-To: <20181213095814.GC21184@phenom.ffwll.local> From: "Rafael J. Wysocki" Date: Thu, 13 Dec 2018 11:23:41 +0100 Message-ID: Subject: Re: [PATCH] drivers/base: use a worker for sysfs unbind To: "Rafael J. Wysocki" , Linux Kernel Mailing List , dri-devel , ramalingam.c@intel.com, Greg Kroah-Hartman , Daniel Vetter Cc: Daniel Vetter Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Dec 13, 2018 at 10:58 AM Daniel Vetter wrote: > > On Thu, Dec 13, 2018 at 10:38:14AM +0100, Rafael J. Wysocki wrote: > > On Mon, Dec 10, 2018 at 9:47 AM Daniel Vetter wrote: > > > > > > Drivers might want to remove some sysfs files, which needs the same > > > locks and ends up angering lockdep. Relevant snippet of the stack > > > trace: > > > > > > kernfs_remove_by_name_ns+0x3b/0x80 > > > bus_remove_driver+0x92/0xa0 > > > acpi_video_unregister+0x24/0x40 > > > i915_driver_unload+0x42/0x130 [i915] > > > i915_pci_remove+0x19/0x30 [i915] > > > pci_device_remove+0x36/0xb0 > > > device_release_driver_internal+0x185/0x250 > > > unbind_store+0xaf/0x180 > > > kernfs_fop_write+0x104/0x190 > > > > Is the acpi_bus_unregister_driver() in acpi_video_unregister() the > > source of the lockdep unhappiness? > > Yeah I guess I cut out too much of the lockdep splat. It complains about > kernfs_fop_write and kernfs_remove_by_name_ns acquiring the same lock > class. It's ofc not the same lock, so no real deadlock. Getting the > device_release_driver outside of the callchain under kernfs_fop_write, > which this patch does, "fixes" it. For "fixes" = shut up lockdep. OK, so the problem really is that the operation is started via sysfs which means that this code is running under a lock already. Which lock does lockdep complain about, exactly? > Other options: > - Anotate the recursion with the usual lockdep annotations. Potentially > results in lockdep not catching real deadlocks (you can still have other > loops closing the deadlock, maybe through some subsystem/bus lock). > > - Rewrite kernfs_fop_write to drop the lock (optionally, for callbacks > that know what they're doing), which should be fine if we refcount > everything properly (bus, driver & device). > > - Also note that probably the same bug exists on the bind sysfs interface, > but we don't use that, so I don't care :-) > > - Most of these issues are never visible in normal usage, since normally > driver bind/unbind is done from a kthread or model_load/unload, neither > of which is running in the context of that kernfs mutex kernfs_fop_write > holds. That's why I think the task work is the best solution, since it > changes the locking context of the unbind sysfs to match the locking > context of module unload and hotunplug. I think that using a task work here makes sense. There is a drawback, which is that the original sysfs write will not wait for the driver to actually be released before returning to user space AFAICS, but that probably isn't a big deal. Also please note that the patch changes the code flow slightly, because passing a non-NULL parent pointer to device_release_driver_internal() potentially has side effects, but that should not be a big deal either. > Unfortunately that trick doesn't work for the bind sysfs file, since that way we can't thread the errno value back to userspace. Right. That is unless we wait for the operation to complete and check the error left behind by it. That should be doable, but somewhat complicated.