From patchwork Tue Apr 11 21:17:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 82198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2869342vqo; Tue, 11 Apr 2023 14:35:37 -0700 (PDT) X-Google-Smtp-Source: AKy350aDSOFZMBe9TbDm5U0A2XJ8Bbe4o1tetKmsV/FoPOotRzgO4KlM7IcRZNNgCZLXlIvwCI+g X-Received: by 2002:a05:6a20:2a08:b0:de:af2f:fddb with SMTP id e8-20020a056a202a0800b000deaf2ffddbmr540739pzh.23.1681248937316; Tue, 11 Apr 2023 14:35:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681248937; cv=none; d=google.com; s=arc-20160816; b=NWXKmzfVWYrUIPRda/CFwIhMrfBl5UsAjaVAICYggemngfUYYl5XBf4q/exShrDO7A hTzitCPJhEpC+L5T38gflKkg2Lk2ZNfsIUUYcLUHuDK+9McVvNEP+zo+To0uCngoeIxK +kGT4OUsbIgJeXZ2WFsR8lM4mfnU9/Z2SAvgKdoRJVE2crxtDVTGAB/AxKdHSOpBIWCU i9KmqBP+g34uw2fcuNEnKMV1fACb4B4xc2z793T+OyVLitZ75EDETydNePENy9cdp8zq WSQkwVJbHYjmFQNoKnVa2i4bk6H7Ht8pbSDcOykkbqLkYmIsYkfbFoMRX+VYNTgU9UiM vUbA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=hP1cRNuxHkIdEXUzblUPdQ1BwU/USkWFGaUyWmDt9gQ=; b=gdhyCGmVgizxZbCttQuH5V2tlGx46ccdzoxwuRVq5KxUaSFuhmJNhXTwCtRi6zueYE qJTaQVCUStbA9W3kUqdlzwSH1SNtYu91REKeXMc5LfdS+Rj9rVIrnftMfZCr474g4tBA rbjjWWzSZmH10b1Wx0ykqPzciFHoMBR5/NqSa56A3+PYKyMSQbf9BzedS9z/Wi7Im1iO QNu6MN8ID69BahNC9Gp+T5npzBkVEOLhd6WgqKDqNtzmcsiRjwZrVdSUZ3K4U4MnKKNH PqOrTGYGIW/B0kIg0Knr6F6rDbkXHg7IT188bN8WJS5xAobKg3yHfCYDB0JR4iHf2txZ rxZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=PQSaP+ez; 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=linux.microsoft.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r21-20020a632b15000000b0051460f12600si12774336pgr.554.2023.04.11.14.35.25; Tue, 11 Apr 2023 14:35: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=@linux.microsoft.com header.s=default header.b=PQSaP+ez; 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=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229800AbjDKVRW (ORCPT + 99 others); Tue, 11 Apr 2023 17:17:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229710AbjDKVRQ (ORCPT ); Tue, 11 Apr 2023 17:17:16 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F2C704482; Tue, 11 Apr 2023 14:17:15 -0700 (PDT) Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 5513C21779A6; Tue, 11 Apr 2023 14:17:15 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 5513C21779A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1681247835; bh=hP1cRNuxHkIdEXUzblUPdQ1BwU/USkWFGaUyWmDt9gQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PQSaP+ezQbaPttIFdbKkZXDsVkwbr1h/eoZ3XuTodlKQSfr8PnuS6r5SHvRKlAuuY ORC1Lajk4ftnf+hT6PSiuJf1X3Hbfmt4gZ6OENNiZMJu6KapcPqBGUr2lwkAGKDfEJ lgpeHtIXivwCHumdkirPPoB8MY8N3hlWLUvAYgqs= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH 2/3] tracing/user_events: Ensure bit is cleared on unregister Date: Tue, 11 Apr 2023 14:17:08 -0700 Message-Id: <20230411211709.15018-3-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230411211709.15018-1-beaub@linux.microsoft.com> References: <20230411211709.15018-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Spam-Status: No, score=-19.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762917285538381495?= X-GMAIL-MSGID: =?utf-8?q?1762917285538381495?= If an event is enabled and a user process unregisters user_events, the bit is left set. Fix this by always clearing the bit in the user process if unregister is successful. Update abi self-test to ensure this occurs properly. Suggested-by: Doug Cook Signed-off-by: Beau Belgrave --- kernel/trace/trace_events_user.c | 34 +++++++++++++++++++ .../testing/selftests/user_events/abi_test.c | 9 +++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index e7dff24aa724..eb195d697177 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -2146,6 +2146,35 @@ static long user_unreg_get(struct user_unreg __user *ureg, return ret; } +static int user_event_mm_clear_bit(struct user_event_mm *user_mm, + unsigned long uaddr, unsigned char bit) +{ + struct user_event_enabler enabler; + int result; + + memset(&enabler, 0, sizeof(enabler)); + enabler.addr = uaddr; + enabler.values = bit; +retry: + /* Prevents state changes from racing with new enablers */ + mutex_lock(&event_mutex); + + /* Force the bit to be cleared, since no event is attached */ + mmap_read_lock(user_mm->mm); + result = user_event_enabler_write(user_mm, &enabler, false); + mmap_read_unlock(user_mm->mm); + + mutex_unlock(&event_mutex); + + if (result) { + /* Attempt to fault-in and retry if it worked */ + if (!user_event_mm_fault_in(user_mm, uaddr)) + goto retry; + } + + return result; +} + /* * Unregisters an enablement address/bit within a task/user mm. */ @@ -2190,6 +2219,11 @@ static long user_events_ioctl_unreg(unsigned long uarg) mutex_unlock(&event_mutex); + /* Ensure bit is now cleared for user, regardless of event status */ + if (!ret) + ret = user_event_mm_clear_bit(mm, reg.disable_addr, + reg.disable_bit); + return ret; } diff --git a/tools/testing/selftests/user_events/abi_test.c b/tools/testing/selftests/user_events/abi_test.c index e0323d3777a7..5125c42efe65 100644 --- a/tools/testing/selftests/user_events/abi_test.c +++ b/tools/testing/selftests/user_events/abi_test.c @@ -109,13 +109,16 @@ TEST_F(user, enablement) { ASSERT_EQ(0, change_event(false)); ASSERT_EQ(0, self->check); - /* Should not change after disable */ + /* Ensure kernel clears bit after disable */ ASSERT_EQ(0, change_event(true)); ASSERT_EQ(1, self->check); ASSERT_EQ(0, reg_disable(&self->check, 0)); + ASSERT_EQ(0, self->check); + + /* Ensure doesn't change after unreg */ + ASSERT_EQ(0, change_event(true)); + ASSERT_EQ(0, self->check); ASSERT_EQ(0, change_event(false)); - ASSERT_EQ(1, self->check); - self->check = 0; } TEST_F(user, bit_sizes) {