Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp654692imm; Fri, 10 Aug 2018 19:46:02 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwCXF04cal0iRuhabb0Xlv5RYI7+E09Zx5oMwVYSzwDo7wENi5vV1deK2gE4AIbJvxzDStc X-Received: by 2002:a62:8559:: with SMTP id u86-v6mr9643950pfd.32.1533955562182; Fri, 10 Aug 2018 19:46:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533955562; cv=none; d=google.com; s=arc-20160816; b=i26Jkrve0Xva2mDnmKmJkBl1a7yOL+be45BhS8XkeiFmo+LjK9blI/sLgOPkojsmQ7 f1CLcw588gN5oVrmPBhaBesYkxnFQdEq9VT9/YrAd6tsNti7Hwx5Kjnn9etWdHBiAmmx IbPixkpqbxj2H0shsxPkkusId5P57gLpBNK6wD6LhKyeXv6TiDEwN4NeXteNMJuAG5kD E7TjKqpBgzY38AQKmKP6IMhQxyOivHk8jVodf/0tiz6nBx6C0bL+fOAYrpTCKeDxi+9Y X+eqJjssWoejklS6MXTA4PZW0fXVOpsLoDBFKDvnLVR/znSke5ITE2Tr0MnMHOnQ2tQX ND4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=JhM/6ooQu2FtN3ggKUlpXke1cU66CFp+EPc8Q8Fn/Mc=; b=YpoEGXG3U20i4edUeUkVIuYfA89rLsvSVYU9SdEF2dCxvunHdckFmiqWiR0LvShMmb ihGLHSOP/3lET5FRm9G4E5dCdZGk7Hw4UiOLbX4gWujVmZFyWY2NzX7dboB+5/aT3lWi hriSAQkTdhpG4dtB79nt4CjEgfB8QPDhmKwInfcQLgIhvhG2OdRkDll7cLO/J9ybxFjg O8ikeYmwIrqHdS57zGoUC+Bl6OJkD7G0IFnVFNjK8U6zrR2fmux+6cq1fPZxbKWBAFdO F3Lon5C+UMSG6gbDzgIrGe9SqjjJUQbBClVvki/EXmcGMxY6cEoLDQzIVPJxEzEY/Yts V3Nw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i10-v6si10977414pgk.203.2018.08.10.19.45.46; Fri, 10 Aug 2018 19:46:02 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727312AbeHKFRa (ORCPT + 99 others); Sat, 11 Aug 2018 01:17:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:49792 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727093AbeHKFRa (ORCPT ); Sat, 11 Aug 2018 01:17:30 -0400 Received: from home.goodmis.org (cpe-66-24-56-78.stny.res.rr.com [66.24.56.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BF3C7223FF; Sat, 11 Aug 2018 02:44:58 +0000 (UTC) Date: Fri, 10 Aug 2018 22:44:56 -0400 From: Steven Rostedt To: Jia-Ju Bai Cc: peterz@infradead.org, mingo@redhat.com, will.deacon@arm.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] kernel: locking: rtmutex: Fix a possible sleep-in-atomic-context bug in rt_mutex_handle_deadlock() Message-ID: <20180811024456.ykccnkbdrac4nbem@home.goodmis.org> References: <20180811023524.13845-1-baijiaju1990@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180811023524.13845-1-baijiaju1990@gmail.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Aug 11, 2018 at 10:35:24AM +0800, Jia-Ju Bai wrote: > The driver may sleep with holding a spinlock. > > The function call paths (from bottom to top) in Linux-4.16 are: > > [FUNC] schedule > kernel/locking/rtmutex.c, 1223: > schedule in rt_mutex_handle_deadlock > kernel/locking/rtmutex.c, 1273: > rt_mutex_handle_deadlock in rt_mutex_slowlock > kernel/locking/rtmutex.c, 1249: > _raw_spin_lock_irqsave in rt_mutex_slowlock > > To fix the bug, the spinlock is released before schedule() and then acquired again. > This is found by my static analysis tool (DSAC). > > Signed-off-by: Jia-Ju Bai > --- > kernel/locking/rtmutex.c | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c > index 2823d4163a37..af03e162f812 100644 > --- a/kernel/locking/rtmutex.c > +++ b/kernel/locking/rtmutex.c > @@ -1205,7 +1205,7 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state, > } > > static void rt_mutex_handle_deadlock(int res, int detect_deadlock, > - struct rt_mutex_waiter *w) > + struct rt_mutex_waiter *w, struct rt_mutex *lock) > { > /* > * If the result is not -EDEADLOCK or the caller requested > @@ -1219,8 +1219,10 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock, > */ > rt_mutex_print_deadlock(w); > while (1) { > + raw_spin_unlock_irq(&lock->wait_lock); > set_current_state(TASK_INTERRUPTIBLE); > schedule(); > + raw_spin_lock_irq(&lock->wait_lock); > } If you look at the code you will notice that it stops the task and never lets it continue. Ever. If we hit this path, it means we are in a deadlock scenario and will not make any forward progress. If anything, it should simply be: rt_mutex_print_deadlock(w); + /* We're not going anywhere, release the wait_lock */ + raw_spin_unlock_irq(&lock->wait_lock); while (1) { set_current_state(TASK_INTERRUPTIBLE); schedule(); } -- Steve > } > > @@ -1269,7 +1271,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state, > if (unlikely(ret)) { > __set_current_state(TASK_RUNNING); > remove_waiter(lock, &waiter); > - rt_mutex_handle_deadlock(ret, chwalk, &waiter); > + rt_mutex_handle_deadlock(ret, chwalk, &waiter, lock); > } > > /* > -- > 2.17.0