diff --git a/meta/lib/oe/__init__.py b/meta/lib/oe/__init__.py
index d8db84dc9a..7357a71f27 100644
--- a/meta/lib/oe/__init__.py
+++ b/meta/lib/oe/__init__.py
@@ -12,4 +12,4 @@ __path__ = extend_path(__path__, __name__)
 BBIMPORTS = ["qa", "data", "path", "utils", "types", "package", "packagedata", \
              "packagegroup", "sstatesig", "lsb", "cachedpath", "license", "qemu", \
              "reproducible", "rust", "buildcfg", "go", "spdx30_tasks", "spdx_common", \
-             "cve_check", "tune", "classextend", "purl", "kernel", "sanity"]
+             "cve_check", "tune", "classextend", "purl", "kernel", "kernel_module", "sanity"]
diff --git a/meta/lib/oe/kernel_module.py b/meta/lib/oe/kernel_module.py
index 0d27fbaa57..edf723033f 100644
--- a/meta/lib/oe/kernel_module.py
+++ b/meta/lib/oe/kernel_module.py
@@ -25,3 +25,24 @@ def kernel_module_os_env(d, env_dict):
         env_dict['KBUILD_EXTRA_SYMBOLS'] = kbuild_extra_symbols
     else:
         env_dict['KBUILD_EXTRA_SYMBOLS'] = ''
+
+def get_ext_mod(d):
+    """
+    Extract the resolved Kbuild M= variable from an out of tree module Makefile variable database.
+    """
+    import re
+    import bb.process
+
+    try:
+        output = bb.process.run(
+            "make -C %s --dry-run --print-data-base" % d.getVar("B")
+        )[0]
+    except bb.process.ExecutionError:
+        return d.getVar("S")
+
+    for line in output.splitlines():
+        m = re.match(r'^M\s*=\s*(.*)$', line)
+        if m:
+            return m.group(1).strip()
+
+    return d.getVar("S")
