Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp61266pxj; Thu, 20 May 2021 04:35:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxl8tUJKBh3wl+khi/+bdjp0zLxXjTAt3BkBxGYC+T8FP6foaLRnDsYMD/jouBa8vnRE777 X-Received: by 2002:a17:906:f283:: with SMTP id gu3mr4210414ejb.91.1621510558751; Thu, 20 May 2021 04:35:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621510558; cv=none; d=google.com; s=arc-20160816; b=OTKkKnq9rLXIsMu96xqiOjLscE0pKbUrLO/KcG9wjf0WWzSS6scjckbEGu3UGQRGcm fRoXZ/8d9x0dfO6F8lobcNRY9gF4T2ZLneyMsG4qbUB0RfJWmpGEpBvM+dkvutmRirDM gcFezXJ1nxp0tGMj9RdGCfJKFWXpBp2DKC86+DCeeF3NSBuLF5K0KbBfAt0ip5FVrKW/ SJaPGKW36/02bpcH8e9jaTqOObZ5btutSgkkMFwm5vW0VFZE1eihcvs6rL5Ki6lIfqGv bHgd1GtFqbsFGPrEqtPysKmqI+oQdlhlyY0RvDexiyhw1Un/f4BX6ZXU2fY9QNU5GVBj 1c6Q== 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=ze46vxdeGQo8WAqgrjGOYTaY2UN3Wys2f4OgtHUoqHc=; b=jsr0oitXIGecIpjmnNv0jv+7d40WCgGBzTxMVh10lyBNign6axFtfDYKryqrkwXI9M Xxg7+icf6Kx4lpIn1/ybMyZkUjEJIf/HZ6D0yhdPA0n3AEl4+NBeab1QD18ynfG3xzdT LTzkpeAedfd47pX6tq0iubzfblk1UoCYBBxRyNBFPg3TnzZpBI0zADSVQvifuC8XMAND SqrlzLQxtrMbCf5nkblj+Zatz+RO9aangyNI5vN2AvEs/D1R28w+zwm9CbvsXLgjbbV/ YOtKEFs1djVsixxdc4ArYOFaQ9vLz012Wt9UxKpFtbZiZt7Q6tEyvjegZurJJfdjc+tL 2Bxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gsOqTlQP; 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 g20si2874701ejz.520.2021.05.20.04.35.35; Thu, 20 May 2021 04:35:58 -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=gsOqTlQP; 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 S241818AbhETLda (ORCPT + 99 others); Thu, 20 May 2021 07:33:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:37174 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239460AbhETLNE (ORCPT ); Thu, 20 May 2021 07:13:04 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EAFFA61D58; Thu, 20 May 2021 10:07:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621505280; bh=65M7+PwmrvMSYRduQjDvzpFjw3nCW07j4Wppdh71EJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gsOqTlQP+C9zOxd/1CZz32wJQJVsvfBLrzd4xdhINfblNCi5L2p6pskmOK3IXd3r2 5rWIOaNnKRpPGf0gN83LKtkFinktAhc2sTPxVfOLHugl5PaZG8gO5mVlfj3DkV2K77 s3buKJEGIAwjKNWUTIGMbqSuKycKhwpTB1H/Uhn8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Steven Rostedt (VMware)" Subject: [PATCH 4.4 063/190] tracing: Map all PIDs to command lines Date: Thu, 20 May 2021 11:22:07 +0200 Message-Id: <20210520092104.245103326@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092102.149300807@linuxfoundation.org> References: <20210520092102.149300807@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 @@ -1562,14 +1562,13 @@ void trace_stop_cmdline_recording(void); 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 @@ -1580,26 +1579,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); @@ -1610,6 +1598,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, ""); @@ -1621,16 +1610,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[])