From patchwork Thu Mar 23 19:06:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13186001 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 404ADC74A5B for ; Thu, 23 Mar 2023 19:06:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D1FF56B007B; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C09176B0075; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 922186B0080; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7FC1E6B007D for ; Thu, 23 Mar 2023 15:06:19 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 56297A07CC for ; Thu, 23 Mar 2023 19:06:19 +0000 (UTC) X-FDA: 80601093678.26.31E11FC Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf08.hostedemail.com (Postfix) with ESMTP id 61B4F160034 for ; Thu, 23 Mar 2023 19:06:17 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=QZIgN7CH; spf=pass (imf08.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=1679598377; 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=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; b=L7mHVFQfl5m9HwXYvdy4o8+UMDB9XT/ZPQxAmmZ1IPVVWwyxTyjNXVLF98hSl6y3WT3mkI /kKUdcG+wz1CiRihn0K0J5zK+kfVEIbNpxH5k2A1AAQlPRCH+bAGoOOOa9SNPq+OHc+Cdm 3xvwA5HIBWrG9LRCR6ZynanO1Sy69G4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=QZIgN7CH; spf=pass (imf08.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=1679598377; a=rsa-sha256; cv=none; b=1XB9E1Ipv0Qibe5ihlxtyY9iSoc1AmidnY/yPYA1w7TiZmVfN+z+TRgjiAaTCFhJCkCvT+ 4u8KhC9IARkO7gxbC2CDhKheX13GQE72ks7T9aheKY8odc+0LO3SDaygHG0pNEmB116V7d 8/R4MEc56/Kfc2WXb3PUw+optEf8fDI= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id 05F3920FC0FA; Thu, 23 Mar 2023 12:06:16 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 05F3920FC0FA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1679598376; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QZIgN7CH8swt7ZObJnHA7Bj18umfbk0i097vthWVnXmjTr+X1av6eksfXuo1P0P93 6yo+alXHSCzqiTPziV9kWyyrxp6PFXlGgbnFpjhDiEyX8YOf4Mb2B5z9g9H2l/6tXg u8atbDgg+XG/qTdLa5JLigkXEeHoKIGW4ksKz3CQ= 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-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND PATCH v8 04/11] tracing/user_events: Fixup enable faults asyncly Date: Thu, 23 Mar 2023 12:06:03 -0700 Message-Id: <20230323190610.251-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230323190610.251-1-beaub@linux.microsoft.com> References: <20230323190610.251-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 61B4F160034 X-Stat-Signature: 3rptwdm7oo8zfun39iyx5q67ewuzc13t X-HE-Tag: 1679598377-298762 X-HE-Meta: U2FsdGVkX1+bbbs6CZVpiP0muFHh1JYvox+f58Jf6Mdmg/+XgPObhyFrfSje8klLm20uKG3L5Qzq94WkVWiIMRO+a3klLV6ExuYult+qrFFU2fCx4QauilqtdbRckD/iKSbtKNHlgGZLOqZYIb5fOnyaP1oGuAL9eVFYSMq/4E/qSHvFPXe6w6ssHn/jC/NzzsIDpe48+6O+LHvkDJpBgEmh2KUe4LFbnTN2zh3RMv81cQ8yJ5xjX+t1bvbcYDKYTMlFMMkpQMI7kAxgMReTyUWAsWbkHHRQrhB08Mr4w0UhZEavJyU9A4oN4EAvuDO4mpksn8rzuUzJoEB50RN5sDuJ4EME7SP8CTNsHiVY2gQGVow3yyVpd6z00cB4poQE/NjzR7Ff80yA184dwYZhBP7UWb30x+TxR/VO169MYKBW2PkAuFg0zKhZrpeZc27LumYj+ABBZzBmFg8eyIr6A5x9AvTk9QRL+YsUHxAT5zSLRmkj+JZKmp+3MOwoUEAjmvmE4kgWlO19CRT9LysIUPEej2JPhfc/cBht5WHirrn5PamOicedmR8TcnUOOOUSHhKjvzdvq/tQYJuIMTMQFIYFmjWa5CQOegU7KM7WggU9brxr0SgeKIA5mqnyc0NqfniKU+9F+F7+TXLCABT8RoQ1GYJAlT5m1SE++E/F+NpCNaCJIgc4dbHzs3gOO9+LsdeRxnvqDuH0exPRuCJ2ZfLu8R1tmr8fx3MBck69dinzRPh8bjzzOryyILLzCePM34EwEkRDxu37gGC0ItVeaH1aWh4ndOSjppcnXJcRsecmY4Mp+iRNKh7Yo49YprKJS9gEYWgyaBSjRUGTrYGLBuMby1hPk3kSNABVg10yTOZArnW/PPQivPdQ+fbcPu5fLA74b69ajxPKnPkzusbyf9YbWB1XIppBDosElEla6zYjBjRgEkL3jK7AjBJzAFIrOeU+2liA2qH2DDbQWgZ lHM/rGsC DGMtHMDN2+/W80QeWyTQ+rmC38TKo2jqHzM5pEXBHtqjhOJChKPxEgNS3cnzrV9pPUNqfevra01KUi2EZyvvV7wQIWdhfrO1OAPLxeojONlggLVVToHxNSZsNLvvZpM/Ph6XeKIPfegZg4Yf4SAVTEWCf4ZkFB0P4BSr7lpArhbtAQxL08yzhq2aT1BMTNFTkwPwnRsOZfrDfImWEv2F45kv/YHXIco1/ZocX1XWoUphDF3rGztzkt9tQrHXe4n9HXcChhtUUg4C8DmUC2o58k4/yeuceY/UA0tNKOzJ5BfnEYWk= 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: When events are enabled within the various tracing facilities, such as ftrace/perf, the event_mutex is held. As events are enabled pages are accessed. We do not want page faults to occur under this lock. Instead queue the fault to a workqueue to be handled in a process context safe way without the lock. The enable address is marked faulting while the async fault-in occurs. This ensures that we don't attempt to fault-in more than is necessary. Once the page has been faulted in, an address write is re-attempted. If the page couldn't fault-in, then we wait until the next time the event is enabled to prevent any potential infinite loops. Signed-off-by: Beau Belgrave --- kernel/trace/trace_events_user.c | 120 +++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 6 deletions(-) diff --git a/kernel/trace/trace_events_user.c b/kernel/trace/trace_events_user.c index 553a82ee7aeb..86bda1660536 100644 --- a/kernel/trace/trace_events_user.c +++ b/kernel/trace/trace_events_user.c @@ -99,9 +99,23 @@ struct user_event_enabler { /* Bits 0-5 are for the bit to update upon enable/disable (0-63 allowed) */ #define ENABLE_VAL_BIT_MASK 0x3F +/* Bit 6 is for faulting status of enablement */ +#define ENABLE_VAL_FAULTING_BIT 6 + /* Only duplicate the bit value */ #define ENABLE_VAL_DUP_MASK ENABLE_VAL_BIT_MASK +#define ENABLE_BITOPS(e) ((unsigned long *)&(e)->values) + +/* Used for asynchronous faulting in of pages */ +struct user_event_enabler_fault { + struct work_struct work; + struct user_event_mm *mm; + struct user_event_enabler *enabler; +}; + +static struct kmem_cache *fault_cache; + /* Global list of memory descriptors using user_events */ static LIST_HEAD(user_event_mms); static DEFINE_SPINLOCK(user_event_mms_lock); @@ -263,7 +277,85 @@ static int user_event_mm_fault_in(struct user_event_mm *mm, unsigned long uaddr) } static int user_event_enabler_write(struct user_event_mm *mm, - struct user_event_enabler *enabler) + struct user_event_enabler *enabler, + bool fixup_fault); + +static void user_event_enabler_fault_fixup(struct work_struct *work) +{ + struct user_event_enabler_fault *fault = container_of( + work, struct user_event_enabler_fault, work); + struct user_event_enabler *enabler = fault->enabler; + struct user_event_mm *mm = fault->mm; + unsigned long uaddr = enabler->addr; + int ret; + + ret = user_event_mm_fault_in(mm, uaddr); + + if (ret && ret != -ENOENT) { + struct user_event *user = enabler->event; + + pr_warn("user_events: Fault for mm: 0x%pK @ 0x%llx event: %s\n", + mm->mm, (unsigned long long)uaddr, EVENT_NAME(user)); + } + + /* Prevent state changes from racing */ + mutex_lock(&event_mutex); + + /* + * 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 + * attempt again directly if the page came in. If we couldn't get + * the page here, then we will try again the next time the event is + * enabled/disabled. + */ + clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + if (!ret) { + mmap_read_lock(mm->mm); + user_event_enabler_write(mm, enabler, true); + mmap_read_unlock(mm->mm); + } + + mutex_unlock(&event_mutex); + + /* In all cases we no longer need the mm or fault */ + user_event_mm_put(mm); + kmem_cache_free(fault_cache, fault); +} + +static bool user_event_enabler_queue_fault(struct user_event_mm *mm, + struct user_event_enabler *enabler) +{ + struct user_event_enabler_fault *fault; + + fault = kmem_cache_zalloc(fault_cache, GFP_NOWAIT | __GFP_NOWARN); + + if (!fault) + return false; + + INIT_WORK(&fault->work, user_event_enabler_fault_fixup); + fault->mm = user_event_mm_get(mm); + fault->enabler = enabler; + + /* Don't try to queue in again while we have a pending fault */ + set_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + if (!schedule_work(&fault->work)) { + /* Allow another attempt later */ + clear_bit(ENABLE_VAL_FAULTING_BIT, ENABLE_BITOPS(enabler)); + + user_event_mm_put(mm); + kmem_cache_free(fault_cache, fault); + + return false; + } + + return true; +} + +static int user_event_enabler_write(struct user_event_mm *mm, + struct user_event_enabler *enabler, + bool fixup_fault) { unsigned long uaddr = enabler->addr; unsigned long *ptr; @@ -278,11 +370,19 @@ 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)))) + return -EBUSY; + ret = pin_user_pages_remote(mm->mm, uaddr, 1, FOLL_WRITE | FOLL_NOFAULT, &page, NULL, NULL); - if (ret <= 0) { - pr_warn("user_events: Enable write failed\n"); + if (unlikely(ret <= 0)) { + if (!fixup_fault) + return -EFAULT; + + if (!user_event_enabler_queue_fault(mm, enabler)) + pr_warn("user_events: Unable to queue fault handler\n"); + return -EFAULT; } @@ -314,7 +414,7 @@ static void user_event_enabler_update(struct user_event *user) list_for_each_entry_rcu(enabler, &mm->enablers, link) if (enabler->event == user) - user_event_enabler_write(mm, enabler); + user_event_enabler_write(mm, enabler, true); rcu_read_unlock(); mmap_read_unlock(mm->mm); @@ -562,7 +662,7 @@ static struct user_event_enabler /* Attempt to reflect the current state within the process */ mmap_read_lock(user_mm->mm); - *write_result = user_event_enabler_write(user_mm, enabler); + *write_result = user_event_enabler_write(user_mm, enabler, false); mmap_read_unlock(user_mm->mm); /* @@ -2201,16 +2301,24 @@ static int __init trace_events_user_init(void) { int ret; + fault_cache = KMEM_CACHE(user_event_enabler_fault, 0); + + if (!fault_cache) + return -ENOMEM; + init_group = user_event_group_create(&init_user_ns); - if (!init_group) + if (!init_group) { + kmem_cache_destroy(fault_cache); return -ENOMEM; + } ret = create_user_tracefs(); if (ret) { pr_warn("user_events could not register with tracefs\n"); user_event_group_destroy(init_group); + kmem_cache_destroy(fault_cache); init_group = NULL; return ret; }