Compiling Kernel 6.x.x For Music Production On Debian And Downstream
These are notes to myself and their validity is not guaranteed.

If you drop by https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.x (where x=the current revision of 6) you will probably find a file in there that looks something like:

    patch-6.334.2001-rt9000.patch.xz

Given that, to save some typing you will probably want to do a search-and-replace in this document and turn

   #KernelVersion into 334,
   #KernelRevision into 2001,
   #PatchVersion into 9000, and
   #MyVersion to a lower-case alphanumeric string to identify your work (capital letters are forbidden).

Although, as I write this, the current kernel is 6.0, which you will note is unfortunately not 6.0.0. In this case, rather than change #KernelRevision, replace .#KernelRevision with nothing.

Also, if there is no RT patch, and you're doing this anyway, delete all the lines with #PatchVersion in them.
Most of the kernel compilation guides I've read assume that you'll be compiling the kernel in the directory '/usr/src', and although even Linus Torvalds say you shouldn't do that, I do, because I sometimes use VirtualBox and when I started writing this it assumed a kernel built there.
To compile in /usr/src one must normally be root. This can be weaseled around by changing the group of the 'src' directory and allowing group writes.
Since I am in the 'sudo' group:
sudo chown root:sudo /usr/src
sudo chmod 775 /usr/src
============

# Step 0: Install the software required to compile the kernel. You shouldn't need to do this step more than once. Let's check which packages aren't installed yet first...
apt-cache policy bc              # needed to make the .deb packages
apt-cache policy fakeroot        # needed to make the .deb packages
apt-cache policy build-essential # basic compilation
apt-cache policy libncurses5-dev # required for kernel configuring via 'make nconfig'
apt-cache policy flex            # required for kernel configuring
apt-cache policy bison           # required for kernel configuring
apt-cache policy libelf-dev      # see note #1 below
apt-cache policy libssl-dev      # ibid
apt-cache policy pahole          # a recently emerged esoteric requirement
apt-cache policy liblz4-tool     # optional, for some methods of kernel compressing
apt-cache policy zstd            # optional, for some methods of kernel compressing
apt-cache policy time            # optional, to see how long building the kernel takes
# Then install the ones needed. Skip the ones already installed, of course!
sudo apt-get install bc
sudo apt-get install fakeroot
sudo apt-get install build-essential
sudo apt-get install libncurses5-dev
sudo apt-get install flex
sudo apt-get install bison
sudo apt-get install libelf-dev
sudo apt-get install libssl-dev
sudo apt-get install pahole
sudo apt-get install liblz4-tool
sudo apt-get install zstd
sudo apt-get install time
# You'll also need the kernel source.
cd /usr/src && wget https://www.kernel.org/pub/linux/kernel/v6.x/linux-6.#KernelVersion.tar.xz
============

# Step 1: Get the current patch and checksum files to test validity, and then do the latter.
rm -fv sha256sums.asc* && wget https://www.kernel.org/pub/linux/kernel/v6.x/sha256sums.asc && wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.#KernelVersion/sha256sums.asc
wget https://www.kernel.org/pub/linux/kernel/v6.x/patch-6.#KernelVersion.#KernelRevision.xz
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.#KernelVersion/patch-6.#KernelVersion.#KernelRevision-rt#PatchVersion.patch.xz
grep `sha256sum linux-6.#KernelVersion.tar.xz` sha256sums.asc
grep `sha256sum patch-6.#KernelVersion.#KernelRevision.xz` sha256sums.asc # unless the current kernel is 6.0, in which case skip this
grep `sha256sum patch-6.#KernelVersion.#KernelRevision-rt#PatchVersion.patch.xz` sha256sums.asc.1
# If you get matching lines, continue; otherwise stop here, cos you're out of luck.

============

# Step 2: Unpack the kernel source; move it into its own directory; change to said directory; apply the patch
tar xf linux-6.#KernelVersion.tar.xz && mv linux-6.#KernelVersion linux-6.#KernelVersion.#KernelRevision-rt && cd linux-6.#KernelVersion.#KernelRevision-rt && xzcat ../patch-6.#KernelVersion.#KernelRevision.xz | patch -p1
xzcat ../patch-6.#KernelVersion.#KernelRevision-rt#PatchVersion.patch.xz | patch -p1
============

# Step 3: Enable the realtime feature. You need to edit the file ./arch/Kconfig and change the line after
# config ARCH_SUPPORTS_RT
# from
# bool
# to
# def_bool y
# Having to do this is, as of this writing, apparently not documented anywhere.
#
# I'm finding that enough of the RT patch set has been mainstreamed that this step is no longer really necessary...

mcedit ./arch/Kconfig # assumes mcedit is installed, of course. Use your preferred editor if you have one.

============

# Step 4: pull in the current kernel configuration.
make olddefconfig
# This creates a kernel .config file based on the running kernel's setup. Some kernel compilation guides suggest doing 'make oldconfig'. This will result in many "what do you want to do about this new feature" questions being asked; 'make olddefconfig' automates the smiling and nodding, and saves pressing Enter many many times.¹

============

# Step 4⅓: save a staggering amount of compilation disk space. If you're running a kernel you made after doing this, 'make olddefconfig' has already set this appropriately so you can skip this step.
scripts/config --disable DEBUG_INFO
============

# Step 4½: set up the kernel for music production. As with Step 4⅓, if you're running a kernel you made after doing this, 'make olddefconfig' has already set this appropriately so you can skip this step.
make nconfig
# here are the options you'll need to set:
# ========================================
    General Setup/Preemption Model
            Set to (Fully Preemptible Kernel (RT)) # a bit optional these days

    General Setup/Timers Subsystem
        [*] High Resolution Timer Support
            [*] Old Idle dynticks config
            [*] Full dynticks system on all CPUs by default (except CPU 0)

    Processor type and features
            Timer frequency (1000 HZ)

    Device Drivers/Sound Card Support/ALSA
            [*] HR-timer backend support
            [*]     Use HR-timer as default sequencer timer
# options you may want to set
# ===========================

General Setup/Timers Subsystem/Timer Tick Handling
<X> Full dynticks system (tickless)

...however, as it says on kernel.org: "systems with aggressive real-time response constraints often run CONFIG_HZ_PERIODIC=y kernels (or CONFIG_NO_HZ=n for older kernels) in order to avoid degrading from-idle transition latencies".
I personally get buffer underruns with a tickless system.
I have seen someone recommend setting CONFIG_NO_HZ_IDLE as a power-saving optimization. Musically, not so much. CONFIG_NO_HZ_FULL or nothing at all [CONFIG_HZ_PERIODIC] are better.

============

# Step 5: compile using some esoteric options I worked out over time.
#FOO=`cat /etc/debian_version`
FOO=`cat /etc/devuan_version`
FOO=`dirname $FOO`
KDEB_CHANGELOG_DIST=$FOO time nice -n 19 make -j $(getconf _NPROCESSORS_ONLN) bindeb-pkg LOCALVERSION=-#MyVersion KDEB_PKGVERSION=$(make kernelversion)-1 > /dev/shm/kcoutput.txt
# FOO is set to the the codename of the distribution. Since I'm running a "testing" version of my distribution, the codename has an unwanted slash-suffix that 'dirname' coincidentally happens to remove.
# KDEB_CHANGELOG_DIST is empty by default, which will trigger a complaint during compilation, so here it's set to FOO. Alert readers may note that when compiling under a stable version of the distribution, FOO could be skipped entirely in favor of
# KDEB_CHANGELOG_DIST=`cat /etc/devuan_version` time nice -n 19 make -j $(getconf _NPROCESSORS_ONLN) bindeb-pkg LOCALVERSION=-#MyVersion KDEB_PKGVERSION=$(make kernelversion)-1 > /dev/shm/kcoutput.txt # or debian_version
# CONCURRENCY_LEVEL is used to compile with all available cores; 'nice' keeps the system responsive.
# there used to be a CONFIG_DEBUG_SECTION_MISMATCH=y flag set here but I can't remember why I used it.
# the Debian kernel compilation page I used for reference has 'make deb-pkg' in place of 'make bindeb-pkg' but all that gets us is the building of an installable source-code package we don't need and a lot of complaints from the build system because we're not distribution kernel maintainers.
# We suppress all the non-error compilation messages like
# CC drivers/acpi/glue.o
# by sending them to a file on the /dev/shm RAM disk.

--it's possible to get CONFIG_X86_X32 enabled but no binutils support in some cases if the kernel source directory belongs to root and you're running as an unprivileged user. Try sudo make. Another common cause of CONFIG_X86_X32 enabled but no binutils support is spaces in the compilation directory. Also CONFIG_X86_32 and CONFIG_X86_X32 are two different variables. CONFIG_X86_X32 (x32 ABI for 64-bit mode) must be set to N to solve the problem related with binutils support.

============

# Step 6: install the new kernel
# Assuming the compilation succeeded, and if it didn't I can't help you, the operative result will be, in /usr/src, several new files, two of which are of no real interest to people who are not kernel devs or maintainers, which can be deleted with
rm ../linux-upstream_6.*amd64.buildinfo
rm ../linux-upstream_6.*amd64.changes
# the actual kernel and its headers can be installed with
sudo dpkg -i ../*-#MyVersion_6.#KernelVersion.#KernelRevision*deb
# if you depend on DKMS to compile a module for your video card you'll also want to install one more file to support that:
sudo dpkg -i ../linux-libc-dev_6.*amd64.deb
Having succeeded at that you are in fact

=== DONE ===
____________

NOTES
=====
Note #1: It appears that you can do without libelf-dev if you disable UNWINDER_ORC in the kernel, and without libssl-dev if you disable SYSTEM_TRUSTED_KEYRING. Whether actually doing these things is unwise I don't know. Probably.

Note #2: Once you've got a kernel working the way you want it, you can use 'make localmodconfig' at this point to filter out all the kernel modules that are not active at the moment you type the command, resulting in a kernel custom-fit to a particular computer.
Pro: smaller kernel, faster compilation.
Con: what you've used since you started the machine is not the same as what you'll need eventually — printer, scanner, storage devices including NFS shares, CDs/DVDs of various types and USB drives (and of course the various filesystems that may be found on them).
Con: When I tried it, it disabled the driver for my onboard sound chip even though it was in use.
As a side note, while making my kernel smaller by hand I disabled wireless support, only to find that I'd lost the ability to monitor the data transfer rate on my ethernet port. In order for ethernet monitoring to work the .config file needed to contain
CONFIG_COMPAT_NETLINK_MESSAGES=y
CONFIG_WANT_DEV_COREDUMP=y
CONFIG_DEV_COREDUMP=y
but those flags disappeared along with wireless support. Getting them turned back on was...tricky.

Note #3: Under the old 'make-kpkg' method you could, having built the kernel, make some trivial change to the kernel configuration and then do a changes-only build.
Using 'make deb-pkg' rather than 'make bindeb-pkg' this is not possible. In fact you can't even recompile afer a 'make clean': the build fails after countless unhelpful error messages, and it's necessary to delete all the files created by the compile and start over from Step 2.
With 'make bindeb-pkg' it appears that everything gets rebuilt.