From owner-svn-src-stable@freebsd.org Thu Nov 8 19:56:31 2018 Return-Path: Delivered-To: svn-src-stable@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F3C4E11031B5; Thu, 8 Nov 2018 19:56:30 +0000 (UTC) (envelope-from tijl@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 83F4D8FA5A; Thu, 8 Nov 2018 19:56:30 +0000 (UTC) (envelope-from tijl@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 62F971440; Thu, 8 Nov 2018 19:56:30 +0000 (UTC) (envelope-from tijl@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id wA8JuUYk028976; Thu, 8 Nov 2018 19:56:30 GMT (envelope-from tijl@FreeBSD.org) Received: (from tijl@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id wA8JuT0l028972; Thu, 8 Nov 2018 19:56:29 GMT (envelope-from tijl@FreeBSD.org) Message-Id: <201811081956.wA8JuT0l028972@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: tijl set sender to tijl@FreeBSD.org using -f From: Tijl Coosemans Date: Thu, 8 Nov 2018 19:56:29 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r340258 - in stable/12/sys: amd64/linux32 compat/linux X-SVN-Group: stable-12 X-SVN-Commit-Author: tijl X-SVN-Commit-Paths: in stable/12/sys: amd64/linux32 compat/linux X-SVN-Commit-Revision: 340258 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 83F4D8FA5A X-Spamd-Result: default: False [-106.86 / 200.00]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; ALLOW_DOMAIN_WHITELIST(-100.00)[FreeBSD.org]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; DMARC_NA(0.00)[FreeBSD.org]; RCVD_COUNT_THREE(0.00)[4]; MX_GOOD(-0.01)[cached: mx1.FreeBSD.org]; NEURAL_HAM_SHORT(-1.00)[-0.999,0]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; RCVD_TLS_LAST(0.00)[]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; IP_SCORE(-3.75)[ip: (-9.91), ipnet: 2610:1c1:1::/48(-4.91), asn: 11403(-3.82), country: US(-0.09)] X-Rspamd-Server: mx1.freebsd.org X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Nov 2018 19:56:31 -0000 Author: tijl Date: Thu Nov 8 19:56:29 2018 New Revision: 340258 URL: https://svnweb.freebsd.org/changeset/base/340258 Log: MFC r340181, r340185: On amd64 both Linux compat modules, linux.ko and linux64.ko, provide linux_ioctl_(un)register_handler that allows other driver modules to register ioctl handlers. The ioctl syscall implementation in each Linux compat module iterates over the list of handlers and forwards the call to the appropriate driver. Because the registration functions have the same name in each module it is not possible for a driver to support both 32 and 64 bit linux compatibility. Move the list of ioctl handlers to linux_common.ko so it is shared by both Linux modules and all drivers receive both 32 and 64 bit ioctl calls with one registration. These ioctl handlers normally forward the call to the FreeBSD ioctl handler which can handle both 32 and 64 bit. Keep the special COMPAT_LINUX32 ioctl handlers in linux.ko in a separate list for now and let the ioctl syscall iterate over that list first. Later, COMPAT_LINUX32 support can be added to the 64 bit ioctl handlers via a runtime check for ILP32 like is done for COMPAT_FREEBSD32 and then this separate list would disappear again. That is a much bigger effort however and this commit is meant to be MFCable. This enables linux64 support in x11/nvidia-driver*. PR: 206711 Reviewed by: kib Approved by: re (gjb) Modified: stable/12/sys/amd64/linux32/linux32_sysvec.c stable/12/sys/compat/linux/linux_common.c stable/12/sys/compat/linux/linux_ioctl.c stable/12/sys/compat/linux/linux_ioctl.h Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/amd64/linux32/linux32_sysvec.c ============================================================================== --- stable/12/sys/amd64/linux32/linux32_sysvec.c Thu Nov 8 19:50:23 2018 (r340257) +++ stable/12/sys/amd64/linux32/linux32_sysvec.c Thu Nov 8 19:56:29 2018 (r340258) @@ -32,6 +32,8 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "opt_compat.h" + #include __FBSDID("$FreeBSD$"); @@ -1071,7 +1073,7 @@ linux_elf_modevent(module_t mod, int type, void *data) error = EINVAL; if (error == 0) { SET_FOREACH(lihp, linux_ioctl_handler_set) - linux_ioctl_register_handler(*lihp); + linux32_ioctl_register_handler(*lihp); LIST_INIT(&futex_list); mtx_init(&futex_mtx, "ftllk", NULL, MTX_DEF); stclohz = (stathz ? stathz : hz); @@ -1093,7 +1095,7 @@ linux_elf_modevent(module_t mod, int type, void *data) } if (error == 0) { SET_FOREACH(lihp, linux_ioctl_handler_set) - linux_ioctl_unregister_handler(*lihp); + linux32_ioctl_unregister_handler(*lihp); mtx_destroy(&futex_mtx); if (bootverbose) printf("Linux ELF exec handler removed\n"); Modified: stable/12/sys/compat/linux/linux_common.c ============================================================================== --- stable/12/sys/compat/linux/linux_common.c Thu Nov 8 19:50:23 2018 (r340257) +++ stable/12/sys/compat/linux/linux_common.c Thu Nov 8 19:56:29 2018 (r340258) @@ -35,9 +35,11 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include +#include #include #include @@ -47,6 +49,11 @@ FEATURE(linuxulator_v4l2, "V4L2 ioctl wrapper support MODULE_VERSION(linux_common, 1); SET_DECLARE(linux_device_handler_set, struct linux_device_handler); + +TAILQ_HEAD(, linux_ioctl_handler_element) linux_ioctl_handlers = + TAILQ_HEAD_INITIALIZER(linux_ioctl_handlers); +struct sx linux_ioctl_sx; +SX_SYSINIT(linux_ioctl, &linux_ioctl_sx, "Linux ioctl handlers"); static eventhandler_tag linux_exec_tag; static eventhandler_tag linux_thread_dtor_tag; Modified: stable/12/sys/compat/linux/linux_ioctl.c ============================================================================== --- stable/12/sys/compat/linux/linux_ioctl.c Thu Nov 8 19:50:23 2018 (r340257) +++ stable/12/sys/compat/linux/linux_ioctl.c Thu Nov 8 19:56:29 2018 (r340258) @@ -161,17 +161,19 @@ DATA_SET(linux_ioctl_handler_set, video2_handler); DATA_SET(linux_ioctl_handler_set, fbsd_usb); DATA_SET(linux_ioctl_handler_set, evdev_handler); -struct handler_element -{ - TAILQ_ENTRY(handler_element) list; - int (*func)(struct thread *, struct linux_ioctl_args *); - int low, high, span; -}; - -static TAILQ_HEAD(, handler_element) handlers = - TAILQ_HEAD_INITIALIZER(handlers); +#ifdef __i386__ +static TAILQ_HEAD(, linux_ioctl_handler_element) linux_ioctl_handlers = + TAILQ_HEAD_INITIALIZER(linux_ioctl_handlers); static struct sx linux_ioctl_sx; SX_SYSINIT(linux_ioctl, &linux_ioctl_sx, "Linux ioctl handlers"); +#else +extern TAILQ_HEAD(, linux_ioctl_handler_element) linux_ioctl_handlers; +extern struct sx linux_ioctl_sx; +#endif +#ifdef COMPAT_LINUX32 +static TAILQ_HEAD(, linux_ioctl_handler_element) linux32_ioctl_handlers = + TAILQ_HEAD_INITIALIZER(linux32_ioctl_handlers); +#endif /* * hdio related ioctls for VMWare support @@ -3684,7 +3686,7 @@ int linux_ioctl(struct thread *td, struct linux_ioctl_args *args) { struct file *fp; - struct handler_element *he; + struct linux_ioctl_handler_element *he; int error, cmd; #ifdef DEBUG @@ -3705,7 +3707,8 @@ linux_ioctl(struct thread *td, struct linux_ioctl_args cmd = args->cmd & 0xffff; sx_slock(&linux_ioctl_sx); mtx_lock(&Giant); - TAILQ_FOREACH(he, &handlers, list) { +#ifdef COMPAT_LINUX32 + TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) { if (cmd >= he->low && cmd <= he->high) { error = (*he->func)(td, args); if (error != ENOIOCTL) { @@ -3716,6 +3719,18 @@ linux_ioctl(struct thread *td, struct linux_ioctl_args } } } +#endif + TAILQ_FOREACH(he, &linux_ioctl_handlers, list) { + if (cmd >= he->low && cmd <= he->high) { + error = (*he->func)(td, args); + if (error != ENOIOCTL) { + mtx_unlock(&Giant); + sx_sunlock(&linux_ioctl_sx); + fdrop(fp, td); + return (error); + } + } + } mtx_unlock(&Giant); sx_sunlock(&linux_ioctl_sx); fdrop(fp, td); @@ -3737,7 +3752,7 @@ linux_ioctl(struct thread *td, struct linux_ioctl_args int linux_ioctl_register_handler(struct linux_ioctl_handler *h) { - struct handler_element *he, *cur; + struct linux_ioctl_handler_element *he, *cur; if (h == NULL || h->func == NULL) return (EINVAL); @@ -3747,7 +3762,7 @@ linux_ioctl_register_handler(struct linux_ioctl_handle * create a new element. */ sx_xlock(&linux_ioctl_sx); - TAILQ_FOREACH(he, &handlers, list) { + TAILQ_FOREACH(he, &linux_ioctl_handlers, list) { if (he->func == h->func) break; } @@ -3756,7 +3771,7 @@ linux_ioctl_register_handler(struct linux_ioctl_handle M_LINUX, M_WAITOK); he->func = h->func; } else - TAILQ_REMOVE(&handlers, he, list); + TAILQ_REMOVE(&linux_ioctl_handlers, he, list); /* Initialize range information. */ he->low = h->low; @@ -3764,14 +3779,14 @@ linux_ioctl_register_handler(struct linux_ioctl_handle he->span = h->high - h->low + 1; /* Add the element to the list, sorted on span. */ - TAILQ_FOREACH(cur, &handlers, list) { + TAILQ_FOREACH(cur, &linux_ioctl_handlers, list) { if (cur->span > he->span) { TAILQ_INSERT_BEFORE(cur, he, list); sx_xunlock(&linux_ioctl_sx); return (0); } } - TAILQ_INSERT_TAIL(&handlers, he, list); + TAILQ_INSERT_TAIL(&linux_ioctl_handlers, he, list); sx_xunlock(&linux_ioctl_sx); return (0); @@ -3780,15 +3795,15 @@ linux_ioctl_register_handler(struct linux_ioctl_handle int linux_ioctl_unregister_handler(struct linux_ioctl_handler *h) { - struct handler_element *he; + struct linux_ioctl_handler_element *he; if (h == NULL || h->func == NULL) return (EINVAL); sx_xlock(&linux_ioctl_sx); - TAILQ_FOREACH(he, &handlers, list) { + TAILQ_FOREACH(he, &linux_ioctl_handlers, list) { if (he->func == h->func) { - TAILQ_REMOVE(&handlers, he, list); + TAILQ_REMOVE(&linux_ioctl_handlers, he, list); sx_xunlock(&linux_ioctl_sx); free(he, M_LINUX); return (0); @@ -3798,3 +3813,69 @@ linux_ioctl_unregister_handler(struct linux_ioctl_hand return (EINVAL); } + +#ifdef COMPAT_LINUX32 +int +linux32_ioctl_register_handler(struct linux_ioctl_handler *h) +{ + struct linux_ioctl_handler_element *he, *cur; + + if (h == NULL || h->func == NULL) + return (EINVAL); + + /* + * Reuse the element if the handler is already on the list, otherwise + * create a new element. + */ + sx_xlock(&linux_ioctl_sx); + TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) { + if (he->func == h->func) + break; + } + if (he == NULL) { + he = malloc(sizeof(*he), M_LINUX, M_WAITOK); + he->func = h->func; + } else + TAILQ_REMOVE(&linux32_ioctl_handlers, he, list); + + /* Initialize range information. */ + he->low = h->low; + he->high = h->high; + he->span = h->high - h->low + 1; + + /* Add the element to the list, sorted on span. */ + TAILQ_FOREACH(cur, &linux32_ioctl_handlers, list) { + if (cur->span > he->span) { + TAILQ_INSERT_BEFORE(cur, he, list); + sx_xunlock(&linux_ioctl_sx); + return (0); + } + } + TAILQ_INSERT_TAIL(&linux32_ioctl_handlers, he, list); + sx_xunlock(&linux_ioctl_sx); + + return (0); +} + +int +linux32_ioctl_unregister_handler(struct linux_ioctl_handler *h) +{ + struct linux_ioctl_handler_element *he; + + if (h == NULL || h->func == NULL) + return (EINVAL); + + sx_xlock(&linux_ioctl_sx); + TAILQ_FOREACH(he, &linux32_ioctl_handlers, list) { + if (he->func == h->func) { + TAILQ_REMOVE(&linux32_ioctl_handlers, he, list); + sx_xunlock(&linux_ioctl_sx); + free(he, M_LINUX); + return (0); + } + } + sx_xunlock(&linux_ioctl_sx); + + return (EINVAL); +} +#endif Modified: stable/12/sys/compat/linux/linux_ioctl.h ============================================================================== --- stable/12/sys/compat/linux/linux_ioctl.h Thu Nov 8 19:50:23 2018 (r340257) +++ stable/12/sys/compat/linux/linux_ioctl.h Thu Nov 8 19:56:29 2018 (r340258) @@ -770,7 +770,18 @@ struct linux_ioctl_handler { int low, high; }; +struct linux_ioctl_handler_element +{ + TAILQ_ENTRY(linux_ioctl_handler_element) list; + int (*func)(struct thread *, struct linux_ioctl_args *); + int low, high, span; +}; + int linux_ioctl_register_handler(struct linux_ioctl_handler *h); int linux_ioctl_unregister_handler(struct linux_ioctl_handler *h); +#ifdef COMPAT_LINUX32 +int linux32_ioctl_register_handler(struct linux_ioctl_handler *h); +int linux32_ioctl_unregister_handler(struct linux_ioctl_handler *h); +#endif #endif /* !_LINUX_IOCTL_H_ */