From patchwork Mon Oct 7 14:49:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13824825 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 DD7A6CFB43F for ; Mon, 7 Oct 2024 14:49:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72AA76B0098; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B4596B0099; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 52E126B009A; Mon, 7 Oct 2024 10:49:49 -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 30A4F6B0098 for ; Mon, 7 Oct 2024 10:49:49 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E27AE1A1787 for ; Mon, 7 Oct 2024 14:49:48 +0000 (UTC) X-FDA: 82647090456.24.8A68F89 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf22.hostedemail.com (Postfix) with ESMTP id 0C0E1C0006 for ; Mon, 7 Oct 2024 14:49:45 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H0wV0mXH; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728312451; 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=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=wbXQhrw0vdqKcKqSRLcPerrWpfhRW4zVDe8LFsWXJi93p0WhtQYl/GGo9c6odpULPJIGoo 1+nf8sAPAAV+aLl3Ngs1HPvkCuSk+fZqHDGzAZX+ZApidWaBivM+AqRUc0HkmrYpqii8LS PjsnFxxalybBRuula6J9UZcHOkcOPXk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728312451; a=rsa-sha256; cv=none; b=7jzAHo76iC7RHrIyLPu8YXWE267cZQ3QOdD/hacJ20y6h8WaPv3YZdiiwr1ybRCIU4Sm3h LFajuCsALiPdMoapfYjKVhyX6XaXefJkdFBalPOyed0Z50tmXREpVjl0lqTZK04yHUoMNn sDkrd8CIPQ2D5axaBPQ1dNs/t3xrzk8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=H0wV0mXH; spf=pass (imf22.hostedemail.com: domain of laoar.shao@gmail.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=laoar.shao@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71de9e1f431so2264180b3a.3 for ; Mon, 07 Oct 2024 07:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728312585; x=1728917385; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=H0wV0mXHUeqRmFbYSLyU1/xdTCOe/EVqoamvRCuIzthsogIEW5wxhDOQoiVMkTLvjO evdlGVZb0qAFLNGFm6O8j6veanOlsnH7zbQf5cDy9z7QJq/w2/W9VoskWbFKIGe9CSwS YwSBxLmgvSc/6Jp6FXU0aCn0OZXbHdgTU7IllQNMmlrXOGxWxmgmSI7YncPzQ5gIxER7 x6LVBLI1DvadWdFoURZ+Hrzz+HgD10zQpV3jowOvC9qRygrs8ekYgRePG3jGFVpNeUB+ YU/0Jm9YOdguNJNtKBav7bAGh4HzeYJe9OBaRAoE3bNYUCB4ln5E0ENcfrjaDicGuOOr 3oag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728312585; x=1728917385; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oVEk6SmaURyo42RvRGYBr53delaAcBEjfSHuuD+Cst4=; b=eZfTNUc5avymhPC6NyQz3/DJ97s3dFMtTAJ8x18erNaAYpO27ogZpTfGCrhxBhASeb 0xf5bL0jhIRGG7+FnbdvQ0KfAodsjMfQ3g3q7QAe0y1IQ5k07lRUXc6ypOjivVq7I2vy Jy5QeNEWbAE/nfwnhj/cC/P1u08c7FWKeHmJhk5oMLM6quumjSJoUX5yhfmAtb9gZZ9k UdNTXBXpCKknnIcicTvILdaPHskmSXSVX5s8QehOuEt4o8o207rfp1TaVxqpVj99qdV3 9rhDZ8rNpqHYPOoF0EJAFOmjlHS0FZhFmQBK0rIiVrvptcL2GAln2KFzzTos0YNNb7bc QN4Q== X-Forwarded-Encrypted: i=1; AJvYcCWbUz0eR5Bn4YwtARDklA4BrsocmfIOrZAZtCuF6bTrtZ52u0NKkHV3SWEh9dmzAqBXMxL/5W1hDg==@kvack.org X-Gm-Message-State: AOJu0YzONYFxA47DKMQahK66/PvTocFFs/pyZLP9t1k3IkcKWTKbmaIX 7UnabDolE5d5V4aT/H+rCtcd7SlfG9liL5trpM57xXXruJGCiEZX X-Google-Smtp-Source: AGHT+IFysx44ym7xeNZ29ZQx56P8LK073YLR3FEq6ZYjPgOvQStiRImQUgMUmAdR1iJQTcm5/XW64g== X-Received: by 2002:a05:6a00:2eaa:b0:718:e162:7374 with SMTP id d2e1a72fcca58-71de239f5e6mr18318586b3a.5.1728312584814; Mon, 07 Oct 2024 07:49:44 -0700 (PDT) Received: from localhost.localdomain ([223.104.210.43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0d7cf82sm4466432b3a.200.2024.10.07.07.49.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Oct 2024 07:49:44 -0700 (PDT) From: Yafang Shao To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, keescook@chromium.org, alx@kernel.org, justinstitt@google.com, ebiederm@xmission.com, alexei.starovoitov@gmail.com, rostedt@goodmis.org, catalin.marinas@arm.com, penguin-kernel@i-love.sakura.ne.jp, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, audit@vger.kernel.org, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, dri-devel@lists.freedesktop.org, Yafang Shao , Alexander Viro , Christian Brauner , Jan Kara , Matus Jokay , "Serge E. Hallyn" Subject: [PATCH v9 1/7] Get rid of __get_task_comm() Date: Mon, 7 Oct 2024 22:49:05 +0800 Message-Id: <20241007144911.27693-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20241007144911.27693-1-laoar.shao@gmail.com> References: <20241007144911.27693-1-laoar.shao@gmail.com> MIME-Version: 1.0 X-Stat-Signature: zeydo3j9fht131hwce3yocpxq64n9euu X-Rspamd-Queue-Id: 0C0E1C0006 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1728312585-389873 X-HE-Meta: U2FsdGVkX1/qsn5P4vY0PdBHEI6nfv94QtToD8101f18NsxL4HSDs0N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h4u8yskULHzwhHyfq1BYb7dCGQSJKSmiELykBcYBvrzBZPR1OF4sUOtATGPB144x3cnsBKpaHWx6A7AEvSAugObs/ICq4HAJP99g/3GypCzGa5My2ZIIpSp5gS1D+20H3EnSR2Fzy/eoaJ4MrgIp3GwO7NzghQHW0/hVvRjRIoZ8GfbzcbjOyPR2hO+cc80VaFecS78Pg61gM2s81qQdg/H00v1qhFxiaNlFIPnoEp98RCk+5wGRP4tTJ5ChT/eWEIfwAZ6bjf9if3r39hiZ5Q5bLq6mx7zADrdFaN4Y+pxl7h1o11xYjCkg/XRe2ZfZuHL8XpZQ7BL+nyb85WluJmoBgmjQUbFP7NLEsmhIxkUZUUuGQToWlTTTYI+V5dcxUiyOnK+Dz04u6gG3/6CMoSUiHVPGmsTaXvwABO6qT7etmcMlHh5BPDDub4q4egadDQZeZ14youcAqOzWRNGaLz4SSUAau4M4aQCqwciS8CFILAwXczTNiFPH6r7piiL8OmzHO+4qXbtIGjWBmyPT3lBBzyAHUuY291QECgPPVdN8WW8wmMw0cWdb60BLiGDPzkv6N2cty9i7U4uyVuB0GXzs9jorMQABfwfIDYrDa2Rac31gg9D1gsB1STiivgHL5oICyU2U3tn8H4= 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: List-Subscribe: List-Unsubscribe: We want to eliminate the use of __get_task_comm() for the following reasons: - The task_lock() is unnecessary Quoted from Linus [0]: : Since user space can randomly change their names anyway, using locking : was always wrong for readers (for writers it probably does make sense : to have some lock - although practically speaking nobody cares there : either, but at least for a writer some kind of race could have : long-term mixed results Suggested-by: Linus Torvalds Link: https://lore.kernel.org/all/CAHk-=wivfrF0_zvf+oj6==Sh=-npJooP8chLPEfaFV0oNYTTBA@mail.gmail.com [0] Link: https://lore.kernel.org/all/CAHk-=whWtUC-AjmGJveAETKOMeMFSTwKwu99v7+b6AyHMmaDFA@mail.gmail.com/ Signed-off-by: Yafang Shao Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: Kees Cook Cc: Alexei Starovoitov Cc: Matus Jokay Cc: Alejandro Colomar Cc: "Serge E. Hallyn" --- fs/exec.c | 10 ---------- fs/proc/array.c | 2 +- include/linux/sched.h | 28 ++++++++++++++++++++++------ kernel/kthread.c | 2 +- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 6c53920795c2..77364806b48d 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1189,16 +1189,6 @@ static int unshare_sighand(struct task_struct *me) return 0; } -char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk) -{ - task_lock(tsk); - /* Always NUL terminated and zero-padded */ - strscpy_pad(buf, tsk->comm, buf_size); - task_unlock(tsk); - return buf; -} -EXPORT_SYMBOL_GPL(__get_task_comm); - /* * These functions flushes out all traces of the currently running executable * so that a new one can be started diff --git a/fs/proc/array.c b/fs/proc/array.c index 34a47fb0c57f..55ed3510d2bb 100644 --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -109,7 +109,7 @@ void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) else if (p->flags & PF_KTHREAD) get_kthread_comm(tcomm, sizeof(tcomm), p); else - __get_task_comm(tcomm, sizeof(tcomm), p); + get_task_comm(tcomm, p); if (escape) seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); diff --git a/include/linux/sched.h b/include/linux/sched.h index e6ee4258169a..28f92c637abf 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1121,9 +1121,12 @@ struct task_struct { /* * executable name, excluding path. * - * - normally initialized setup_new_exec() - * - access it with [gs]et_task_comm() - * - lock it with task_lock() + * - normally initialized begin_new_exec() + * - set it with set_task_comm() + * - strscpy_pad() to ensure it is always NUL-terminated and + * zero-padded + * - task_lock() to ensure the operation is atomic and the name is + * fully updated. */ char comm[TASK_COMM_LEN]; @@ -1938,10 +1941,23 @@ static inline void set_task_comm(struct task_struct *tsk, const char *from) __set_task_comm(tsk, from, false); } -extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk); +/* + * - Why not use task_lock()? + * User space can randomly change their names anyway, so locking for readers + * doesn't make sense. For writers, locking is probably necessary, as a race + * condition could lead to long-term mixed results. + * The strscpy_pad() in __set_task_comm() can ensure that the task comm is + * always NUL-terminated and zero-padded. Therefore the race condition between + * reader and writer is not an issue. + * + * - BUILD_BUG_ON() can help prevent the buf from being truncated. + * Since the callers don't perform any return value checks, this safeguard is + * necessary. + */ #define get_task_comm(buf, tsk) ({ \ - BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ - __get_task_comm(buf, sizeof(buf), tsk); \ + BUILD_BUG_ON(sizeof(buf) < TASK_COMM_LEN); \ + strscpy_pad(buf, (tsk)->comm); \ + buf; \ }) #ifdef CONFIG_SMP diff --git a/kernel/kthread.c b/kernel/kthread.c index db4ceb0f503c..74d20f46fa30 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -101,7 +101,7 @@ void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk) struct kthread *kthread = to_kthread(tsk); if (!kthread || !kthread->full_name) { - __get_task_comm(buf, buf_size, tsk); + strscpy(buf, tsk->comm, buf_size); return; }