Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1336252ybj; Thu, 7 May 2020 22:00:52 -0700 (PDT) X-Google-Smtp-Source: APiQypJ9OTTaO75gs7u1lUs1V1dG5T8cEBL0cHjVCaohEOx15uEKb7pAcBbrXnIaMoN1vSL8wSRA X-Received: by 2002:a50:e8c7:: with SMTP id l7mr619926edn.309.1588914052501; Thu, 07 May 2020 22:00:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588914052; cv=none; d=google.com; s=arc-20160816; b=W9ovQgkm12lBciHTW7ZT4G0sQQAXKULug11/4QqpWIAN5hNQ3xYjXZ4evfQ1KcjP1m +PcYUgixom3HVnZEnESpa58+MDiN1ND6POawsN6mCHW++4g8Z/BzzqHn+H9TW4F8CdXd OrfPpaxc8VDvTpGraU77HBR7+8vVvXXuvulY9b7avcMTsG0wzhCgkkRfFZ1U1I7O+vTS tvsm/vaygI0pOSw7vj95KKgqeRZG1YyIIy7Y4xDWTleV3PF5O6Neq3p6550yVOFvgU8E ZB+CFaP3UUFA1dbdQ2sl77mgOOg7n1oOwSenXNkBmL9X+Rs2comit7a3e0ZTjG5tHRc1 J5qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=x68VNmAINfXid1iNJuaPcuSDsx8kamsfOgqkBmYYLqA=; b=LvbREXgk+600uOW976MMjAYsCy+PT14SRQ9g3yTihDS1OKNgRp4yXIlF37xu8zcdZi Hbl71cOYXeKchI+xfbRddqkCX1uRT+atT1OvqeUmLUCD8M/K04cHYz93qE8Yx52c48uK 2FvQKNRpaSolDNL3IRQjgyKef/1Z9ypkzIMThBSNdWpCEA2Fcef2lXyJvSsf4uZDXVmp hdqOJifxhO+ekc0jkkIkqsPVtXY54Q7zB5WbgJCXzoHSstBy95aOIfXFQkUV3IeB/HHq VQXfEmENxcM2NpicihdKGCdoWYCvgxT3V5zhCD7GS3ZPaON4YQDLfuSJvBk5MB6eTZCb pdwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dV7pThsF; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g26si330844edu.55.2020.05.07.22.00.29; Thu, 07 May 2020 22:00:52 -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=@google.com header.s=20161025 header.b=dV7pThsF; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726683AbgEHE5I (ORCPT + 99 others); Fri, 8 May 2020 00:57:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725681AbgEHE5H (ORCPT ); Fri, 8 May 2020 00:57:07 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BECBAC05BD43 for ; Thu, 7 May 2020 21:57:07 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id x8so779514qkf.3 for ; Thu, 07 May 2020 21:57:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=x68VNmAINfXid1iNJuaPcuSDsx8kamsfOgqkBmYYLqA=; b=dV7pThsFMm+cFN1gBiT8ezbUUuQVYKGqyrqVQrYkfY7dMTmx7pxxfKk0q07XMZ/Aui M8NQ6FePm12ujIC+Eq3mMKiIkL166CdottUpGDDRQxTMB++3R3LnNbMuRosM2HOC86vS nMj/1Ynz2w8Boix8gTEgtfENTwXX5tWhI0pqbmFGB4re52urwyInSTxV/mqV5IJe8gSv eypQMSJnhKRLor9LmHKO8hdkf0VCcoQx5RM/vwdVvM7CKW65e1sy6LuSgSSzTFPoJ9hG pUXumO1UR3+HR7jjZjwLhszQbmq4NKo+uMl7xWXTw7MacCVUKvyqwbHXpZAB2UAcu71j Dsfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=x68VNmAINfXid1iNJuaPcuSDsx8kamsfOgqkBmYYLqA=; b=jS9XwVyBp/xmudUSZ7+oGBGDmEiy2yaVR0uXvrhHz5F1rtJNFcyRAPmfbs8l2o/Yxk PwPYaTpAaYJU4ms9ggW/KdF7Yg8u+rCxlREem93ZwYm7HNgs+itOGZhjPv/Sedt7MRZD DYGARvSIELDGrMbKC4Etz5G3KHlwDcbJpWP0F0P//zcFN1nDQqj+KvLP+EqnA4Ov1zsK 55sQVjDsFruAX+6hckvQ0fu+1nqkBOdrSy0sq4b/wOCks4IpXgtLM+TqUj4LKL/rVBBb psnCOOIjnR/etw8zVitLYUGtENKmmex1LRlImkxa1nd7cqTgwj3aFXqwTzONyyYfwPdB M1QQ== X-Gm-Message-State: AGi0PuZaCS8+FHcTxa9TEJ28sQA1DtvccCt36GxcrMwHVSUVpPEIcSMt EiM3lpFYTHsbnOzJ8BGi/Z/VGLTpK/cc X-Received: by 2002:a0c:eb50:: with SMTP id c16mr891878qvq.151.1588913826901; Thu, 07 May 2020 21:57:06 -0700 (PDT) Date: Fri, 8 May 2020 14:56:43 +1000 Message-Id: <20200508145624.1.I4d7421c6bbb1f83ea58419082481082e19097841@changeid> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH] perf record: Use an eventfd to wakeup when done From: Anand K Mistry To: linux-perf-users@vger.kernel.org Cc: Anand K Mistry , Alexander Shishkin , Arnaldo Carvalho de Melo , Ingo Molnar , Jiri Olsa , Mark Rutland , Namhyung Kim , Peter Zijlstra , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Anand K Mistry --- tools/perf/builtin-record.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 1ab349abe90469..ce5fc3860131d2 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -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; + 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); + atexit(record__sig_exit); signal(SIGCHLD, sig_handler); signal(SIGINT, sig_handler); -- 2.26.2.645.ge9eca65c58-goog