Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2855972lqo; Mon, 20 May 2024 21:36:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUUuqgAuVlieVVRRr76Kjp4OEL/OlRCn6Uj06jRQP8AtoQIJphFHD3mt5NTjdM/StJv0NwkNtkwbwrdS7UCaATsk1MnsX/wFReY1Zlhcg== X-Google-Smtp-Source: AGHT+IEIK1axX1thz/n3oA3IpbZY8OybtLVBOq1GVjdPCxEPtXdpjiy7LQGKn/7WhPo7JhoB0Sm3 X-Received: by 2002:a05:6a00:1949:b0:6ee:1b6e:662a with SMTP id d2e1a72fcca58-6f4e0385338mr33725804b3a.32.1716266169195; Mon, 20 May 2024 21:36:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716266169; cv=pass; d=google.com; s=arc-20160816; b=vNZmR676LCNIzC5dXxQk9NtPZMu4MlFHTJqds0FJajOZmBJuGsyFPxjo7mJblnzV/D xVW/c8w7VZWDfPSpAKhi14nYsbP/cAgZMQmdzHIgvMNB1qnQw6otnt/hO0L52bBonqiO jwfiOp7CrrCgzVOKrIQGAiNzA/pOkdB2QP1Ut285bD6SJHOxmwgAHhGLkQ0ULtwVBwlY PFBSnzf1/08jqW21kucx5Y/tZXB07OO2ZImgCp9nXw/RMxF7hvSyb1tlRy1HPNfM4upd vgumZw9HZZ+8+xqmFKNRiYPnaRwFE/tXLI/IP3X5WQzvaPhgWyXy+gNZthjdtfDJU8LO 52Hw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :dkim-signature; bh=bu1zy9E7az32y56EOjWw+O7OSWd3qh6SwlhX3MCkEJU=; fh=t0xjoQfw2KioFrLVS1RJoipG7AE5c7a5aFROjTfV3yQ=; b=zopzQUnvLiAQ1JWVAtjDJnQr+MXOWjOacK7L73iPqDsB3YgBp7V9DoacelW5xbsTNz TTOsdgdVmgYLWcR2TTtCRoQmMt8GXnPerdDPO4HnTFimzTdNetxn7vB79D4RtCK9dzmW QxqA4z9IIBCig9R/YqSa2MwJWGVNYiigThov2UNY+7pkrq6oRGB3fHOgrqTqBps5FNCg ABY/yDIgn5c8HhmPVLXdKgNaAN6lTvNWR8ck+3atLHcUXMWIJo+hgVWpsDFXovUaZhKw RYbr5jJojdak9n93dlOEQohS5ha3gAOe2a+m1A9lddfpddgNbR4NdV0FCh9xqYeRz6vm vckQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SlAmuSfl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-184493-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184493-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 41be03b00d2f7-66b1c7c0ff4si3192795a12.759.2024.05.20.21.36.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 May 2024 21:36:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-184493-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SlAmuSfl; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-184493-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-184493-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 6B958B21D69 for ; Tue, 21 May 2024 04:36:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1BE2E2E85E; Tue, 21 May 2024 04:35:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SlAmuSfl" Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DCBC219FD for ; Tue, 21 May 2024 04:35:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716266155; cv=none; b=DI+E+SisKeAFTkR4Jn16nDjwNGwaBhekGTyxUElMyk4uh0QBSD2Vt6fYg3IGa9xX7PHsT2R6i1SvJvAKmctsBG29e0qLUN3ki4dcOoA3lkxEvbGtiypaXClU8M4CuyMr1LhOSCCWxoKn9WtARLHK31hyvW1rJjuy46zWlzp/ThI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716266155; c=relaxed/simple; bh=oIb1+7uh+06Z+q5AS8/+QPd2zvTrWldoSW9ItO1LXn0=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=i0WkqqPmtF+T1UXEAdnW41cvn9lwi8VdsicKZs+yscBBblQbISiHx27uGmTpJsCCABDDd7BSX7ogQG+kHWswYHXkQ4kShzFli5cGAzSk3AsO4nFKgMPBM7Q5zXJAlbbNwimrb8apxL4oHjNNrYhX0t9zjbY4qrXeEnwFQ+asvKI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SlAmuSfl; arc=none smtp.client-ip=209.85.167.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-518948e1ec8so7931e87.0 for ; Mon, 20 May 2024 21:35:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716266150; x=1716870950; darn=vger.kernel.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=bu1zy9E7az32y56EOjWw+O7OSWd3qh6SwlhX3MCkEJU=; b=SlAmuSflaDC56/H5Imkc/qHy6MqN19TMm/Qz1qkLktNAGFFMfkmf7uAKoH7U/JjF+y cjJmjfh2mL40bldWKMRAFYSI7rDer49yTtIrOS80nGkfm4jGaAkYmidCx5OG7aZlBdK/ aQQifmzMEJ3DPlG7nnoYBsFhS/GA4PFSUacXUJJwdMVi+SzjpFfHfhSQ/AcVFLL6hZHp /6SuWiVHmvyOKKgyS/nxMFCopv+iFTE6PMTrzL1uT+y7ZxhVhJnwKbTD4DONIZ3ZEa7q 2AV7Q8MmVDnywGiCRFfvnhrs6plibrsKX/VwuOUVsAr08eUSnxVSQAj6rauvRWpGnn8C AzcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716266150; x=1716870950; 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=bu1zy9E7az32y56EOjWw+O7OSWd3qh6SwlhX3MCkEJU=; b=VMq4BOKqccd3BOfX6BOyv3TAofYsNq7CmaJD1EwXSyad1N3qQ/LAdXH3AsKSMLFsez X+p1wxYnUxcHyJtba7rGPbcX/HgAN7IHfrebjvTvAb5qBUMSfKbncMU+YRbbLSrMRk4C 10+fN8RMQHnEx4kirGe140yhhHVxxEIlqZUwLVwK+xh1dOYtGdVm78GcK/qNNc+92UAL 8DPaGKvt9h5pGI34qhYaNjF7rdcM0XU0QG7sJevC+37ZaotnQX44gQAUPpXklZ9n4Nn+ LrjXVob3kBsswlTKboR5ehFOBzRzd8nVYziMGGCzlkTWXYxz2Jzqu9rKsIMP8rEu3iZv XACg== X-Forwarded-Encrypted: i=1; AJvYcCUwC7XcR7vCo/QBWup9yugJnqMKdH9RkCQG0LGHgNQhHHHraiVV7TTdPqQTcZsVUY1EPtB/q5gk/I3ku2sZhewAH1U6SBVYJducq3YW X-Gm-Message-State: AOJu0Yz4A/Bc7GbGpu31HcBMGb0lGzQ5RXmbGh62pPR5T/DltTUy1UBK kVribcwv6SVUAcD/xvytdjUsZjM+vdARD59Te8Oy0klbIpLCMXNAIK3+3wFFZBHewSmHvTDPL4j SoXScUeDjwhBfzwJ8aY/dpNOwDp43uWvKeQnB X-Received: by 2002:a05:6512:3a9:b0:523:69f8:ead with SMTP id 2adb3069b0e04-5240a1004e9mr297353e87.1.1716266150078; Mon, 20 May 2024 21:35:50 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240520205856.162910-1-andrey.konovalov@linux.dev> In-Reply-To: <20240520205856.162910-1-andrey.konovalov@linux.dev> From: Dmitry Vyukov Date: Tue, 21 May 2024 06:35:37 +0200 Message-ID: Subject: Re: [PATCH] kcov, usb: disable interrupts in kcov_remote_start_usb_softirq To: andrey.konovalov@linux.dev Cc: Alan Stern , Greg Kroah-Hartman , Andrey Konovalov , Marco Elver , Alexander Potapenko , kasan-dev@googlegroups.com, Tetsuo Handa , Tejun Heo , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" On Mon, 20 May 2024 at 22:59, wrote: > > From: Andrey Konovalov > > After commit 8fea0c8fda30 ("usb: core: hcd: Convert from tasklet to BH > workqueue"), usb_giveback_urb_bh() runs in the BH workqueue with > interrupts enabled. > > Thus, the remote coverage collection section in usb_giveback_urb_bh()-> > __usb_hcd_giveback_urb() might be interrupted, and the interrupt handler > might invoke __usb_hcd_giveback_urb() again. > > This breaks KCOV, as it does not support nested remote coverage collection > sections within the same context (neither in task nor in softirq). > > Update kcov_remote_start/stop_usb_softirq() to disable interrupts for the > duration of the coverage collection section to avoid nested sections in > the softirq context (in addition to such in the task context, which are > already handled). Besides the issue pointed by the test robot: Acked-by: Dmitry Vyukov Thanks for fixing this. This section of code does not rely on reentrancy, right? E.g. one callback won't wait for completion of another callback? At some point we started seeing lots of "remote cover enable write trace failed (errno 17)" errors while running syzkaller. Can these errors be caused by this issue? > Reported-by: Tetsuo Handa > Closes: https://lore.kernel.org/linux-usb/0f4d1964-7397-485b-bc48-11c01e2fcbca@I-love.SAKURA.ne.jp/ > Closes: https://syzkaller.appspot.com/bug?extid=0438378d6f157baae1a2 > Suggested-by: Alan Stern > Fixes: 8fea0c8fda30 ("usb: core: hcd: Convert from tasklet to BH workqueue") > Signed-off-by: Andrey Konovalov > --- > drivers/usb/core/hcd.c | 12 +++++++----- > include/linux/kcov.h | 44 +++++++++++++++++++++++++++++++++--------- > 2 files changed, 42 insertions(+), 14 deletions(-) > > diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c > index c0e005670d67..fb1aa0d4fc28 100644 > --- a/drivers/usb/core/hcd.c > +++ b/drivers/usb/core/hcd.c > @@ -1623,6 +1623,7 @@ static void __usb_hcd_giveback_urb(struct urb *urb) > struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); > struct usb_anchor *anchor = urb->anchor; > int status = urb->unlinked; > + unsigned long flags; > > urb->hcpriv = NULL; > if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && > @@ -1640,13 +1641,14 @@ static void __usb_hcd_giveback_urb(struct urb *urb) > /* pass ownership to the completion handler */ > urb->status = status; > /* > - * This function can be called in task context inside another remote > - * coverage collection section, but kcov doesn't support that kind of > - * recursion yet. Only collect coverage in softirq context for now. > + * Only collect coverage in the softirq context and disable interrupts > + * to avoid scenarios with nested remote coverage collection sections > + * that KCOV does not support. > + * See the comment next to kcov_remote_start_usb_softirq() for details. > */ > - kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum); > + flags = kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum); > urb->complete(urb); > - kcov_remote_stop_softirq(); > + kcov_remote_stop_softirq(flags); > > usb_anchor_resume_wakeups(anchor); > atomic_dec(&urb->use_count); > diff --git a/include/linux/kcov.h b/include/linux/kcov.h > index b851ba415e03..ebcfc271aee3 100644 > --- a/include/linux/kcov.h > +++ b/include/linux/kcov.h > @@ -55,21 +55,47 @@ static inline void kcov_remote_start_usb(u64 id) > > /* > * The softirq flavor of kcov_remote_*() functions is introduced as a temporary > - * work around for kcov's lack of nested remote coverage sections support in > - * task context. Adding support for nested sections is tracked in: > - * https://bugzilla.kernel.org/show_bug.cgi?id=210337 > + * workaround for KCOV's lack of nested remote coverage sections support. > + * > + * Adding support is tracked in https://bugzilla.kernel.org/show_bug.cgi?id=210337. > + * > + * kcov_remote_start_usb_softirq(): > + * > + * 1. Only collects coverage when called in the softirq context. This allows > + * avoiding nested remote coverage collection sections in the task context. > + * For example, USB/IP calls usb_hcd_giveback_urb() in the task context > + * within an existing remote coverage collection section. Thus, KCOV should > + * not attempt to start collecting coverage within the coverage collection > + * section in __usb_hcd_giveback_urb() in this case. > + * > + * 2. Disables interrupts for the duration of the coverage collection section. > + * This allows avoiding nested remote coverage collection sections in the > + * softirq context (a softirq might occur during the execution of a work in > + * the BH workqueue, which runs with in_serving_softirq() > 0). > + * For example, usb_giveback_urb_bh() runs in the BH workqueue with > + * interrupts enabled, so __usb_hcd_giveback_urb() might be interrupted in > + * the middle of its remote coverage collection section, and the interrupt > + * handler might invoke __usb_hcd_giveback_urb() again. > */ > > -static inline void kcov_remote_start_usb_softirq(u64 id) > +static inline unsigned long kcov_remote_start_usb_softirq(u64 id) > { > - if (in_serving_softirq()) > + unsigned long flags = 0; > + > + if (in_serving_softirq()) { > + local_irq_save(flags); > kcov_remote_start_usb(id); > + } > + > + return flags; > } > > -static inline void kcov_remote_stop_softirq(void) > +static inline void kcov_remote_stop_softirq(unsigned long flags) > { > - if (in_serving_softirq()) > + if (in_serving_softirq()) { > kcov_remote_stop(); > + local_irq_restore(flags); > + } > } > > #ifdef CONFIG_64BIT > @@ -103,8 +129,8 @@ static inline u64 kcov_common_handle(void) > } > static inline void kcov_remote_start_common(u64 id) {} > static inline void kcov_remote_start_usb(u64 id) {} > -static inline void kcov_remote_start_usb_softirq(u64 id) {} > -static inline void kcov_remote_stop_softirq(void) {} > +static inline unsigned long kcov_remote_start_usb_softirq(u64 id) {} > +static inline void kcov_remote_stop_softirq(unsigned long flags) {} > > #endif /* CONFIG_KCOV */ > #endif /* _LINUX_KCOV_H */ > -- > 2.25.1 >