Re: [RESEND PATCH] spmi: spmi-pmic-arb: fix irq_set_type race condition

From: Stephen Boyd
Date: Tue Aug 03 2021 - 04:45:03 EST


Quoting David Collins (2021-08-02 18:37:46)
> On 7/31/21 1:20 AM, Stephen Boyd wrote:
> >
> > Could we have a qpnpint_spmi_set_bit/clear_bit() API that takes the bit
> > we want to touch as an argument and then does it all under the originial
> > pmic_arb->lock? Then we don't need a different lock, we can avoid that
> > drop the lock under the else if condition above, and the area for the
> > lock will be contained within the set/clear function instead of here.
>
> pmic_arb->lock is currently used tightly around the code in the SPMI bus
> callback functions which write to SPMI PMIC arbiter registers to trigger
> an SPMI transaction, poll in a loop to wait for completion, and read any
> command results. Each of these uses correspond to a command defined in
> the MIPI SPMI spec. There is no read-modify-write command in the spec.
>
> Thus, implementing qpnpint_spmi_set_bit/clear_bit() functions would
> require an approach like one of these:
>
> 1. Removing the locking from pmic_arb_read_cmd() and pmic_arb_write_cmd(),
> defining new wrapper functions around them to just contain the locking,
> and adding a read-modify-write wrapper function that locks and calls both
> pmic_arb_read_cmd() and pmic_arb_write_cmd().
>
> 2. Or, create a new function that duplicates the contents of both
> pmic_arb_read_cmd() and pmic_arb_write_cmd(), allowing it to issue two
> SPMI bus commands with pmic_arb->lock held.
>
> Option #1 seems like it would result in less clear and messy code than is
> currently present. It would also have a minor performance impact during
> simultaneous SPMI requests due to non-contentious checks, address look-ups
> and command formatting unnecessarily waiting for lock acquisition.

Sorry I don't get it. Does pmic_arb_read_cmd() no longer do any locking
after this change? I was thinking there would be

pmic_arb_read_cmd_unlocked()

pmic_arb_read_cmd()
take lock
pmic_arb_read_cmd_unlocked()
release lock

pmic_arb_write_cmd_unlocked()

pmic_arb_write_cmd()
take lock
pmic_arb_write_cmd_unlocked()
release lock

pmic_arb_read_modify_write()
take lock
pmic_arb_read_cmd_unlocked()
do bit twiddle
pmic_arb_write_cmd_unlocked()
release lock

but if the formatting is intensive then it could also be extracted to
another function

pmic_arb_fmt_read_cmd()

pmic_arb_read_cmd_unlocked()

pmic_arb_read_cmd()
pmic_arb_fmt_read_cmd()
take lock
pmic_arb_read_cmd_unlocked()
release lock

pmic_arb_fmt_write_cmd()

pmic_arb_write_cmd_unlocked()

pmic_arb_write_cmd()
pmic_arb_fmt_write_cmd()
take lock
pmic_arb_write_cmd_unlocked()
release lock

pmic_arb_read_modify_write()
r = pmic_arb_fmt_read_cmd()
w = pmic_arb_fmt_write_cmd()
take lock
pmic_arb_read_cmd_unlocked(r)
r &= w
pmic_arb_write_cmd_unlocked(w)
release lock

>
> Option #2 would likely be less messy than option #1; however, it results
> in duplication of low-level code which is undesirable.
>
> I prefer the approach used in this patch as it doesn't disrupt the
> architecture of the SPMI bus and PMIC IRQ functions. However, I'm willing
> to switch to your suggestion if you think it is a better design and
> cleaner/clearer solution. Please let me know your thoughts. Would you
> want option #1, #2, or something else?
>

It would probably become a huge patch which isn't great, but it would
focus the critical section to the thing that actually matters. This is
irq code so maybe we should write it in a way that keeps the spinlock as
tight as possible. It looks like the current spinlock is placed tightly
for this purpose, but then we use function pointers to format the
message and wait, which isn't good for straight line code.