Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2663918ioo; Tue, 24 May 2022 03:15:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyI/cYwsjVpw9B53iWVzb2idL0szr8cl58pW+fZeCHgx2STIMklHBEYBf22T+D0lyEv0e6o X-Received: by 2002:a05:6402:518f:b0:42b:4404:63d4 with SMTP id q15-20020a056402518f00b0042b440463d4mr15627475edd.177.1653387351898; Tue, 24 May 2022 03:15:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653387351; cv=none; d=google.com; s=arc-20160816; b=wjbCtGyytkkywtIxki9660EFLq1m+L9INW8wXQMA+6gXy0Ys4CGqyxizMPDH231ktk 2SRmVfmKTA/ngqEF7l4umjnIM1KvrqNpnsdC3W1IUb9XJMoIK9JIIFTGl+e435jtZ2Ux ulRasFP2XmRwSFeAYuWK1Aet62A5+dq3+qztHcqsW71ytKt8f7tyB9AZnYMN6Mwuy0x0 xGk5HR4siFmVqXpFvne9x3fRerdFq1+hKgHEEN4ngyBaUkeai86x3ybu820FtiHeNSPG ZRpU3+DoGAu10tGqYaqGInoGYoKlasMWMjJ4ufbdqgpWZgmlyl8VI6PlB7b83axnU3vC nHLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vBkx9/je7nDyB+6MMJGNSqtAxz9DvJ1+u+v1K7otlCo=; b=xmVF7pWvkkRkBd+eC/a5jZ6ur0egNdwRh5YfLiEgY4cFH6qU6vL5yPLtW+OxE/MKCA KDR6JNHLtuWLArvTUzcjPhWIqP9D4gPh4a11Os6aDXPqlRX1f58JPmHJQ1LI8U0deLwo hC/Ya3AtuPva0rnAt9F989VmhS33ImyH89ThqQA1vi6WSc/YFwFliM0/dXkwaAlUGdK7 MX0NmHHjFXY5h8f6h1u4AmRuIuhm9hAIEz32fyptzDxpV8GRAD2HUyMtqFqSsOP9kmWI QqJ+EyDq2VPiVAz+iFxOzi4v1hmHCa06narFaOfii+LTCfeP+fCJIImIEeAEAFLjiyGj dvFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="oCve/KDF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mp22-20020a1709071b1600b006f4a3a09585si20138004ejc.782.2022.05.24.03.15.25; Tue, 24 May 2022 03:15:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="oCve/KDF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233013AbiEXGjM (ORCPT + 99 others); Tue, 24 May 2022 02:39:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233019AbiEXGjK (ORCPT ); Tue, 24 May 2022 02:39:10 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C54197282 for ; Mon, 23 May 2022 23:39:08 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id bq30so29224091lfb.3 for ; Mon, 23 May 2022 23:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vBkx9/je7nDyB+6MMJGNSqtAxz9DvJ1+u+v1K7otlCo=; b=oCve/KDFWOvJXlCjrDxr59ZkXz3gpAMriZ1CVyW2u3n/AfE5CYatZcPDTesNJ0T2+X q8037e5u8YjM1/cpy44xNB2+W6t/XDb9b/ON+b+w9ZxeF1f5amqqXObaxe281MkPgrsa oxy2ABaoZ5hyijHZQaYHOn2zCbFDj87w9KHQ5EJhmp5Kj0tQ9JNr/TqI8IBdAKrn4jzM GFuDOzeYweicfF9LiGHCLZ08mNrywURmSVwk52tQIGnuDmUW2/wrj1zNfCQefQqh7MxX WJ9RQy7xEElthYpYHKyglDr5DpfqYxhPzBhGgiuGcnv3Ms1WNqMCOIim0RpxH9RCs0Pr irUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vBkx9/je7nDyB+6MMJGNSqtAxz9DvJ1+u+v1K7otlCo=; b=GP+aEVSJfB6b4Pf+5to3mCgM0wb5gTyxTN6KvKGLdkMBrKkHi8+r4toHhL5EW292S+ NCJkmq4aPQxUvBadevOLV4Y0rVjTNoPOKOYTPAYRYNwKzzveMslIQINCFsK8S+SP5IsU uEn/DjqXNAD1P6M8PrL3vhABsekS8W0990Iz+CJmEciEySeO5bjIoj+vIV07ZIqws6Ft gv2CeisBmyepIfSXwiPLuUp6kYRxdSAi1qEF53fawtTKFiztLH2omh4OU8NxRWzrdnLs D175S94i6LzSe7p6XzwaOWpSD8Tx2C82NER+MFk+p7+OX+h0PCsFqLFaOJERWp4pNKRI GtbQ== X-Gm-Message-State: AOAM530cSnQ0cKwj680EDi7Syg2YzNwU+CsD9fV0Cs1kN8xIJGdtiYgv NZlppyl94o7PfrHiFe1r04qKvi4/bdp4Eod/dmdQAwr9yVM= X-Received: by 2002:a05:6512:3da0:b0:478:5b79:d76e with SMTP id k32-20020a0565123da000b004785b79d76emr11591958lfv.540.1653374346548; Mon, 23 May 2022 23:39:06 -0700 (PDT) MIME-Version: 1.0 References: <20220523053531.1572793-1-liu3101@purdue.edu> In-Reply-To: From: Dmitry Vyukov Date: Tue, 24 May 2022 08:38:55 +0200 Message-ID: Subject: Re: [PATCH v2] kcov: update pos before writing pc in trace function To: "Liu, Congyu" Cc: "andreyknvl@gmail.com" , "kasan-dev@googlegroups.com" , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 24 May 2022 at 05:08, Liu, Congyu wrote: > > It was actually first found in the kernel trace module I wrote for my research > project. For each call instruction I instrumented one trace function before it > and one trace function after it, then expected traces generated from > them would match since I only instrumented calls that return. But it turns > out that it didn't match from time to time in a non-deterministic manner. > Eventually I figured out it was actually caused by the overwritten issue > from interrupt. I then referred to kcov for a solution but it also suffered from > the same issue...so here's this patch :). Ah, interesting. Thanks for sharing. > ________________________________________ > From: Dmitry Vyukov > Sent: Monday, May 23, 2022 4:38 > To: Liu, Congyu > Cc: andreyknvl@gmail.com; kasan-dev@googlegroups.com; linux-kernel@vger.kernel.org > Subject: Re: [PATCH v2] kcov: update pos before writing pc in trace function > > On Mon, 23 May 2022 at 07:35, Congyu Liu wrote: > > > > In __sanitizer_cov_trace_pc(), previously we write pc before updating pos. > > However, some early interrupt code could bypass check_kcov_mode() > > check and invoke __sanitizer_cov_trace_pc(). If such interrupt is raised > > between writing pc and updating pos, the pc could be overitten by the > > recursive __sanitizer_cov_trace_pc(). > > > > As suggested by Dmitry, we cold update pos before writing pc to avoid > > such interleaving. > > > > Apply the same change to write_comp_data(). > > > > Signed-off-by: Congyu Liu > > This version looks good to me. > I wonder how you encountered this? Do you mind sharing a bit about > what you are doing with kcov? > > Reviewed-by: Dmitry Vyukov > > Thanks > > > --- > > PATCH v2: > > * Update pos before writing pc as suggested by Dmitry. > > > > PATCH v1: > > https://lore.kernel.org/lkml/20220517210532.1506591-1-liu3101@purdue.edu/ > > --- > > kernel/kcov.c | 14 ++++++++++++-- > > 1 file changed, 12 insertions(+), 2 deletions(-) > > > > diff --git a/kernel/kcov.c b/kernel/kcov.c > > index b3732b210593..e19c84b02452 100644 > > --- a/kernel/kcov.c > > +++ b/kernel/kcov.c > > @@ -204,8 +204,16 @@ void notrace __sanitizer_cov_trace_pc(void) > > /* The first 64-bit word is the number of subsequent PCs. */ > > pos = READ_ONCE(area[0]) + 1; > > if (likely(pos < t->kcov_size)) { > > - area[pos] = ip; > > + /* Previously we write pc before updating pos. However, some > > + * early interrupt code could bypass check_kcov_mode() check > > + * and invoke __sanitizer_cov_trace_pc(). If such interrupt is > > + * raised between writing pc and updating pos, the pc could be > > + * overitten by the recursive __sanitizer_cov_trace_pc(). > > + * Update pos before writing pc to avoid such interleaving. > > + */ > > WRITE_ONCE(area[0], pos); > > + barrier(); > > + area[pos] = ip; > > } > > } > > EXPORT_SYMBOL(__sanitizer_cov_trace_pc); > > @@ -236,11 +244,13 @@ static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip) > > start_index = 1 + count * KCOV_WORDS_PER_CMP; > > end_pos = (start_index + KCOV_WORDS_PER_CMP) * sizeof(u64); > > if (likely(end_pos <= max_pos)) { > > + /* See comment in __sanitizer_cov_trace_pc(). */ > > + WRITE_ONCE(area[0], count + 1); > > + barrier(); > > area[start_index] = type; > > area[start_index + 1] = arg1; > > area[start_index + 2] = arg2; > > area[start_index + 3] = ip; > > - WRITE_ONCE(area[0], count + 1); > > } > > } > > > > -- > > 2.34.1 > >