*   assignment causes no harm).
         */
        new_bfqq->bic = NULL;
+       /*
+        * If the queue is shared, the pid is the pid of one of the associated
+        * processes. Which pid depends on the exact sequence of merge events
+        * the queue underwent. So printing such a pid is useless and confusing
+        * because it reports a random pid between those of the associated
+        * processes.
+        * We mark such a queue with a pid -1, and then print SHARED instead of
+        * a pid in logging messages.
+        */
+       new_bfqq->pid = -1;
        bfqq->bic = NULL;
        /* release process reference to bfqq */
        bfq_put_queue(bfqq);
 
 #define BFQ_DEFAULT_GRP_IOPRIO 0
 #define BFQ_DEFAULT_GRP_CLASS  IOPRIO_CLASS_BE
 
+#define MAX_PID_STR_LENGTH 12
+
 /*
  * Soft real-time applications are extremely more latency sensitive
  * than interactive ones. Over-raise the weight of the former to
 /* --------------- end of interface of B-WF2Q+ ---------------- */
 
 /* Logging facilities. */
+static inline void bfq_pid_to_str(int pid, char *str, int len)
+{
+       if (pid != -1)
+               snprintf(str, len, "%d", pid);
+       else
+               snprintf(str, len, "SHARED-");
+}
+
 #ifdef CONFIG_BFQ_GROUP_IOSCHED
 struct bfq_group *bfqq_group(struct bfq_queue *bfqq);
 
 #define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do {                    \
+       char pid_str[MAX_PID_STR_LENGTH];       \
+       bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH);       \
        blk_add_cgroup_trace_msg((bfqd)->queue,                         \
                        bfqg_to_blkg(bfqq_group(bfqq))->blkcg,          \
-                       "bfq%d%c " fmt, (bfqq)->pid,                    \
+                       "bfq%s%c " fmt, pid_str,                        \
                        bfq_bfqq_sync((bfqq)) ? 'S' : 'A', ##args);     \
 } while (0)
 
 
 #else /* CONFIG_BFQ_GROUP_IOSCHED */
 
-#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) \
-       blk_add_trace_msg((bfqd)->queue, "bfq%d%c " fmt, (bfqq)->pid,   \
+#define bfq_log_bfqq(bfqd, bfqq, fmt, args...) do {    \
+       char pid_str[MAX_PID_STR_LENGTH];       \
+       bfq_pid_to_str((bfqq)->pid, pid_str, MAX_PID_STR_LENGTH);       \
+       blk_add_trace_msg((bfqd)->queue, "bfq%s%c " fmt, pid_str,       \
                        bfq_bfqq_sync((bfqq)) ? 'S' : 'A',              \
-                               ##args)
+                               ##args);        \
+} while (0)
 #define bfq_log_bfqg(bfqd, bfqg, fmt, args...)         do {} while (0)
 
 #endif /* CONFIG_BFQ_GROUP_IOSCHED */