<linux/device.h> ends up being included by 532 files.  On amd64 this
represents almost 1/3 of the 1674 kernel source files.  So any change
in a header it includes will result in a large number of rebuild.

<sys/proc.h> is one of these headers.  In drm land it is necessary to
make some scheduling decision based on the states of threads.  Most of
the files doing such dance already include specific '#ifdef __linux__',
so I believe it's reasonnable to include <sys/proc.h> directly in those
files.

With the diff below a change in <sys/proc.h> triggers the rebuild of
171 files.

It implements task_pid_nr() and get_task_comm() since they allow us to
reduce the difference with Linux sources as well as reduce the direct
usages of <sys/proc.h>.

Debug macros are now wrappers around a function such that only one place
needs to dereference `curproc'.

Tested on Intel only, I don't know any amgpu machine.

Index: drm_ioctl.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/drm_ioctl.c,v
retrieving revision 1.17
diff -u -p -r1.17 drm_ioctl.c
--- drm_ioctl.c 5 Jan 2020 04:29:11 -0000       1.17
+++ drm_ioctl.c 18 Feb 2020 13:02:38 -0000
@@ -35,6 +35,8 @@
 #ifdef __linux__
 #include <drm/drm_auth.h>
 #include "drm_legacy.h"
+#else
+#include <sys/proc.h>
 #endif
 #include "drm_internal.h"
 #include "drm_crtc_internal.h"
Index: drm_linux.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/drm_linux.c,v
retrieving revision 1.56
diff -u -p -r1.56 drm_linux.c
--- drm_linux.c 16 Jan 2020 16:35:03 -0000      1.56
+++ drm_linux.c 18 Feb 2020 13:20:38 -0000
@@ -23,6 +23,8 @@
 #include <sys/kthread.h>
 #include <sys/stat.h>
 #include <sys/unistd.h>
+#include <sys/proc.h>
+#include <sys/signalvar.h>
 #include <linux/dma-buf.h>
 #include <linux/mod_devicetable.h>
 #include <linux/acpi.h>
@@ -119,6 +121,19 @@ wake_up_process(struct proc *p)
        return wakeup_proc(p, NULL);
 }
 
+pid_t
+task_pid_nr(struct proc *p)
+{
+       return p->p_p->ps_pid;
+}
+
+char *
+__get_task_comm(char *dst, size_t dstsize, struct proc *p)
+{
+       strlcpy(dst, p->p_p->ps_comm, dstsize);
+       return dst;
+}
+
 void
 flush_workqueue(struct workqueue_struct *wq)
 {
@@ -1886,4 +1901,28 @@ drmbackoff(long npages)
                npages -= ret;
                shrinker = TAILQ_NEXT(shrinker, next);
        }
+}
+
+int
+signal_pending(struct proc *p)
+{
+       return SIGPENDING(p);
+}
+
+int
+signal_pending_state(long state, struct proc *p)
+{
+       return SIGPENDING(p);
+}
+
+void
+_dev_print(const char *type, const char *func, const char *fmt, ...)
+{
+       va_list ap;
+
+       printf("drm:pid%d:%s *%s* ", curproc->p_p->ps_pid, func, type);
+
+       va_start(ap, fmt);
+       vprintf(fmt, ap);
+       va_end(ap);
 }
Index: amd/amdgpu/amdgpu_vm.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/amd/amdgpu/amdgpu_vm.c,v
retrieving revision 1.6
diff -u -p -r1.6 amdgpu_vm.c
--- amd/amdgpu/amdgpu_vm.c      1 Jan 2020 00:10:18 -0000       1.6
+++ amd/amdgpu/amdgpu_vm.c      18 Feb 2020 13:13:09 -0000
@@ -35,6 +35,10 @@
 #include "amdgpu_amdkfd.h"
 #include "amdgpu_gmc.h"
 
+#ifndef __linux__
+#include <sys/proc.h>
+#endif
+
 /**
  * DOC: GPUVM
  *
@@ -3145,8 +3149,7 @@ void amdgpu_vm_set_task_info(struct amdg
                }
 #else
                vm->task_info.pid = curproc->p_p->ps_pid;
-               strlcpy(vm->task_info.task_name, curproc->p_p->ps_comm,
-                   sizeof(vm->task_info.task_name));
+               get_task_comm(vm->task_info.task_name, current);
 #endif
        }
 }
Index: i915/i915_gem.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/i915_gem.c,v
retrieving revision 1.124
diff -u -p -r1.124 i915_gem.c
--- i915/i915_gem.c     13 Nov 2019 06:41:55 -0000      1.124
+++ i915/i915_gem.c     18 Feb 2020 13:02:21 -0000
@@ -47,6 +47,10 @@
 #include <linux/pci.h>
 #include <linux/dma-buf.h>
 
+#ifndef __linux__
+#include <sys/proc.h>
+#endif
+
 static void i915_gem_flush_free_objects(struct drm_i915_private *i915);
 
 static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
Index: i915/i915_gem_context.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/i915_gem_context.c,v
retrieving revision 1.18
diff -u -p -r1.18 i915_gem_context.c
--- i915/i915_gem_context.c     17 Jan 2020 23:17:01 -0000      1.18
+++ i915/i915_gem_context.c     18 Feb 2020 13:01:32 -0000
@@ -92,6 +92,10 @@
 #include "i915_trace.h"
 #include "intel_workarounds.h"
 
+#ifndef __linux__
+#include <sys/proc.h>
+#endif
+
 #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
 
 static void lut_close(struct i915_gem_context *ctx)
Index: i915/i915_gpu_error.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/i915_gpu_error.c,v
retrieving revision 1.3
diff -u -p -r1.3 i915_gpu_error.c
--- i915/i915_gpu_error.c       14 Apr 2019 10:14:51 -0000      1.3
+++ i915/i915_gpu_error.c       18 Feb 2020 12:58:52 -0000
@@ -33,6 +33,8 @@
 #include <drm/drm_print.h>
 #ifdef __linux__
 #include <linux/ascii85.h>
+#else
+#include <sys/proc.h>  /* p_p->ps_comm & p_p->ps_pid */
 #endif
 
 #include "i915_drv.h"
Index: i915/i915_irq.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/i915_irq.c,v
retrieving revision 1.33
diff -u -p -r1.33 i915_irq.c
--- i915/i915_irq.c     14 Apr 2019 10:14:51 -0000      1.33
+++ i915/i915_irq.c     18 Feb 2020 13:00:26 -0000
@@ -37,6 +37,10 @@
 #include "i915_trace.h"
 #include "intel_drv.h"
 
+#ifndef __linux__
+#include <sys/proc.h>  /* p->p_stat */
+#endif
+
 /**
  * DOC: interrupt handling
  *
Index: i915/intel_breadcrumbs.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/intel_breadcrumbs.c,v
retrieving revision 1.6
diff -u -p -r1.6 intel_breadcrumbs.c
--- i915/intel_breadcrumbs.c    18 Feb 2020 12:13:39 -0000      1.6
+++ i915/intel_breadcrumbs.c    18 Feb 2020 12:56:57 -0000
@@ -28,6 +28,7 @@
 #else
 #include <sys/types.h>
 #include <sys/param.h>
+#include <sys/proc.h>
 #endif
 
 #include "i915_drv.h"
Index: i915/intel_engine_cs.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/i915/intel_engine_cs.c,v
retrieving revision 1.2
diff -u -p -r1.2 intel_engine_cs.c
--- i915/intel_engine_cs.c      4 Jul 2019 01:53:23 -0000       1.2
+++ i915/intel_engine_cs.c      18 Feb 2020 12:58:28 -0000
@@ -28,6 +28,10 @@
 #include "intel_ringbuffer.h"
 #include "intel_lrc.h"
 
+#ifndef __linux__
+#include <sys/proc.h>  /* p_p->ps_comm & p_p->ps_pid in drm_printf() */
+#endif
+
 /* Haswell does have the CXT_SIZE register however it does not appear to be
  * valid. Now, docs explain in dwords what is in the context object. The full
  * size is 70720 bytes, however, the power context and execlist context will
Index: include/linux/device.h
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/include/linux/device.h,v
retrieving revision 1.1
diff -u -p -r1.1 device.h
--- include/linux/device.h      14 Apr 2019 10:14:53 -0000      1.1
+++ include/linux/device.h      18 Feb 2020 12:35:53 -0000
@@ -7,7 +7,7 @@
 #include <sys/systm.h>
 #include <sys/device.h>
 #include <sys/param.h>
-#include <sys/proc.h>
+#include <sys/event.h>
 #include <linux/slab.h>
 #include <linux/ioport.h>
 #include <linux/lockdep.h>
@@ -36,28 +36,24 @@ struct device_attribute {
 
 #define devm_kzalloc(x, y, z)  kzalloc(y, z)
 
+void _dev_print(const char *, const char *, const char *, ...);
+
 #define dev_warn(dev, fmt, arg...)                             \
-       printf("drm:pid%d:%s *WARNING* " fmt, curproc->p_p->ps_pid,     \
-           __func__ , ## arg)
+       _dev_print("WARNING", __func__, fmt, ## arg)
 #define dev_notice(dev, fmt, arg...)                           \
-       printf("drm:pid%d:%s *NOTICE* " fmt, curproc->p_p->ps_pid,      \
-           __func__ , ## arg)
+       _dev_print("NOTICE", __func__, fmt, ## arg)
 #define dev_crit(dev, fmt, arg...)                             \
-       printf("drm:pid%d:%s *ERROR* " fmt, curproc->p_p->ps_pid,       \
-           __func__ , ## arg)
+       _dev_print("CRIT", __func__, fmt, ## arg)
 #define dev_err(dev, fmt, arg...)                              \
-       printf("drm:pid%d:%s *ERROR* " fmt, curproc->p_p->ps_pid,       \
-           __func__ , ## arg)
-#define dev_printk(level, dev, fmt, arg...)                            \
-       printf("drm:pid%d:%s *PRINTK* " fmt, curproc->p_p->ps_pid,      \
-           __func__ , ## arg)
+       _dev_print("ERROR", __func__, fmt, ## arg)
+#define dev_printk(level, dev, fmt, arg...)                    \
+       _dev_print("PRINTK", __func__, fmt, ## arg)
 
 #ifdef DRMDEBUG
 #define dev_info(dev, fmt, arg...)                             \
        printf("drm: " fmt, ## arg)
 #define dev_dbg(dev, fmt, arg...)                              \
-       printf("drm:pid%d:%s *DEBUG* " fmt, curproc->p_p->ps_pid,       \
-           __func__ , ## arg)
+       _dev_print("DEBUG", __func__, fmt, ## arg)
 #else
 #define dev_info(dev, fmt, arg...)                             \
            do { } while(0)
Index: include/linux/sched.h
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/include/linux/sched.h,v
retrieving revision 1.2
diff -u -p -r1.2 sched.h
--- include/linux/sched.h       18 Feb 2020 12:13:40 -0000      1.2
+++ include/linux/sched.h       18 Feb 2020 13:10:40 -0000
@@ -48,5 +48,9 @@ long schedule_timeout(long);
 
 struct proc;
 int wake_up_process(struct proc *p);
+pid_t task_pid_nr(struct proc *p);
+
+#define get_task_comm(_b, _p)  __get_task_comm((_b), sizeof(_b), (_p))
+char *__get_task_comm(char *, size_t, struct proc *);
 
 #endif
Index: include/linux/sched/signal.h
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/include/linux/sched/signal.h,v
retrieving revision 1.2
diff -u -p -r1.2 signal.h
--- include/linux/sched/signal.h        18 Feb 2020 07:40:49 -0000      1.2
+++ include/linux/sched/signal.h        18 Feb 2020 12:48:23 -0000
@@ -3,10 +3,10 @@
 #ifndef _LINUX_SCHED_SIGNAL_H
 #define _LINUX_SCHED_SIGNAL_H
 
-#include <sys/systm.h>
-#include <sys/signalvar.h>
+#define current curproc
 
-#define signal_pending_state(x, y) SIGPENDING(curproc)
-#define signal_pending(y) SIGPENDING(curproc)
+struct proc;
+int signal_pending(struct proc *);
+int signal_pending_state(long, struct proc *);
 
 #endif
Index: radeon/radeon_gem.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/radeon/radeon_gem.c,v
retrieving revision 1.12
diff -u -p -r1.12 radeon_gem.c
--- radeon/radeon_gem.c 14 Apr 2019 10:14:54 -0000      1.12
+++ radeon/radeon_gem.c 18 Feb 2020 12:49:34 -0000
@@ -79,7 +79,7 @@ retry:
                return r;
        }
        *obj = &robj->gem_base;
-       robj->pid = curproc->p_p->ps_pid;
+       robj->pid = task_pid_nr(current);
 
        mutex_lock(&rdev->gem.mutex);
        list_add_tail(&robj->list, &rdev->gem.objects);
Index: scheduler/gpu_scheduler.c
===================================================================
RCS file: /cvs/src/sys/dev/pci/drm/scheduler/gpu_scheduler.c,v
retrieving revision 1.5
diff -u -p -r1.5 gpu_scheduler.c
--- scheduler/gpu_scheduler.c   18 Feb 2020 12:13:40 -0000      1.5
+++ scheduler/gpu_scheduler.c   18 Feb 2020 13:19:42 -0000
@@ -49,6 +49,8 @@
 #include <linux/sched.h>
 #ifdef __linux__
 #include <uapi/linux/sched/types.h>
+#else
+#include <sys/proc.h>
 #endif
 #include <drm/drmP.h>
 #include <drm/gpu_scheduler.h>

Reply via email to