Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755000AbdCIQc2 (ORCPT ); Thu, 9 Mar 2017 11:32:28 -0500 Received: from mx0a-00010702.pphosted.com ([148.163.156.75]:44399 "EHLO mx0b-00010702.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754894AbdCIQcZ (ORCPT ); Thu, 9 Mar 2017 11:32:25 -0500 From: Julia Cartwright To: Julia Lawall , Gilles Muller , Nicolas Palix , Michal Marek CC: , Thomas Gleixner , Sebastian Andrzej Siewior , Linus Walleij , Subject: [PATCH 01/19] Coccinelle: locks: identify callers of spin_lock{,_irq,_irqsave}() in irqchip implementations Date: Thu, 9 Mar 2017 10:21:48 -0600 Message-ID: <23190c912611c5d1d36529488e46706cdc3e4d87.1489015238.git.julia@ni.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-03-09_13:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703090122 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-03-09_13:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=30 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=30 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703090122 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3481 Lines: 134 On PREEMPT_RT, the spinlock_t type becomes an object which sleeps under contention. The codepaths used to support scheduling (irq dispatching, arch code, the scheduler, timers) therefore must make use of the raw_spin_lock{,_irq,_irqsave}() variations which preserve the non-sleeping spinlock behavior. Because the irq_chip callbacks are invoked in the process of interrupt dispatch, they cannot therefore make use of spin_lock_t type. Instead, the usage of raw_spinlock_t is appropriate. Provide a spatch to identify (and attempt to patch) such problematic irqchip implementations. Note to those generating patches using this spatch; in order to maintain correct semantics w/ PREEMPT_RT, it is necessary to audit the raw_spinlock_t-protected codepaths to ensure their execution is bounded and minimal. This is a manual audit process. See commit 47b03ca903fb0 ("pinctrl: qcom: Use raw spinlock variants") as an example of _one_ such instance, which fixed a real bug seen in the field. Cc: Thomas Gleixner Cc: Sebastian Andrzej Siewior Cc: Linus Walleij Signed-off-by: Julia Cartwright --- .../coccinelle/locks/irq_chip_raw_spinlock.cocci | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 scripts/coccinelle/locks/irq_chip_raw_spinlock.cocci diff --git a/scripts/coccinelle/locks/irq_chip_raw_spinlock.cocci b/scripts/coccinelle/locks/irq_chip_raw_spinlock.cocci new file mode 100644 index 000000000000..73fd4519ae29 --- /dev/null +++ b/scripts/coccinelle/locks/irq_chip_raw_spinlock.cocci @@ -0,0 +1,96 @@ +// Copyright: (C) 2017 National Instruments Corp. GPLv2. +// Author: Julia Cartwright +// +// Identify callers of non-raw spinlock_t functions in hardirq irq_chip +// callbacks. +// +// spin_lock{_irq,_irqsave}(), w/ PREEMPT_RT are "sleeping" spinlocks, and so +// therefore "sleep" under contention; identify (and potentially patch) callers +// to use raw_spinlock_t instead. +// +// Confidence: Moderate + +virtual report +virtual patch + +@match@ +identifier __irqchip; +identifier __irq_mask; +@@ + static struct irq_chip __irqchip = { + .irq_mask = __irq_mask, + }; + +@match2 depends on match@ +identifier match.__irq_mask; +identifier data; +identifier x; +identifier l; +type T; +position j0; +expression flags; +@@ + static void __irq_mask(struct irq_data *data) + { + ... + T *x; + ... +( + spin_lock_irqsave(&x->l@j0, flags); +| + spin_lock_irq(&x->l@j0); +| + spin_lock(&x->l@j0); +) + ... + } + +@match3 depends on match2 && patch@ +type match2.T; +identifier match2.l; +@@ + T { + ... +- spinlock_t l; ++ raw_spinlock_t l; + ... + }; + +@match4 depends on match2 && patch@ +type match2.T; +identifier match2.l; +expression flags; +T *x; +@@ + +( +-spin_lock(&x->l) ++raw_spin_lock(&x->l) +| +-spin_lock_irqsave(&x->l, flags) ++raw_spin_lock_irqsave(&x->l, flags) +| +-spin_lock_irq(&x->l) ++raw_spin_lock_irq(&x->l) +| +-spin_unlock(&x->l) ++raw_spin_unlock(&x->l) +| +-spin_unlock_irq(&x->l) ++raw_spin_unlock_irq(&x->l) +| +-spin_unlock_irqrestore(&x->l, flags) ++raw_spin_unlock_irqrestore(&x->l, flags) +| +-spin_lock_init(&x->l) ++raw_spin_lock_init(&x->l) +) + +@script:python wat depends on match2 && report@ +j0 << match2.j0; +t << match2.T; +l << match2.l; +@@ + +msg = "Use of non-raw spinlock is illegal in this context (%s::%s)" % (t, l) +coccilib.report.print_report(j0[0], msg) -- 2.11.1