Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9119664pxu; Mon, 28 Dec 2020 07:07:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJx5L6XQiwda2ci1UT6tTRCTBEVHIMhnAOmbpYEziCwmg92p41kP030+Hu6F+wJQPStSeH9M X-Received: by 2002:a05:6402:104e:: with SMTP id e14mr43215340edu.316.1609168047421; Mon, 28 Dec 2020 07:07:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609168047; cv=none; d=google.com; s=arc-20160816; b=Mo0+fn7BhVeDaE10QcN/DnOtfSxHuWaJncwwDbOBbDQoZQhWgqu49egwc1xEcf6XNG KjUvA0/+2InooDrvndtYoVfvClMV1K4k1xgYtGSOwLQgTE4zpRj2uoXKpZ6B1XDqkN6+ fvrjlYFRrgX5+S7GUq62oQ8UOwrGgK5F6m+k2LuKi2hGjgKPoaWocijCussae8xiPqJT FE5rsPayijM1bcS8UtCitZL3BIxK/wWKteMjDeBIWH7MnsyDf7yUVll75itLblhZvDoR WJWMALE2uDjD7PBO96QVK0boNEFkgU8iDAKrK0LOPESWALPBMM4DCdJJODW1bfyPy3hA DsdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=660sEowX8NUl1TY2PSGi855PhDC79tS6p6oPbmXtfW8=; b=oumtjVWK18PMhsgtEU9/f8pm5V0HrFWR106D5h7zdrEs9HxJrgrG+R6MDymwYnM2ja mT42BEKOHhk/LZ68m3XqqSR6t14TDH7lR87TqBqSuW7ar6zfFQoXJpyxXYziWrz0tbWg 8nQN4uU+oBP0SAEE+QyKSOoU5RIXGumfF+JEO+s2VLVviqLgFm7qT7AeFuySQhNz+sq/ 8Ik40mEYOEnXf03pB4harOwj75KU870OQ0of3wUdPsorFjxQwX8ENZEXH5WesO0WOBjR h7wQHyodPAG8QqgWgX+qSbKiOwZd3hPOhw/DtYakykbo6Y9G4+QO9hftnRLPFInNqMtM oMGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=fBVUWIf8; 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 v21si19777055ejg.492.2020.12.28.07.06.59; Mon, 28 Dec 2020 07:07:27 -0800 (PST) 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=20161025 header.b=fBVUWIf8; 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 S2439589AbgL1OLT (ORCPT + 99 others); Mon, 28 Dec 2020 09:11:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2439164AbgL1OKz (ORCPT ); Mon, 28 Dec 2020 09:10:55 -0500 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78DB3C061798 for ; Mon, 28 Dec 2020 06:10:15 -0800 (PST) Received: by mail-qk1-x736.google.com with SMTP id 19so8817984qkm.8 for ; Mon, 28 Dec 2020 06:10:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=660sEowX8NUl1TY2PSGi855PhDC79tS6p6oPbmXtfW8=; b=fBVUWIf8P3+k3u5W4NjqjSEmtkBfy31xsHGz2+wwYB3OWCmY1NJDqzYjnpwg9UcXn2 1Gj5NdEfnTDuxNPVFSvl3Ky0U0XBMi5DYNgVfAi4ZmICQLoZ18soaqGPUhSz3VXzOPGR jrRAlisAAj+pPA8j4CjlWTqKV54VDj587pMPWQh1InG9qoYXvBgrGCHz1nTN69FWiX1N zMDzZbvABPFD14qXk/J6+Qe1g0cIrDf8knaxw5sfC1CWPoDiYWL31tFF+J653midM54S 4HCEnVmBG1iQ4KthlhHyfHXT0CQFRMRinVb3UE54jqIzZnJlmHVtn+oSLiUp23Q8+JO7 a86Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=660sEowX8NUl1TY2PSGi855PhDC79tS6p6oPbmXtfW8=; b=Wqr0exUhNOjGtNdAzoP+N1AlpdM1oYo0l1Jr8RABp2ltyfaZRazucx/XbCYErHUBZt RLlTC/AZITib+z6gHoAsmzsHTetlJbXUMJYCMEsGgXKsLt/4XEKpL6iUX/4KTwZ28OkT 8jXSa5E1VPOgi3j5Foa6J9uB4RZN3mBJdyPuyq5ej20G9NnsbNnV6/N93QDYmUQp0NGi 9oettza5w7ovKaNXllsU/Zrv/XkpvMabQkvu3lOEKpEJbVdtNQjszJwYR7oCtXtrWp/O n8e0Eyv1YIRZNX6BNmZkBl/0ghlxU+LR1taG9i8KLSi91igWpdKloUO03mEbgCskDkGf gahg== X-Gm-Message-State: AOAM532fF/511Bl7ASo4aZ+8EF87o/KMvCyIRtUBdMKat39fHDREfphC 2edg6Snu5arhF4eFaeFp/453o8Y0emkm+IYFxtQ= X-Received: by 2002:ae9:f312:: with SMTP id p18mr43728735qkg.311.1609164614671; Mon, 28 Dec 2020 06:10:14 -0800 (PST) MIME-Version: 1.0 References: <20201217170009.GA29186@192.168.3.9> In-Reply-To: <20201217170009.GA29186@192.168.3.9> From: Weiping Zhang Date: Mon, 28 Dec 2020 22:10:03 +0800 Message-ID: Subject: Re: [RFC PATCH v2] taskstats: add /proc/taskstats to fetch pid/tgid status To: davem@davemloft.net, bsingharora@gmail.com, Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi David, Could you help review this patch ? thanks On Fri, Dec 18, 2020 at 1:24 AM Weiping Zhang wrote: > > If a program needs monitor lots of process's status, it needs two > syscalls for every process. The first one is telling kernel which > pid/tgid should be monitored by send a command(write socket) to kernel. > The second one is read the statistics by read socket. This patch add > a new interface /proc/taskstats to reduce two syscalls to one ioctl. > The user just set the target pid/tgid to the struct taskstats.ac_pid, > then kernel will collect statistics for that pid/tgid. > > Signed-off-by: Weiping Zhang > --- > Changes since v1: > * use /proc/taskstats instead of /dev/taskstats > > include/uapi/linux/taskstats.h | 5 +++ > kernel/taskstats.c | 73 +++++++++++++++++++++++++++++++++- > 2 files changed, 77 insertions(+), 1 deletion(-) > > diff --git a/include/uapi/linux/taskstats.h b/include/uapi/linux/taskstats.h > index ccbd08709321..077eab84c77e 100644 > --- a/include/uapi/linux/taskstats.h > +++ b/include/uapi/linux/taskstats.h > @@ -214,6 +214,11 @@ enum { > > #define TASKSTATS_CMD_ATTR_MAX (__TASKSTATS_CMD_ATTR_MAX - 1) > > +/* ioctl command */ > +#define TASKSTATS_IOC_ATTR_PID _IO('T', TASKSTATS_CMD_ATTR_PID) > +#define TASKSTATS_IOC_ATTR_TGID _IO('T', TASKSTATS_CMD_ATTR_TGID) > + > + > /* NETLINK_GENERIC related info */ > > #define TASKSTATS_GENL_NAME "TASKSTATS" > diff --git a/kernel/taskstats.c b/kernel/taskstats.c > index a2802b6ff4bb..c0f9e2f2308b 100644 > --- a/kernel/taskstats.c > +++ b/kernel/taskstats.c > @@ -21,6 +21,8 @@ > #include > #include > #include > +#include > +#include > > /* > * Maximum length of a cpumask that can be specified in > @@ -28,6 +30,10 @@ > */ > #define TASKSTATS_CPUMASK_MAXLEN (100+6*NR_CPUS) > > +#ifdef CONFIG_PROC_FS > +#define PROC_TASKSTATS "taskstats" > +#endif > + > static DEFINE_PER_CPU(__u32, taskstats_seqnum); > static int family_registered; > struct kmem_cache *taskstats_cache; > @@ -666,6 +672,60 @@ static struct genl_family family __ro_after_init = { > .n_ops = ARRAY_SIZE(taskstats_ops), > }; > > +#ifdef CONFIG_PROC_FS > +static long taskstats_ioctl_pid_tgid(unsigned long arg, bool tgid) > +{ > + struct taskstats kstat; > + struct taskstats *ustat = (struct taskstats *)arg; > + u32 id; > + unsigned long offset = offsetof(struct taskstats, ac_pid); > + long ret; > + > + /* userspace set monitored pid/tgid to the field "ac_pid" */ > + if (unlikely(copy_from_user(&id, (void *)(arg + offset), sizeof(u32)))) > + return -EFAULT; > + > + if (tgid) > + ret = fill_stats_for_tgid(id, &kstat); > + else > + ret = fill_stats_for_pid(id, &kstat); > + if (ret < 0) > + return ret; > + > + if (unlikely(copy_to_user(ustat, &kstat, sizeof(struct taskstats)))) > + return -EFAULT; > + > + return 0; > +} > + > +static long taskstats_ioctl(struct file *file, unsigned int cmd, unsigned long arg) > +{ > + long ret; > + > + switch (cmd) { > + case TASKSTATS_IOC_ATTR_PID: > + ret = taskstats_ioctl_pid_tgid(arg, false); > + break; > + case TASKSTATS_IOC_ATTR_TGID: > + ret = taskstats_ioctl_pid_tgid(arg, true); > + break; > + default: > + ret = -EINVAL; > + break; > + } > + > + return ret; > +} > + > +static const struct proc_ops taskstats_proc_ops = { > + .proc_ioctl = taskstats_ioctl, > +#ifdef CONFIG_COMPAT > + .proc_compat_ioctl = taskstats_ioctl, > +#endif > +}; > +#endif > + > + > /* Needed early in initialization */ > void __init taskstats_init_early(void) > { > @@ -682,9 +742,20 @@ static int __init taskstats_init(void) > { > int rc; > > +#ifdef CONFIG_PROC_FS > + if (!proc_create(PROC_TASKSTATS, 0, NULL, &taskstats_proc_ops)) { > + pr_err("failed to create /proc/%s\n", PROC_TASKSTATS); > + return -1; > + } > +#endif > + > rc = genl_register_family(&family); > - if (rc) > + if (rc) { > +#ifdef CONFIG_PROC_FS > + remove_proc_entry(PROC_TASKSTATS, NULL); > +#endif > return rc; > + } > > family_registered = 1; > pr_info("registered taskstats version %d\n", TASKSTATS_GENL_VERSION); > -- > 2.17.2 >