Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp236519ybg; Mon, 8 Jun 2020 22:19:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoNv23KDN72ZBWd7hnkzNUN2ZnjivroG2Zyesc30BYlEz7QBy6HQb8Tqnq87p9E/T48O1j X-Received: by 2002:a17:906:f913:: with SMTP id lc19mr23592751ejb.286.1591679977049; Mon, 08 Jun 2020 22:19:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591679977; cv=none; d=google.com; s=arc-20160816; b=BAW9VwmqFc1+iBM2JEYVfsARTVDdsGiUzuEZnqBx4qgTvxRq+iSh94fadXsJKc0Mc+ unVV33V2a/n9xAwL6u8FH4EQerDA+WlMHDlfL0KNt7IKkMo2iMLRa2DC4+gAyGnG4QhF YREJvmzRuxk2CdqVN+M/h91EHnGnqUTDaeZkLWiDJ8ILv3LWSTSBvRUYrNUXJFaqqRNV 8Qplt5NwbCX78iXMBlHS0+8nACZQiDTtKaKcxsRG5h+m/EgP9Qn7RpBm8xbssNCPDGoJ bCRFGOQsk0t3+YIZTU7aeJGiN3KdOtoVsk4tBFUPAvishYKlzbDm9irP1VpPQeRgWKIu I8xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=HvLmUO9WfIu9EpDHvCN9J3AGrpe38k/zLFBsg+AFDwY=; b=hAnO6aSv+YjzP2Dd2gxDZnxmmu0ZoZZUEwaLNKo2gYS1DbApYBoTe87jcsVA+07PJm kly+42DAIZAzKz8d/4E7LXEfZWVAeY1daf0jUUiW+OBVJ6rJgsmN8sNUMHkqZ0dLuWGZ dA4D5Ip8MZJWcXFf8uumtOJy+aC7YBRRBDciAnyDiW2TjasNJSNmF3wiZyj575NC/39m zjVu1lDi+fTXk1GKSYd2ZaXW16A/alKIyvZAmqR0RbEWE0kj/AIbh5+GO0pcOjWSpAVw sLnmBhIXS+lCKae3bdcAiXi4+cKzafbIg3ba5VXpq3XdAwI3gD04jKBqzWQ2PxMF/84U xKGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=iszL6iYl; 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=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u7si9924591ejn.279.2020.06.08.22.18.59; Mon, 08 Jun 2020 22:19:37 -0700 (PDT) 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=@oracle.com header.s=corp-2020-01-29 header.b=iszL6iYl; 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=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726803AbgFIFPF (ORCPT + 99 others); Tue, 9 Jun 2020 01:15:05 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:35488 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725770AbgFIFPF (ORCPT ); Tue, 9 Jun 2020 01:15:05 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0595CVQm136842; Tue, 9 Jun 2020 05:14:31 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2020-01-29; bh=HvLmUO9WfIu9EpDHvCN9J3AGrpe38k/zLFBsg+AFDwY=; b=iszL6iYlKhWol9vdCUWhrzgkJJb+oMfZ/a0pOWH8y1Or5OZZhaOrkoI+k9ELCuIqVvID 10B9oBJ0YOdpHMemK5Wv/7H5FnPD+TmBn6fxV6EXqKPAY3OGOXiP91AjNhTumNsyxMPP 0SYH10AxC7qwII63f8AJ+1VokEux0NKpn7wxtJHRdqrj8CYIf9U+19FZEf/48O0czYoL vc5nJMD1PheyGaRJ0nW/MvXJKAYP8nSrHzakv2ic1lnh2GR1y0C2lYh7HQTUUFwf7eFq mWWY4CecbtgZOuuoQ8qI8IKX5l7qQ6LnuErWEM67L08Gbkik+Fvt2rd9JtxgA2ptYyc8 Sw== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 31g3smtd5n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 09 Jun 2020 05:14:31 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 05958KcB033010; Tue, 9 Jun 2020 05:14:30 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 31gn24tm1y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 09 Jun 2020 05:14:30 +0000 Received: from abhmp0010.oracle.com (abhmp0010.oracle.com [141.146.116.16]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 0595ETpL004959; Tue, 9 Jun 2020 05:14:29 GMT Received: from dhcp-10-159-136-131.vpn.oracle.com (/10.159.136.131) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 08 Jun 2020 22:14:29 -0700 Subject: Re: [PATCH] sample-trace-array: Fix sleeping function called from invalid context To: Kefeng Wang , linux-kernel@vger.kernel.org Cc: Steven Rostedt , Aruna Ramakrishna References: <20200608075437.1760242-1-wangkefeng.wang@huawei.com> From: Divya Indi Message-ID: <0047f80f-4fff-16ff-7ac0-20181f03f06a@oracle.com> Date: Mon, 8 Jun 2020 22:14:28 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 In-Reply-To: <20200608075437.1760242-1-wangkefeng.wang@huawei.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9646 signatures=668680 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=3 mlxscore=0 phishscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006090039 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9646 signatures=668680 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 priorityscore=1501 lowpriorityscore=0 impostorscore=0 cotscore=-2147483648 suspectscore=3 spamscore=0 bulkscore=0 malwarescore=0 phishscore=0 mlxscore=0 mlxlogscore=999 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2004280000 definitions=main-2006090039 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Kefeng, Thanks for catching this issue. Please find my comments line - On 6/8/20 12:54 AM, Kefeng Wang wrote: > BUG: sleeping function called from invalid context at kernel/locking/mutex.c:935 > in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 0, name: swapper/5 > 1 lock held by swapper/5/0: > #0: ffff80001002bd90 (samples/ftrace/sample-trace-array.c:38){+.-.}-{0:0}, at: call_timer_fn+0x8/0x3e0 > CPU: 5 PID: 0 Comm: swapper/5 Not tainted 5.7.0+ #8 > Hardware name: QEMU QEMU Virtual Machine, BIOS 0.0.0 02/06/2015 > Call trace: > dump_backtrace+0x0/0x1a0 > show_stack+0x20/0x30 > dump_stack+0xe4/0x150 > ___might_sleep+0x160/0x200 > __might_sleep+0x58/0x90 > __mutex_lock+0x64/0x948 > mutex_lock_nested+0x3c/0x58 > __ftrace_set_clr_event+0x44/0x88 > trace_array_set_clr_event+0x24/0x38 > mytimer_handler+0x34/0x40 [sample_trace_array] > > mutex_lock() will be called in interrupt context, using workqueueu to fix it. /s/workqueueu/workqueue Fixes: 89ed42495ef4 ("tracing: Sample module to demonstrate kernel access to Ftrace instances.") > Signed-off-by: Kefeng Wang > --- > samples/ftrace/sample-trace-array.c | 16 +++++++++++----- > 1 file changed, 11 insertions(+), 5 deletions(-) > > diff --git a/samples/ftrace/sample-trace-array.c b/samples/ftrace/sample-trace-array.c > index d523450d73eb..41684c7dbd7b 100644 > --- a/samples/ftrace/sample-trace-array.c > +++ b/samples/ftrace/sample-trace-array.c > @@ -20,6 +20,16 @@ struct trace_array *tr; > static void mytimer_handler(struct timer_list *unused); > static struct task_struct *simple_tsk; > > +static void trace_work_fn(struct work_struct *work) > +{ > + /* > + * Disable tracing for event "sample_event". > + */ > + trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", > + false); > +} > +static DECLARE_WORK(trace_work, trace_work_fn); > + > /* > * mytimer: Timer setup to disable tracing for event "sample_event". This > * timer is only for the purposes of the sample module to demonstrate access of > @@ -29,11 +39,7 @@ static DEFINE_TIMER(mytimer, mytimer_handler); > > static void mytimer_handler(struct timer_list *unused) > { > - /* > - * Disable tracing for event "sample_event". > - */ > - trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", > - false); > + schedule_work(&trace_work); > } > > static void simple_thread_func(int count) I think we also need to use cancel_work_sync() to handle the case - 1. Module unloaded 2. Timer already ran and scheduled work to disable trace event 3. When the work runs we no longer have the relevant trace array. static int simple_thread(void *arg) { .....  del_timer(&mytimer); ..... return 0; } Thanks, Divya