Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp794627pxb; Sat, 6 Mar 2021 18:14:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJxZIn5l1qHuCj0s4ApInAr2/yaWiBdRtVvF9x4TR8jyv4bhGdWtdWABfo0PPNCTNW2ZbJU0 X-Received: by 2002:a17:906:6047:: with SMTP id p7mr9002009ejj.400.1615083294349; Sat, 06 Mar 2021 18:14:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615083294; cv=none; d=google.com; s=arc-20160816; b=GgvHuLkCt11BLnVGBYFb0b2OkbEyThw/9OlUck6MvZzy2q4PpexoXyp5wU/UywToYS qKRYnM0877Khkeb0iDYuvcktgjvDy57x3nIAOh75+Z8VR4s9fuFsomLtHBUOGfJ6P7Eg FeJ2Wec47p61ucsORKGAo0GDeRpEAjy3Siu5yIcEqV0Jq7JiMYDqpcdfef37XxVokt1f O/dG9fd1RSzZkcKx2agriLJHexNq9Ch54kvQbTTo6Kf1mTm73k8FmQ/GHiZZUaCiXkKC yqlkztcXDujQJFvdG8j9uFad9U/5hOPoDtSwQHWQHe56w08PQgnNGCJleo3n8RI53rg8 kH1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=QxvB5zhcmMCmsEmz42AGBb9LKLykoB0qkc+n27okF9U=; b=UJJSLA6VRoSjJWTlc7BS0ZCYbxrnXEL+Pwkv2i0UrIazhq+XatiNFNU+Lig2KSIEZx O868xFbX8ZiWedO7jiHRkEssX/NDzMJF6hTDWrwualvjHfm4FeHU08Gs+kIHy2wTL275 KFb4FW31LUWBBEcZUql3lw+Pu/u4mcEIyrNrkusNuWDulolHL1zyX6GuE7C8Vl1yOZ6y 12VzGgt5B8xCskflCGifMF5KJGhyB54QIkf4FgfXbsEYI4mfKm+7MiS0wHNixm3HOrUT bWoqJdGm5D8lOYJtHG8xelDqadjt+QMrneEEtcDPPqpF04PUvfTl22h/pBoa2xuKKIrQ fW3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=j8G7N+tY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id zc18si4028036ejb.148.2021.03.06.18.13.57; Sat, 06 Mar 2021 18:14:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=j8G7N+tY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230005AbhCGCEC (ORCPT + 99 others); Sat, 6 Mar 2021 21:04:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229906AbhCGCDq (ORCPT ); Sat, 6 Mar 2021 21:03:46 -0500 Received: from mail-qt1-x831.google.com (mail-qt1-x831.google.com [IPv6:2607:f8b0:4864:20::831]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A6E5C06174A; Sat, 6 Mar 2021 18:03:46 -0800 (PST) Received: by mail-qt1-x831.google.com with SMTP id t13so3620291qta.11; Sat, 06 Mar 2021 18:03:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=QxvB5zhcmMCmsEmz42AGBb9LKLykoB0qkc+n27okF9U=; b=j8G7N+tYVahLmY1MwTWX5p3UxVFf34QuZnb4RQzdrtNrbmS87lbelnaXcCrFCRACzR h67H50sVB32nEE4wjNsUQdJI7qXCb7w3FzD2hquz5G27z8vz/yxTXzhgJNULhELecPBk iVMmJrPMkpM9eUtsXb5l8LpF2KDziudQ22GqHl5v+MQtuoBBxXfahPtaWiTFWW5CFeNx 8lHrvoLd/Gak0an8gp8tXRtViD/u5rX1bs5wDaY3xbhQUmlrasHeEol2X6yxCb4VY++c oSDl4ZfFHzUXHGtjtpOzro583eyFi3/ECutAzwPH/JEBYda6tBkzmRL98loH2yntuYXb Fc5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=QxvB5zhcmMCmsEmz42AGBb9LKLykoB0qkc+n27okF9U=; b=NBU4BV4Y52315ueDenU04hOlDZ7yPAD006w3crs8T/gd6zVjvHtbDiBPyCocj4QaIc QbcQnsB+sh2KTEG+dSKU5ujAXrmm7scDt5tgKy6cw8XCpDdMZt/lfEwejIt7wFCsQcgm gbiwDXAWXUeoh+qHD+esw8Nwtue2xe5DGld/v0zTC2/NlJP1kD2ZmuIuHBxXO2Le0oXY kqIxP8Tnb+dD9exYD1ayDTCquHHrZfZfZFKBrUVc5+un5Q8VlUcucg2JEEDQS2cAFnH8 QSY+zhlIHCNeblOBhhOjzY8WP24VuwIBDNRocrfr0CWkodxzjFHrh8O/wXbfbGyhqlVB +LJQ== X-Gm-Message-State: AOAM530B9uFkqKo6SKkzbu0JqIF8NBaNI5cBDhBY66MFcUoBXpXJN5aZ jwpNCawaHSPZJEQ3D46RoQ8FId9MofM= X-Received: by 2002:ac8:735a:: with SMTP id q26mr15092970qtp.5.1615082625033; Sat, 06 Mar 2021 18:03:45 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id j1sm5058669qti.55.2021.03.06.18.03.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 06 Mar 2021 18:03:44 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id C40C127C0054; Sat, 6 Mar 2021 21:03:43 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Sat, 06 Mar 2021 21:03:43 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledruddtledggeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtugfgjgesthekredttddtudenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpedtfefgfeeuleevvedtheejgeetueejkeeujeethfeiteekffehvdevveff jefgteenucfkphepudefuddruddtjedrudegjedruddvieenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhht hhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquh hnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Received: from localhost (unknown [131.107.147.126]) by mail.messagingengine.com (Postfix) with ESMTPA id 7F2B21080064; Sat, 6 Mar 2021 21:03:41 -0500 (EST) Date: Sun, 7 Mar 2021 10:02:32 +0800 From: Boqun Feng To: Marc Kleine-Budde Cc: Andrea Righi , Pavel Machek , Dan Murphy , linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org, kernel@pengutronix.de, schuchmann@schleissheimer.de Subject: Re: [PATCH] leds: trigger: fix potential deadlock with libata Message-ID: References: <20201102104152.GG9930@xps-13-7390> <20210306203954.ya5oqgkdalcw45c4@pengutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210306203954.ya5oqgkdalcw45c4@pengutronix.de> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Mar 06, 2021 at 09:39:54PM +0100, Marc Kleine-Budde wrote: > Hello *, > > On 02.11.2020 11:41:52, Andrea Righi wrote: > > We have the following potential deadlock condition: > > > > ======================================================== > > WARNING: possible irq lock inversion dependency detected > > 5.10.0-rc2+ #25 Not tainted > > -------------------------------------------------------- > > swapper/3/0 just changed the state of lock: > > ffff8880063bd618 (&host->lock){-...}-{2:2}, at: ata_bmdma_interrupt+0x27/0x200 > > but this lock took another, HARDIRQ-READ-unsafe lock in the past: > > (&trig->leddev_list_lock){.+.?}-{2:2} > > > > and interrupts could create inverse lock ordering between them. > > [...] > > > --- > > drivers/leds/led-triggers.c | 5 +++-- > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c > > index 91da90cfb11d..16d1a93a10a8 100644 > > --- a/drivers/leds/led-triggers.c > > +++ b/drivers/leds/led-triggers.c > > @@ -378,14 +378,15 @@ void led_trigger_event(struct led_trigger *trig, > > enum led_brightness brightness) > > { > > struct led_classdev *led_cdev; > > + unsigned long flags; > > > > if (!trig) > > return; > > > > - read_lock(&trig->leddev_list_lock); > > + read_lock_irqsave(&trig->leddev_list_lock, flags); > > list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) > > led_set_brightness(led_cdev, brightness); > > - read_unlock(&trig->leddev_list_lock); > > + read_unlock_irqrestore(&trig->leddev_list_lock, flags); > > } > > EXPORT_SYMBOL_GPL(led_trigger_event); > > meanwhile this patch hit v5.10.x stable and caused a performance > degradation on our use case: > > It's an embedded ARM system, 4x Cortex A53, with an SPI attached CAN > controller. CAN stands for Controller Area Network and here used to > connect to some automotive equipment. Over CAN an ISOTP (a CAN-specific > Transport Protocol) transfer is running. With this patch, we see CAN > frames delayed for ~6ms, the usual gap between CAN frames is 240?s. > > Reverting this patch, restores the old performance. > > What is the best way to solve this dilemma? Identify the critical path > in our use case? Is there a way we can get around the irqsave in > led_trigger_event()? > Probably, we can change from rwlock to rcu here, POC code as follow, only compile tested. Marc, could you see whether this help the performance on your platform? Please note that I haven't test it in a running kernel and I'm not that familir with led subsystem, so use it with caution ;-) (While at it, I think maybe we miss the leddev_list_lock in net/mac80211 in the patch) Regards, Boqun ------->8 diff --git a/drivers/leds/led-triggers.c b/drivers/leds/led-triggers.c index 4e7b78a84149..ae68ccab6cc9 100644 --- a/drivers/leds/led-triggers.c +++ b/drivers/leds/led-triggers.c @@ -171,10 +171,12 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig) /* Remove any existing trigger */ if (led_cdev->trigger) { - write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); - list_del(&led_cdev->trig_list); - write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, + spin_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); + list_del_rcu(&led_cdev->trig_list); + spin_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags); + /* Wait for the readers gone */ + synchronize_rcu(); cancel_work_sync(&led_cdev->set_brightness_work); led_stop_software_blink(led_cdev); if (led_cdev->trigger->deactivate) @@ -186,9 +188,9 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig) led_set_brightness(led_cdev, LED_OFF); } if (trig) { - write_lock_irqsave(&trig->leddev_list_lock, flags); - list_add_tail(&led_cdev->trig_list, &trig->led_cdevs); - write_unlock_irqrestore(&trig->leddev_list_lock, flags); + spin_lock_irqsave(&trig->leddev_list_lock, flags); + list_add_tail_rcu(&led_cdev->trig_list, &trig->led_cdevs); + spin_unlock_irqrestore(&trig->leddev_list_lock, flags); led_cdev->trigger = trig; if (trig->activate) @@ -223,9 +225,12 @@ int led_trigger_set(struct led_classdev *led_cdev, struct led_trigger *trig) trig->deactivate(led_cdev); err_activate: - write_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); - list_del(&led_cdev->trig_list); - write_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags); + spin_lock_irqsave(&led_cdev->trigger->leddev_list_lock, flags); + list_del_rcu(&led_cdev->trig_list); + spin_unlock_irqrestore(&led_cdev->trigger->leddev_list_lock, flags); + + /* XXX could use call_rcu() here? */ + synchronize_rcu(); led_cdev->trigger = NULL; led_cdev->trigger_data = NULL; led_set_brightness(led_cdev, LED_OFF); @@ -285,7 +290,7 @@ int led_trigger_register(struct led_trigger *trig) struct led_classdev *led_cdev; struct led_trigger *_trig; - rwlock_init(&trig->leddev_list_lock); + spin_lock_init(&trig->leddev_list_lock); INIT_LIST_HEAD(&trig->led_cdevs); down_write(&triggers_list_lock); @@ -378,15 +383,14 @@ void led_trigger_event(struct led_trigger *trig, enum led_brightness brightness) { struct led_classdev *led_cdev; - unsigned long flags; if (!trig) return; - read_lock_irqsave(&trig->leddev_list_lock, flags); - list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) + rcu_read_lock(); + list_for_each_entry_rcu(led_cdev, &trig->led_cdevs, trig_list) led_set_brightness(led_cdev, brightness); - read_unlock_irqrestore(&trig->leddev_list_lock, flags); + rcu_read_unlock(); } EXPORT_SYMBOL_GPL(led_trigger_event); @@ -397,20 +401,19 @@ static void led_trigger_blink_setup(struct led_trigger *trig, int invert) { struct led_classdev *led_cdev; - unsigned long flags; if (!trig) return; - read_lock_irqsave(&trig->leddev_list_lock, flags); - list_for_each_entry(led_cdev, &trig->led_cdevs, trig_list) { + rcu_read_lock(); + list_for_each_entry_rcu(led_cdev, &trig->led_cdevs, trig_list) { if (oneshot) led_blink_set_oneshot(led_cdev, delay_on, delay_off, invert); else led_blink_set(led_cdev, delay_on, delay_off); } - read_unlock_irqrestore(&trig->leddev_list_lock, flags); + rcu_read_unlock(); } void led_trigger_blink(struct led_trigger *trig, diff --git a/include/linux/leds.h b/include/linux/leds.h index 6a8d6409c993..5acc0e8a9f18 100644 --- a/include/linux/leds.h +++ b/include/linux/leds.h @@ -356,7 +356,7 @@ struct led_trigger { struct led_hw_trigger_type *trigger_type; /* LEDs under control by this trigger (for simple triggers) */ - rwlock_t leddev_list_lock; + spinlock_t leddev_list_lock; struct list_head led_cdevs; /* Link to next registered trigger */ diff --git a/net/mac80211/led.c b/net/mac80211/led.c index b275c8853074..5ec5070fe210 100644 --- a/net/mac80211/led.c +++ b/net/mac80211/led.c @@ -283,10 +283,10 @@ static void tpt_trig_timer(struct timer_list *t) } } - read_lock(&local->tpt_led.leddev_list_lock); + rcu_read_lock(); list_for_each_entry(led_cdev, &local->tpt_led.led_cdevs, trig_list) led_blink_set(led_cdev, &on, &off); - read_unlock(&local->tpt_led.leddev_list_lock); + rcu_read_unlock(); } const char * @@ -349,10 +349,10 @@ static void ieee80211_stop_tpt_led_trig(struct ieee80211_local *local) tpt_trig->running = false; del_timer_sync(&tpt_trig->timer); - read_lock(&local->tpt_led.leddev_list_lock); + rcu_read_lock(); list_for_each_entry(led_cdev, &local->tpt_led.led_cdevs, trig_list) led_set_brightness(led_cdev, LED_OFF); - read_unlock(&local->tpt_led.leddev_list_lock); + rcu_read_unlock(); } void ieee80211_mod_tpt_led_trig(struct ieee80211_local *local,