From nobody Sat Nov 30 16:51:31 2024 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 4Y0x046jZwz5fJD9; Sat, 30 Nov 2024 16:51:32 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y0x040L6Lz4nyL; Sat, 30 Nov 2024 16:51:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732985492; 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=AeH4YnOEfhXLWIANVFWNwNJ/5EQK19xYPx/JMjNdg/g=; b=t1ZhHYl0QKiKAumzBK2EPpycWx/v0CvulBUppFLlTezHlK1xBVCr+RPQiAXsFWbG2Zs7xM eYLxJrkquxVrllexMZa0VGcwNTN2o5Dpt/C+5jrDqSkTHc/kS2jvi6An0otkRwc4epymYS 4oVfnyGgP6EYi/vHsvklN2340jmt6ALj8oZGjyPuFKrrxT6ZCYHqRUvSDiHrtrfpcoyrB7 jz2xmiA+edMsnzTy7ti08YjO+mqUnHnlkSfQvihgCYJVd2BoU0c+ogbp3K7vz4h4Mde1HC 92lJnaIPaucfuhp3L6/u+F5JjL2S6ioaP2C4JuYA7MJERetk8OwO7BnSqoLvIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732985492; 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=AeH4YnOEfhXLWIANVFWNwNJ/5EQK19xYPx/JMjNdg/g=; b=D4mYDIjpkheRY133OP1PRjs6IxBS08qodtOdSk0Behnc1q+x5ZanL4XjHJ6PNofBNlW9/T dPA+A31TfBm1qJR6QqfIiOLKBRtkvRtU3lue7yfb9mx66OnR2J60SGIS6G3OxsBUiMC+Is FKQK6W56LIiVoVmS/fwjy44tSDvoYjfIU4mtrJbLGQK8ujmdFhrXidYMRcI3MMjVHWOj1v egBkxaQgajP4T8lk4yzlqiVnNBdMxgTxoKOmRkCUtwIbW3xb0VshfAZGUNKlutSjlhNU+P 4z85eOGk42Z+iROmzwICQfaMVw7sMqepsAQT5ULt1iRJ6K9rLtO7/yNfwrbDdQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732985492; a=rsa-sha256; cv=none; b=fQZySq6CsHnUK038UWriWaFzDAO8nE5CNgiUNpzgMBR6GNZ+vYHyPuL8BeH8EH+0d+C+Qe DNOrnu1sXSLBd9RXK6DqzOfDZNZpfm2KSstKHTqiX07zjvSQ4gEiFAGAvnmbVhbw2c+NeL 3l560sQ+00UhtjNgMRGIymML+cmq6GoHXvvi0PXbaHfJtesBItVcZzJgryqRint/uOIyg4 Bu7EzO4sw9a8eu1HbFXAm+J1vpgtsMR7eb/EuJkR7VqHdVYy9pAGg0Lq6dDhwFJg9umorx Ic6dQjFLENv5YwiiKqKrisuJIBD4U7tN9HOHTsi/6d6cnbS+oQvHhCVgnKVATA== 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 4Y0x0372tGzGNp; Sat, 30 Nov 2024 16:51:31 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AUGpVIJ046948; Sat, 30 Nov 2024 16:51:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AUGpVwt046945; Sat, 30 Nov 2024 16:51:31 GMT (envelope-from git) Date: Sat, 30 Nov 2024 16:51:31 GMT Message-Id: <202411301651.4AUGpVwt046945@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 33ab06dda86a - stable/13 - linuxkpi: Provide a non-NULL value for THIS_MODULE 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 33ab06dda86a0f2a4a494c6c9908412d3373721f Auto-Submitted: auto-generated The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=33ab06dda86a0f2a4a494c6c9908412d3373721f commit 33ab06dda86a0f2a4a494c6c9908412d3373721f Author: Austin Shafer AuthorDate: 2024-03-06 17:48:07 +0000 Commit: John Baldwin CommitDate: 2024-11-29 19:20:56 +0000 linuxkpi: Provide a non-NULL value for THIS_MODULE THIS_MODULE is used to differentiate modules on Linux. We currently completely stub out any Linux struct module usage, but THIS_MODULE is still used to populate the "owner" fields of various drivers. Even though we don't actually dereference these "owner" fields they are still used by drivers to check if devices/dmabufs/etc come from different modules. For example, during DRM GEM import some drivers check if the dmabuf's owner matches the dev's owner. If they match because they are both NULL drivers may incorrectly think two resources come from the same module. This adds a general purpose __this_linker_file which will point to the linker file of the module that uses it. We can then use that pointer to have a valid value for THIS_MODULE. Reviewed by: bz, jhb Differential Revision: https://reviews.freebsd.org/D44306 (cherry picked from commit 28a59100b54ff245a45fbd328266266f1c14eb8c) --- sys/compat/linuxkpi/common/include/linux/module.h | 21 +++++++++++++++++++++ sys/kern/kern_linker.c | 14 ++++++++++++++ sys/sys/linker.h | 6 ++++++ 3 files changed, 41 insertions(+) diff --git a/sys/compat/linuxkpi/common/include/linux/module.h b/sys/compat/linuxkpi/common/include/linux/module.h index 3018ba2fb8a4..df34843657db 100644 --- a/sys/compat/linuxkpi/common/include/linux/module.h +++ b/sys/compat/linuxkpi/common/include/linux/module.h @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include #include @@ -52,7 +54,26 @@ #define MODULE_SUPPORTED_DEVICE(name) #define MODULE_IMPORT_NS(_name) +/* + * THIS_MODULE is used to differentiate modules on Linux. We currently + * completely stub out any Linux struct module usage, but THIS_MODULE is still + * used to populate the "owner" fields of various drivers. Even though we + * don't actually dereference these "owner" fields they are still used by + * drivers to check if devices/dmabufs/etc come from different modules. For + * example, during DRM GEM import some drivers check if the dmabuf's owner + * matches the dev's owner. If they match because they are both NULL drivers + * may incorrectly think two resources come from the same module. + * + * To handle this we specify an undefined symbol __this_linker_file, which + * will get special treatment from the linker when resolving. This will + * populate the usages of __this_linker_file with the linker_file_t of the + * module. + */ +#ifdef KLD_MODULE +#define THIS_MODULE ((struct module *)&__this_linker_file) +#else #define THIS_MODULE ((struct module *)0) +#endif #define __MODULE_STRING(x) __stringify(x) diff --git a/sys/kern/kern_linker.c b/sys/kern/kern_linker.c index caee3aaedcd7..4f573018e18f 100644 --- a/sys/kern/kern_linker.c +++ b/sys/kern/kern_linker.c @@ -833,6 +833,20 @@ linker_file_lookup_symbol_internal(linker_file_t file, const char *name, KLD_DPF(SYM, ("linker_file_lookup_symbol: file=%p, name=%s, deps=%d\n", file, name, deps)); + /* + * Treat the __this_linker_file as a special symbol. This is a + * global that linuxkpi uses to populate the THIS_MODULE + * value. In this case we can simply return the linker_file_t. + * + * Modules compiled statically into the kernel are assigned NULL. + */ + if (strcmp(name, "__this_linker_file") == 0) { + address = (file == linker_kernel_file) ? NULL : (caddr_t)file; + KLD_DPF(SYM, ("linker_file_lookup_symbol: resolving special " + "symbol __this_linker_file to %p\n", address)); + return (address); + } + if (LINKER_LOOKUP_SYMBOL(file, name, &sym) == 0) { LINKER_SYMBOL_VALUES(file, sym, &symval); if (symval.value == 0) diff --git a/sys/sys/linker.h b/sys/sys/linker.h index b94cd9f27405..4a3a8f27ae59 100644 --- a/sys/sys/linker.h +++ b/sys/sys/linker.h @@ -125,6 +125,12 @@ typedef int linker_predicate_t(linker_file_t, void *); */ extern linker_file_t linker_kernel_file; +/* + * Special symbol which will be replaced by a reference to the linker_file_t + * of the module it is used in. + */ +extern linker_file_t __this_linker_file; + /* * Obtain a reference to a module, loading it if required. */