Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1793124ybk; Mon, 11 May 2020 04:31:31 -0700 (PDT) X-Google-Smtp-Source: APiQypIP0BaDNa5eRH/nxwhXUngAei+ESNrK9on2RrJO/LOV5b5wb0TaDYl4kfQOxxnSsKtFr2G/ X-Received: by 2002:a05:6402:2035:: with SMTP id ay21mr5705924edb.279.1589196690905; Mon, 11 May 2020 04:31:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589196690; cv=none; d=google.com; s=arc-20160816; b=L3CMFigPIsOJQ+9U5rsCZuauYbp3pVXO8N599FaKVJZAorBTt2pZHHk0Hqvd72iVKa 5A0Gs3oApbhWQKxfkQAnfrwnNzo9rQCg4NgpntiK7s4kLHsTjnG1HXGjJRIghF3kqF1i KjuTN5f5SYbtnFdSRywHXIVQIugcCCo+776PMfnoW/8EQLnspW3se6phmaYlF6t8ccXM YjyEktn222frUSEb6qCBRYDkC8qyj564U5LC2+1NUan3rD4WHH0B10wgzVmvj5gCuutN 4NOyauMhlFZXIH+jxK+YKkySopnrRmZW//xXSQllLdwPtpIUfAXeVpVF6cOxBUzvLSWo geHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=kMThpkceIHVkmBtaCW1DTUS61tjMEkTTbd1IFAK0wUA=; b=JQLRE+x1wVBCilyIfDJdRWjx5Xs9EaLlClzCv+PIG1Dw3dBYzRNR0+4uz5BtI8BIT6 V3e6olBU0b7j/Xaj6DtRmJQ9P38QNo+SmMyzYTUzAYuZvtupWT4YANhlHIHJedDLxN0v gR8aGoM0ZhHhf8n+pO6di1FW8GKEH3rQZbW86tw870GwOXdZ5RspJdHwD7pyIQImMfwZ 1w68JYOfvVMb2lf+ZaKWwD3kawCcVorYWGj46IvyjgLanjB94YWKkvRkjiK1lUIb9JJQ 2h63XOaYrefYo806g7N8d7lGMBYVV7wW7jBK13XJNZlTQEPTUaHJme58S9qvz0DZQalQ cWDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ReG51ffh; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p62si5765415edp.385.2020.05.11.04.31.07; Mon, 11 May 2020 04:31:30 -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=@redhat.com header.s=mimecast20190719 header.b=ReG51ffh; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730057AbgEKL2n (ORCPT + 99 others); Mon, 11 May 2020 07:28:43 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:42850 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730048AbgEKL2k (ORCPT ); Mon, 11 May 2020 07:28:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589196519; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=kMThpkceIHVkmBtaCW1DTUS61tjMEkTTbd1IFAK0wUA=; b=ReG51ffhaegyAspANLL/LnKwUeh+VltMTQKDaBgXZ/TLbVU8Z18UlpKavOusqAajdvSi/X mNHXTMzPzmeN9CvsfkIQyxgg5OKQ2/b3viwj6GMF/vwr5YcIH6Qnv/kggpdYoJGggkFDJ3 8nRXcwHjD6M4CxB5bNtB0a7jW2JosGE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-231-isU4oOttPfiFwIgzJyiw7A-1; Mon, 11 May 2020 07:28:37 -0400 X-MC-Unique: isU4oOttPfiFwIgzJyiw7A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4019E107ACCD; Mon, 11 May 2020 11:28:36 +0000 (UTC) Received: from krava (unknown [10.40.194.31]) by smtp.corp.redhat.com (Postfix) with SMTP id 4414B9CB9; Mon, 11 May 2020 11:28:34 +0000 (UTC) Date: Mon, 11 May 2020 13:28:33 +0200 From: Jiri Olsa To: Anand K Mistry Cc: linux-perf-users@vger.kernel.org, Alexander Shishkin , Arnaldo Carvalho de Melo , Ingo Molnar , Mark Rutland , Namhyung Kim , Peter Zijlstra , linux-kernel@vger.kernel.org Subject: Re: [PATCH] perf record: Use an eventfd to wakeup when done Message-ID: <20200511112833.GE2986380@krava> References: <20200508145624.1.I4d7421c6bbb1f83ea58419082481082e19097841@changeid> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200508145624.1.I4d7421c6bbb1f83ea58419082481082e19097841@changeid> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 08, 2020 at 02:56:43PM +1000, Anand K Mistry wrote: > The setting and checking of 'done' contains a rare race where the signal > handler setting 'done' is run after checking to break the loop, but > before waiting in evlist__poll(). In this case, the main loop won't wake > up until either another signal is sent, or the perf data fd causes a > wake up. > > The following simple script can trigger this condition (but you might > need to run it for several hours): > for ((i = 0; i >= 0; i++)) ; do > echo "Loop $i" > delay=$(echo "scale=4; 0.1 * $RANDOM/32768" | bc) > ./perf record -- sleep 30000000 >/dev/null& > pid=$! > sleep $delay > kill -TERM $pid > echo "PID $pid" > wait $pid > done > > At some point, the loop will stall. Adding logging, even though perf has > received the SIGTERM and set 'done = 1', perf will remain sleeping until > a second signal is sent. so it's just few instructions in between the check and the evlist__poll if (done || draining) break; err = evlist__poll(rec->evlist, -1); nice catch! SNIP > @@ -518,15 +519,19 @@ static int record__pushfn(struct mmap *map, void *to, void *bf, size_t size) > > static volatile int signr = -1; > static volatile int child_finished; > +static int done_fd = -1; > > static void sig_handler(int sig) > { > + u64 tmp = 1; > if (sig == SIGCHLD) > child_finished = 1; > else > signr = sig; > > done = 1; could you please put some explaining comment in here, so we are not confused by this in few months ;-) > + if (write(done_fd, &tmp, sizeof(tmp)) < 0) > + pr_err("failed to signal wakeup fd\n"); > } > > static void sigsegv_handler(int sig) > @@ -1424,6 +1429,9 @@ static int __cmd_record(struct record *rec, int argc, const char **argv) > int fd; > float ratio = 0; > > + done_fd = eventfd(0, EFD_NONBLOCK); > + evlist__add_pollfd(rec->evlist, done_fd); both of those can fail, please check the return values thanks, jirka > + > atexit(record__sig_exit); > signal(SIGCHLD, sig_handler); > signal(SIGINT, sig_handler); > -- > 2.26.2.645.ge9eca65c58-goog >