From patchwork Tue Mar 28 23:52:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Beau Belgrave X-Patchwork-Id: 13191670 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 7E0AEC761A6 for ; Tue, 28 Mar 2023 23:52:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 693226B007E; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641696B0080; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB3E6B0081; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2A2496B007E for ; Tue, 28 Mar 2023 19:52:29 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id F41651C5E6F for ; Tue, 28 Mar 2023 23:52:28 +0000 (UTC) X-FDA: 80619958776.17.258C0E8 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by imf01.hostedemail.com (Postfix) with ESMTP id 38C6640003 for ; Tue, 28 Mar 2023 23:52:27 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=goI6L3YH; spf=pass (imf01.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=1680047547; 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=spol5Qx+Z5Xyl0Y29xgZsGzJ2+fB1/FLm5KBcBePFeo0Fo9A9Ntdn/+TPGqvh6wJfXwA57 QARHmi6z6Z4R2LCN1F6PqFv50ZRk/2OQJ/6ixy6lPl9lH5gn7Lr55APZ83gELBIxjDILxO wlpNg4TlbPrqyckQ7r4oFulyvNtT9po= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.microsoft.com header.s=default header.b=goI6L3YH; spf=pass (imf01.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=1680047547; a=rsa-sha256; cv=none; b=fTsLoGPPmI7MF6vFS8hJLEhm3Qh8pBvXlbzP8lrYRVUg4xIyOP2ZV4omIThqRvh3+DPR9J +pHL0XAxNrNeGfjBPH5+3qwd5M4hOos5xr+wOoNt4HrpMZJAvhrHylrfiLb3U48mkCYuSs SULYOEtz2Vlx7MJgB4QllGVl1aX12UQ= Received: from W11-BEAU-MD.localdomain (unknown [76.135.27.212]) by linux.microsoft.com (Postfix) with ESMTPSA id F070820FDAB4; Tue, 28 Mar 2023 16:52:25 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com F070820FDAB4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1680047546; bh=LAT6es1zlcxHgyXQ+ccAd1DGKcAvO/9IswEU0C+E8Yk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=goI6L3YHE0QXrAG4uRyN8KhwBCKsJbfeylHtLhxjZnzS5bQH+mMKgRk33MDMC5gMr obbzbehtVxM9ZGkBBqlJMRjbHERiakZS8yAS/bcCB+EBzR6/acZlKl4B1Klu78Zhpa HrKj/wcAH2+BBxvCLe2M5RVKsjYX36edJGxdUfDk= 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: [PATCH v10 04/12] tracing/user_events: Fixup enable faults asyncly Date: Tue, 28 Mar 2023 16:52:11 -0700 Message-Id: <20230328235219.203-5-beaub@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230328235219.203-1-beaub@linux.microsoft.com> References: <20230328235219.203-1-beaub@linux.microsoft.com> MIME-Version: 1.0 X-Stat-Signature: 4zb6urg6doccgskrfujrmqwz1biu7ed5 X-Rspam-User: X-Rspamd-Queue-Id: 38C6640003 X-Rspamd-Server: rspam06 X-HE-Tag: 1680047547-988005 X-HE-Meta: U2FsdGVkX1/rxtfKDl7eRNp/klajmiapiKupxhQ0jc/8CXQb2OC4m2HbZSzZZUOMhfxiG5ZbBB7W8DnwcVIPkKSWsj+aINFX/rr0pUZOenwdWrBVaRM1iHp7lEQ1mWaSb85eU8/3Z8qc9dATQrvNRya3MQ9aOSaUCItFwA6JhvAMxJaq7/9C7p/770xGkIC3RlK80/TL57zryhOe/UEBAxo/P74RizjlTIXUjIQ++lQeEChFaX1PmxdEku4GPBuNt05rQAlwgm0h9UENz+C7wRlIdaRYC0Q2RxnTjIh6SaxhrvpdJTumJJp2ce//s+efwU/UCN7rJOILr8Sn5CH+L45bnt3U+adECKfPGwZAWvLddM600ERrxFpafIcQrHMLX/Ra6e7dsZHg5ajsCjOQOFp2mGO2sSDfqcp+m/DCpsHMN15Xr0dsi0aayryDVPBVAg42awU4mCAuqCN6DOW812r8Vpyc4peUQetGelpMKp8ZgSCM92O+HU+7jLWIlLpLbDG6REMeDPQJ/FUWWz3j0zJ3FmQ98HWqGigyb5LJNMFawMVE8VZ2c4RJ2g89MYB6vmIMnIW4zpfgFq8doTEKQ2hyV2Cq/SBScq8xUTxQ8IClm7xMq79GrsQ04xENFjcNSf6Eodds4PPWHq1Lw/5+Ixaz3DoPQgTGInEl69wrvZTU17o/QZLlogVnq2oDjCZrr+GEcyDThJ4dZlK+Y+9vCS0UrpJp/uQHMRH9bEg44QPUcr9B4567yN9n/iZj8KVI1ZAtdNfUujQGuECjb3ys3LkmW8FWfYGeKq9SW8EgQIGjYFyp6t0hO50rbOvG5xxDS4bj2xgTqTfE/7J6/hUl3WCDFF9i3sC16TWHXh8iG1NWf0KqOjrezpY0zI6RB1PL6MwtJYrlK6mmQK+rM4ZpQMCaYbFQtW7xecQtej6bScYKjyTVzg5GE3hGBk4ymYvpfakplM9PbV7ZPt3e7kh rQj3MZm4 VnpsGE/Fd+UJMwPClBM9p3LDMBxopT7g1RczOBwGrUl0f3ULsxxx83FbEqo/w81qne3K3IQc2UX4kbQi8rLhozTIHfJsqlkfQZnrVDYzVMHzgCQPqZ8yZ9mftjdJbr94Ge5iVU/IBlyi75Zu8cVpvLbzyMED3uHycey5VhaXsB9yGA0iBwMpTPLBnKaTtELyssMJsnUxYOiiAGw1+B5Hp2NucGQIOhOZCSydKNJoMScXLJlXs+RdLhNEIL7l06Isq5hif2zAklY6gkaYm/g1bTcV2fcupa//6N3V45SDmpK9AF1k= 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; }