From patchwork Mon Nov 1 06:04:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595863 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B49D6C433F5 for ; Mon, 1 Nov 2021 06:04:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D59460EBC for ; Mon, 1 Nov 2021 06:04:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231133AbhKAGHO (ORCPT ); Mon, 1 Nov 2021 02:07:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55200 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230096AbhKAGHF (ORCPT ); Mon, 1 Nov 2021 02:07:05 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3937FC061714; Sun, 31 Oct 2021 23:04:32 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id y14-20020a17090a2b4e00b001a5824f4918so8469706pjc.4; Sun, 31 Oct 2021 23:04:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jvb/C5XEaJIeXrLXfpoEk1LkH8WdHTYd7IpTBiq7fdU=; b=QzDoHhHVbtF/bMpHfWbGSlGfWhrLJ3uom/bYNboWDxPau83NZSUbxcsyS3VC0UFwNm IYFWwZxp+p4qvZ5D2tK0osULt9fSRrn39+6+qRP+w8MHRqyPsENAX3Qbce93XloeW0b7 bhZt0u3DwNeykH6ljwHZncNhJdpGAHRDhJ+vIJ5DBB7n6lBWqcVSVnF3oo2IyGW0E2kq JGcTLy1m5V0sKMiuH0G7VtSf8DXS60yNBtgOiW7rsb0a+93/1HmKSOD1N4EI+UEMmIzY d2ig8smuUy3ycMA8HdA6saon+W6X9JZ04Wu2GeG9I6XPzrEbZl3+/fmY4uZ51gpf30GP lfVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jvb/C5XEaJIeXrLXfpoEk1LkH8WdHTYd7IpTBiq7fdU=; b=plSWlPFiegA4DXLYdGmQ8H80+yBa1zLZs5rjg3FgYMVG7uI/I22dj0ai88yHMtoafA OvjuS3vqLgDqYGJPmI8DsvvOr+qnQRRTSYI7+DfNYDu7TwTOu5zYOQW3bS3q3mxGY3cp SzRB/gBgwgW88o24spqvA4dmT+cgq4ojjfUPfSKi9xeXKgthMBRiUNoWbVAX3RJmOuj5 UMXE/M8LnTCjCScjI9vCzRstnG/4HQxwoCEfT7ODUa6A7PIvhMuD0Ldd/V3cEtyUSk2X aHtujJJntasDmcvHeMKOJXgeQRBwAcxeRvLGngS0BoNpgr9iehD8T2HUKQrkuczkOjC8 fsLA== X-Gm-Message-State: AOAM532768AUKzgzYetxW2Z0V7SgcOKFGlJFScaVAf7Ixfx7NmWJCcpV iw+cK9IeclNM6PvuJGC3HzI= X-Google-Smtp-Source: ABdhPJz8UISjP4gb7nXyp0XisjyBJXUE5NhjaTe21LGX2ejW8wzAuI9dQOwUx1G/kySMdPO2jta5Ag== X-Received: by 2002:a17:90b:30c6:: with SMTP id hi6mr16797530pjb.201.1635746671810; Sun, 31 Oct 2021 23:04:31 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:31 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 01/11] fs/exec: make __set_task_comm always set a nul terminated string Date: Mon, 1 Nov 2021 06:04:09 +0000 Message-Id: <20211101060419.4682-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Make sure the string set to task comm is always nul terminated. Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- fs/exec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index a098c133d8d7..404156b5b314 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1224,7 +1224,7 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec) { task_lock(tsk); trace_task_rename(tsk, buf); - strlcpy(tsk->comm, buf, sizeof(tsk->comm)); + strscpy_pad(tsk->comm, buf, sizeof(tsk->comm)); task_unlock(tsk); perf_event_comm(tsk, exec); } From patchwork Mon Nov 1 06:04:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595865 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3281DC433FE for ; Mon, 1 Nov 2021 06:04:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F2666108D for ; Mon, 1 Nov 2021 06:04:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231219AbhKAGHV (ORCPT ); Mon, 1 Nov 2021 02:07:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbhKAGHH (ORCPT ); Mon, 1 Nov 2021 02:07:07 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CA0CC061714; Sun, 31 Oct 2021 23:04:34 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id n11-20020a17090a2bcb00b001a1e7a0a6a6so15307312pje.0; Sun, 31 Oct 2021 23:04:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HfuDNEiJT3BfJis8gojDnNsP87rFWM/LtilUesqMR5U=; b=hPb9/J7ocOjTulc4EreHCv8ZMiDH7pY0cCDo/lAIUe0tnuOnhIo5NHHug7nWuJhrNC 6vtVwF5uYsRSCf7RfEpnZD/J0nqa25t6NpDZ053/nVgcg1n+PRqGI40eSjB5C9FL96df 4D6KcJrwmQXgwdK4zJJfcYP3RS9yKvQgt8/ZkbbVP3+ilc93odwkhmd3O0NAarTD5WDq ZnoJsc4bZN1D3V6Fo82j0RR5+FKeHn+QsNLQHdHfs84ZO8yuGicC3Jn5/yvYglIqpHXU 8obAblxVypVdciBUpGDBoPUuxBiOWFZ1wv+VfYR44yyHma+mcUd73KOAlrLzu1v57TYJ aR1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HfuDNEiJT3BfJis8gojDnNsP87rFWM/LtilUesqMR5U=; b=wZyWVoWw4PhVZ+2VZgjbKAF9ueAIkgMjaZ/ia3GkczKcsPn/joaBsz57Nm86VECdkl mMTZovq8QS7EodoaxmWtaC4oJb/bOPTaXt43e/Gb55dvx/VpOiwoa+oaKMQ6v4Mh+lfB XSqescdex1xM7Q4qEXNVIQ/wmhO1p4L9GB2E7DRF8q0tZ5rbVZa4YQpSNxC3MZW3Skk4 LEqyTBppSaGuSFkkHxwMRkgFw4/KU2PPTPt1b5KKJi4NRPAJHx5AFXCB5TmxlmH0JLcD jYGzqVXVKD8PDvV5y4LpZAmViiQjx3g1d2ulVPTiVhBTQbB7iI/CFMf9ZwZm9tK56Wkz aNzw== X-Gm-Message-State: AOAM53312Ytsh/DmQKyAvMT94Ll6Xq+linlZCAXn3nQIw63AvGWe/18T kfkFYC7PhRuD7Sr1FAzDxRw= X-Google-Smtp-Source: ABdhPJzGByODALzastMQt6vIF+ynjmZShKqwIA1uQ394MFzxHKuVJACVDnLYpsoO/05vk972q4vbYA== X-Received: by 2002:a17:90b:30e:: with SMTP id ay14mr15464050pjb.60.1635746674092; Sun, 31 Oct 2021 23:04:34 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:33 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 02/11] fs/exec: make __get_task_comm always get a nul terminated string Date: Mon, 1 Nov 2021 06:04:10 +0000 Message-Id: <20211101060419.4682-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org If the dest buffer size is smaller than sizeof(tsk->comm), the buffer will be without null ternimator, that may cause problem. We can make sure the buffer size not smaller than comm at the callsite to avoid that problem, but there may be callsite that we can't easily change. Using strscpy_pad() instead of strncpy() in __get_task_comm() can make the string always nul ternimated. Suggested-by: Kees Cook Suggested-by: Steven Rostedt Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- fs/exec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/exec.c b/fs/exec.c index 404156b5b314..013b707d995d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1209,7 +1209,8 @@ static int unshare_sighand(struct task_struct *me) char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) { task_lock(tsk); - strncpy(buf, tsk->comm, buf_size); + /* Always NUL terminated and zero-padded */ + strscpy_pad(buf, tsk->comm, buf_size); task_unlock(tsk); return buf; } From patchwork Mon Nov 1 06:04:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595867 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B02FC433EF for ; Mon, 1 Nov 2021 06:04:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 84089610FC for ; Mon, 1 Nov 2021 06:04:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231309AbhKAGH3 (ORCPT ); Mon, 1 Nov 2021 02:07:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230396AbhKAGHJ (ORCPT ); Mon, 1 Nov 2021 02:07:09 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA417C061746; Sun, 31 Oct 2021 23:04:36 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id x16-20020a17090a789000b001a69735b339so614987pjk.5; Sun, 31 Oct 2021 23:04:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rZRlybNT1VQOoNxdQue8vo9tAC5dSJmDk+dmEknNUrc=; b=a1M395Bqh+HLw+RAGcbuG3Ee9gQ5Pt14RuYZaJHZtAq8IsesqFhaR4qs895G5hbMKL iQS5vYZGfgH7a5b3h4KU8iCUQfcJ2pMAUZ7xUYJlwNlcv/9R98mCr+O99gLbYNgvnTdf Jq1Q2Y9+75eoLINoepcqswMcLPx+E9zUvTqyDgQoDj1JjQ7GhueGgF6r1EyHzZ4+n1+P 4VyRgUsHzVN0JaYnojDA5tc4RqapLPK5V8yI7l8aREDyn7EoVRQ484iSc2Ox4D6ZVWo3 RXydvoGhJx9s1/xKjIdOKtfqIsQHqS0jolmiHdBqkJBvY53eM6lKKs8AgkFuHiKGjnlr HK4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rZRlybNT1VQOoNxdQue8vo9tAC5dSJmDk+dmEknNUrc=; b=cQ1F9IaxwbEGSb72WC/p4WoxODTWncYrTKV7fctFjOBdFJpO+uZNCHPlnlfwHxt/eL I8vBjnP5hDABeYdeeqvw1oFMLEMZbuXNHg0x05/zjUfVtM4iytRc6L3ziK4mBD/Ez45V +2kjeUmS5kcCcTWXMCQAwscqLs8Xhvl7f+6qkwbAzm4IsBnDP9eyyPJ9uYC6z2rRMWdv G3gJsNKJji4oV3dYG85X4qNi8VEvzivPvkTx1v32vMyZV5uLfCNcvtIq7cNEpHWkh9E2 XidZqT/pBp7Ni0yZGo9+DJw5t7kMjtOuDrgRKtpwIDHxozaah6J1m3bCGjbRDW70Sqcp OYsw== X-Gm-Message-State: AOAM532Y5xa0sSlQbseIlBPXpUe+4zNm21k3Yo5pEsvn03DRQpnw24pB aQ7OD3ItrunuU10d9lTt7yk= X-Google-Smtp-Source: ABdhPJzL4jPx+ZhAWUkFzp5hvgKpDWtgYKzpi4hD6dRWbzMDiPGRwPGbJb/YNiUpFAuwRHKj4szYdg== X-Received: by 2002:a17:902:8bcb:b0:141:b34e:d54f with SMTP id r11-20020a1709028bcb00b00141b34ed54fmr15789414plo.68.1635746676451; Sun, 31 Oct 2021 23:04:36 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:35 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 03/11] sched.h: use __must_be_array instead of BUILD_BUG_ON in get_task_comm Date: Mon, 1 Nov 2021 06:04:11 +0000 Message-Id: <20211101060419.4682-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Now that __get_task_comm() will truncate and pad, it's safe to use on both too-small and too-big arrays. So it is not needed to check array length any more. For the original goal of making sure get_task_comm() is being used on a char array, we can use __must_be_array(). Below is the verification when I changed the dest buffer of get_task_comm() in a driver code, CC [M] drivers/infiniband/hw/qib/qib_file_ops.o In file included from ./include/linux/bits.h:22:0, from ./include/linux/ioport.h:13, from ./include/linux/pci.h:31, from drivers/infiniband/hw/qib/qib_file_ops.c:35: drivers/infiniband/hw/qib/qib_file_ops.c: In function ‘setup_ctxt’: ./include/linux/build_bug.h:16:51: error: negative width in bit-field ‘’ #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) ^ ./include/linux/compiler.h:258:28: note: in expansion of macro ‘BUILD_BUG_ON_ZERO’ #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) ^~~~~~~~~~~~~~~~~ ./include/linux/sched.h:1941:23: note: in expansion of macro ‘__must_be_array’ __get_task_comm(buf, __must_be_array(buf) + sizeof(buf), tsk) ^~~~~~~~~~~~~~~ drivers/infiniband/hw/qib/qib_file_ops.c:1325:2: note: in expansion of macro ‘get_task_comm’ get_task_comm(test, current); It hit this warnig as expected. Suggested-by: Kees Cook Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- include/linux/sched.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index c1a927ddec64..b9c85c52fed0 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1931,10 +1931,8 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from) } extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk); -#define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ -}) +#define get_task_comm(buf, tsk) \ + __get_task_comm(buf, __must_be_array(buf) + sizeof(buf), tsk) #ifdef CONFIG_SMP static __always_inline void scheduler_ipi(void) From patchwork Mon Nov 1 06:04:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595869 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12311C433F5 for ; Mon, 1 Nov 2021 06:05:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0365E6108F for ; Mon, 1 Nov 2021 06:05:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231475AbhKAGHi (ORCPT ); Mon, 1 Nov 2021 02:07:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230478AbhKAGHM (ORCPT ); Mon, 1 Nov 2021 02:07:12 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 483DBC0613F5; Sun, 31 Oct 2021 23:04:39 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id h74so1417841pfe.0; Sun, 31 Oct 2021 23:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=o3r423UmuBw5jHyHMAFihSOkgDAeE6bhihnHvX68874=; b=LMNo4W3TjV5llf6jnlGy9NTRwR+hoKt0QLv5GmrPdv3tnhWv9SWc7CJ7IPQ25LBnzk bcRtJNIrFLRYdb+nwHDIMCzxk8OcTqZj1+KQoDpEpEyi4CNOYzHG7irT+iMD/qus1bRP gJpNr3rEp+9oEiUuNaizI1x0DMWAc3vLy2WnsYbX1+wB63+hqz8QVD8LDeRx0SgfgqKw 29ejtUEHKc/vPJ8SAql+Fv/wLIFhF7PSo8w1Ydn9qX+mHD0mR9MnwZ+am4lPfe/PBi3u mJbBqphRMOFBQ/gfkyGl+Ss6tcUTIXKBTaH7ednulSWrpVWbdBFxaDEy0u77cY4ekOZz EDRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=o3r423UmuBw5jHyHMAFihSOkgDAeE6bhihnHvX68874=; b=4kDLVy6fi03MtQMae0GXUuUZGKvT/KUe5XndsaWgvG8i2O87eDUjbWPOpX6LxTt0jP L39PQ3+btavrdPpSlpAaTGPoh9cmpfGYYGXWag7ulN1Na12paPJFmQsoAEjca4jxbfni 5gR490TNwaQFsBWtl9V6D5e5rwSOJsAI0js6JTYwE6UPGb0hlX7K6h39ux6roj6rxaA4 fDVMahbJXOcTwV/iH21BG5DUL8ERR5hpvNSWsgCGR32QLywTvl5LrLz1V8MO8uTzY5FS 4OjkHMHYbNtilf+h0RutrCFN1/8mc3Ynf1eRZ83j3eQ/euj6ZKNc6SLqt07qMBv8MlSs toAQ== X-Gm-Message-State: AOAM533SbDH1jvzzb+7nnHfwRjEVtiHAcuG+OdeYTX98zZZOjF6/rM9/ 5lF0+kYk73gOWL9c+r1iMHQ= X-Google-Smtp-Source: ABdhPJxaybbOZX2waEJCer7qTNlHbiwvvo3L2vD0OkVD9dD70R5sF0i0k+8R93jpwPu6/vpMsOcejw== X-Received: by 2002:a05:6a00:2311:b0:431:c19f:2a93 with SMTP id h17-20020a056a00231100b00431c19f2a93mr26982527pfh.11.1635746678877; Sun, 31 Oct 2021 23:04:38 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:38 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 04/11] drivers/infiniband: make setup_ctxt always get a nul terminated task comm Date: Mon, 1 Nov 2021 06:04:12 +0000 Message-Id: <20211101060419.4682-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Use get_task_comm() instead of open-coded strlcpy() to make the comm always nul terminated. As the comment above the hard-coded 16, we can replace it with TASK_COMM_LEN, then it will adopt to the comm size change. Signed-off-by: Yafang Shao Acked-by: Dennis Dalessandro Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- drivers/infiniband/hw/qib/qib.h | 2 +- drivers/infiniband/hw/qib/qib_file_ops.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/hw/qib/qib.h b/drivers/infiniband/hw/qib/qib.h index 9363bccfc6e7..a8e1c30c370f 100644 --- a/drivers/infiniband/hw/qib/qib.h +++ b/drivers/infiniband/hw/qib/qib.h @@ -196,7 +196,7 @@ struct qib_ctxtdata { pid_t pid; pid_t subpid[QLOGIC_IB_MAX_SUBCTXT]; /* same size as task_struct .comm[], command that opened context */ - char comm[16]; + char comm[TASK_COMM_LEN]; /* pkeys set by this use of this ctxt */ u16 pkeys[4]; /* so file ops can get at unit */ diff --git a/drivers/infiniband/hw/qib/qib_file_ops.c b/drivers/infiniband/hw/qib/qib_file_ops.c index 63854f4b6524..aa290928cf96 100644 --- a/drivers/infiniband/hw/qib/qib_file_ops.c +++ b/drivers/infiniband/hw/qib/qib_file_ops.c @@ -1321,7 +1321,7 @@ static int setup_ctxt(struct qib_pportdata *ppd, int ctxt, rcd->tid_pg_list = ptmp; rcd->pid = current->pid; init_waitqueue_head(&dd->rcd[ctxt]->wait); - strlcpy(rcd->comm, current->comm, sizeof(rcd->comm)); + get_task_comm(rcd->comm, current); ctxt_fp(fp) = rcd; qib_stats.sps_ctxts++; dd->freectxts--; From patchwork Mon Nov 1 06:04:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595871 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5A8DC4332F for ; Mon, 1 Nov 2021 06:05:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D17F06108D for ; Mon, 1 Nov 2021 06:05:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231191AbhKAGHs (ORCPT ); Mon, 1 Nov 2021 02:07:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55258 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231138AbhKAGHO (ORCPT ); Mon, 1 Nov 2021 02:07:14 -0400 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E35A8C061764; Sun, 31 Oct 2021 23:04:41 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id n23so5638840pgh.8; Sun, 31 Oct 2021 23:04:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ap2gAMjIVTbmjeLM5LRGgC4luOpifeHIYUs0krjMvxk=; b=Xww3fwhQ6GVEpXF7vRbHR0oD+u34jHMA9vEJrrh55zmht9IdE5Qb/0vjbQs4HN5gvY POcNuU5T+abx6eRSGb9nZuGqjMlnYzI3jqvFYBpqbT6poqQDzWL/OCelJl7Ho3Jrf54u 5YN/78Tr6wNXbUZYW94XoWas9EC868iRRB80bXY8hf7ZBFghg6/WFfP5WyJZQXRoDLAc 5qWLDy0anYrl3w8SOS4VT5/w0N/L80lgTsNBBnkKg70ESOn1Peuixzj+ADfGsxOt+hYL hcPflq3MyEUgj2IPSiAxx5uysxJUuGk+9g1qy+Fnllx7jZ0Mylv4WfpxvfDZiclIXnpB nNMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ap2gAMjIVTbmjeLM5LRGgC4luOpifeHIYUs0krjMvxk=; b=mzZAw8q7G9WAIbu0mr5dwx2+rjgpH809Jv+KdJxmgXU5M7Gb7HDQJBVNMQiUq0Ob1H H+8eMnyy+sRKeyEqLOTNqFinTc5qU7srYhlzJ48+GP9R23iblu3z32y+lrO4DKMsAQJy 07JIdmrYOIgyd8DHgfJbZw7BpQKyemALGOrghrppIECnPuNEfh7dv+qO1XcRdG1Vfslk BOVqIxKBHOARef2qef52A4dz+Op9pSQexCzCHoAjFrUtImrsc8+zGEo0trcQ1xBF5bCQ 0v40EQFyT6S4yo8yWMD/8dMwEUaTybX9deBsx16xaTDtoLMtOdarAA4NHKFNejpUSLxn 8Q+w== X-Gm-Message-State: AOAM530DZyryQUp7WGIapJHSPQqAffzTen4660n/4CfySKyaAKyUPGmt Ev1zwb3RjenEUyzxU7IV5k0= X-Google-Smtp-Source: ABdhPJzKLE+vunSqJAXuq5W8qJV8bLajJd/LjCll+mqrJULVSs7WJNF0By6dsSaTkrqw5K5k4BauhA== X-Received: by 2002:a62:1743:0:b0:480:a01f:bf14 with SMTP id 64-20020a621743000000b00480a01fbf14mr11691513pfx.72.1635746681116; Sun, 31 Oct 2021 23:04:41 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:40 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Andrii Nakryiko Subject: [PATCH v7 05/11] fs/binfmt_elf: make prpsinfo always get a nul terminated task comm Date: Mon, 1 Nov 2021 06:04:13 +0000 Message-Id: <20211101060419.4682-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org kernel test robot reported a -Wstringop-truncation warning after I extend task comm from 16 to 24. Below is the detailed warning: fs/binfmt_elf.c: In function 'fill_psinfo.isra': >> fs/binfmt_elf.c:1575:9: warning: 'strncpy' output may be truncated copying 16 bytes from a string of length 23 [-Wstringop-truncation] 1575 | strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Replacing the open-coded strncpy() with the helper get_task_comm() can avoid this warning. As the struct prpsinfo is externally parsed, for example by the crash utility, we can't change the size of pr_fname. So I just leave it as-is. I also verfied if it still work well when I extend the comm size to 24. struct elf_prpsinfo is used to dump the task information in userspace coredump or kernel vmcore. Below is the verfication of vmcore, crash> ps PID PPID CPU TASK ST %MEM VSZ RSS COMM 0 0 0 ffffffff9d21a940 RU 0.0 0 0 [swapper/0] > 0 0 1 ffffa09e40f85e80 RU 0.0 0 0 [swapper/1] > 0 0 2 ffffa09e40f81f80 RU 0.0 0 0 [swapper/2] > 0 0 3 ffffa09e40f83f00 RU 0.0 0 0 [swapper/3] > 0 0 4 ffffa09e40f80000 RU 0.0 0 0 [swapper/4] > 0 0 5 ffffa09e40f89f80 RU 0.0 0 0 [swapper/5] 0 0 6 ffffa09e40f8bf00 RU 0.0 0 0 [swapper/6] > 0 0 7 ffffa09e40f88000 RU 0.0 0 0 [swapper/7] > 0 0 8 ffffa09e40f8de80 RU 0.0 0 0 [swapper/8] > 0 0 9 ffffa09e40f95e80 RU 0.0 0 0 [swapper/9] > 0 0 10 ffffa09e40f91f80 RU 0.0 0 0 [swapper/10] > 0 0 11 ffffa09e40f93f00 RU 0.0 0 0 [swapper/11] > 0 0 12 ffffa09e40f90000 RU 0.0 0 0 [swapper/12] > 0 0 13 ffffa09e40f9bf00 RU 0.0 0 0 [swapper/13] > 0 0 14 ffffa09e40f98000 RU 0.0 0 0 [swapper/14] > 0 0 15 ffffa09e40f9de80 RU 0.0 0 0 [swapper/15] It works well as expected. Reported-by: kernel test robot Suggested-by: Kees Cook Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- fs/binfmt_elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index a813b70f594e..138956fd4a88 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1572,7 +1572,7 @@ static int fill_psinfo(struct elf_prpsinfo *psinfo, struct task_struct *p, SET_UID(psinfo->pr_uid, from_kuid_munged(cred->user_ns, cred->uid)); SET_GID(psinfo->pr_gid, from_kgid_munged(cred->user_ns, cred->gid)); rcu_read_unlock(); - strncpy(psinfo->pr_fname, p->comm, sizeof(psinfo->pr_fname)); + get_task_comm(psinfo->pr_fname, p); return 0; } From patchwork Mon Nov 1 06:04:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595873 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5CC6C433EF for ; Mon, 1 Nov 2021 06:05:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C05A86108D for ; Mon, 1 Nov 2021 06:05:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231750AbhKAGH5 (ORCPT ); Mon, 1 Nov 2021 02:07:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbhKAGHd (ORCPT ); Mon, 1 Nov 2021 02:07:33 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6897C06120B; Sun, 31 Oct 2021 23:04:43 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 75so16328164pga.3; Sun, 31 Oct 2021 23:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CZbW1Aleg8hcQ6oriKfCOVzwEMQjjmsZLoOFChfxiOE=; b=hLIVcr6CSGMn7q+duUxCWW0eGrpmQBAmB6Wq4FCLbLyQvr2Upd8TZDDWhXwETwIru5 gVsFVTteDomWNPaucmxRqkCCuS8WRWbKXTXFS4O738YFeGYfE7jLWgBhWcP5Wyl3Ixx1 e9YODtfNTOnD4iIYhMk9WCAiID1UqwO6uuIqcXKNmu7EhFjFVJ/VFfeT8r6Dr1npBbY7 zyuY/z7kwCaKGMOzNmsXif+LtjafxWuoctWRkorVboIJqLwC2HTMPIEGJO5cyPXEJAeh sgohILu4rfKSjNpvEjhK4xvo6H+wsmaaOY6Ipo4srFpfRbzLHJNYJVKU90aLM7DlOEJ/ 4Qrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CZbW1Aleg8hcQ6oriKfCOVzwEMQjjmsZLoOFChfxiOE=; b=uwrfkDDJuATNgmtGsB8YLWZ2e20Hg1M+FUgUeoLyPC4LRzN+vM/No+LykePhwvGpDF m9pk49EyFROd3T+k4z89+ZCR3PQ5kpcv7zkQnbMBbVPJUr8xhATBGZd6hSHEaWYo0Aix a/KsgUPXv4XwjyDbHU/2C6rYe+7AVVgl8ebGJjuSAKRtThzKykEuZvRAz0MNODoqCd/3 +i99jy0FqWiOK3NdCjCZr/o10Xyq5SX+mA+ZvzHBVl3Ip0DVBFr5ceIWsOhbP6mHOXQa r9q1uTnWaL6Hw63A+v2Dcf6Qdw/m3AaChis8bGUuJycoYzJSWcNRYcMG033iQk3HMa4n 2Alg== X-Gm-Message-State: AOAM532r06wRdBecCggO9q/ygmu7Sl1r6cSheTAWn1WnI+sjyLYnODWJ 7TWof55FHSV/URUY2gzErLQ= X-Google-Smtp-Source: ABdhPJy5ahFUxXTC3lwrjdq2rffkJFK0bqTnEW/fg2sOG/fpUs8BcRn22EW5Z/8v2ha5HNBZ374p9Q== X-Received: by 2002:a62:e510:0:b0:44c:e06e:80dd with SMTP id n16-20020a62e510000000b0044ce06e80ddmr27033507pff.48.1635746683498; Sun, 31 Oct 2021 23:04:43 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:43 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 06/11] samples/bpf/test_overhead_kprobe_kern: make it adopt to task comm size change Date: Mon, 1 Nov 2021 06:04:14 +0000 Message-Id: <20211101060419.4682-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org bpf_probe_read_kernel_str() will add a nul terminator to the dst, then we don't care about if the dst size is big enough. This patch also replaces the hard-coded 16 with TASK_COMM_LEN to make it adopt to task comm size change. Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- samples/bpf/offwaketime_kern.c | 4 ++-- samples/bpf/test_overhead_kprobe_kern.c | 11 ++++++----- samples/bpf/test_overhead_tp_kern.c | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/samples/bpf/offwaketime_kern.c b/samples/bpf/offwaketime_kern.c index 4866afd054da..eb4d94742e6b 100644 --- a/samples/bpf/offwaketime_kern.c +++ b/samples/bpf/offwaketime_kern.c @@ -113,11 +113,11 @@ static inline int update_counts(void *ctx, u32 pid, u64 delta) /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; }; diff --git a/samples/bpf/test_overhead_kprobe_kern.c b/samples/bpf/test_overhead_kprobe_kern.c index f6d593e47037..8fdd2c9c56b2 100644 --- a/samples/bpf/test_overhead_kprobe_kern.c +++ b/samples/bpf/test_overhead_kprobe_kern.c @@ -6,6 +6,7 @@ */ #include #include +#include #include #include #include @@ -22,17 +23,17 @@ int prog(struct pt_regs *ctx) { struct signal_struct *signal; struct task_struct *tsk; - char oldcomm[16] = {}; - char newcomm[16] = {}; + char oldcomm[TASK_COMM_LEN] = {}; + char newcomm[TASK_COMM_LEN] = {}; u16 oom_score_adj; u32 pid; tsk = (void *)PT_REGS_PARM1(ctx); pid = _(tsk->pid); - bpf_probe_read_kernel(oldcomm, sizeof(oldcomm), &tsk->comm); - bpf_probe_read_kernel(newcomm, sizeof(newcomm), - (void *)PT_REGS_PARM2(ctx)); + bpf_probe_read_kernel_str(oldcomm, sizeof(oldcomm), &tsk->comm); + bpf_probe_read_kernel_str(newcomm, sizeof(newcomm), + (void *)PT_REGS_PARM2(ctx)); signal = _(tsk->signal); oom_score_adj = _(signal->oom_score_adj); return 0; diff --git a/samples/bpf/test_overhead_tp_kern.c b/samples/bpf/test_overhead_tp_kern.c index eaa32693f8fc..80edadacb692 100644 --- a/samples/bpf/test_overhead_tp_kern.c +++ b/samples/bpf/test_overhead_tp_kern.c @@ -4,6 +4,7 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ +#include #include #include @@ -11,8 +12,8 @@ struct task_rename { __u64 pad; __u32 pid; - char oldcomm[16]; - char newcomm[16]; + char oldcomm[TASK_COMM_LEN]; + char newcomm[TASK_COMM_LEN]; __u16 oom_score_adj; }; SEC("tracepoint/task/task_rename") From patchwork Mon Nov 1 06:04:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595875 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2496C4321E for ; Mon, 1 Nov 2021 06:05:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9E3460EBC for ; Mon, 1 Nov 2021 06:05:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231768AbhKAGH7 (ORCPT ); Mon, 1 Nov 2021 02:07:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231393AbhKAGHe (ORCPT ); Mon, 1 Nov 2021 02:07:34 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E1E7C06120C; Sun, 31 Oct 2021 23:04:46 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id k4so1069630plx.8; Sun, 31 Oct 2021 23:04:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UnbvpW9WGZPPHsD461H5NLpgI+BcZRpk9Uvkg/7/FsI=; b=mBgEqR2cuqWgG4wbE+Ee9nl7PI0oPuF//CHOFEk3rqDKH5Wv+2P8q+SgcLACl7Eu1S DYIBVewqg0Hdo0Z9cjB8Sc4otEiDDpzHVGJDzdQm/23So6LaXX9kNiFXBxpugZ7V+xVS 0csKWuVOGikNjqyzPgk+VenqX+qZTV7bDJrecXDWyeYudRBWBkdXOMGaT3EtG6yamLnt oT+8TiedZHp3qG2ml51vaYf8orZUmG0H45pcDpUssVRSPBh7l6VY8IyPdChDJul82kv5 Ev7EY/jQQfLSgW8XNit4lnKgN4T8Rq4bUuBwJvyrwIhPyecKtKCfMJM4hdXsmzwSwxCL KqcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UnbvpW9WGZPPHsD461H5NLpgI+BcZRpk9Uvkg/7/FsI=; b=X6fkjcpe6mzh8qOKc3DsZ7Cu7SMm9wxGkzruf7WvtS83k69P424BXLWq2/2HOp1y0f kSHXyOM/H5NtS7l/xvtBCYJ/zLFFuQeLp3y7wyOuf4vk4DjDwI69jahyyC9o5LDVvjgU NpD4Afvqw5KEG0Q/2LAHGugWO8XgAu4l6R4Z8yM3UpVEtvt8qkGrxnLxWoM6duT3/lo2 yFilMrXsUGXAGtyKd4GB8jIKTxHfQUo8UanW4mg/pbuzlKrlIgLnRc+iv4/Dk1n2y7N+ 353FKnIoJJ1Ete1q684kk0JqayGyUq3sqZ+c3t6BSfmewYQug+dix2zOuQ6buBu5GCXG XRPQ== X-Gm-Message-State: AOAM530TZMlNX+EOfKyPjS7WU8YfP2HK6fuO8TW/tcYaBsDUzlQ9OKmj e/hZ1GTahlL/P1iBDVZV66I= X-Google-Smtp-Source: ABdhPJzGCvt5iEpyM474KY6O62Zlsfcvpf7wrXcYqDsBGf7Az/ne89nFa22e0UxYm86jwgxdTn2BTA== X-Received: by 2002:a17:902:bd01:b0:141:6232:6f89 with SMTP id p1-20020a170902bd0100b0014162326f89mr23251730pls.12.1635746685828; Sun, 31 Oct 2021 23:04:45 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:45 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 07/11] tools/bpf/bpftool/skeleton: make it adopt to task comm size change Date: Mon, 1 Nov 2021 06:04:15 +0000 Message-Id: <20211101060419.4682-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org bpf_probe_read_kernel_str() will add a nul terminator to the dst, then we don't care about if the dst size is big enough. Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Acked-by: Andrii Nakryiko --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index d9b420972934..f70702fcb224 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -71,8 +71,8 @@ int iter(struct bpf_iter__task_file *ctx) e.pid = task->tgid; e.id = get_obj_id(file->private_data, obj_type); - bpf_probe_read_kernel(&e.comm, sizeof(e.comm), - task->group_leader->comm); + bpf_probe_read_kernel_str(&e.comm, sizeof(e.comm), + task->group_leader->comm); bpf_seq_write(ctx->meta->seq, &e, sizeof(e)); return 0; From patchwork Mon Nov 1 06:04:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595883 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52E69C433FE for ; Mon, 1 Nov 2021 06:05:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D0016108F for ; Mon, 1 Nov 2021 06:05:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231364AbhKAGIR (ORCPT ); Mon, 1 Nov 2021 02:08:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231486AbhKAGHi (ORCPT ); Mon, 1 Nov 2021 02:07:38 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D41DC06120E; Sun, 31 Oct 2021 23:04:48 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id q187so16345025pgq.2; Sun, 31 Oct 2021 23:04:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2AOLQynizUK/9ub+MV3+x/PqsjWBik6Y2qklqSq5kvM=; b=l0EIvzonzcNlCGT6TjXsysXA3uAstk65U4vdn7q433mY6i9VaZO0ffvw4f0T7ELy+H v5u+ZBM7SAyqPzYBN8O3x3hA2xIUlUhwyCu+BC5V53HvycjI8Hi1umyK7IQJacmk7i6o khFyYXCW5nWroZrXjJsWFekl96dq67dFeaypF802J/p4iwuZyg6Sdmu4rhSJT2qJi5Xn 00XpVbToRKPbkrTugqQahtpW6uED5b5mo1TqnqCLLEU/0vh43M+JEmJ14r4WejDOBaEu +jeAFO9mXb76Vk4n5De0pRxb2KCQFi3+BU1G2dJDHO+QI+K2i7tRGETK7bbMtOtDZjJV Xahg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2AOLQynizUK/9ub+MV3+x/PqsjWBik6Y2qklqSq5kvM=; b=7TDhLvw507GCM14b7tNxqC1cWY3cs4ODkGLJYvBQjiso4MeLSyR4wUsb2v++OqdhQd +CI5b6GPE7yrORxCCU8cl+TpGbGoz4T+hRb3N2BllWiDIo1waMVWG5t81wXtwz1a2kdZ 9tWI2HJy3vJHAF67PTIh6Mm4lv5ICfPJMiW6d9lxSn9M+bAdHjyvHtnsIcLPPf/U4Q7J UTWXj32nBWuPsuXLBdRfBDJNoLaelRAB09tWks2SaM4kmfeIqIxqu0KzWG8Eqe2QxaAb MYjxhKwujd7SP7tiIEt9NXM/jLkcANhjztlLk1pBWXbn8Wwod5R1Q+hWbwk+uwSXA2Wz /cDA== X-Gm-Message-State: AOAM530MQHR+Fie1l5v6z0lVc6GT2ZabVV9h7hGTzTr82TuHSsiWfOhg 8+uCBt1VOPmf+hBNXHUj2L0= X-Google-Smtp-Source: ABdhPJyam8xih1Z6tKYYGpuiGMnnmxuj4/L4ZJpVlE3q5S3rpYWdBTOmUb41H+/yZOg+DA2kjkENOw== X-Received: by 2002:a63:8f4a:: with SMTP id r10mr20105733pgn.337.1635746688077; Sun, 31 Oct 2021 23:04:48 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:47 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 08/11] tools/perf/test: make perf test adopt to task comm size change Date: Mon, 1 Nov 2021 06:04:16 +0000 Message-Id: <20211101060419.4682-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org kernel test robot reported a perf-test failure after I extended task comm size from 16 to 24. The failure as follows, 2021-10-13 18:00:46 sudo /usr/src/perf_selftests-x86_64-rhel-8.3-317419b91ef4eff4e2f046088201e4dc4065caa0/tools/perf/perf test 15 15: Parse sched tracepoints fields : FAILED! The reason is perf-test requires a fixed-size task comm. If we extend task comm size to 24, it will not equil with the required size 16 in perf test. After some analyzation, I found perf itself can adopt to the size change, for example, below is the output of perf-sched after I extend comm size to 24 - task 614 ( kthreadd: 84), nr_events: 1 task 615 ( systemd: 843), nr_events: 1 task 616 ( networkd-dispat: 1026), nr_events: 1 task 617 ( systemd: 846), nr_events: 1 $ cat /proc/843/comm networkd-dispatcher The task comm can be displayed correctly as expected. Replace old hard-coded 16 with the new one can fix the warning, but we'd better make the test accept both old and new sizes, then it can be backward compatibility. After this patch, the perf-test succeeds no matter task comm is 16 or 24 - 15: Parse sched tracepoints fields : Ok This patch is a preparation for the followup patch. Reported-by: kernel test robot Suggested-by: Arnaldo Carvalho de Melo Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- tools/include/linux/sched.h | 11 +++++++++++ tools/perf/tests/evsel-tp-sched.c | 26 ++++++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 tools/include/linux/sched.h diff --git a/tools/include/linux/sched.h b/tools/include/linux/sched.h new file mode 100644 index 000000000000..0d575afd7f43 --- /dev/null +++ b/tools/include/linux/sched.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _TOOLS_LINUX_SCHED_H +#define _TOOLS_LINUX_SCHED_H + +/* Keep both length for backward compatibility */ +enum { + TASK_COMM_LEN_16 = 16, + TASK_COMM_LEN = 24, +}; + +#endif /* _TOOLS_LINUX_SCHED_H */ diff --git a/tools/perf/tests/evsel-tp-sched.c b/tools/perf/tests/evsel-tp-sched.c index f9e34bd26cf3..029f2a8c8e51 100644 --- a/tools/perf/tests/evsel-tp-sched.c +++ b/tools/perf/tests/evsel-tp-sched.c @@ -1,11 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include "evsel.h" #include "tests.h" #include "debug.h" -static int evsel__test_field(struct evsel *evsel, const char *name, int size, bool should_be_signed) +static int evsel__test_field_alt(struct evsel *evsel, const char *name, + int size, int alternate_size, bool should_be_signed) { struct tep_format_field *field = evsel__field(evsel, name); int is_signed; @@ -23,15 +25,24 @@ static int evsel__test_field(struct evsel *evsel, const char *name, int size, bo ret = -1; } - if (field->size != size) { - pr_debug("%s: \"%s\" size (%d) should be %d!\n", + if (field->size != size && field->size != alternate_size) { + pr_debug("%s: \"%s\" size (%d) should be %d", evsel->name, name, field->size, size); + if (alternate_size > 0) + pr_debug(" or %d", alternate_size); + pr_debug("!\n"); ret = -1; } return ret; } +static int evsel__test_field(struct evsel *evsel, const char *name, + int size, bool should_be_signed) +{ + return evsel__test_field_alt(evsel, name, size, -1, should_be_signed); +} + int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtest __maybe_unused) { struct evsel *evsel = evsel__newtp("sched", "sched_switch"); @@ -42,7 +53,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtes return -1; } - if (evsel__test_field(evsel, "prev_comm", 16, false)) + if (evsel__test_field_alt(evsel, "prev_comm", TASK_COMM_LEN_16, + TASK_COMM_LEN, false)) ret = -1; if (evsel__test_field(evsel, "prev_pid", 4, true)) @@ -54,7 +66,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtes if (evsel__test_field(evsel, "prev_state", sizeof(long), true)) ret = -1; - if (evsel__test_field(evsel, "next_comm", 16, false)) + if (evsel__test_field_alt(evsel, "next_comm", TASK_COMM_LEN_16, + TASK_COMM_LEN, false)) ret = -1; if (evsel__test_field(evsel, "next_pid", 4, true)) @@ -72,7 +85,8 @@ int test__perf_evsel__tp_sched_test(struct test *test __maybe_unused, int subtes return -1; } - if (evsel__test_field(evsel, "comm", 16, false)) + if (evsel__test_field_alt(evsel, "comm", TASK_COMM_LEN_16, + TASK_COMM_LEN, false)) ret = -1; if (evsel__test_field(evsel, "pid", 4, true)) From patchwork Mon Nov 1 06:04:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595877 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E7D1C43219 for ; Mon, 1 Nov 2021 06:05:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8CADD61078 for ; Mon, 1 Nov 2021 06:05:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231892AbhKAGIM (ORCPT ); Mon, 1 Nov 2021 02:08:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231532AbhKAGHj (ORCPT ); Mon, 1 Nov 2021 02:07:39 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 004BBC061210; Sun, 31 Oct 2021 23:04:50 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id y4so1005212pfa.5; Sun, 31 Oct 2021 23:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=d7LTw3kPVa/zFmJb8s8WHpppDtuC/dCZbGEz654o0Z0=; b=H/Nt6ip1DACtHq57o9Mdyh3bCgZ2fAcALeHvM0Gv+XQbwcYyRHXWzojtO6xw8zIvFg of7ARcpuxOICZBLLWsgHgEjiLZ0NGPmGsuSOqYNFLvMqSv4w8r/p8nLl5pblmv9MgEXK 21ou7ksmzx/LPZaw79nrvSnwPYYF223o7/HBbYq0pHxF7nGPcAxZCCl6cOjOw/p4oGNc VxIcq8UNpcJv+RFjFLhBnY8fbD5NrNaZXXYN5J97DSOc3G3PRDERiKp/Ha/Guhftz0eU E9yXRyZQp5jPEOXF7gKMmJZHCv2oeodhDAX0AAFrN+gmh4zIDj2PfHr03avz0ORhIkM/ aw+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d7LTw3kPVa/zFmJb8s8WHpppDtuC/dCZbGEz654o0Z0=; b=bssYcQhuHz2agv7qxG/Q1CWChUxVZ4uOAvVo5e0rYO8mMcfHQ7lZphu6jqJFip7o8y 3MJ0ckdAAJpz4UqyY5B1yd7heJ8E9tflktCWPApGPmH8893WCFeGEN4lMQgtXe/m4jSO 4tarRIwNfB5+tcr3az3AHgCylm0quUiBee74uRj9r3zauqDpoRwcSIebf18aB5KU0263 aApFEQAwfo8i+6wbSgyulrlM8dbFu1PtPYgmRwsZgJZ0qG/cspA7brQoY1Du6qawtSpM OmNgY0mm0TiUSCKjWepuszxLfDje5kL3rLxhCov14ZkHPKy3XPiJPnYMiB02WJVYjspP YUKA== X-Gm-Message-State: AOAM533LaemEc4aqaTd6HJlE31rubWkyIAEmG0VxeazcGO2JlbG40RHF 3659EmGUBf6TcGHmSo5tKpU= X-Google-Smtp-Source: ABdhPJxHtNbH9HB5n5DPJRDQzLHf3MgJ9R6ynW9CWqOunCISp1/PBfsWZl/KKejsKvsqjLu1N78mrQ== X-Received: by 2002:a63:7e05:: with SMTP id z5mr20295506pgc.354.1635746690496; Sun, 31 Oct 2021 23:04:50 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:49 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Andrii Nakryiko Subject: [PATCH v7 09/11] tools/testing/selftests/bpf: make it adopt to task comm size change Date: Mon, 1 Nov 2021 06:04:17 +0000 Message-Id: <20211101060419.4682-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org The hard-coded 16 is used in various bpf progs. These progs get task comm either via bpf_get_current_comm() or prctl() or bpf_core_read_str(), all of which can work well even if the task comm size is changed. In these BPF programs, one thing to be improved is the sched:sched_switch tracepoint args. As the tracepoint args are derived from the kernel, we'd better make it same with the kernel. So the macro TASK_COMM_LEN is converted to type enum, then all the BPF programs can get it through BTF. The BPF program which wants to use TASK_COMM_LEN should include the header vmlinux.h. Regarding the test_stacktrace_map and test_tracepoint, as the type defined in linux/bpf.h are also defined in vmlinux.h, so we don't need to include linux/bpf.h again. Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek Acked-by: Andrii Nakryiko --- include/linux/sched.h | 9 +++++++-- tools/testing/selftests/bpf/progs/test_stacktrace_map.c | 6 +++--- tools/testing/selftests/bpf/progs/test_tracepoint.c | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index b9c85c52fed0..09ac13e54549 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -274,8 +274,13 @@ struct task_group; #define get_current_state() READ_ONCE(current->__state) -/* Task command name length: */ -#define TASK_COMM_LEN 16 +/* + * Define the task command name length as enum, then it can be visible to + * BPF programs. + */ +enum { + TASK_COMM_LEN = 16, +}; extern void scheduler_tick(void); diff --git a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c index 00ed48672620..e9b602a6dc1b 100644 --- a/tools/testing/selftests/bpf/progs/test_stacktrace_map.c +++ b/tools/testing/selftests/bpf/progs/test_stacktrace_map.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2018 Facebook -#include +#include #include #ifndef PERF_MAX_STACK_DEPTH @@ -41,11 +41,11 @@ struct { /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; }; diff --git a/tools/testing/selftests/bpf/progs/test_tracepoint.c b/tools/testing/selftests/bpf/progs/test_tracepoint.c index 4b825ee122cf..f21982681e28 100644 --- a/tools/testing/selftests/bpf/progs/test_tracepoint.c +++ b/tools/testing/selftests/bpf/progs/test_tracepoint.c @@ -1,17 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 // Copyright (c) 2017 Facebook -#include +#include #include /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ struct sched_switch_args { unsigned long long pad; - char prev_comm[16]; + char prev_comm[TASK_COMM_LEN]; int prev_pid; int prev_prio; long long prev_state; - char next_comm[16]; + char next_comm[TASK_COMM_LEN]; int next_pid; int next_prio; }; From patchwork Mon Nov 1 06:04:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595879 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7510DC433FE for ; Mon, 1 Nov 2021 06:05:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 59ED060EBC for ; Mon, 1 Nov 2021 06:05:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231920AbhKAGIO (ORCPT ); Mon, 1 Nov 2021 02:08:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230501AbhKAGHj (ORCPT ); Mon, 1 Nov 2021 02:07:39 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F17EC061220; Sun, 31 Oct 2021 23:04:53 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id gx15-20020a17090b124f00b001a695f3734aso681469pjb.0; Sun, 31 Oct 2021 23:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=LvSuBTtzWbrXgjb4RLiUJa0DKbJeUJhJDjpVybnmpmo=; b=Mons/lRRxlmoaqOVtmFZnSu86Gkf5r4H5cY0kniY9gdH+szl8upHhpaZR7MR4xQ4tw dE2pI6UqL1jzYM4vvLqKghrUPQDn79hRqU7KxiMF+5YggE6jafzi9LymBwddjrin53xA DS/vBbsVNijV75Vx09TumG5j4U1GZmxVHVKUrh58sYOCTTMBcRzBvqBQmlqQjnp9UiE6 CSWjjS2dsqFC7BDjyMH2pF3tVsU6TtlHkUNzoI8cXteZR0F4qZ685VNLWN0UoeXzxvyY 29Os19nVMJ2skD60ZUbljxErtaiTL7ovw6A0gtYiuQbZsZ494tasBFvKYe9V+eptBdn1 Gtvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=LvSuBTtzWbrXgjb4RLiUJa0DKbJeUJhJDjpVybnmpmo=; b=Bz66TtPRSWcDegBj4jP7PKAeZsZxeB7kQy8Vq1BHUwiIVeEA6XTlqbQhwpkU+kmOp5 xamIhZMa3j3/pEbg8FMu9ssTWMXsbi1qkZRCDTs1BFQgEWk+2GC9VR1QOS6qemtVBE8l B90H2Q4k8uH/jqmKDzJlUleAP42DhqZTmDb3pXVT1HMVJ4L4doXgjOJGvfNb3UlkViq5 TaFTPcvhB6OAFv5cGRrqUucgLh467TgavzWDmKCsrk0dmVLFHnuA5lxwvzgEE9OM3V/U ukBmG6BMDTmUwaAkmJ6n4LLQNhcqlODWNw4tVaT24J2P1E/56yfOZvV3ZrU8IVedbqDa Fnfg== X-Gm-Message-State: AOAM532eyZHcHrIS9rE2jNz6K+MdjkMd+8FUs09r9Yq5zKYSnEHJbX9S djXgG/VhVI5p42+OKRyUDaU= X-Google-Smtp-Source: ABdhPJx4c5pscRjAJUsqapKZZ8TRE5l4wRfA5yn4IJZjx67PGHHeSXi7SUSuCfLV4PBYO3mVczta1w== X-Received: by 2002:a17:90b:3149:: with SMTP id ip9mr33599543pjb.45.1635746692795; Sun, 31 Oct 2021 23:04:52 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:52 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 10/11] sched.h: extend task comm from 16 to 24 Date: Mon, 1 Nov 2021 06:04:18 +0000 Message-Id: <20211101060419.4682-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org When I was implementing a new per-cpu kthread cfs_migration, I found the comm of it "cfs_migration/%u" is truncated due to the limitation of TASK_COMM_LEN. For example, the comm of the percpu thread on CPU10~19 are all with the same name "cfs_migration/1", which will confuse the user. This issue is not critical, because we can get the corresponding CPU from the task's Cpus_allowed. But for kthreads correspoinding to other hardware devices, it is not easy to get the detailed device info from task comm, for example, jbd2/nvme0n1p2- xfs-reclaim/sdf We can also shorten the name to work around this problem, but I find there are so many truncated kthreads: rcu_tasks_kthre rcu_tasks_rude_ rcu_tasks_trace poll_mpt3sas0_s ext4-rsv-conver xfs-reclaim/sd{a, b, c, ...} xfs-blockgc/sd{a, b, c, ...} xfs-inodegc/sd{a, b, c, ...} audit_send_repl ecryptfs-kthrea vfio-irqfd-clea jbd2/nvme0n1p2- ... We should improve this problem fundamentally by extending comm size to 24 bytes. task_struct is growing rather regularly by 8 bytes. After this change, the truncated kthreads listed above will be displayed as: rcu_tasks_kthread rcu_tasks_rude_kthread rcu_tasks_trace_kthread poll_mpt3sas0_statu ext4-rsv-conversion xfs-reclaim/sdf1 xfs-blockgc/sdf1 xfs-inodegc/sdf1 audit_send_reply ecryptfs-kthread vfio-irqfd-cleanup jbd2/nvme0n1p2-8 As we have converted all the unsafe copy of task comm to the safe one, this change won't make any trouble to the kernel or the in-tree tools. The safe one and unsafe one of comm copy as follows, Unsafe Safe strlcpy strscpy_pad strncpy strscpy_pad bpf_probe_read_kernel bpf_probe_read_kernel_str bpf_core_read_str bpf_get_current_comm perf_event__prepare_comm prctl(2) Regarding the possible risk it may take to the out-of-tree user tools, if the user tools get the task comm through kernel API like prctl(2), bpf_get_current_comm() and etc, the tools still work well after this change. While If the user tools get the task comm through direct string copy, it must make sure the copied string should be with a nul terminator. Signed-off-by: Yafang Shao Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- include/linux/sched.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index 09ac13e54549..a8822e26653e 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -276,10 +276,11 @@ struct task_group; /* * Define the task command name length as enum, then it can be visible to - * BPF programs. + * BPF programs. The TASK_COMM_LEN_16 is kept for backward-compitability. */ enum { - TASK_COMM_LEN = 16, + TASK_COMM_LEN_16 = 16, + TASK_COMM_LEN = 24, }; extern void scheduler_tick(void); From patchwork Mon Nov 1 06:04:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 12595881 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0E7FC433F5 for ; Mon, 1 Nov 2021 06:05:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ADF9760EBC for ; Mon, 1 Nov 2021 06:05:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231926AbhKAGIP (ORCPT ); Mon, 1 Nov 2021 02:08:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231663AbhKAGHn (ORCPT ); Mon, 1 Nov 2021 02:07:43 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D3B8C06122C; Sun, 31 Oct 2021 23:04:55 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id l13so5851568pls.3; Sun, 31 Oct 2021 23:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QKwddORb13OigBCy9mDZcUnsG5h+t9yQLv8LVp5siCU=; b=j5LFKzVUzTdpQtOquEYQiQ93/QxNXDe/080ZPzqen1nnJEcnWv0Arg1m3H5YGUWw8M stUDWkjS1zFBojQqjlqwmgBfiFDvw/FY5w1DdoEUTOEo04//87rHjl76eKfDkRV6JYaD wHniweh+Bt9WBNr9zbaJOafXiibJeSYbktM2cOjCLSm/QMXoinVi4CBxQO0XAGBypciT vft62rQAmLIxRWsns2FxpBvhq592YznXCJfnsuQVHL7l548rjILVi1WcAe1E24RkOJBv 6xba3tgT8iJOTopxgjfSx7YFqWx5gxJbgzz6VMxiVUScwsGxaEWyzxex2OUaialtPu/r 8Emw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QKwddORb13OigBCy9mDZcUnsG5h+t9yQLv8LVp5siCU=; b=ejNQ3ZWHYJ19MF44lpdAswwzMjH2DL96bejXoDBGJAc0yB6p4zPTy9VxORoYptl/eq /9vgSu2bQPmJid/zehsBnikd2cp2bT23NCEQdWNwdRjjYOKp/Qinu5nlmoTIE8I+yv0e SqeZ34xks1kPdOcxHMPmSKeh1c0ccqEmY7MzcexY4yjw28vkYUcChIepqZ48NPmavZbj 3H2PAQbaeqACGG+kANVuRKRu/KQESYdVYfbI5dj1mPck5c/toMUY3itLBn9HQNPCkL91 U0guMUc42RUlVvy5PzTDkCaMz9mS8uB4dEiGatyk5IEkgBMnfKrN8QDCMuts4vW9LZ5o wLoQ== X-Gm-Message-State: AOAM532L6cDo/KB5/i9IBKqGbrfkTDURPrr5iLGtUPKy58Ek0xVOsbo6 0zbbSb4qccEH7vl3mgus5ck= X-Google-Smtp-Source: ABdhPJyrJq+mOKZ0lf2kZ6Y6bPXWfxcFbUFKsxFZDJ2CDMm20OFCcbYj+83X+q3vioiE0f2mNOqwFQ== X-Received: by 2002:a17:90a:930c:: with SMTP id p12mr36038293pjo.176.1635746695055; Sun, 31 Oct 2021 23:04:55 -0700 (PDT) Received: from localhost.localdomain ([144.202.123.152]) by smtp.gmail.com with ESMTPSA id g8sm3277586pfc.65.2021.10.31.23.04.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Oct 2021 23:04:54 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org, keescook@chromium.org, rostedt@goodmis.org, mathieu.desnoyers@efficios.com, arnaldo.melo@gmail.com, pmladek@suse.com, peterz@infradead.org, viro@zeniv.linux.org.uk, valentin.schneider@arm.com, qiang.zhang@windriver.com, robdclark@chromium.org, christian@brauner.io, dietmar.eggemann@arm.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, davem@davemloft.net, kuba@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, dennis.dalessandro@cornelisnetworks.com, mike.marciniszyn@cornelisnetworks.com, dledford@redhat.com, jgg@ziepe.ca Cc: linux-rdma@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, oliver.sang@intel.com, lkp@intel.com, Yafang Shao , Alexei Starovoitov , Andrii Nakryiko Subject: [PATCH v7 11/11] kernel/kthread: show a warning if kthread's comm is truncated Date: Mon, 1 Nov 2021 06:04:19 +0000 Message-Id: <20211101060419.4682-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20211101060419.4682-1-laoar.shao@gmail.com> References: <20211101060419.4682-1-laoar.shao@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org Show a warning if task comm is truncated. Below is the result of my test case: truncated kthread comm:I-am-a-kthread-with-a-l (pid:178) by 8 characters As we have extended task comm to 24, all the existing in-tree kthreads/workqueues are not truncated anymore. So this warning will only be printed for the newly introduced one if it has a long name. Suggested-by: Petr Mladek Signed-off-by: Yafang Shao Reviewed-by: Kees Cook Cc: Mathieu Desnoyers Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: Andrii Nakryiko Cc: Peter Zijlstra Cc: Steven Rostedt Cc: Al Viro Cc: Kees Cook Cc: Petr Mladek --- kernel/kthread.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/kthread.c b/kernel/kthread.c index 5b37a8567168..63f38d3a4f62 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -399,12 +399,17 @@ struct task_struct *__kthread_create_on_node(int (*threadfn)(void *data), if (!IS_ERR(task)) { static const struct sched_param param = { .sched_priority = 0 }; char name[TASK_COMM_LEN]; + int len; /* * task is already visible to other tasks, so updating * COMM must be protected. */ - vsnprintf(name, sizeof(name), namefmt, args); + len = vsnprintf(name, sizeof(name), namefmt, args); + if (len >= TASK_COMM_LEN) { + pr_warn("truncated kthread comm:%s (pid:%d) by %d characters\n", + name, task->pid, len - TASK_COMM_LEN + 1); + } set_task_comm(task, name); /* * root may have changed our (kthreadd's) priority or CPU mask.