From nobody Wed Jan 24 05:12:16 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TKXBn3Mw4z57THQ; Wed, 24 Jan 2024 05:12:17 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TKXBn0rF4z4ZJR; Wed, 24 Jan 2024 05:12:17 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706073137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Zdberss6sO5CxHkk8tyO4PKvPbly0Nqdhz5PEyb2SHA=; b=jSBuikbgcG59BM2C2CbYmEhzUuywBcrrvCJQCp3If+KCa7pDLx/gNY/GI8NvWQEsJPrQZ7 w/GDJDREknpZ2Y/GqKd5XyynjwtWAEDp8rnQhJivUkPhHNvzj7F1IkwMvqeqP4tx+rDyEJ I/x9br490tny6S6C62GyYsw0FpIWKaITZnxfn67dG1WiL2kN5jCCb/hnGv69ic/6pxz+aE xNgP1SMHRjLYxpCAtV/1iSJXVhraaDKJjkgF+FYreXV2JEZONXmg7RTs3Bdc6v1/j7Zm6H xaAwLJKYsHdy5WIQBGTepNsKCIDWIr9KMIQ43FU6IoXWeDfh9F2voYAEmQ4EQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706073137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Zdberss6sO5CxHkk8tyO4PKvPbly0Nqdhz5PEyb2SHA=; b=cXXLseui6pqYFzOxs4qUhUy1wBXjY+9JACZWSqzuEo3VGXpqztiYOasOi+xlVtB5CTvQHm yM2cSkqfsa1pUUFoub2HMV3lVXYY+7LHqVVQS0XVo3V3Ut+UQ3VQrj1QuYgT/GtS8+q3qp xLH/xHNHmKAGhXYWz+RO0s+cX4882YKjF6UuvfhYriw7Rlmj+KSKDoO4vUdTGCZ/ylFf3e KcK0oXZhBasrxDIuQSFLYg+f5m4+Ivf3AlshZhISzqh2T1DSHhIGISWvyegeK1nrHPZJCS t5EHxjzzPkgW5tH4cSeK3MKyfaq7XGmAfAeclV4UL1k2FxcgC5ABoA5SC8Boug== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1706073137; a=rsa-sha256; cv=none; b=vaKgiNH4rI/EsutR1T9PLAaCDlpsndfvBsKdTan1p7l4v2erP+O+Ynpav5d1K5+qhEmUCe fHiRblkdnabooB6V61RUlgqxcVkt59njFkCB51yhx84Od6CYM20ySlKYMk75Rmh526WfxM LUXADRN6oTzEExliCxNeVJYIbCiTDMCvfxwwzxfDqvYxKTLtDmxJ2VIOWCvCJM8KzDDWjd EWJbd6Hmj4etuAZxSOkdsWp2VGXE4vW0HlVD0XTYgyAF+7CuBLYHSM1FTwRbprK7HRJhfq ykH3DC7WdrPsa6njlP5RVsQ54SlFD95+CkBpk40hQmK2+GInqvy9YfLwj/fOFg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TKXBm73fDz11lF; Wed, 24 Jan 2024 05:12:16 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40O5CG2j040064; Wed, 24 Jan 2024 05:12:16 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40O5CGGe040061; Wed, 24 Jan 2024 05:12:16 GMT (envelope-from git) Date: Wed, 24 Jan 2024 05:12:16 GMT Message-Id: <202401240512.40O5CGGe040061@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: d8decc9ae31a - main - Add kcmp(2) kernel bits List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d8decc9ae31af7ffc77276c89639fb13eb1020cc Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=d8decc9ae31af7ffc77276c89639fb13eb1020cc commit d8decc9ae31af7ffc77276c89639fb13eb1020cc Author: Konstantin Belousov AuthorDate: 2024-01-19 19:49:36 +0000 Commit: Konstantin Belousov CommitDate: 2024-01-24 05:11:26 +0000 Add kcmp(2) kernel bits This is based purely on reading the Linux kcmp(2) man page. In addition to the Linux set of comparators, I also added KCMP_FILEOBJ to compare underlying file' objects. Tested by: manu Reviewed by: brooks, markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D43518 --- sys/kern/sys_generic.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ sys/kern/syscalls.master | 10 +++++- sys/sys/syscallsubr.h | 2 ++ sys/sys/systm.h | 2 ++ sys/sys/unistd.h | 7 ++++ 5 files changed, 107 insertions(+), 1 deletion(-) diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index 8687bb447251..7698f5d60063 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -65,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -2071,3 +2072,89 @@ kern_posix_error(struct thread *td, int error) td->td_retval[0] = error; return (0); } + +int +kcmp_cmp(uintptr_t a, uintptr_t b) +{ + if (a == b) + return (0); + else if (a < b) + return (1); + return (2); +} + +static int +kcmp_pget(struct thread *td, pid_t pid, struct proc **pp) +{ + if (pid == td->td_proc->p_pid) { + *pp = td->td_proc; + return (0); + } + return (pget(pid, PGET_CANDEBUG | PGET_NOTWEXIT | PGET_HOLD, pp)); +} + +int +kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type, + uintptr_t idx1, uintptr_t idx2) +{ + struct proc *p1, *p2; + struct file *fp1, *fp2; + int error, res; + + res = -1; + p1 = p2 = NULL; + error = kcmp_pget(td, pid1, &p1); + if (error == 0) + error = kcmp_pget(td, pid2, &p2); + if (error != 0) + goto out; + + switch (type) { + case KCMP_FILE: + case KCMP_FILEOBJ: + error = fget_remote(td, p1, idx1, &fp1); + if (error == 0) { + error = fget_remote(td, p2, idx2, &fp2); + if (error == 0) { + if (type == KCMP_FILEOBJ) + res = fo_cmp(fp1, fp2, td); + else + res = kcmp_cmp((uintptr_t)fp1, + (uintptr_t)fp2); + fdrop(fp2, td); + } + fdrop(fp1, td); + } + break; + case KCMP_FILES: + res = kcmp_cmp((uintptr_t)p1->p_fd, (uintptr_t)p2->p_fd); + break; + case KCMP_SIGHAND: + res = kcmp_cmp((uintptr_t)p1->p_sigacts, + (uintptr_t)p2->p_sigacts); + break; + case KCMP_VM: + res = kcmp_cmp((uintptr_t)p1->p_vmspace, + (uintptr_t)p2->p_vmspace); + break; + default: + error = EINVAL; + break; + } + +out: + if (p1 != NULL && p1 != td->td_proc) + PRELE(p1); + if (p2 != NULL && p2 != td->td_proc) + PRELE(p2); + + td->td_retval[0] = res; + return (error); +} + +int +sys_kcmp(struct thread *td, struct kcmp_args *uap) +{ + return (kern_kcmp(td, uap->pid1, uap->pid2, uap->type, + uap->idx1, uap->idx2)); +} diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index 709b01f0abbe..2f1bea33ac8f 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -3329,6 +3329,14 @@ _Out_opt_ _Contains_long_timet_ struct itimerspec *old_value ); } - +588 AUE_NULL STD { + int kcmp( + pid_t pid1, + pid_t pid2, + int type, + uintptr_t idx1, + uintptr_t idx2 + ); + } ; vim: syntax=off diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index 1eca098365e7..2c3059b27c82 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -198,6 +198,8 @@ int kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data); int kern_jail(struct thread *td, struct jail *j); int kern_jail_get(struct thread *td, struct uio *options, int flags); int kern_jail_set(struct thread *td, struct uio *options, int flags); +int kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type, + uintptr_t idx1, uintptr_t idx2); int kern_kevent(struct thread *td, int fd, int nchanges, int nevents, struct kevent_copyops *k_ops, const struct timespec *timeout); int kern_kevent_anonymous(struct thread *td, int nevents, diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 508690cd639e..c684f3365c35 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -485,6 +485,8 @@ int poll_no_poll(int events); /* XXX: Should be void nanodelay(u_int nsec); */ void DELAY(int usec); +int kcmp_cmp(uintptr_t a, uintptr_t b); + /* Root mount holdback API */ struct root_hold_token { int flags; diff --git a/sys/sys/unistd.h b/sys/sys/unistd.h index 87f79fc8d554..6128aab61877 100644 --- a/sys/sys/unistd.h +++ b/sys/sys/unistd.h @@ -195,6 +195,13 @@ RFPROCDESC | RFSPAWN | RFPPWAIT) #define RFKERNELONLY (RFSTOPPED | RFHIGHPID | RFPROCDESC) +/* kcmp() options. */ +#define KCMP_FILE 100 +#define KCMP_FILEOBJ 101 +#define KCMP_FILES 102 +#define KCMP_SIGHAND 103 +#define KCMP_VM 104 + #define SWAPOFF_FORCE 0x00000001 /*