From nobody Wed Nov 27 07:52:59 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B9B918E20; Sun, 13 Oct 2024 18:18:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.165.32 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728843540; cv=none; b=SGtBqnCFhu9Iwg0UfcAHJHG0MvBlROmqweANqPezEAeASx0JWXkGqCbUwUOsTuKw9iR1RPxcfXmv5rpop1TQ938iGLoKhN4/FtjwqcgHSD7pyRT4vkdE99ymHxcVm2vNKt+tFJ8ILbUfTNORiqXs614uqKEWAHycuzc8rwxMmtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728843540; c=relaxed/simple; bh=WKgSRZF8U/lEEfgudbmnwFpjS1QLnZzRS1IJzoc0CrU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gOCcjx2ho0ED543Ar0amxckSNF1W2l5Eq9F0FUG9d2D+bcbOg9Xj7DoZ8snS+bVkYhl8fqCPI3JgV2wU+dEvBSxsfyNSlsYUy1rFuu6YpNyTVs9X+upPo2JC9NOXnsZf4eOT0p0uBPvdp6x12XUhWms7MK8YP/Gjw+lZ9YTKLUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=k+N4NwDZ; arc=none smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="k+N4NwDZ" Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49DFNuDL010082; Sun, 13 Oct 2024 17:06:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=hrCoF lgvo/STp2VEv7cUaEajniYQSt7LvflbN+xj+Ss=; b=k+N4NwDZEyysXTFDnXQ3g 5LFijXNtKvvr/Oz4FtXA3KTXIL7OIKJw4uRSbiXrZbHZWBtSZAZ2qk7Sn4EXG0sK m1Z8ug79m30NrYKIltWspH56UDHJuKJMoFyuDdj+fpoRHRBBpmBSF8OItdoXXpE2 bUK6kcHF482MG/p1na/pFSkA1DfdayvYiSfJTeYdCnQ58A0UocMAxKRHTZjoXpPU 1SrlYLW+I2Vvg6OZTi/oG8johPafB+U0esr5pYO9WXq/DLJnqaZQf780z/QZr4td FjEV/ycfcrJpuOwLifJClRVT22n7M6oybni9pAkdWkIunea1nCX8hZskhrKlFgxE Q== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427gq79ahm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 13 Oct 2024 17:06:27 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49DB3eFY026283; Sun, 13 Oct 2024 17:06:26 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj561dm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 13 Oct 2024 17:06:26 +0000 Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 49DH6MFA040166; Sun, 13 Oct 2024 17:06:25 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 427fj561c0-2; Sun, 13 Oct 2024 17:06:25 +0000 From: Anjali Kulkarni To: davem@davemloft.net, Liam.Howlett@Oracle.com Cc: edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, jiri@resnulli.us, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, akpm@linux-foundation.org, shuah@kernel.org, linux-kselftest@vger.kernel.org, anjali.k.kulkarni@oracle.com, peili.io@oracle.com Subject: [PATCH net-next v1 1/3] connector/cn_proc: Add hash table for threads Date: Sun, 13 Oct 2024 10:06:15 -0700 Message-ID: <20241013170617.2139204-2-anjali.k.kulkarni@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241013170617.2139204-1-anjali.k.kulkarni@oracle.com> References: <20241013170617.2139204-1-anjali.k.kulkarni@oracle.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-13_11,2024-10-11_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 adultscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410130128 X-Proofpoint-ORIG-GUID: Y-5PaFBOD-4Vo56mwUxOJleYK6meMjJz X-Proofpoint-GUID: Y-5PaFBOD-4Vo56mwUxOJleYK6meMjJz Content-Type: text/plain; charset="utf-8" Add a new type PROC_CN_MCAST_NOTIFY to proc connector API, which allows a thread to notify the kernel that is going to exit with a non-zero exit code and specify the exit code in it. When thread exits in the kernel, it will send this exit code as a proc filter notification to any listening process. Exiting thread can call this either when it wants to call pthread_exit() with non-zero value or from signal handler. Add a new file cn_hash.c which implements a hash table storing the exit codes of abnormally exiting threads, received by the system call above. The key used for the hash table is the pid of the thread, so when the thread actually exits, we lookup it's pid in the hash table and retrieve the exit code sent by user. If the exit code in struct task is 0, we then replace it with the user supplied non-zero exit code. cn_hash.c implements the hash table add, delete, lookup operations. mutex_lock() and mutex_unlock() operations are used to safeguard the integrity of the hash table while adding or deleting elements. connector.c has the API calls, called from cn_proc.c, as well as calls to allocate, initialize and free the hash table. Add a new flag in PF_* flags of task_struct - EXIT_NOTIFY. This flag is set when user sends the exit code via PROC_CN_MCAST_NOTIFY. While exiting, this flag is checked and the hash table add or delete calls are only made if this flag is set. A refcount field hrefcnt is added in struct cn_hash_dev, to keep track of number of threads which have added an entry in hash table. Before freeing the struct cn_hash_dev, this value must be 0. Signed-off-by: Anjali Kulkarni --- drivers/connector/Makefile | 2 +- drivers/connector/cn_hash.c | 195 ++++++++++++++++++++++++++++++++++ drivers/connector/cn_proc.c | 58 +++++++++- drivers/connector/connector.c | 83 ++++++++++++++- include/linux/connector.h | 43 ++++++++ include/linux/sched.h | 2 +- include/uapi/linux/cn_proc.h | 4 +- 7 files changed, 378 insertions(+), 9 deletions(-) create mode 100644 drivers/connector/cn_hash.c diff --git a/drivers/connector/Makefile b/drivers/connector/Makefile index 1bf67d3df97d..cb1dcdf067ad 100644 --- a/drivers/connector/Makefile +++ b/drivers/connector/Makefile @@ -2,4 +2,4 @@ obj-$(CONFIG_CONNECTOR) +=3D cn.o obj-$(CONFIG_PROC_EVENTS) +=3D cn_proc.o =20 -cn-y +=3D cn_queue.o connector.o +cn-y +=3D cn_hash.o cn_queue.o connector.o diff --git a/drivers/connector/cn_hash.c b/drivers/connector/cn_hash.c new file mode 100644 index 000000000000..a0211cd99132 --- /dev/null +++ b/drivers/connector/cn_hash.c @@ -0,0 +1,195 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Author: Anjali Kulkarni + * + * Copyright (c) 2024 Oracle and/or its affiliates. + */ + +#include +#include +#include +#include +#include + +#include + +struct cn_hash_dev *cn_hash_alloc_dev(const char *name) +{ + struct cn_hash_dev *hdev; + + hdev =3D kzalloc(sizeof(*hdev), GFP_KERNEL); + if (!hdev) + return NULL; + + snprintf(hdev->name, sizeof(hdev->name), "%s", name); + atomic_set(&hdev->hrefcnt, 0); + mutex_init(&hdev->uexit_hash_lock); + hash_init(hdev->uexit_pid_htable); + return hdev; +} + +void cn_hash_free_dev(struct cn_hash_dev *hdev) +{ + struct uexit_pid_hnode *hnode; + struct hlist_node *tmp; + int bucket; + + pr_debug("%s: Freeing entire hdev %p\n", __func__, hdev); + + mutex_lock(&hdev->uexit_hash_lock); + hash_for_each_safe(hdev->uexit_pid_htable, bucket, tmp, + hnode, uexit_pid_hlist) { + hash_del(&hnode->uexit_pid_hlist); + pr_debug("%s: Freeing node for pid %d\n", + __func__, hnode->pid); + kfree(hnode); + } + + mutex_unlock(&hdev->uexit_hash_lock); + mutex_destroy(&hdev->uexit_hash_lock); + + while (atomic_read(&hdev->hrefcnt)) { + pr_info("Waiting for %s to become free: refcnt=3D%d\n", + hdev->name, atomic_read(&hdev->hrefcnt)); + msleep(1000); + } + + kfree(hdev); +} + +static struct uexit_pid_hnode *cn_hash_alloc_elem(__u32 uexit_code, pid_t = pid) +{ + struct uexit_pid_hnode *elem; + + elem =3D kzalloc(sizeof(*elem), GFP_KERNEL); + if (!elem) + return NULL; + + INIT_HLIST_NODE(&elem->uexit_pid_hlist); + elem->uexit_code =3D uexit_code; + elem->pid =3D pid; + return elem; +} + +void cn_hash_free_elem(struct uexit_pid_hnode *elem) +{ + kfree(elem); +} + +int cn_hash_add_elem(struct cn_hash_dev *hdev, __u32 uexit_code, pid_t pid) +{ + struct uexit_pid_hnode *elem, *hnode; + + elem =3D cn_hash_alloc_elem(uexit_code, pid); + if (!elem) { + pr_err("%s: cn_hash_alloc_elem() returned NULL pid %d\n", + __func__, pid); + return -ENOMEM; + } + + mutex_lock(&hdev->uexit_hash_lock); + /* + * Check if an entry for the same pid already exists + */ + hash_for_each_possible(hdev->uexit_pid_htable, + hnode, uexit_pid_hlist, pid) { + if (hnode->pid =3D=3D pid) { + mutex_unlock(&hdev->uexit_hash_lock); + cn_hash_free_elem(elem); + pr_debug("%s: pid %d already exists in hash table\n", + __func__, pid); + return -EEXIST; + } + } + + hash_add(hdev->uexit_pid_htable, &elem->uexit_pid_hlist, pid); + mutex_unlock(&hdev->uexit_hash_lock); + + atomic_inc(&hdev->hrefcnt); + + pr_debug("%s: After hash_add of pid %d elem %p hrefcnt %d\n", + __func__, pid, elem, atomic_read(&hdev->hrefcnt)); + return 0; +} + +int cn_hash_del_elem(struct cn_hash_dev *hdev, pid_t pid) +{ + struct uexit_pid_hnode *hnode; + struct hlist_node *tmp; + + mutex_lock(&hdev->uexit_hash_lock); + hash_for_each_possible_safe(hdev->uexit_pid_htable, + hnode, tmp, uexit_pid_hlist, pid) { + if (hnode && hnode->pid =3D=3D pid) { + hash_del(&hnode->uexit_pid_hlist); + mutex_unlock(&hdev->uexit_hash_lock); + kfree(hnode); + atomic_dec(&hdev->hrefcnt); + pr_debug("%s: After hash_del of pid %d, hrefcnt %d\n", + __func__, pid, + atomic_read(&hdev->hrefcnt)); + return 0; + } + } + + mutex_unlock(&hdev->uexit_hash_lock); + pr_err("%s: pid %d not found in hash table\n", + __func__, pid); + return -EINVAL; +} + +__u32 cn_hash_del_get_exval(struct cn_hash_dev *hdev, pid_t pid) +{ + struct uexit_pid_hnode *hnode; + struct hlist_node *tmp; + __u32 excde; + + mutex_lock(&hdev->uexit_hash_lock); + hash_for_each_possible_safe(hdev->uexit_pid_htable, + hnode, tmp, uexit_pid_hlist, pid) { + if (hnode->pid =3D=3D pid) { + excde =3D hnode->uexit_code; + hash_del(&hnode->uexit_pid_hlist); + mutex_unlock(&hdev->uexit_hash_lock); + kfree(hnode); + atomic_dec(&hdev->hrefcnt); + pr_debug("%s: After hash_del of pid %d, found exit code %u hrefcnt %d\n= ", + __func__, pid, excde, + atomic_read(&hdev->hrefcnt)); + return excde; + } + } + + mutex_unlock(&hdev->uexit_hash_lock); + pr_err("%s: pid %d not found in hash table\n", + __func__, pid); + return 0; +} + +__u32 cn_hash_get_exval(struct cn_hash_dev *hdev, pid_t pid) +{ + struct uexit_pid_hnode *hnode; + __u32 excde; + + mutex_lock(&hdev->uexit_hash_lock); + hash_for_each_possible(hdev->uexit_pid_htable, + hnode, uexit_pid_hlist, pid) { + if (hnode->pid =3D=3D pid) { + excde =3D hnode->uexit_code; + mutex_unlock(&hdev->uexit_hash_lock); + pr_debug("%s: Found exit code %u for pid %d\n", + __func__, excde, pid); + return excde; + } + } + + mutex_unlock(&hdev->uexit_hash_lock); + pr_debug("%s: pid %d not found in hash table\n", + __func__, pid); + return -EINVAL; +} + +bool cn_hash_table_empty(struct cn_hash_dev *hdev) +{ + return hash_empty(hdev->uexit_pid_htable); +} diff --git a/drivers/connector/cn_proc.c b/drivers/connector/cn_proc.c index 44b19e696176..317ddde377ec 100644 --- a/drivers/connector/cn_proc.c +++ b/drivers/connector/cn_proc.c @@ -69,6 +69,8 @@ static int cn_filter(struct sock *dsk, struct sk_buff *sk= b, void *data) if ((__u32)val =3D=3D PROC_EVENT_ALL) return 0; =20 + pr_debug("%s: val %lx, what %x\n", __func__, val, what); + /* * Drop packet if we have to report only non-zero exit status * (PROC_EVENT_NONZERO_EXIT) and exit status is 0 @@ -326,9 +328,15 @@ void proc_exit_connector(struct task_struct *task) struct proc_event *ev; struct task_struct *parent; __u8 buffer[CN_PROC_MSG_SIZE] __aligned(8); + __u32 uexit_code; =20 - if (atomic_read(&proc_event_num_listeners) < 1) + if (atomic_read(&proc_event_num_listeners) < 1) { + if (likely(!(task->flags & PF_EXIT_NOTIFY))) + return; + + cn_del_elem(task->pid); return; + } =20 msg =3D buffer_to_cn_msg(buffer); ev =3D (struct proc_event *)msg->data; @@ -337,7 +345,26 @@ void proc_exit_connector(struct task_struct *task) ev->what =3D PROC_EVENT_EXIT; ev->event_data.exit.process_pid =3D task->pid; ev->event_data.exit.process_tgid =3D task->tgid; - ev->event_data.exit.exit_code =3D task->exit_code; + if (unlikely(task->flags & PF_EXIT_NOTIFY)) { + task_lock(task); + task->flags &=3D ~PF_EXIT_NOTIFY; + task_unlock(task); + + uexit_code =3D cn_del_get_exval(task->pid); + if (uexit_code =3D=3D 0) { + pr_debug("%s: Returning with task's exit code %u\n", + __func__, task->exit_code); + ev->event_data.exit.exit_code =3D task->exit_code; + } else { + ev->event_data.exit.exit_code =3D uexit_code; + pr_debug("%s: Reset PF_EXIT_NOTIFY & retrieved exit code %u from hash t= able, pid %d\n", + __func__, task->pid, + ev->event_data.exit.exit_code); + } + } else { + ev->event_data.exit.exit_code =3D task->exit_code; + } + ev->event_data.exit.exit_signal =3D task->exit_signal; =20 rcu_read_lock(); @@ -413,6 +440,15 @@ static void cn_proc_mcast_ctl(struct cn_msg *msg, if (msg->len =3D=3D sizeof(*pinput)) { pinput =3D (struct proc_input *)msg->data; mc_op =3D pinput->mcast_op; + if (mc_op =3D=3D PROC_CN_MCAST_NOTIFY) { + pr_debug("%s: Received PROC_CN_MCAST_NOTIFY, pid %d\n", + __func__, current->pid); + task_lock(current); + current->flags |=3D PF_EXIT_NOTIFY; + task_unlock(current); + err =3D cn_add_elem(pinput->uexit_code, current->pid); + return; + } ev_type =3D pinput->event_type; } else if (msg->len =3D=3D sizeof(mc_op)) { mc_op =3D *((enum proc_cn_mcast_op *)msg->data); @@ -432,6 +468,8 @@ static void cn_proc_mcast_ctl(struct cn_msg *msg, sk->sk_user_data =3D kzalloc(sizeof(struct proc_input), GFP_KERNEL); if (sk->sk_user_data =3D=3D NULL) { + pr_err("%s: ENOMEM for sk_user_data, pid %d\n", + __func__, current->pid); err =3D ENOMEM; goto out; } @@ -442,21 +480,33 @@ static void cn_proc_mcast_ctl(struct cn_msg *msg, } ((struct proc_input *)(sk->sk_user_data))->event_type =3D ev_type; + pr_debug("%s: sk: %p pid: %d event_type: %x\n", + __func__, sk, current->pid, ev_type); ((struct proc_input *)(sk->sk_user_data))->mcast_op =3D mc_op; } =20 switch (mc_op) { case PROC_CN_MCAST_LISTEN: - if (initial || (prev_mc_op !=3D PROC_CN_MCAST_LISTEN)) + if (initial || (prev_mc_op !=3D PROC_CN_MCAST_LISTEN)) { atomic_inc(&proc_event_num_listeners); + pr_debug("%s: PROC_CN_MCAST_LISTEN pid %d: Incremented listeners to %d\= n", + __func__, current->pid, + atomic_read(&proc_event_num_listeners)); + } break; case PROC_CN_MCAST_IGNORE: - if (!initial && (prev_mc_op !=3D PROC_CN_MCAST_IGNORE)) + if (!initial && (prev_mc_op !=3D PROC_CN_MCAST_IGNORE)) { atomic_dec(&proc_event_num_listeners); + pr_debug("%s: PROC_CN_MCAST_IGNORE pid %d: Decremented listeners to %d\= n", + __func__, current->pid, + atomic_read(&proc_event_num_listeners)); + } ((struct proc_input *)(sk->sk_user_data))->event_type =3D PROC_EVENT_NONE; break; default: + pr_warn("%s: Invalid value for mc_op %d\n", + __func__, mc_op); err =3D EINVAL; break; } diff --git a/drivers/connector/connector.c b/drivers/connector/connector.c index 4028e8eeba82..506e3cbedf85 100644 --- a/drivers/connector/connector.c +++ b/drivers/connector/connector.c @@ -271,6 +271,67 @@ static int __maybe_unused cn_proc_show(struct seq_file= *m, void *v) return 0; } =20 +__u32 cn_del_get_exval(pid_t pid) +{ + struct cn_dev *dev =3D &cdev; + __u32 exval; + + if (!cn_already_initialized) + return 0; + + exval =3D cn_hash_del_get_exval(dev->hdev, pid); + return exval; +} +EXPORT_SYMBOL_GPL(cn_del_get_exval); + +int cn_del_elem(pid_t pid) +{ + struct cn_dev *dev =3D &cdev; + int ret; + + if (!cn_already_initialized) + return 0; + + ret =3D cn_hash_del_elem(dev->hdev, pid); + return ret; +} +EXPORT_SYMBOL_GPL(cn_del_elem); + +int cn_add_elem(__u32 uexit_code, pid_t pid) +{ + struct cn_dev *dev =3D &cdev; + + if (!cn_already_initialized) + return 0; + + return cn_hash_add_elem(dev->hdev, uexit_code, pid); +} +EXPORT_SYMBOL_GPL(cn_add_elem); + +__u32 cn_get_exval(pid_t pid) +{ + struct cn_dev *dev =3D &cdev; + __u32 exval; + + if (!cn_already_initialized) + return 0; + + exval =3D cn_hash_get_exval(dev->hdev, pid); + return exval; +} +EXPORT_SYMBOL_GPL(cn_get_exval); + +bool cn_table_empty(void) +{ + struct cn_dev *dev =3D &cdev; + + if (!cn_already_initialized) + return 0; + + return cn_hash_table_empty(dev->hdev); +} +EXPORT_SYMBOL_GPL(cn_table_empty); + static int cn_init(void) { struct cn_dev *dev =3D &cdev; @@ -283,18 +344,35 @@ static int cn_init(void) }; =20 dev->nls =3D netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg); - if (!dev->nls) + if (!dev->nls) { + pr_err("%s: netlink_kernel_create failed, connector not initialized\n", + __func__); return -EIO; + } =20 dev->cbdev =3D cn_queue_alloc_dev("cqueue", dev->nls); if (!dev->cbdev) { + pr_err("%s: Allocation of dev->cbdev failed, connector not initialized\n= ", + __func__); netlink_kernel_release(dev->nls); return -EINVAL; } =20 + dev->hdev =3D cn_hash_alloc_dev("pid hash table"); + if (!dev->hdev) { + pr_err("%s: Allocation of dev->hdev failed, connector not initialized\n", + __func__); + netlink_kernel_release(dev->nls); + cn_queue_free_dev(dev->cbdev); + return -ENOMEM; + } + + pr_debug("Connector initialized, allocated hdev %p\n", dev->hdev); + cn_already_initialized =3D 1; =20 - proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show); + proc_create_single("connector", S_IRUGO, init_net.proc_net, + cn_proc_show); =20 return 0; } @@ -308,6 +386,7 @@ static void cn_fini(void) remove_proc_entry("connector", init_net.proc_net); =20 cn_queue_free_dev(dev->cbdev); + cn_hash_free_dev(dev->hdev); netlink_kernel_release(dev->nls); } =20 diff --git a/include/linux/connector.h b/include/linux/connector.h index 70bc1160f3d8..094e1730a4f6 100644 --- a/include/linux/connector.h +++ b/include/linux/connector.h @@ -18,6 +18,8 @@ #include =20 #define CN_CBQ_NAMELEN 32 +#define HASHT_NAMELEN 32 +#define PID_HASH_TABLE_BITS 10 =20 struct cn_queue_dev { atomic_t refcnt; @@ -45,6 +47,19 @@ struct cn_callback_entry { u32 seq, group; }; =20 +struct uexit_pid_hnode { + __u32 uexit_code; + pid_t pid; + struct hlist_node uexit_pid_hlist; +}; + +struct cn_hash_dev { + atomic_t hrefcnt; + unsigned char name[HASHT_NAMELEN]; + struct mutex uexit_hash_lock; + DECLARE_HASHTABLE(uexit_pid_htable, PID_HASH_TABLE_BITS); +}; + struct cn_dev { struct cb_id id; =20 @@ -52,6 +67,7 @@ struct cn_dev { struct sock *nls; =20 struct cn_queue_dev *cbdev; + struct cn_hash_dev *hdev; }; =20 /** @@ -137,4 +153,31 @@ void cn_queue_free_dev(struct cn_queue_dev *dev); =20 int cn_cb_equal(const struct cb_id *, const struct cb_id *); =20 +struct cn_hash_dev *cn_hash_alloc_dev(const char *name); +void cn_hash_free_dev(struct cn_hash_dev *hdev); +struct uexit_pid_hnode *cn_hash_find_pid_node(struct cn_hash_dev *hdev, + pid_t pid); +void cn_hash_free_elem(struct uexit_pid_hnode *elem); +int cn_hash_add_elem(struct cn_hash_dev *hdev, __u32 uexit_code, pid_t pid= ); +int cn_hash_del_elem(struct cn_hash_dev *hdev, pid_t pid); +__u32 cn_hash_del_get_exval(struct cn_hash_dev *hdev, pid_t pid); + +int cn_add_elem(__u32 uexit_code, pid_t pid); +int cn_del_elem(pid_t pid); +__u32 cn_del_get_exval(pid_t pid); +__u32 cn_get_exval(pid_t pid); + +struct cn_hash_dev *cn_hash_alloc_dev(const char *name); +void cn_hash_free_dev(struct cn_hash_dev *hdev); +struct uexit_pid_hnode *cn_hash_find_pid_node(struct cn_hash_dev *hdev, + pid_t pid); +void cn_hash_free_elem(struct uexit_pid_hnode *elem); +int cn_hash_add_elem(struct cn_hash_dev *hdev, __u32 uexit_code, pid_t pid= ); +int cn_hash_del_elem(struct cn_hash_dev *hdev, pid_t pid); +__u32 cn_hash_del_get_exval(struct cn_hash_dev *hdev, pid_t pid); +__u32 cn_hash_get_exval(struct cn_hash_dev *hdev, pid_t pid); + +bool cn_table_empty(void); +bool cn_hash_table_empty(struct cn_hash_dev *hdev); + #endif /* __CONNECTOR_H */ diff --git a/include/linux/sched.h b/include/linux/sched.h index e6ee4258169a..a2339ae6208b 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1673,7 +1673,7 @@ extern struct pid *cad_pid; #define PF_USED_MATH 0x00002000 /* If unset the fpu must be initialized b= efore use */ #define PF_USER_WORKER 0x00004000 /* Kernel thread cloned from userspace = thread */ #define PF_NOFREEZE 0x00008000 /* This thread should not be frozen */ -#define PF__HOLE__00010000 0x00010000 +#define PF_EXIT_NOTIFY 0x00010000 /* This thread has sent an exit value t= o be sent as a notification to listening processes */ #define PF_KSWAPD 0x00020000 /* I am kswapd */ #define PF_MEMALLOC_NOFS 0x00040000 /* All allocations inherit GFP_NOFS. S= ee memalloc_nfs_save() */ #define PF_MEMALLOC_NOIO 0x00080000 /* All allocations inherit GFP_NOIO. S= ee memalloc_noio_save() */ diff --git a/include/uapi/linux/cn_proc.h b/include/uapi/linux/cn_proc.h index 18e3745b86cd..2b12a24e4651 100644 --- a/include/uapi/linux/cn_proc.h +++ b/include/uapi/linux/cn_proc.h @@ -27,7 +27,8 @@ */ enum proc_cn_mcast_op { PROC_CN_MCAST_LISTEN =3D 1, - PROC_CN_MCAST_IGNORE =3D 2 + PROC_CN_MCAST_IGNORE =3D 2, + PROC_CN_MCAST_NOTIFY =3D 3 }; =20 #define PROC_EVENT_ALL (PROC_EVENT_FORK | PROC_EVENT_EXEC | PROC_EVENT_UID= | \ @@ -65,6 +66,7 @@ enum proc_cn_event { struct proc_input { enum proc_cn_mcast_op mcast_op; enum proc_cn_event event_type; + __u32 uexit_code; }; =20 static inline enum proc_cn_event valid_event(enum proc_cn_event ev_type) --=20 2.46.0