From patchwork Wed Nov 7 05:42:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 150368 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp4773325ljp; Tue, 6 Nov 2018 21:43:22 -0800 (PST) X-Google-Smtp-Source: AJdET5d6ONHD0hIqCBZqIb0OFXY+EWyaM3NPDPLYf4TCQSHdRjaV4yxX1ht0xwP+OrW6I1WYNC+A X-Received: by 2002:a17:902:67:: with SMTP id 94-v6mr544796pla.225.1541569402042; Tue, 06 Nov 2018 21:43:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541569402; cv=none; d=google.com; s=arc-20160816; b=L3XgdalaedTyp/WTulEp6jWzIkWajCDhvvf+tnUKqyTj6NINH4V+FJBYKYljUsCr1y Pp2fvUlKiDCV1xImaoucAXq6wNprFYgzThYTwtseW1yoyR+3HR6G3nNTmdpnEkXRhfSo zcoo36gjpHZ+srCmhcw7YhomP2w57ZpoQVzS56KbrTH8dF/iAKtsbKH7kIaqqZPIVjiV ntcMkiJ8v/EbMtTNSU7yrElfUbYurl94WKJPHiD6f8aZLLcg3ruMTXr95WnKzfR/AS0R JIq60Tl5Yiwp2Je416SPxRdLonu4y1HZNl+2zQVBWClMHY/ljgAYJk61qsD+NpHfFVcB vIhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=GNQpj3rjBVklO8bagXhiz60SxP62hFGYHRM9p2t7PbQ=; b=ek58k7PvUzNd1/cU7ngQJZb87IZB77MArDB9IKeK/sAoTS3fb7rWLGFghwSeS5f9LB onDZGjpry8s65+fuYScAsaonDnvI+IqzCcmHLFdUG0pM8bUt3nKMnRfr+1NZnepM/8UR YUlLPejnAmGO8Axue0IVoWQzsu45Df+wbzuSQ7XrXfaajt9oILi+7Jg8XL38VvHi5/ty gavf8qW64KWtx1jQkkIDj6OPOF5caydksz4NYQqmZPSFZzqGm19jlO0FLC8MkDJacpd3 fvyjrLhQqOWHZohb2m6QhVvoQXdvCtVbJajp3siXo0Z/c90K8XAtHxkNuA7pSQTQ9Dvq 3Ntw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KyqvwaHw; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 129-v6si49642284pfy.164.2018.11.06.21.43.21; Tue, 06 Nov 2018 21:43:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-leds-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=@linaro.org header.s=google header.b=KyqvwaHw; spf=pass (google.com: best guess record for domain of linux-leds-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-leds-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727353AbeKGPMK (ORCPT + 1 other); Wed, 7 Nov 2018 10:12:10 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:34066 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727043AbeKGPMJ (ORCPT ); Wed, 7 Nov 2018 10:12:09 -0500 Received: by mail-pf1-f195.google.com with SMTP id y18-v6so4840511pfn.1 for ; Tue, 06 Nov 2018 21:43:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=GNQpj3rjBVklO8bagXhiz60SxP62hFGYHRM9p2t7PbQ=; b=KyqvwaHwrmynroxJ/Y0w04DGuD3qh76/KvfOHJZ56DliOLlcmthoRMJXtv3KR8yVWE eWeLtAoirOVAXaE1FWQcc/2rSQaupPemopsT5uv8Ph5NDqmJ7T2a5AWlzfvremtegQnQ l0T1InJAk70NwjQaEJvjpIxuUGPPEKOW6BV8g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=GNQpj3rjBVklO8bagXhiz60SxP62hFGYHRM9p2t7PbQ=; b=eo944UtAm3HYmZwOgJg2b0QwR30qhbTfF22Zyo+P2w4yjtTwoFN74QEAEWcfmIA1fG 3JkS54ieZf6Ez3xdsf4igWOGjRgDqXN5thMOZGGz4FMaaeifLbTNIuNcZApVtdRLj9kC iU9zBLngpUazvteUKtjeFGDRM47ZG3aaq/ktX5KUtQDIgzhLpDjWVzowsawj2NuQOu/a HTU0gsxTq713t1/rAYben5Ev5wbLFgEG2ZJL97M18TvsJi0/Fgjw8L+UyPbTU4Y+ENZ1 TsKPT3JZhuVaMiZU0LUyBxnTLf+4knBuEEftdZCgApBCCiwruz8VMIO/gMGLltTxe6uj AUaw== X-Gm-Message-State: AGRZ1gL/X/VRcXUxNY/lWBLN4XNfUi1tbpcF1KRXxZwFoNJak3ac4cY4 Ndcv1Xoc0+hm21CjTTOHBt6d7A== X-Received: by 2002:a63:d547:: with SMTP id v7mr455466pgi.339.1541569400022; Tue, 06 Nov 2018 21:43:20 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.102]) by smtp.gmail.com with ESMTPSA id r6sm5850627pgk.91.2018.11.06.21.43.16 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 06 Nov 2018 21:43:19 -0800 (PST) From: Baolin Wang To: jacek.anaszewski@gmail.com, pavel@ucw.cz Cc: rteysseyre@gmail.com, baolin.wang@linaro.org, broonie@kernel.org, geert@linux-m68k.org, linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] leds: trigger: Fix sleeping function called from invalid context Date: Wed, 7 Nov 2018 13:42:58 +0800 Message-Id: <5f1cfb9c87f591b12efa2e7a0063f15532649634.1541563908.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-leds-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-leds@vger.kernel.org We will meet below issue due to mutex_lock() is called in interrupt context. The mutex lock is used to protect the pattern trigger data, but before changing new pattern trigger data (pattern values or repeat value) by users, we always cancel the timer firstly to clear previous patterns' performance. That means there is no race in pattern_trig_timer_function(), so we can drop the mutex lock in pattern_trig_timer_function() to avoid this issue. Moreover we can move the timer cancelling into mutex protection, since there is no deadlock risk if we remove the mutex lock in pattern_trig_timer_function(). BUG: sleeping function called from invalid context at kernel/locking/mutex.c:254 in_atomic(): 1, irqs_disabled(): 0, pid: 0, name: swapper/1 CPU: 1 PID: 0 Comm: swapper/1 Not tainted 4.20.0-rc1-koelsch-00841-ga338c8181013c1a9 #171 Hardware name: Generic R-Car Gen2 (Flattened Device Tree) [] (unwind_backtrace) from [] (show_stack+0x10/0x14) [] (show_stack) from [] (dump_stack+0x7c/0x9c) [] (dump_stack) from [] (___might_sleep+0xf4/0x158) [] (___might_sleep) from [] (mutex_lock+0x18/0x60) [] (mutex_lock) from [] (pattern_trig_timer_function+0x1c/0x11c) [] (pattern_trig_timer_function) from [] (call_timer_fn+0x1c/0x90) [] (call_timer_fn) from [] (expire_timers+0x94/0xa4) [] (expire_timers) from [] (run_timer_softirq+0x108/0x15c) [] (run_timer_softirq) from [] (__do_softirq+0x1d4/0x258) [] (__do_softirq) from [] (irq_exit+0x64/0xc4) [] (irq_exit) from [] (__handle_domain_irq+0x80/0xb4) [] (__handle_domain_irq) from [] (gic_handle_irq+0x58/0x90) [] (gic_handle_irq) from [] (__irq_svc+0x58/0x74) Exception stack(0xeb483f60 to 0xeb483fa8) 3f60: 00000000 00000000 eb9afaa0 c0217e80 00000000 ffffe000 00000000 c0e06408 3f80: 00000002 c0e0647c c0c6a5f0 00000000 c0e04900 eb483fb0 c0207ea8 c0207e98 3fa0: 60020013 ffffffff [] (__irq_svc) from [] (arch_cpu_idle+0x1c/0x38) [] (arch_cpu_idle) from [] (do_idle+0x138/0x268) [] (do_idle) from [] (cpu_startup_entry+0x18/0x1c) [] (cpu_startup_entry) from [<402022ec>] (0x402022ec) Fixes: 5fd752b6b3a2 ("leds: core: Introduce LED pattern trigger") Reported-by: Geert Uytterhoeven Signed-off-by: Baolin Wang --- drivers/leds/trigger/ledtrig-pattern.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) -- 1.7.9.5 diff --git a/drivers/leds/trigger/ledtrig-pattern.c b/drivers/leds/trigger/ledtrig-pattern.c index 174a298..1870cf8 100644 --- a/drivers/leds/trigger/ledtrig-pattern.c +++ b/drivers/leds/trigger/ledtrig-pattern.c @@ -75,8 +75,6 @@ static void pattern_trig_timer_function(struct timer_list *t) { struct pattern_trig_data *data = from_timer(data, t, timer); - mutex_lock(&data->lock); - for (;;) { if (!data->is_indefinite && !data->repeat) break; @@ -117,8 +115,6 @@ static void pattern_trig_timer_function(struct timer_list *t) break; } - - mutex_unlock(&data->lock); } static int pattern_trig_start_pattern(struct led_classdev *led_cdev) @@ -177,14 +173,10 @@ static ssize_t repeat_store(struct device *dev, struct device_attribute *attr, if (res < -1 || res == 0) return -EINVAL; - /* - * Clear previous patterns' performence firstly, and remove the timer - * without mutex lock to avoid dead lock. - */ - del_timer_sync(&data->timer); - mutex_lock(&data->lock); + del_timer_sync(&data->timer); + if (data->is_hw_pattern) led_cdev->pattern_clear(led_cdev); @@ -235,14 +227,10 @@ static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev, struct pattern_trig_data *data = led_cdev->trigger_data; int ccount, cr, offset = 0, err = 0; - /* - * Clear previous patterns' performence firstly, and remove the timer - * without mutex lock to avoid dead lock. - */ - del_timer_sync(&data->timer); - mutex_lock(&data->lock); + del_timer_sync(&data->timer); + if (data->is_hw_pattern) led_cdev->pattern_clear(led_cdev);