From patchwork Tue Feb 21 21:11:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13148403 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5157C678D5 for ; Tue, 21 Feb 2023 21:12:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C887B6B007E; Tue, 21 Feb 2023 16:11:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B65F96B0081; Tue, 21 Feb 2023 16:11:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 680A06B007E; Tue, 21 Feb 2023 16:11:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4C0C96B007D for ; Tue, 21 Feb 2023 16:11:54 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 111BF160A48 for ; Tue, 21 Feb 2023 21:11:54 +0000 (UTC) X-FDA: 80492546148.08.CCE8466 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf26.hostedemail.com (Postfix) with ESMTP id 173C514001A for ; Tue, 21 Feb 2023 21:11:51 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=jv3UDZtG; spf=pass (imf26.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1677013912; a=rsa-sha256; cv=none; b=OFmF/nGGucbZQZoGYAZYdMRofb/tOSl4MLhZLIoCaEl07baNC3Zt8j6fE8nPpWY4/wWK28 tdcz9UfXhPPD2gVVZEzYfggOnOH0udHkGoYk/gwLW+v0qoQvxsz6EEcayfJEbl7/57CesQ PRrjDG00CDeErEyaSUBpUk570r2EdqA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=jv3UDZtG; spf=pass (imf26.hostedemail.com: domain of beaub@linux.microsoft.com designates 13.77.154.182 as permitted sender) smtp.mailfrom=beaub@linux.microsoft.com; dmarc=pass (policy=none) header.from=linux.microsoft.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1677013912; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hPint7oilCioEigkIIslQUbcyG4C0cJ57YmYs/t8wm4=; b=i9TJ4o+sBxNm8soIGJmaXXNnaWdvjmZ0MkIxzvuevf4gN5FsP56EavBHsqgNcN2XdLe0jV l6QItuph989fcoO+3RcLOg+8y6g/JpivsRhZEU+f1h3VPrkLujJlBZAXxDsmLd9nzjDfzL OEePAzUSUvknbcZxGNviqc6OlZNLyBQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 4A74C20B9C3E; Tue, 21 Feb 2023 13:11:50 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 4A74C20B9C3E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1677013910; bh=hPint7oilCioEigkIIslQUbcyG4C0cJ57YmYs/t8wm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jv3UDZtGn4MCJzppxz97f4vtpUvEDWVn9grdnd5DHfIYBdQYH0tWC57TDrXDygH9r /ejC3JrJSkat9FDpQJUtBt8htUhJrxgVNZbHqwuNxLwyLoOWndWR8GVj0jnfBZx/pS FrZdvmgb+hmbVHo4FmBegwQVXLNo5/hH4qGK9irg= From: Beau Belgrave To: rostedt@goodmis.org, mhiramat@kernel.org, mathieu.desnoyers@efficios.com, dcook@linux.microsoft.com, alanau@linux.microsoft.com, brauner@kernel.org, akpm@linux-foundation.org, ebiederm@xmission.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-trace-devel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v8 05/11] tracing/user_events: Add ioctl for disabling addresses Date: Tue, 21 Feb 2023 13:11:37 -0800 Message-Id: <20230221211143.574-6-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230221211143.574-1-beaub@linux.microsoft.com> References: <20230221211143.574-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 173C514001A X-Rspamd-Server: rspam01 X-Stat-Signature: cti7kk5chz3hxb5yno7e3hchenmbi3no X-HE-Tag: 1677013911-842317 X-HE-Meta: U2FsdGVkX19cpCmPhjjNY1NMZGRx4iyN4A/1aUTGcdddL3MdeU9B6PNqjh//DsQa91/A2PmSJDlGBuUd5NhIgg3WhPgBiYf4y1R0NW0b44DcC+A96bZDFGkFuXk7ODFO5XMeq9RTgsPki7i2S8WXXZPsbJQBELceAunWTpzfS4ZLT2N576EgqhZiY9lEA8CAHB3CHHDiXAGCfiZrsKfUwhmSJvzxQPaoZ1j4G/EtDhpA31dGx6e95ntlJj3cOyQuYxJ5SdeE15tDiqlKWN9YlGilMuoUgdnp3xeiGBTNKpA0fPp+kYOnoJXW4CcGhjUjUWIeXUJW/asfz7+ysg9PRR6aEy7uh1Yi1KcoCLYPdi4ctZ09Oc+CbP/+zxYv+OhsC9SWeBcUCTp08oCtlslb/Z5zr70WivotrP4sNNkcKD7E9BRaImCeMHiYem5DkWScE67sQXhbKQd+qZ5GcwskNX3yEfNAd+9ASGI6NVfH853ctKjXp0aM0MMvWpi2PX1csRa7LjK6tFWekuidoNVUB96ahbHNEfSK2149TJxST7CEVi8yHxmOsQSGFxKJ67TuE6POtmmB8aJ7pmJYYCepll2AvFC7BXgPhhkzGr9igbYH4kjY5J4yWF6eHuruj8jxb54S4AMjOfakW/KsiDk9AGbuIMwJgfdfQIRKhvYY69OUasToMScv04PQWFEk8BMK0o1XhB1bIO83phulKOq+XeTsyQjgDNH/I6DTmMSyFhVLNt5fjgxfO8D65rXOzkqg2o0txrWkzL4adwOmErNRFsdz6WkdHli+0IcwZCD/cSHwOF7bkqMJGwKsrp86r7U5Rvr0SvoZQCFy8pgc04yzHdq34yZRRAfzBOKh3GFawe/Q5BHqL9xpwmJ6wqR5A2pLhECKGgSFpw4k55FXO9UC7yAyN6CNzCAIyNCTYoen/sTw8CSI82JhhUYxbb7wQF0Ld1LmJu7cQiwlQxdLD+2 hwNx+9A7 StObFph5PwuZMCd3oT3Cwx5LDWLUBzkkQJhQBLBLEUdDGFxa5LVu34c2cabrDPW2VL8KmLNy8azg0vkq1GQgAj9I6YmcX3D2/SIIiNXcxeHjr7lsIwD1npB7DStbPWwK53bzxdrrr1J326i+JP1IuxnD2160dojg/7y6uJ6M08hvKTGmsvERDm6iWpGM6EHq4KjzCLp5CqyiP7e0inU+Cmq21TzoVMPmlEGPYaCaJUNDgc/nO/DPrfu6AdafgGrNHQ1oKKA4MXOnQP+xwpnmO/Kq7/szkWcQ4HtmqEsxNe6ze6FU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Enablements are now tracked by the lifetime of the task/mm. User processes need to be able to disable their addresses if tracing is requested to be turned off. Before unmapping the page would suffice. However, we now need a stronger contract. Add an ioctl to enable this. A new flag bit is added, freeing, to user_event_enabler to ensure that if the event is attempted to be removed while a fault is being handled that the remove is delayed until after the fault is reattempted. Signed-off-by: Beau Belgrave --- include/uapi/linux/user_events.h | 24 +++++++++ kernel/trace/trace_events_user.c | 93 +++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/user_events.h b/include/uapi/linux/user_events.h index 22521bc622db..3e7275e3234a 100644 --- a/include/uapi/linux/user_events.h +++ b/include/uapi/linux/user_events.h @@ -46,6 +46,27 @@ struct user_reg { __u32 write_index; } __attribute__((__packed__)); +/* + * Describes an event unregister, callers must set the size, address and bit. + * This structure is passed to the DIAG_IOCSUNREG ioctl to disable bit updates. + */ +struct user_unreg { + /* Input: Size of the user_unreg structure being used */ + __u32 size; + + /* Input: Bit to unregister */ + __u8 disable_bit; + + /* Input: Reserved, set to 0 */ + __u8 __reserved; + + /* Input: Reserved, set to 0 */ + __u16 __reserved2; + + /* Input: Address to unregister */ + __u64 disable_addr; +} __attribute__((__packed__)); + #define DIAG_IOC_MAGIC '*' /* Request to register a user_event */ @@ -54,4 +75,7 @@ struct user_reg { /* Request to delete a user_event */ #define DIAG_IOCSDEL _IOW(DIAG_IOC_MAGIC, 1, char *) +/* Requests to unregister a user_event */ +#define DIAG_IOCSUNREG _IOW(DIAG_IOC_MAGIC, 2, struct user_unreg*) + #endif /* _UAPI_LINUX_USER_EVENTS_H */ diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 86bda1660536..e4ee25d16f3b 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -102,6 +102,9 @@ struct user_event_enabler { /* Bit 6 is for faulting status of enablement */ #define ENABLE_VAL_FAULTING_BIT 6 +/* Bit 7 is for freeing status of enablement */ +#define ENABLE_VAL_FREEING_BIT 7 + /* Only duplicate the bit value */ #define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK @@ -301,6 +304,12 @@ static void user_event_enabler_fault_fixup(struct work_struct *work) /* Prevent state changes from racing */ mutex_lock(&event_mutex); + /* User asked for enabler to be removed during fault */ + if (test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler))) { + user_event_enabler_destroy(enabler); + goto out; + } + /* * If we managed to get the page, re-issue the write. We do not * want to get into a possible infinite loop, which is why we only @@ -315,7 +324,7 @@ static void user_event_enabler_fault_fixup(struct work_struct *work) user_event_enabler_write(mm, enabler, true); mmap_read_unlock(mm->mm); } - +out: mutex_unlock(&event_mutex); /* In all cases we no longer need the mm or fault */ @@ -370,7 +379,8 @@ static int user_event_enabler_write(struct user_event_mm *mm, if (refcount_read(&mm->tasks) == 0) return -ENOENT; - if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)))) + if (unlikely(test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)) || + test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)))) return -EBUSY; ret = pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, @@ -428,6 +438,10 @@ static bool user_event_enabler_dup(struct user_event_enabler *orig, { struct user_event_enabler *enabler; + /* Skip pending frees */ + if (unlikely(test_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(orig)))) + return true; + enabler = kzalloc(sizeof(*enabler), GFP_NOWAIT); if (!enabler) @@ -2086,6 +2100,75 @@ static long user_events_ioctl_del(struct user_event_file_info *info, return ret; } +static long user_unreg_get(struct user_unreg __user *ureg, + struct user_unreg *kreg) +{ + u32 size; + long ret; + + ret = get_user(size, &ureg->size); + + if (ret) + return ret; + + if (size > PAGE_SIZE) + return -E2BIG; + + if (size < offsetofend(struct user_unreg, disable_addr)) + return -EINVAL; + + ret = copy_struct_from_user(kreg, sizeof(*kreg), ureg, size); + + return ret; +} + +/* + * Unregisters an enablement address/bit within a task/user mm. + */ +static long user_events_ioctl_unreg(unsigned long uarg) +{ + struct user_unreg __user *ureg = (struct user_unreg __user *)uarg; + struct user_event_mm *mm = current->user_event_mm; + struct user_event_enabler *enabler, *next; + struct user_unreg reg; + long ret; + + ret = user_unreg_get(ureg, ®); + + if (ret) + return ret; + + if (!mm) + return -ENOENT; + + ret = -ENOENT; + + /* + * Flags freeing and faulting are used to indicate if the enabler is in + * use at all. When faulting is set a page-fault is occurring asyncly. + * During async fault if freeing is set, the enabler will be destroyed. + * If no async fault is happening, we can destroy it now since we hold + * the event_mutex during these checks. + */ + mutex_lock(&event_mutex); + + list_for_each_entry_safe(enabler, next, &mm->enablers, link) + if (enabler->addr == reg.disable_addr && + (enabler->values & ENABLE_VAL_BIT_MASK) == reg.disable_bit) { + set_bit(ENABLE_VAL_FREEING_BIT, ENABLE_BITOPS(enabler)); + + if (!test_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler))) + user_event_enabler_destroy(enabler); + + /* Removed at least one */ + ret = 0; + } + + mutex_unlock(&event_mutex); + + return ret; +} + /* * Handles the ioctl from user mode to register or alter operations. */ @@ -2108,6 +2191,12 @@ static long user_events_ioctl(struct file *file, unsigned int cmd, ret = user_events_ioctl_del(info, uarg); mutex_unlock(&group->reg_mutex); break; + + case DIAG_IOCSUNREG: + mutex_lock(&group->reg_mutex); + ret = user_events_ioctl_unreg(uarg); + mutex_unlock(&group->reg_mutex); + break; } return ret;