Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6519843ybi; Wed, 31 Jul 2019 16:11:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqwZ5iDL+WVbL0uXOFgq5Nf/506TouFp6zS1kcCl8lqdlTHd2zGKud/sqyXjDWBEluvLFrf7 X-Received: by 2002:a62:82c2:: with SMTP id w185mr51707250pfd.202.1564614700534; Wed, 31 Jul 2019 16:11:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564614700; cv=none; d=google.com; s=arc-20160816; b=uyh5uswzHH0Z48Oe48Ex6OOY18sO+W8Y7X2vPwqmN22IoH1Jcm/s6P2ndkpgihGfsp lZxnFjTkLRehXgXRrxOai0PQ0iHbJISwnlmvnhf0fYTjiGC0WEWyrhjtvJj4U2+tFmgE kXkUOgxYLxPZbpYc2UnokXCAjwlFI8TTxXxXUfI79wDvQ+uDc084STj4i0INxnvd3jGS vlSQwL3a3zdj9D9a2qY2CFPN8LsHfoDyAZSiTPS5Jx0HlMH48tJTxv3hYvrc0UHeXsgl o45slF9qBYM9mj+6EyQWGKMNui8MH9Hqj5Ol1HH4wepmdjxweMj2uCb3zKPPekupZn0v XuJQ== 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:dkim-signature; bh=JAw0aj3zYvR732ClhJNo3dihpW65fT0od1LsxS/SmUU=; b=0c/TPuEZpcSTZU8YoFMP+IklIuqK5Ae06ZxijpT29FMMk5RPTgEVNBS7DKC3GQPIDT jfQ9sU59Dl0rL2Q3sjepPqjuWMOtO35Hg+4zYMTWiU6JXxbD+qUFs9e1l24yepfX5HR5 XRe0+5J0hqaRvuTD+lCjYNTttJP/MQPhBXcCr2BcCgqJGM9luAUpvn6kpmW0TBpzXcx+ 7tUrwv1KVHhaesHn+/F13VUKEysc68H7v2n5ZR9r8bzhpDq+h5XvB15akIUfgCu+bsti nQwJlISt/ED6AsfzDPwLtzjzI5HfmZJky67SHxTI1geC2j/cmos+nEm5BKxVNGzBjF+E ofMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=d1eCP1wf; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y186si32900509pgd.440.2019.07.31.16.10.52; Wed, 31 Jul 2019 16:11:40 -0700 (PDT) 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; dkim=pass header.i=@android.com header.s=20161025 header.b=d1eCP1wf; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729795AbfGaVXi (ORCPT + 99 others); Wed, 31 Jul 2019 17:23:38 -0400 Received: from mail-oi1-f194.google.com ([209.85.167.194]:38442 "EHLO mail-oi1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726626AbfGaVXh (ORCPT ); Wed, 31 Jul 2019 17:23:37 -0400 Received: by mail-oi1-f194.google.com with SMTP id v186so51897019oie.5 for ; Wed, 31 Jul 2019 14:23:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=JAw0aj3zYvR732ClhJNo3dihpW65fT0od1LsxS/SmUU=; b=d1eCP1wf92yvxG0S3ECQy4F+w9zltGiv2cTd1+G2J/uGIjVe6famiZSAgWqUfohSyi SJZegBYqJcnE4Wa9lD35HN9eTFIlE7NbW+ULno28lMD8Jtd2YvyoQjll3QEcKnpa3EMQ doINo8YPFBway69F/sCwe91YKDOqBdq86pzeywsb8jaiQEiF5UnTeruIuL+ehE0Pjw5A NiCDyCVYt/shrqbDU3I9gj72t4baOFC12zd6zSRX/YSj6YLoENQimft0H1zd9mdobHQY 3dyAxq7DwIYyUv/Ks4tJpMW7DoESHBO6TxvmLhRnoMFqLaPdKi6hHpMOcrYaJraOS83/ kSYQ== 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=JAw0aj3zYvR732ClhJNo3dihpW65fT0od1LsxS/SmUU=; b=OUsxpX6yaPh+B99eHtS5Hc7Q66/UUgQVU0hn+T+hkZT3Nb9rlbXAs4UDWZPlxrj8T9 yX+Wmkp/nu6cVDVsm28xUdS6YNyaPsWwdwwEq6iW6QX/u9kvAdAxY0JV/lTMpmE9Btfu ALrZn+7eznWMAwa1xdzcxaI9F6wkk32wkV0w5l7SJrL3zNQQ1Zl3z0nJ/pqYmIRCfHxQ ZMKf4OE1M9OXyyjMRthQk+E44NTuJZUyXqCYe+1Vl6HOUGTe61GYbvlZb2iuo2ecYCnU tXsmv90WFz6zANKtxltX+SlG4z/fqI7+jhNv70avfpw21oQClX6MswZwrlOTSDh8yH65 Qinw== X-Gm-Message-State: APjAAAUKjiKnfOU0NHy27QaHneS7faLkgV1yNNGP2oDPSsCo6Vrmnh5i MHcmeH8IQxua8hXOt4F2Xy1LdBiLzWnqkcO2BkM= X-Received: by 2002:aca:f552:: with SMTP id t79mr54537837oih.145.1564608215732; Wed, 31 Jul 2019 14:23:35 -0700 (PDT) MIME-Version: 1.0 References: <20190730024309.233728-1-trong@android.com> <5d40d5b3.1c69fb81.6047f.1cc3@mx.google.com> <3963324.N1Qi0Ay72S@kreacher> <5d41cc55.1c69fb81.9480d.8a49@mx.google.com> In-Reply-To: From: Tri Vo Date: Wed, 31 Jul 2019 14:23:24 -0700 Message-ID: Subject: Re: [PATCH v5] PM / wakeup: show wakeup sources stats in sysfs To: "Rafael J. Wysocki" Cc: Stephen Boyd , "Rafael J. Wysocki" , Greg Kroah-Hartman , Viresh Kumar , Hridya Valsaraju , Sandeep Patil , Kalesh Singh , Ravi Chandra Sadineni , Linux Kernel Mailing List , Linux PM , "Cc: Android Kernel" , kbuild test robot 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 Wed, Jul 31, 2019 at 2:19 PM Rafael J. Wysocki wrote: > > On Wed, Jul 31, 2019 at 7:14 PM Stephen Boyd wrote: > > > > Quoting Rafael J. Wysocki (2019-07-31 04:58:36) > > > On Wednesday, July 31, 2019 10:34:11 AM CEST Rafael J. Wysocki wrote: > > > > On Wed, Jul 31, 2019 at 1:41 AM Stephen Boyd wrote: > > > > > > > > > > > > > > We can run into the same problem when two buses name their devices the > > > > > same name and then we attempt to attach a wakeup source to those two > > > > > devices. Or we can have a problem where a virtual wakeup is made with > > > > > the same name, and again we'll try to make a duplicate named device. > > > > > Using something like 'event' or 'wakeup' or 'ws' as the prefix avoids this > > > > > problem and keeps things clean. > > > > > > > > Or suffix, like ". > > > > > > > > But if prefixes are used by an existing convention, I would prefer > > > > "ws-" as it is concise enough and should not be confusing. > > > > Another possibility is 'eventN', so it reads as /sys/class/wakeup/event0 > > > > > > > > > > > We should probably avoid letting the same virtual wakeup source be made > > > > > with the same name anyway, because userspace will be confused about what > > > > > virtual wakeup it is otherwise. I concede that using the name of the > > > > > wakeup source catches this problem without adding extra code. > > > > > > > > > > Either way, I'd like to see what you outline implemented so that we > > > > > don't need to do more work than is necessary when userspace writes to > > > > > the file. > > > > > > > > Since we agree here, let's make this change first. I can cut a patch > > > > for that in a reasonable time frame I think if no one else beats me to > > > > that. > > > > > > So maybe something like the patch below (untested). > > > > > > Index: linux-pm/drivers/base/power/wakeup.c > > > =================================================================== > > > --- linux-pm.orig/drivers/base/power/wakeup.c > > > +++ linux-pm/drivers/base/power/wakeup.c > > > @@ -265,15 +244,29 @@ int device_wakeup_enable(struct device * > > > if (pm_suspend_target_state != PM_SUSPEND_ON) > > > dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__); > > > > > > + spin_lock_irq(&dev->power.lock); > > > + > > > + if (dev->power.wakeup) { > > > + spin_unlock_irq(&dev->power.lock); > > > + return -EEXIST; > > > + } > > > + dev->power.wakeup = ERR_PTR(-EBUSY); > > > + > > > + spin_unlock_irq(&dev->power.lock); > > > + > > > ws = wakeup_source_register(dev_name(dev)); > > > if (!ws) > > > return -ENOMEM; > > > > > > > Let's say that device_wakeup_enable() is called twice at around the same > > time. First thread gets to wakeup_source_register() and it fails, we > > return -ENOMEM. > > The return is premature. dev->power.wakeup should be reset back to > NULL if the wakeup source creation fails. > > > dev->power.wakeup is assigned to ERR_PTR(-EBUSY). Second > > thread is at the spin_lock_irq() above, it grabs the lock and sees > > dev->power.wakeup is ERR_PTR(-EBUSY) so it bails out with return > > -EEXIST. I'd think we would want to try to create the wakeup source > > instead. > > > > CPU0 CPU1 > > ---- ---- > > spin_lock_irq(&dev->power.lock) > > ... > > dev->power.wakeup = ERR_PTR(-EBUSY) > > spin_unlock_irq(&dev->power.lock) > > ws = wakeup_source_register(...) > > if (!ws) > > return -ENOMEM; spin_lock_irq(&dev->power.lock) > > if (dev->power.wakeup) > > return -EEXIST; // Bad > > > > > > Similar problems probably exist with wakeup destruction racing with > > creation. I think it might have to be a create and then publish pointer > > style of code to keep the spinlock section small? > > There is a problem when there are two concurrent callers of > device_wakeup_enable() running in parallel with a caller of > device_wakeup_disable(), but that can be prevented by an extra check > in the latter. > > Apart from that I missed a few if (dev->power.wakeup) checks to convert. > > I'll update the patch and resend it. Ok thanks, I'll ignore the device_wakeup_enable() issue in this patch, since you're addressing it in a separate patch. IIUC checking and assigning to dev->power.wakeup must be in the same critical section for correctness, implying that allocation of the wakeup source must also be in that critical section (since we check dev->power.wakeup to see whether we need a wakeup source). Wakeup source virtual device registration can fail (it allocates memory), in which case dev->power.wakeup need to be cleaned up. Meaning that wakeup source virtual device registration need to be in that same critical section. So I'm not sure it is at all possible to satisfy these conditions at the same time (1) avoid creating an extra wakeup source (2) not hold the spinlock while creating/registering the wakeup source.