From patchwork Wed Mar 22 20:24:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Liang, Kan" X-Patchwork-Id: 73668 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2561640wrt; Wed, 22 Mar 2023 13:45:37 -0700 (PDT) X-Google-Smtp-Source: AK7set+hY3BiBkgBWP4hf2RB18wwBCa+5ntlxwePcoPTGrGfkcxYXUOg8BZNMwNKtEwDRZXCH5o2 X-Received: by 2002:a05:6402:78f:b0:4af:6c5e:225c with SMTP id d15-20020a056402078f00b004af6c5e225cmr7327686edy.33.1679517937663; Wed, 22 Mar 2023 13:45:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679517937; cv=none; d=google.com; s=arc-20160816; b=OvIgekIPi292czx2toMUo2D63WmSjltUlQ7Q9rgo0jW2mhe+86QeFGMtXL34x0cK3B tk+GqgI5wdXf0OHljPk0iq3CcoqEony1DjIxsf7tDEv+pblRF3YxF6oopmbYMvHUsmA8 ctcoOOxljgrijGsxnwcMu0EWh1kLA7mruirPxjWtgXZYQq+KZJUx3pFZHXCvKfFB1nRK HUDJ9KpHTdV5kjoE+fCvjdYwwPneMjwcSvgDiVa3l2PRvD0ywjPdK3cOeq5j924r+/wV Pckd9nJsdsZfdRkbFFUjht6rGBg64kN/2SVr+1xpH1hfIkU2MuNZk6RZvxlqjl8aQ/LP 6uDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=OqBYXCXyngy3Pv7J6Z3W77hgBoZBKflsYI0QGyf9bx0=; b=R4xeNcAc4sEsqV2SEwb3Io0hEzaHTVZvmyhq/oa1OD148NRYKKnzKCR62f8K+9YMaK 0ymflNYVYwznyG6DqU1m2IRStb1iYtirEau71N79My9ec2XPN77IcEKPUll25ZjTAC39 4aVp0+nZPU/h9W8XDADmgEJM3fN4xwm7Vxdz+IdGpeHwsLNybT6Fq+mLYF+jotoYDo9T ZELDld7FHbmMMUxZAXSsK8RS0g+hOA/y2HkCoRQ9hUm6M9Xn24dPqQdwCCTdA1esSIpb iHQLJAJdSdg0LidU9LPqdxdV6VsTfN8xx4sfyWt9d33YAkMsl8Art3JQs5B7c3sXiFGI +kmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="lMlOp/JE"; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t6-20020aa7d706000000b00501d531e632si6975032edq.449.2023.03.22.13.45.14; Wed, 22 Mar 2023 13:45:37 -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=@intel.com header.s=Intel header.b="lMlOp/JE"; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230489AbjCVUe5 (ORCPT + 99 others); Wed, 22 Mar 2023 16:34:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231781AbjCVUeg (ORCPT ); Wed, 22 Mar 2023 16:34:36 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13B3985B2E for ; Wed, 22 Mar 2023 13:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679516762; x=1711052762; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Hu1OeAgYiuid6HW80nky3z1Z//8vmWo2s1l38YSR9qQ=; b=lMlOp/JESrrvQf86ompygVvd4NKOAhH/LuexIZ0b+kxoM0ciCsy3fF94 YtcX2FoOtbXSvbcxJ8vikXb0rLWbM74BsaFx5h0JNqWwwDD0yorWgLFe4 /eNirtVlJ4tBNxh60KfvMliX1/LI+GqcY6REg3nDMz8xlUUbGqTAVH9hR r6EuVgHMDXhkLFHbrHurA5iBaFeiP1dMOs6NR0Fw4/T+X8K4k9Bjozwqc W4+S25RLQNOk2avajDmM+ALg79oNOZ3Bgjzwn/2ibbYQdNyA7/hrTRdKg PtmTCnf0we4hy36+mcjv2uJ1BVKIfAgtLqxuo7R4y9Uw0diFPp8D5SJ1y w==; X-IronPort-AV: E=McAfee;i="6600,9927,10657"; a="339360504" X-IronPort-AV: E=Sophos;i="5.98,282,1673942400"; d="scan'208";a="339360504" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Mar 2023 13:25:09 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10657"; a="792714125" X-IronPort-AV: E=Sophos;i="5.98,282,1673942400"; d="scan'208";a="792714125" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by fmsmga002.fm.intel.com with ESMTP; 22 Mar 2023 13:25:09 -0700 From: kan.liang@linux.intel.com To: peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: alexander.shishkin@linux.intel.com, ak@linux.intel.com, adrian.hunter@intel.com, nadav.amit@gmail.com, Kan Liang , Zhengjun Xing Subject: [PATCH V2] perf/core: Fix the same task check in perf_event_set_output Date: Wed, 22 Mar 2023 13:24:49 -0700 Message-Id: <20230322202449.512091-1-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.4 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, URIBL_BLOCKED autolearn=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761067442198522396?= X-GMAIL-MSGID: =?utf-8?q?1761102200948019459?= From: Kan Liang The same task check in perf_event_set_output has some potential issues for some usages. For the current perf code, there is a problem if using of perf_event_open() to have multiple samples getting into the same mmap’d memory when they are both attached to the same process. https://lore.kernel.org/all/92645262-D319-4068-9C44-2409EF44888E@gmail.com/ Because the event->ctx is not ready when the perf_event_set_output() is invoked in the perf_event_open(). Besides the above issue, before the commit bd2756811766 ("perf: Rewrite core context handling"), perf record can errors out when sampling with a hardware event and a software event as below. $ perf record -e cycles,dummy --per-thread ls failed to mmap with 22 (Invalid argument) That's because that prior to the commit a hardware event and a software event are from different task context. The problem should be a long time issue since commit c3f00c70276d ("perk: Separate find_get_context() from event initialization"). The task struct is stored in the event->hw.target for each per-thread event. It is a more reliable way to determine whether two events are attached to the same task. The event->hw.target was also introduced several years ago by the commit 50f16a8bf9d7 ("perf: Remove type specific target pointers"). It can not only be used to fix the issue with the current code, but also back port to fix the issues with an older kernel. Note: The event->hw.target was introduced later than commit c3f00c70276d. The patch may cannot be applied between the commit c3f00c70276d and commit 50f16a8bf9d7. Anybody that wants to back-port this at that period may have to find other solutions. Fixes: c3f00c70276d ("perf: Separate find_get_context() from event initialization") Reviewed-by: Zhengjun Xing Signed-off-by: Kan Liang --- Changes since V1: - Update the description to clarify the history, especially the changes because of the rewrite. - Update the Fixes to reflect the correct commit which introduced the issue. kernel/events/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/events/core.c b/kernel/events/core.c index 0c49183656fd..07e46cb098d1 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12178,7 +12178,7 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event) /* * If its not a per-cpu rb, it must be the same task. */ - if (output_event->cpu == -1 && output_event->ctx != event->ctx) + if (output_event->cpu == -1 && output_event->hw.target != event->hw.target) goto out; /*