From patchwork Fri May 20 16:59:18 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9129919 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EEBA960467 for ; Fri, 20 May 2016 16:59:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DF0C722067 for ; Fri, 20 May 2016 16:59:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D396A27BF1; Fri, 20 May 2016 16:59:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1A1F622067 for ; Fri, 20 May 2016 16:59:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755542AbcETQ7X (ORCPT ); Fri, 20 May 2016 12:59:23 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:33195 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754819AbcETQ7W (ORCPT ); Fri, 20 May 2016 12:59:22 -0400 Received: by mail-pf0-f194.google.com with SMTP id y7so11892872pfb.0; Fri, 20 May 2016 09:59:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=cwmXYnqzv8d5Km+AHTVDmrjSKFvp2QecY1Fq3yag2Zg=; b=BKVg8KTTbc0ycg0hgnSDret1gCpVn2QWBQIl01+9IApTzN73+8c0LwktsmDv5NtFQE R+8HFCLN1rFjCkzJFCPyPOoi5yinZXR/yuukMTpSQbHcqM+tTg3ApTc2DTpgt53ZXHCm Kr8ou4wstu26e7c5YuVCtWem90nB+RLCnd5ovDzKjZw33OHooTiNkAGyfGvxFodXMCuc 15s+KLqgZZeCqm7G5XPkD9PadBzuHfOnxfwxlN8HdhUhOyJZzFbYn7ot+OpQvSgAn5sG Kl2bAMLoBuc5gIX/7NlGZcR4DC3vpGhec1W5KEWbsrTK6DgO3Hr0uXOxnRBP6q5x/fXq dldg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=cwmXYnqzv8d5Km+AHTVDmrjSKFvp2QecY1Fq3yag2Zg=; b=PxRVwDNEmxc9h1ocCLDFkVG27O7qi5rXk08jYPNtW/O7khGEAWt4AZ6CdkGHu4qvaS 8bskSw3D0H7w7MqvlLA5IuUrLOU6XIZ+K72fVDHYthy6J+VlMyWKplmJfIafutQz5bmF xTIg7KRhH1/RoWKOJB5v4VbYGClYP92SFH7VvnoKa0B45YytNIjsvWHb4YOlY8XbTfnS ii1d5YTqjVgfOu+15Znir0TtPW20S+3haVZ0oFWDUNsVD2YuGeEyQWbpOhZAQ1b5GMzj gabh2Ym7x1Z7EBuNBvgpqbRvG4xJp7xexjVm9JJaMKWPmF/EdYMjOoLnOGqo2UkMZ6dy ccIA== X-Gm-Message-State: AOPr4FX+8WQI1b7Y091Tv/EiRXRuvmQzUyLHzTZTDBZtTincOIV8MTtlz4IhSqPzhSxE1A== X-Received: by 10.98.74.218 with SMTP id c87mr6348349pfj.78.1463763561609; Fri, 20 May 2016 09:59:21 -0700 (PDT) Received: from dtor-ws ([2620:0:1000:1311:716a:5a4c:4c5a:b8e]) by smtp.gmail.com with ESMTPSA id u127sm28464197pfb.82.2016.05.20.09.59.20 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 20 May 2016 09:59:20 -0700 (PDT) Date: Fri, 20 May 2016 09:59:18 -0700 From: Dmitry Torokhov To: Manfred Schlaegl Cc: Thierry Reding , Manfred Schlaegl , Luis de Bethencourt , Olivier Sobrie , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: Re: [PATCH] Input: pwm-beeper - fix: scheduling while atomic Message-ID: <20160520165918.GE14951@dtor-ws> References: <56C4735E.6020300@gmx.at> <20160222194639.GD26177@dtor-ws> <20160512121852.GB26824@ulmo.ba.sec> <5735F4E3.20008@gmx.at> <573C8761.9070601@gmx.at> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <573C8761.9070601@gmx.at> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Hi Manfred, On Wed, May 18, 2016 at 05:16:49PM +0200, Manfred Schlaegl wrote: > @@ -133,6 +149,8 @@ static int pwm_beeper_remove(struct platform_device *pdev) > { > struct pwm_beeper *beeper = platform_get_drvdata(pdev); > > + cancel_work_sync(&beeper->work); > + > input_unregister_device(beeper->input); This is racy, request to play may come in after cancel_work_sync() returns but before we unregistered input device. I think you want the version below. diff --git a/drivers/input/misc/pwm-beeper.c b/drivers/input/misc/pwm-beeper.c index f2261ab..27de150 100644 --- a/drivers/input/misc/pwm-beeper.c +++ b/drivers/input/misc/pwm-beeper.c @@ -20,21 +20,41 @@ #include #include #include +#include struct pwm_beeper { struct input_dev *input; struct pwm_device *pwm; + struct work_struct work; unsigned long period; }; #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x)) +static void __pwm_beeper_set(struct pwm_beeper *beeper) +{ + unsigned long period = beeper->period; + + pwm_config(beeper->pwm, period / 2, period); + + if (period == 0) + pwm_disable(beeper->pwm); + else + pwm_enable(beeper->pwm); +} + +static void pwm_beeper_work(struct work_struct *work) +{ + struct pwm_beeper *beeper = + container_of(work, struct pwm_beeper, work); + + __pwm_beeper_set(beeper); +} + static int pwm_beeper_event(struct input_dev *input, unsigned int type, unsigned int code, int value) { - int ret = 0; struct pwm_beeper *beeper = input_get_drvdata(input); - unsigned long period; if (type != EV_SND || value < 0) return -EINVAL; @@ -49,22 +69,31 @@ static int pwm_beeper_event(struct input_dev *input, return -EINVAL; } - if (value == 0) { - pwm_disable(beeper->pwm); - } else { - period = HZ_TO_NANOSECONDS(value); - ret = pwm_config(beeper->pwm, period / 2, period); - if (ret) - return ret; - ret = pwm_enable(beeper->pwm); - if (ret) - return ret; - beeper->period = period; - } + if (value == 0) + beeper->period = 0; + else + beeper->period = HZ_TO_NANOSECONDS(value); + + schedule_work(&beeper->work); return 0; } +static void pwm_beeper_stop(struct pwm_beeper *beeper) +{ + cancel_work_sync(&beeper->work); + + if (beeper->period) + pwm_disable(beeper->pwm); +} + +static void pwm_beeper_close(struct input_dev *input) +{ + struct pwm_beeper *beeper = input_get_drvdata(input); + + pwm_beeper_stop(beeper); +} + static int pwm_beeper_probe(struct platform_device *pdev) { unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev); @@ -87,6 +116,8 @@ static int pwm_beeper_probe(struct platform_device *pdev) goto err_free; } + INIT_WORK(&beeper->work, pwm_beeper_work); + beeper->input = input_allocate_device(); if (!beeper->input) { dev_err(&pdev->dev, "Failed to allocate input device\n"); @@ -106,6 +137,7 @@ static int pwm_beeper_probe(struct platform_device *pdev) beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL); beeper->input->event = pwm_beeper_event; + beeper->input->close = pwm_beeper_close; input_set_drvdata(beeper->input, beeper); @@ -135,7 +167,6 @@ static int pwm_beeper_remove(struct platform_device *pdev) input_unregister_device(beeper->input); - pwm_disable(beeper->pwm); pwm_free(beeper->pwm); kfree(beeper); @@ -147,8 +178,7 @@ static int __maybe_unused pwm_beeper_suspend(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); - if (beeper->period) - pwm_disable(beeper->pwm); + pwm_beeper_stop(beeper); return 0; } @@ -157,10 +187,8 @@ static int __maybe_unused pwm_beeper_resume(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); - if (beeper->period) { - pwm_config(beeper->pwm, beeper->period / 2, beeper->period); - pwm_enable(beeper->pwm); - } + if (beeper->period) + __pwm_beeper_set(beeper); return 0; }