All of lore.kernel.org
 help / color / mirror / Atom feed
From: George Spelvin <lkml@sdf.org>
To: chenxiang66@hisilicon.com, ming.lei@redhat.com
Cc: axboe@kernel.dk, john.garry@huawei.com, kbusch@kernel.org,
	linux-block@vger.kernel.org, linuxarm@huawei.com, lkml@sdf.org,
	tglx@linutronix.de
Subject: Re: The irq Affinity is changed after the patch(Fixes: b1a5a73e64e9 ("genirq/affinity: Spread vectors on node according to nr_cpu ratio"))
Date: Sun, 8 Dec 2019 07:42:23 GMT	[thread overview]
Message-ID: <201912080742.xB87gNOS011043@sdf.org> (raw)
In-Reply-To: <a8a89884-8323-ff70-f35e-0fcf5d7afefc@hisilicon.com>

On Tue, 19 Nov 2019 at 11:05:55 +0800, chenxiang (M)" <chenxiang66@hisilicon.com> wrote:
> Sorry, I can't access the link you provide, but I can provide those
> irqs' affinity in the attachment.  I also write a small testcase,
> and find id is 1, 2, 3, 0 after calling sort().
> 
> struct ex_s {
>     int id;
>     int cpus;
> };
> struct ex_s ex_total[4] = {
>     {0, 8},
>     {1, 8},
>     {2, 8},
>     {3, 8}
> };
> 
> static int cmp_func(const void *l, const void *r)
> {
>     const struct ex_s *ln = l;
>     const struct ex_s *rn = r;
> 
>     return ln->cpus - rn->cpus;
> }

Your cmp_func is the problem.  sort() in the Linux kernel, like
qsort() in the C library, has never been stable, meaning that if
cmp_func() returns 0, there is no guarantee what order *l and *r
will end up in.  Minor changes to the implementation can change the
result.  You're sorting on the cpus field, which is all 8, so your
cmp_func is saying "I don't care what order the results appear in".

(A web search on "stable sort" will produce decades of discussion
on the subject, but basically an unstable sort has numerous
implementation advantages, and problems can usually be solved by
adjusting the comparison function.)

If you want to sort by ->id if ->cpus is the same, then change the
cmp_func to say so:

static int cmp_func(const void *l, const void *r)
{
    const struct ex_s *ln = l;
    const struct ex_s *rn = r;

    if (ln->cpus != rn->cpus)
	return ln->cpus - rn->cpus;
    else
	return ln->id - rn->id;
}
(This simple subtract-based compare depends on the fact that "cpus"
and "id" are both guaranteed to fit into 31 bits.  If there's any chance
The true difference could exceed INT_MAX, you need to get a bit fancier.)

  parent reply	other threads:[~2019-12-08  7:47 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-19  1:25 The irq Affinity is changed after the patch(Fixes: b1a5a73e64e9 ("genirq/affinity: Spread vectors on node according to nr_cpu ratio")) chenxiang (M)
2019-11-19  1:42 ` Ming Lei
     [not found]   ` <a8a89884-8323-ff70-f35e-0fcf5d7afefc@hisilicon.com>
2019-11-19  3:17     ` Ming Lei
2019-11-19  3:32       ` chenxiang (M)
2019-11-19  6:56         ` Ming Lei
2019-12-08  7:42     ` George Spelvin [this message]
2019-12-09  2:58       ` chenxiang (M)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201912080742.xB87gNOS011043@sdf.org \
    --to=lkml@sdf.org \
    --cc=axboe@kernel.dk \
    --cc=chenxiang66@hisilicon.com \
    --cc=john.garry@huawei.com \
    --cc=kbusch@kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linuxarm@huawei.com \
    --cc=ming.lei@redhat.com \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.