Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp745573pxb; Wed, 29 Sep 2021 08:51:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsgOlBs47AxK0gr7Na4xA3slNW98yyZF+DR1wQDxt7kpv/Aax7LLptDyHHAeXejaMJCFZJ X-Received: by 2002:a17:906:7f01:: with SMTP id d1mr512592ejr.318.1632930700707; Wed, 29 Sep 2021 08:51:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632930700; cv=none; d=google.com; s=arc-20160816; b=s3+AChzdRzFYE/DtXUZG5KGkMFB/BDcES+lWPrY6LBgC8Lhpv0f/hsTZ3mGu6RePdY b9X04g8fBVdxw2izC+82vZyaKg06SiOhzkkmX8sL5WAe3JTOi8KOxpyjmBWzq2CmNN+7 S45dOBXXli2lx3yG1t7R1f9zeS5bZqG2/14rLArzo8w7x1nGNYRS8L1EB3Tr+nhDCK3a TXLOq8LJhpo82Moe18TZyOh5s3btgcrzPet6XYgUUrA/gJVHgONRrvPGgesV7EXhlAb0 RpjGpzh1iSq30KqAyFbSQAK7cYiShmyedBZvFDTlWFnEN/Y1wa/f/pVKoQDT8jH2bVlr DWHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=XkEDSADHBvfevoxljJ4T1PZQMMJ1MoZGL9A5io5n/G4=; b=qG42dRHHTl3AdFdvYhrty5+jTh3a2DXDuYxdiEVZex7Iyh45aAWIcRD/mLC6MVqBxw E2BPn4OtjriwDAWQt5KDp53pgKTE4PyJ2uGsbU7mg6NgKzYaDuoObGDk4E0NhD27BlwE EsPDxZ7IOhCmU+BjzHDjk3/N+ByPT+shaeKqC+ToYfgJgrEpacKKKtfH8YeF8plWiK0R YglVP8Xz5HN+N5zEey8I0rShoTF3SLIpTHwGuDNj8WKV6mbFSQQKEZk4RJcJsARt1k7h MBYRk41tVETQeiuhhM6CAgkFJdkhLJftkiM81/8kvYQ3SEWGV1L9BBtfK+xUdsdCAAkG GeRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=l65l906O; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x16si492844edd.121.2021.09.29.08.51.15; Wed, 29 Sep 2021 08:51:40 -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=@gmail.com header.s=20210112 header.b=l65l906O; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344060AbhI2NLI (ORCPT + 99 others); Wed, 29 Sep 2021 09:11:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344040AbhI2NLF (ORCPT ); Wed, 29 Sep 2021 09:11:05 -0400 Received: from mail-io1-xd33.google.com (mail-io1-xd33.google.com [IPv6:2607:f8b0:4864:20::d33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD7A1C06161C for ; Wed, 29 Sep 2021 06:09:23 -0700 (PDT) Received: by mail-io1-xd33.google.com with SMTP id s20so3010542ioa.4 for ; Wed, 29 Sep 2021 06:09:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=XkEDSADHBvfevoxljJ4T1PZQMMJ1MoZGL9A5io5n/G4=; b=l65l906OE79blcJYbrneuky2sOG6RNx0kOJ44MNaSCkQhaBp0BrjIQsfK4cSY99FTz T/HvPXfhqpX5SDliwK1g0ge2M+XBiQ+rFsofcM2jrxF//j6T69DYNf1AiQF8f86tzjSC w1K81Bdpet7IrVmc9CzrhehQFXuRcr6zGckCskXQ0IIYm5P/sFQHQHf4UQbScFpyeue5 H56ptcHwCkcuZ9uSjU0R1o12+rmmlGW3dAIwFsoyFM/QDaG7keH9e+OBanGPP1zghnFT IkJHgGDZ5XhC4S2phbNquiS+Nr2SD9MAD19SkzM9f/fNU+r3g/8X3xIBrZAKTKhcxGa+ pTWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=XkEDSADHBvfevoxljJ4T1PZQMMJ1MoZGL9A5io5n/G4=; b=SjKwGsis0RoQzhi8YI+QWf8Nfblo3MgK+B624KGLubba5bPDKjMIqwTbKo199I9z6J c5uOQo9j6O0A6isolRuzVTcffTx2NFN1dFmGvyNq6E9AwVVFGnDUFhB4EhNEOIzCpVZT Ma3AsAY4YiBhqWEyKwyQKvOn4UJGAZzQcrJfKQWDeIWEVO/x4sRJtrePSR8opHMsNq1J UxfJuN24hOY+0cEIXzo8vkyKuwkgPHcQvwit/aA69+hOoLqqgrtBtKy0T+5YXNq8UTnC WMrfS7mTU1i3LqZzKdZMSG2+pVvE76rtWb0Jd/mrapHXndyOxykc84noPIa56CGgrdAH vshw== X-Gm-Message-State: AOAM5330mpMgSQVJjLDmJOKcKcLAHrL8my8/ps+cgwHE4Z7Se5eCeO2/ cnRKqu/c/9LwwfCmZ1PZTC2iznUx1xBIPE44L5k= X-Received: by 2002:a05:6638:2722:: with SMTP id m34mr9515111jav.49.1632920963356; Wed, 29 Sep 2021 06:09:23 -0700 (PDT) MIME-Version: 1.0 References: <20210929115036.4851-1-laoar.shao@gmail.com> <20210929115036.4851-3-laoar.shao@gmail.com> In-Reply-To: <20210929115036.4851-3-laoar.shao@gmail.com> From: Yafang Shao Date: Wed, 29 Sep 2021 21:08:47 +0800 Message-ID: Subject: Re: [PATCH 2/5] kernel/fork: allocate task->comm dynamicly To: Andrew Morton , Petr Mladek , Peter Zijlstra , Valentin Schneider , Kees Cook , Mathieu Desnoyers , qiang.zhang@windriver.com, robdclark@chromium.org, Al Viro , christian@brauner.io, Dietmar Eggemann Cc: LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 29, 2021 at 7:51 PM Yafang Shao wrote: > > task->comm is defined as an array embedded in struct task_struct before. > This patch changes it to a char pointer. It will be allocated in the fork > and freed when the task is freed. > > Signed-off-by: Yafang Shao > --- > include/linux/sched.h | 2 +- > kernel/fork.c | 19 +++++++++++++++++++ > 2 files changed, 20 insertions(+), 1 deletion(-) > > diff --git a/include/linux/sched.h b/include/linux/sched.h > index e12b524426b0..b387b5943db4 100644 > --- a/include/linux/sched.h > +++ b/include/linux/sched.h > @@ -1051,7 +1051,7 @@ struct task_struct { > * - access it with [gs]et_task_comm() > * - lock it with task_lock() > */ > - char comm[TASK_COMM_LEN]; > + char *comm; > > struct nameidata *nameidata; > > diff --git a/kernel/fork.c b/kernel/fork.c > index 38681ad44c76..227aec240501 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -721,6 +721,20 @@ static void mmdrop_async(struct mm_struct *mm) > } > } > > +static int task_comm_alloc(struct task_struct *p) > +{ > + p->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL); > + if (!p->comm) > + return -ENOMEM; > + > + return 0; > +} > + > +static void task_comm_free(struct task_struct *p) > +{ > + kfree(p->comm); > +} > + > static inline void free_signal_struct(struct signal_struct *sig) > { > taskstats_tgid_free(sig); > @@ -753,6 +767,7 @@ void __put_task_struct(struct task_struct *tsk) > bpf_task_storage_free(tsk); > exit_creds(tsk); > delayacct_tsk_free(tsk); > + task_comm_free(tsk); > put_signal_struct(tsk->signal); > sched_core_free(tsk); > > @@ -2076,6 +2091,10 @@ static __latent_entropy struct task_struct *copy_process( > if (data_race(nr_threads >= max_threads)) > goto bad_fork_cleanup_count; > > + retval = task_comm_alloc(p); > + if (retval) > + goto bad_fork_cleanup_count; > + > delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ > p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY); > p->flags |= PF_FORKNOEXEC; > -- > 2.17.1 > After sending the series, I find that I forget to move the task_comm_alloc() to the end of fork(). Below is the updated one, --- include/linux/sched.h | 2 +- kernel/fork.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index e12b524426b0..b387b5943db4 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1051,7 +1051,7 @@ struct task_struct { * - access it with [gs]et_task_comm() * - lock it with task_lock() */ - char comm[TASK_COMM_LEN]; + char *comm; struct nameidata *nameidata; diff --git a/kernel/fork.c b/kernel/fork.c index 38681ad44c76..d1e0c38464ed 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -721,6 +721,20 @@ static void mmdrop_async(struct mm_struct *mm) } } +static int task_comm_alloc(struct task_struct *p) +{ + p->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL); + if (!p->comm) + return -ENOMEM; + + return 0; +} + +static void task_comm_free(struct task_struct *p) +{ + kfree(p->comm); +} + static inline void free_signal_struct(struct signal_struct *sig) { taskstats_tgid_free(sig); @@ -753,6 +767,7 @@ void __put_task_struct(struct task_struct *tsk) bpf_task_storage_free(tsk); exit_creds(tsk); delayacct_tsk_free(tsk); + task_comm_free(tsk); put_signal_struct(tsk->signal); sched_core_free(tsk); @@ -2352,6 +2367,10 @@ static __latent_entropy struct task_struct *copy_process( goto bad_fork_cancel_cgroup; } + retval = task_comm_alloc(p); + if (retval) + goto bad_fork_cancel_cgroup; + /* past the last point of failure */ if (pidfile) fd_install(pidfd, pidfile); -- Thanks Yafang