From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4B91C3DA61 for ; Mon, 29 Jul 2024 09:43:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54CAC6B007B; Mon, 29 Jul 2024 05:43:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FA116B0083; Mon, 29 Jul 2024 05:43:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C20F6B0085; Mon, 29 Jul 2024 05:43:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 20FBB6B007B for ; Mon, 29 Jul 2024 05:43:12 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B1D591A0205 for ; Mon, 29 Jul 2024 09:43:11 +0000 (UTC) X-FDA: 82392301782.26.0521C71 Received: from mail-vs1-f44.google.com (mail-vs1-f44.google.com [209.85.217.44]) by imf16.hostedemail.com (Postfix) with ESMTP id EF33C18000C for ; Mon, 29 Jul 2024 09:43:09 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0OtIk7jg; spf=pass (imf16.hostedemail.com: domain of elver@google.com designates 209.85.217.44 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722246186; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZoJ3ZacNS55oRX7hJpHDUlm+X3SpBJFA/ypVEqbSLQ8=; b=Z7GjzhDjIF8EBYVX6cBTnZZ+ac/LIm+mpu77US1UBrc7vt0B/pSJSkXuAJiPlZW1ikT8Y9 JO8vBljqm6gsIWGMmGLcv1IsvA+HvKgzQZ31291nlyyIiQ2op6Re1RnqXXmUR5n456dLcC WqbZ+pAlXbSChrqG8AnSmKp7RpkeOxw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=0OtIk7jg; spf=pass (imf16.hostedemail.com: domain of elver@google.com designates 209.85.217.44 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722246186; a=rsa-sha256; cv=none; b=1n99Afgdwh/HtZDP/VhUGTSu05P85vz4W8Z42kC+jWMa2+Qdc4xr+cnavWjZpzCCXELo5F oqNcilPQBwgqRpheL2Ha5D+VVqMdNQawpixky7pziB3Ech2skUBP9iulvVHdKEXsylexAl Iy1k+8hz892GIldvBTI9o4V22zstYb0= Received: by mail-vs1-f44.google.com with SMTP id ada2fe7eead31-4929992a5e2so219028137.2 for ; Mon, 29 Jul 2024 02:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722246189; x=1722850989; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ZoJ3ZacNS55oRX7hJpHDUlm+X3SpBJFA/ypVEqbSLQ8=; b=0OtIk7jgh3Ul6BRn7Z2M40GlBwYpfNBKN2IL0OhlYkuXy/SWHSLJqNK97BYyiJGHuJ +YgILt+VZfAOmAJupD6Whsm/kbsmOhqSngw7ybMTsXrXRe0D/nUqztvYiJfXT4irL8qh LUXz5CbNyyLj4O6G0J3nGkNC3u1oi8PldPwGbDHMOMEH+ZDjzy1g7Iy+GXTCwiWo9oqy 01mS71K27i+9XGLO086YhYM+KQIAh2TT3vpfocP5PmLiU6U6mRQ9yjZe+ySbn6q6YfC4 yoOn2SaCA8rvPZFrAM9pfzqrvPwlRifWdLZ6utM/2yQsY295YQkcyUUPR8GhVX49IXsL 6yag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722246189; x=1722850989; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZoJ3ZacNS55oRX7hJpHDUlm+X3SpBJFA/ypVEqbSLQ8=; b=hO65HlgOEEdL2M5jV7quoGDVe0xAERLieUTuvWbAh+8ojiYOqob1eG6b7bN/O/APl0 rxeqf5YCgkYE8fVbVP//3gy9BqVIqiIW1G8wcriEa3XrE66w6GEwszZVlzi3MfxiLhHt WRaOCeFBfMXDNRzYubljl4fv8WinaeJN+5HZoe8dkGIwOZPen9PNPWz90Dygpa5l/43M IwyNyzG9V8t0MWNdd9ZgHzHuV/i+u/Pq3PFPPeosWVtqtF9F70dE7xyB/OLXa1WSDrwn +7xBlnnPBOakYcqX5P+TaVHdPHmYpqORs2h1yUZR+bbblQNQT6ZIg65KuCcatYeZX0Fk ltNw== X-Forwarded-Encrypted: i=1; AJvYcCXWb+yVMRiCO7zTiDViMedktr6rYKAclftyn7f1GOVVIz8zkerQSZESGFIemBq0bZbTHxppkzFTePWqs9q1PWmelXc= X-Gm-Message-State: AOJu0Yw0khGvAfPwhdaPUqd7EaZf7cTffGRNlU3V0xruOfQXC9S6T9w4 4d2RI7QnDI6fohvG7wqgoBPF7AogUx7/puYQ6pjfS5Y7Dltd0g9PNtytPVwsXQ+NDAAPQgR/NIa +AtP86m5+5LbI2oza75Lfdk7S0FcVolv5dDVg X-Google-Smtp-Source: AGHT+IEswDxk0VCMm32cFENqTsoXGj13514gdRwA6aQk9DZX2174YDOKdaN+jZodkccBMjEYKq6GfgKsKv7ISSQQam8= X-Received: by 2002:a05:6102:c4e:b0:493:d360:6f58 with SMTP id ada2fe7eead31-493fad1515dmr3478038137.20.1722246188837; Mon, 29 Jul 2024 02:43:08 -0700 (PDT) MIME-Version: 1.0 References: <20240729022158.92059-1-andrey.konovalov@linux.dev> In-Reply-To: <20240729022158.92059-1-andrey.konovalov@linux.dev> From: Marco Elver Date: Mon, 29 Jul 2024 11:42:30 +0200 Message-ID: Subject: Re: [PATCH] kcov: properly check for softirq context To: andrey.konovalov@linux.dev Cc: Dmitry Vyukov , Andrew Morton , Andrey Konovalov , Aleksandr Nogikh , Alexander Potapenko , kasan-dev@googlegroups.com, linux-mm@kvack.org, Alan Stern , Greg Kroah-Hartman , Marcello Sylvester Bauer , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+2388cdaeb6b10f0c13ac@syzkaller.appspotmail.com, stable@vger.kernel.org, Sebastian Andrzej Siewior Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Stat-Signature: qgwp9fr5niajw1aq8oohotunifz8eax9 X-Rspamd-Queue-Id: EF33C18000C X-Rspamd-Server: rspam11 X-HE-Tag: 1722246189-640991 X-HE-Meta: U2FsdGVkX1+We/EU1DI+zosY17ALdC9e555H1r6xnlTfJzIWwogZeso+JVAfEhdev45/0Orv4RQTkqDWaqISYQTt9IK29MD4iq3PU7Lu6Aw7KdWR6lUqOO2Fbj+2IhZvh7UK9Cbg2Bpa/FzQNQwMrRZZ6SQIQFHLwfJclEruCOVIX9LwR5cWljK0m4AjOQesGoKsIQsax+3SCM/mOrfnPO62ycS1ug69Z+7swPitYMAalDWHM/eCTJCkM54BSxMfEU5R2/M/g6lha0A1gupmxzmAgxS8s3N4byZGhNJ9VTM1AaleMseXJFlwMj3f873Lea/3fy7XIOAsCJDgNGOhUbmfqa53NFsQtxmx/DGqAFo6r12YGmhZrQ6JyjddWi/lkd43Piezjj442C8PESD3DeigdljIo5N4QZOXJbc2eC8nxVgcIoGJO85oAKqkH1ih5od3xMHSVT7HL0aqClJj9xCD+i6SX45InsK6vumIl62Rb/gB+eytZ6/9o0ll8Np5o8xEszIkqsTCCjP/vE56+0xYBMogLUG7FudWQCTb5aWdipetSMtXSEG6sbISNyqQZeOSxZKFwCKeTqlaYEuhagYO9gc89CWYEv3BZwo9Id8d7m6SDTEL9J+IFV0HcI9lZqssPXI+e03m3UAMk0x9E13pQKo6XaK2drC2G795MiORfGV8mNf+dgt4odUvp714+0ewmAv5iH/xqsbYRXfEm3V55GVOJiJw6R3C1QSoi8YclQRJks5S7t9M6N27HUkYFAhBgor2HU8FUnYATnqWRuzv1d+RauA08MNDrWNlop1NxjWmz4UH8plc3ssGcNdDhEGF6aCP5KlpK9xNXCydQ9SNp66907HEIhstZbrQdWq03JAwBYm+JVWWplw9Zjx7m+FETWYMsfW5tag8ollNe291jvd+GYhD9K1dH2o12E9Rls9YP5ql/dEK+nUxqyN7HA7/ziKXLnI1IllvfyE 4swOOjsJ l5J/fAo0URcuRJrhLlyCqxGBjpw7nqxquvNROG6UkZLLnXFbE14PbHYUgXjmBIsDtKaoOUDzW60xBnlFWi7wUFardeOI1wLhzU8TUjF3o/o4wf+TWDr0WlZKrQsn9tCZRG53CknGMtSSf5p9Xj1nga+m1sjDNB5sAgolTm33t0nCKN7nMeTzIiHPaXQL2099/QmJHhWcWZ52I5yHNPNG+xkughF3H39/EdEE9HXWon17rmm8R0IpUi8+RPPT+iO026d6LbYe8/9tMnbXlpICoOJSm6SqDBfa3eMEu9675VM5d9yW7lJqbx8Wvh3nDsTi7/2+x+FY76K043o6HdS0WKQfhb+P4WclLA0Lo6hSlrgxATa2pyP4oD4YPcSsgPViVYFJtvjnnM4aUzqCPQlMj+Jj+4GTYmXZySLNpFWgBpUxHRhNYVPj2dCKxQwUvObSMpa53EZpNTcuyfgkmnteUkRB3OA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, 29 Jul 2024 at 04:22, wrote: > > From: Andrey Konovalov > > When collecting coverage from softirqs, KCOV uses in_serving_softirq() to > check whether the code is running in the softirq context. Unfortunately, > in_serving_softirq() is > 0 even when the code is running in the hardirq > or NMI context for hardirqs and NMIs that happened during a softirq. > > As a result, if a softirq handler contains a remote coverage collection > section and a hardirq with another remote coverage collection section > happens during handling the softirq, KCOV incorrectly detects a nested > softirq coverate collection section and prints a WARNING, as reported > by syzbot. > > This issue was exposed by commit a7f3813e589f ("usb: gadget: dummy_hcd: > Switch to hrtimer transfer scheduler"), which switched dummy_hcd to using > hrtimer and made the timer's callback be executed in the hardirq context. > > Change the related checks in KCOV to account for this behavior of > in_serving_softirq() and make KCOV ignore remote coverage collection > sections in the hardirq and NMI contexts. > > This prevents the WARNING printed by syzbot but does not fix the inability > of KCOV to collect coverage from the __usb_hcd_giveback_urb when dummy_hcd > is in use (caused by a7f3813e589f); a separate patch is required for that. > > Reported-by: syzbot+2388cdaeb6b10f0c13ac@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=2388cdaeb6b10f0c13ac > Fixes: 5ff3b30ab57d ("kcov: collect coverage from interrupts") > Cc: stable@vger.kernel.org > Signed-off-by: Andrey Konovalov > --- > kernel/kcov.c | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/kernel/kcov.c b/kernel/kcov.c > index f0a69d402066e..274b6b7c718de 100644 > --- a/kernel/kcov.c > +++ b/kernel/kcov.c > @@ -161,6 +161,15 @@ static void kcov_remote_area_put(struct kcov_remote_area *area, > kmsan_unpoison_memory(&area->list, sizeof(area->list)); > } > > +/* > + * Unlike in_serving_softirq(), this function returns false when called during > + * a hardirq or an NMI that happened in the softirq context. > + */ > +static inline bool in_softirq_really(void) > +{ > + return in_serving_softirq() && !in_hardirq() && !in_nmi(); > +} Not sure you need this function. Check if just this will give you what you want: interrupt_context_level() == 1 I think the below condition could then also just become: if (interrupt_context_level() == 1 && t->kcov_softirq) Although the softirq_count() helper has a special PREEMPT_RT variant, and interrupt_context_level() doesn't, so it's not immediately obvious to me if that's also ok on PREEMPT_RT kernels. Maybe some RT folks can help confirm that using interrupt_context_level()==1 does what your above function does also on RT kernels. > static notrace bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t) > { > unsigned int mode; > @@ -170,7 +179,7 @@ static notrace bool check_kcov_mode(enum kcov_mode needed_mode, struct task_stru > * so we ignore code executed in interrupts, unless we are in a remote > * coverage collection section in a softirq. > */ > - if (!in_task() && !(in_serving_softirq() && t->kcov_softirq)) > + if (!in_task() && !(in_softirq_really() && t->kcov_softirq)) > return false; > mode = READ_ONCE(t->kcov_mode); > /* > @@ -849,7 +858,7 @@ void kcov_remote_start(u64 handle) > > if (WARN_ON(!kcov_check_handle(handle, true, true, true))) > return; > - if (!in_task() && !in_serving_softirq()) > + if (!in_task() && !in_softirq_really()) > return; > > local_lock_irqsave(&kcov_percpu_data.lock, flags); > @@ -991,7 +1000,7 @@ void kcov_remote_stop(void) > int sequence; > unsigned long flags; > > - if (!in_task() && !in_serving_softirq()) > + if (!in_task() && !in_softirq_really()) > return; > > local_lock_irqsave(&kcov_percpu_data.lock, flags); > -- > 2.25.1 >