Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2490367pxj; Mon, 10 May 2021 04:21:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy7hV5tW191Sm3Ryg+1h07pLeSp+qfY49UpvnQCbn5pIDAhSyyK+aSE2U28GrfNPyN/f6+x X-Received: by 2002:a17:906:1d0a:: with SMTP id n10mr8132973ejh.341.1620645712867; Mon, 10 May 2021 04:21:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620645712; cv=none; d=google.com; s=arc-20160816; b=f5X71FIfm7hF5WeRQ/4Z3StwbQTjllLLBCrCzjxMyCVUe3m5zh5dXimyhLbdZ8QRVF fxfIAUt2O49KLE8Qhn3jdpeatw7808aY+NqxF/CSwmchcsHxmkwqJkY4t8G742Y+malE pt0hxTalrvAySHKmSD/JGF8JZJAXCBEEeaVJBk8pDEScn8KtSPiQNoK4Dp7zHNbnGIHt CJoN9fhhr6F+MJZw2fDIYzTlCSUiE0cSCCJB7mhuF40J9hECa8b0IU1et2GpnAY04mtr yd5lx+ae5f6hau/XukmAiKHHU8hnqXplEy4tEbMv11k44s2AGzEL/YogqyoFakoIW/3p K9bA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=IwjKSd28VIZCjW/qDSiKmI9nmXHpAcoHHJbCD+gkOAM=; b=qpTDf3Ur2kxn7GFyvocndCRQAg9cJr0JTx3bAde/wLT3rdFGep4N3oK406NilV6OFI LfMbzn0sEb08VII6mPb0xnHVOXD19e5SsU+kmV/CorHgVPlYeBA72PUXrXrWIGeI5H97 /H0cV2rVSqjowh61eOqdSgXuQnT3dYZBuv2i9cOu70WIt/q5oW1DdNWLA7lK49moDeAs FIvn34+ioFfoIOjXaZgU6tt5ijHk1sAaYQnZt+JtjHMviKwXubBhf9dDOiuSgOwEmscQ Dv0JdfN0uIqSZrp3lMM43GQCxFPYtQ6AUzZTEmbl86C2kXSKoPQIHs/H7I5bY6UPx3Sp JHPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jGtTNzqG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z26si6897458ejr.406.2021.05.10.04.21.29; Mon, 10 May 2021 04:21:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=jGtTNzqG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233682AbhEJLS6 (ORCPT + 99 others); Mon, 10 May 2021 07:18:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:53006 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233968AbhEJKzl (ORCPT ); Mon, 10 May 2021 06:55:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id D95B761C29; Mon, 10 May 2021 10:42:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643379; bh=5cNnRC+bFfEcdlFXVaWzUifaY90iZD94uQ3ED7Ndf48=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jGtTNzqG6GZnoTKKMMX5j8Rxemcyfv3rhzhTPvGYlZkz6cYX8K6Wmtm1pOK82Z1Zo zE/TzDEctE0IZn6iRCrcNz/I3xo36ZLZjhD9okWLSAbKStUZEUZlaOFZNthgSA95Ds InkVkvBklsGX5k2Xzo9xio1rNPsAsP1ucgCinTJg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Steven Rostedt (VMware)" Subject: [PATCH 5.10 291/299] tracing: Map all PIDs to command lines Date: Mon, 10 May 2021 12:21:28 +0200 Message-Id: <20210510102014.537180011@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Steven Rostedt (VMware) commit 785e3c0a3a870e72dc530856136ab4c8dd207128 upstream. The default max PID is set by PID_MAX_DEFAULT, and the tracing infrastructure uses this number to map PIDs to the comm names of the tasks, such output of the trace can show names from the recorded PIDs in the ring buffer. This mapping is also exported to user space via the "saved_cmdlines" file in the tracefs directory. But currently the mapping expects the PIDs to be less than PID_MAX_DEFAULT, which is the default maximum and not the real maximum. Recently, systemd will increases the maximum value of a PID on the system, and when tasks are traced that have a PID higher than PID_MAX_DEFAULT, its comm is not recorded. This leads to the entire trace to have "<...>" as the comm name, which is pretty useless. Instead, keep the array mapping the size of PID_MAX_DEFAULT, but instead of just mapping the index to the comm, map a mask of the PID (PID_MAX_DEFAULT - 1) to the comm, and find the full PID from the map_cmdline_to_pid array (that already exists). This bug goes back to the beginning of ftrace, but hasn't been an issue until user space started increasing the maximum value of PIDs. Link: https://lkml.kernel.org/r/20210427113207.3c601884@gandalf.local.home Cc: stable@vger.kernel.org Fixes: bc0c38d139ec7 ("ftrace: latency tracer infrastructure") Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/trace.c | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -2387,14 +2387,13 @@ static void tracing_stop_tr(struct trace static int trace_save_cmdline(struct task_struct *tsk) { - unsigned pid, idx; + unsigned tpid, idx; /* treat recording of idle task as a success */ if (!tsk->pid) return 1; - if (unlikely(tsk->pid > PID_MAX_DEFAULT)) - return 0; + tpid = tsk->pid & (PID_MAX_DEFAULT - 1); /* * It's not the end of the world if we don't get @@ -2405,26 +2404,15 @@ static int trace_save_cmdline(struct tas if (!arch_spin_trylock(&trace_cmdline_lock)) return 0; - idx = savedcmd->map_pid_to_cmdline[tsk->pid]; + idx = savedcmd->map_pid_to_cmdline[tpid]; if (idx == NO_CMDLINE_MAP) { idx = (savedcmd->cmdline_idx + 1) % savedcmd->cmdline_num; - /* - * Check whether the cmdline buffer at idx has a pid - * mapped. We are going to overwrite that entry so we - * need to clear the map_pid_to_cmdline. Otherwise we - * would read the new comm for the old pid. - */ - pid = savedcmd->map_cmdline_to_pid[idx]; - if (pid != NO_CMDLINE_MAP) - savedcmd->map_pid_to_cmdline[pid] = NO_CMDLINE_MAP; - - savedcmd->map_cmdline_to_pid[idx] = tsk->pid; - savedcmd->map_pid_to_cmdline[tsk->pid] = idx; - + savedcmd->map_pid_to_cmdline[tpid] = idx; savedcmd->cmdline_idx = idx; } + savedcmd->map_cmdline_to_pid[idx] = tsk->pid; set_cmdline(idx, tsk->comm); arch_spin_unlock(&trace_cmdline_lock); @@ -2435,6 +2423,7 @@ static int trace_save_cmdline(struct tas static void __trace_find_cmdline(int pid, char comm[]) { unsigned map; + int tpid; if (!pid) { strcpy(comm, ""); @@ -2446,16 +2435,16 @@ static void __trace_find_cmdline(int pid return; } - if (pid > PID_MAX_DEFAULT) { - strcpy(comm, "<...>"); - return; + tpid = pid & (PID_MAX_DEFAULT - 1); + map = savedcmd->map_pid_to_cmdline[tpid]; + if (map != NO_CMDLINE_MAP) { + tpid = savedcmd->map_cmdline_to_pid[map]; + if (tpid == pid) { + strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN); + return; + } } - - map = savedcmd->map_pid_to_cmdline[pid]; - if (map != NO_CMDLINE_MAP) - strlcpy(comm, get_saved_cmdlines(map), TASK_COMM_LEN); - else - strcpy(comm, "<...>"); + strcpy(comm, "<...>"); } void trace_find_cmdline(int pid, char comm[])