From nobody Mon May 23 22:11:35 2022 X-Original-To: dev-commits-src-all@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 CF1E91B34D1B; Mon, 23 May 2022 22:11:35 +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 4L6Wkv5Jmcz4p6V; Mon, 23 May 2022 22:11:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653343895; 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=8IcellpGNnuQHCqLnwJqhVerOvG8220nemUcbw+hzsU=; b=abcBlakfBqeC+R6xaOhp6xlryDSy5usZR9cvFnxgen2Yps0oHwqMCtEwfkguF19eGa3L+/ feayuSUh9FkIPtfZ12RkkRS2iLSej80LHKF7n08Dm3CsmfP4x1nFO5j8LtWdXOsVn/xF/z YwZlfgJ94+8bdOoXc+jFpyydQGnuEWtelaLAvYhqqms3dgTmbIFVBpzpZ2l5t7EYmKauZ+ H2ZVeY25u+JSmwSt3gP4VgXSS6Q4VwWFXq4cIAhd+NE+YiTsL8hbnvZ7w/8KtBtOIx0E+7 ZNzHtA6d+gUCPVbY3proDasVvzbwTdW0r4jouGU42U63ShE8H3PERIWKEVyKzQ== 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 962FF18360; Mon, 23 May 2022 22:11:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24NMBZWX094554; Mon, 23 May 2022 22:11:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24NMBZAL094553; Mon, 23 May 2022 22:11:35 GMT (envelope-from git) Date: Mon, 23 May 2022 22:11:35 GMT Message-Id: <202205232211.24NMBZAL094553@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Justin Hibbits Subject: git: 3b70988d7376 - stable/12 - cpuset: Byte swap cpuset for compat32 on big endian architectures List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhibbits X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 3b70988d73766cafdfd142e43f61f4498edd4b20 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1653343895; 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=8IcellpGNnuQHCqLnwJqhVerOvG8220nemUcbw+hzsU=; b=ON8KTPVBlKjKN2xU9OwAfLHGbxCwupRaaJOPNdeUQ/sZfzTqNWZFgxydMzzX2x+ZIHoO// u+YSMcYzKhBxdFkC1e0gD0RC0e8Zxx3+UoRG5mxT//TYZqh9klmBUUVyPG1q8oDveHmjQn ZcmEJHi2prUxi6OKTbwKxIF8FAULN7FzPTudJei0fyV0oyQI3LUzAEDh3kFjFuKYyP+ls6 Yta2TA7SXLD20c4p+h8pK0JmEYGIPmXa4w5uXcpkZsCnpKvOfkEa9fbtN6ucEaELNI/0oy 0MOFK1/KNoPJUW5Hs80nbJQ3EKD6RiYkeP3Z0yCEDNja8UHK5/HOV0QCKrCGqg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1653343895; a=rsa-sha256; cv=none; b=F6otRzWrck0TIb2aumHs5x+tTR5+EGsilNxyCWiWYh2N7bG5eottDa9yYovkhKShT3ojJy 6/VbNpzqdLQ4yuYdN67s9zFvfPRCdRMCR+wupTFbNZx7G+EKyW0FNJ6y5YyIj/cI7i1EJU /azVPptKP4EoQE/ON4qU0GoiGZOOuIyOC6gXHNQXK9QlQbxuYVCCU2kwlSd+51UeItPEXU VSL/6MBZhQ82c9s8ew+U6usPDVV8sJFNtcMrrsLDFC0elvzRlvozYq+z6po2ip/5abcGq0 /kAHRJRUWxd3ijr8IYTpRD2fPeZpS0rNebzIwhnyA1ZmAFV7JEPvakMUapb5zg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/12 has been updated by jhibbits: URL: https://cgit.FreeBSD.org/src/commit/?id=3b70988d73766cafdfd142e43f61f4498edd4b20 commit 3b70988d73766cafdfd142e43f61f4498edd4b20 Author: Justin Hibbits AuthorDate: 2022-05-12 20:38:22 +0000 Commit: Justin Hibbits CommitDate: 2022-05-23 19:15:45 +0000 cpuset: Byte swap cpuset for compat32 on big endian architectures Summary: BITSET uses long as its basic underlying type, which is dependent on the compile type, meaning on 32-bit builds the basic type is 32 bits, but on 64-bit builds it's 64 bits. On little endian architectures this doesn't matter, because the LSB is always at the low bit, so the words get effectively concatenated moving between 32-bit and 64-bit, but on big-endian architectures it throws a wrench in, as setting bit 0 in 32-bit mode is equivalent to setting bit 32 in 64-bit mode. To demonstrate: 32-bit mode: BIT_SET(foo, 0): 0x00000001 64-bit sees: 0x0000000100000000 cpuset is the only system interface that uses bitsets, so solve this by swapping the integer sub-components at the copyin/copyout points. Reviewed by: kib Sponsored by: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D35225 (cherry picked from commit 47a57144af25a7bd768b29272d50a36fdf2874ba) --- sys/compat/freebsd32/freebsd32_misc.c | 70 +++++++++++++++++++++++++++++++++-- sys/kern/kern_cpuset.c | 33 +++++++++++------ sys/sys/cpuset.h | 9 +++++ sys/sys/syscallsubr.h | 10 +++-- 4 files changed, 102 insertions(+), 20 deletions(-) diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c index 68a17f4fb004..438004f4a088 100644 --- a/sys/compat/freebsd32/freebsd32_misc.c +++ b/sys/compat/freebsd32/freebsd32_misc.c @@ -3008,13 +3008,72 @@ freebsd32_cpuset_getid(struct thread *td, PAIR32TO64(id_t, uap->id), uap->setid)); } +static int +copyin32_set(const void *u, void *k, size_t size) +{ +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + int rv; + struct bitset *kb = k; + int *p; + + rv = copyin(u, k, size); + if (rv != 0) + return (rv); + + p = (int *)kb->__bits; + /* Loop through swapping words. + * `size' is in bytes, we need bits. */ + for (int i = 0; i < __bitset_words(size * 8); i++) { + int tmp = p[0]; + p[0] = p[1]; + p[1] = tmp; + p += 2; + } + return (0); +#else + return (copyin(u, k, size)); +#endif +} + +static int +copyout32_set(const void *k, void *u, size_t size) +{ +#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + const struct bitset *kb = k; + struct bitset *ub = u; + const int *kp = (const int *)kb->__bits; + int *up = (int *)ub->__bits; + int rv; + + for (int i = 0; i < __bitset_words(CPU_SETSIZE); i++) { + /* `size' is in bytes, we need bits. */ + for (int i = 0; i < __bitset_words(size * 8); i++) { + rv = suword32(up, kp[1]); + if (rv == 0) + rv = suword32(up + 1, kp[0]); + if (rv != 0) + return (EFAULT); + } + } + return (0); +#else + return (copyout(k, u, size)); +#endif +} + +static const struct cpuset_copy_cb cpuset_copy32_cb = { + .copyin = copyin32_set, + .copyout = copyout32_set +}; + int freebsd32_cpuset_getaffinity(struct thread *td, struct freebsd32_cpuset_getaffinity_args *uap) { return (kern_cpuset_getaffinity(td, uap->level, uap->which, - PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); + PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask, + &cpuset_copy32_cb)); } int @@ -3023,7 +3082,8 @@ freebsd32_cpuset_setaffinity(struct thread *td, { return (kern_cpuset_setaffinity(td, uap->level, uap->which, - PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask)); + PAIR32TO64(id_t,uap->id), uap->cpusetsize, uap->mask, + &cpuset_copy32_cb)); } int @@ -3032,7 +3092,8 @@ freebsd32_cpuset_getdomain(struct thread *td, { return (kern_cpuset_getdomain(td, uap->level, uap->which, - PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); + PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy, + &cpuset_copy32_cb)); } int @@ -3041,7 +3102,8 @@ freebsd32_cpuset_setdomain(struct thread *td, { return (kern_cpuset_setdomain(td, uap->level, uap->which, - PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy)); + PAIR32TO64(id_t,uap->id), uap->domainsetsize, uap->mask, uap->policy, + &cpuset_copy32_cb)); } int diff --git a/sys/kern/kern_cpuset.c b/sys/kern/kern_cpuset.c index e2e45f53c0af..f8176256aee2 100644 --- a/sys/kern/kern_cpuset.c +++ b/sys/kern/kern_cpuset.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1646,6 +1647,11 @@ cpuset_check_capabilities(struct thread *td, cpulevel_t level, cpuwhich_t which, return (0); } +static const struct cpuset_copy_cb copy_set = { + .copyin = copyin, + .copyout = copyout +}; + #ifndef _SYS_SYSPROTO_H_ struct cpuset_args { cpusetid_t *setid; @@ -1787,12 +1793,12 @@ sys_cpuset_getaffinity(struct thread *td, struct cpuset_getaffinity_args *uap) { return (kern_cpuset_getaffinity(td, uap->level, uap->which, - uap->id, uap->cpusetsize, uap->mask)); + uap->id, uap->cpusetsize, uap->mask, ©_set)); } int kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, - id_t id, size_t cpusetsize, cpuset_t *maskp) + id_t id, size_t cpusetsize, cpuset_t *maskp, const struct cpuset_copy_cb *cb) { struct thread *ttd; struct cpuset *nset; @@ -1879,7 +1885,7 @@ kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, if (p) PROC_UNLOCK(p); if (error == 0) - error = copyout(mask, maskp, size); + error = cb->copyout(mask, maskp, size); out: free(mask, M_TEMP); return (error); @@ -1899,12 +1905,13 @@ sys_cpuset_setaffinity(struct thread *td, struct cpuset_setaffinity_args *uap) { return (kern_cpuset_setaffinity(td, uap->level, uap->which, - uap->id, uap->cpusetsize, uap->mask)); + uap->id, uap->cpusetsize, uap->mask, ©_set)); } int kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, - id_t id, size_t cpusetsize, const cpuset_t *maskp) + id_t id, size_t cpusetsize, const cpuset_t *maskp, + const struct cpuset_copy_cb *cb) { struct cpuset *nset; struct cpuset *set; @@ -1919,7 +1926,7 @@ kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, if (error != 0) return (error); mask = malloc(cpusetsize, M_TEMP, M_WAITOK | M_ZERO); - error = copyin(maskp, mask, cpusetsize); + error = cb->cpuset_copyin(maskp, mask, cpusetsize); if (error) goto out; /* @@ -2025,12 +2032,13 @@ sys_cpuset_getdomain(struct thread *td, struct cpuset_getdomain_args *uap) { return (kern_cpuset_getdomain(td, uap->level, uap->which, - uap->id, uap->domainsetsize, uap->mask, uap->policy)); + uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set)); } int kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, - id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp) + id_t id, size_t domainsetsize, domainset_t *maskp, int *policyp, + const struct cpuset_copy_cb *cb) { struct domainset outset; struct thread *ttd; @@ -2128,7 +2136,7 @@ kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, } DOMAINSET_COPY(&outset.ds_mask, mask); if (error == 0) - error = copyout(mask, maskp, domainsetsize); + error = cb->copyout(mask, maskp, domainsetsize); if (error == 0) if (suword32(policyp, outset.ds_policy) != 0) error = EFAULT; @@ -2152,12 +2160,13 @@ sys_cpuset_setdomain(struct thread *td, struct cpuset_setdomain_args *uap) { return (kern_cpuset_setdomain(td, uap->level, uap->which, - uap->id, uap->domainsetsize, uap->mask, uap->policy)); + uap->id, uap->domainsetsize, uap->mask, uap->policy, ©_set)); } int kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, - id_t id, size_t domainsetsize, const domainset_t *maskp, int policy) + id_t id, size_t domainsetsize, const domainset_t *maskp, int policy, + const struct cpuset_copy_cb *cb) { struct cpuset *nset; struct cpuset *set; @@ -2178,7 +2187,7 @@ kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, return (error); memset(&domain, 0, sizeof(domain)); mask = malloc(domainsetsize, M_TEMP, M_WAITOK | M_ZERO); - error = copyin(maskp, mask, domainsetsize); + error = cb->copyin(maskp, mask, domainsetsize); if (error) goto out; /* diff --git a/sys/sys/cpuset.h b/sys/sys/cpuset.h index 0af1351c8067..697e220a70df 100644 --- a/sys/sys/cpuset.h +++ b/sys/sys/cpuset.h @@ -131,6 +131,15 @@ struct prison; struct proc; struct thread; +/* + * Callbacks for copying in/out a cpuset or domainset. Used for alternate + * ABIs, like compat32. + */ +struct cpuset_copy_cb { + int (*copyin)(const void *, void *, size_t); + int (*copyout)(const void *, void *, size_t); +}; + struct cpuset *cpuset_thread0(void); struct cpuset *cpuset_ref(struct cpuset *); void cpuset_rel(struct cpuset *); diff --git a/sys/sys/syscallsubr.h b/sys/sys/syscallsubr.h index 2b5ab76c9168..bd6102cfb051 100644 --- a/sys/sys/syscallsubr.h +++ b/sys/sys/syscallsubr.h @@ -39,6 +39,7 @@ #include struct __wrusage; +struct cpuset_copy_cb; struct file; struct filecaps; enum idtype; @@ -103,16 +104,17 @@ int kern_close(struct thread *td, int fd); int kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa); int kern_cpuset_getaffinity(struct thread *td, cpulevel_t level, - cpuwhich_t which, id_t id, size_t cpusetsize, cpuset_t *maskp); + cpuwhich_t which, id_t id, size_t cpusetsize, cpuset_t *maskp, + const struct cpuset_copy_cb *cb); int kern_cpuset_setaffinity(struct thread *td, cpulevel_t level, cpuwhich_t which, id_t id, size_t cpusetsize, - const cpuset_t *maskp); + const cpuset_t *maskp, const struct cpuset_copy_cb *cb); int kern_cpuset_getdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, id_t id, size_t domainsetsize, - domainset_t *maskp, int *policyp); + domainset_t *maskp, int *policyp, const struct cpuset_copy_cb *cb); int kern_cpuset_setdomain(struct thread *td, cpulevel_t level, cpuwhich_t which, id_t id, size_t domainsetsize, - const domainset_t *maskp, int policy); + const domainset_t *maskp, int policy, const struct cpuset_copy_cb *cb); int kern_cpuset_getid(struct thread *td, cpulevel_t level, cpuwhich_t which, id_t id, cpusetid_t *setid); int kern_cpuset_setid(struct thread *td, cpuwhich_t which,