LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Stephen Boyd <sboyd@kernel.org>
To: David Collins <collinsd@codeaurora.org>, linux-kernel@vger.kernel.org
Cc: linux-arm-msm@vger.kernel.org,
	Kiran Gunda <kgunda@codeaurora.org>,
	Anirudh Ghayal <aghayal@codeaurora.org>,
	Subbaraman Narayanamurthy <subbaram@codeaurora.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Marc Zyngier <maz@kernel.org>
Subject: Re: [RESEND PATCH] spmi: spmi-pmic-arb: fix irq_set_type race condition
Date: Tue, 03 Aug 2021 01:44:57 -0700	[thread overview]
Message-ID: <162798029780.3896750.11640439486576896470@swboyd.mtv.corp.google.com> (raw)
In-Reply-To: <53360f31-2b3e-f2ed-d894-538d2215d6c5@codeaurora.org>

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.

      reply	other threads:[~2021-08-03  8:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-28 18:02 David Collins
2021-07-31  8:20 ` Stephen Boyd
2021-08-03  1:37   ` David Collins
2021-08-03  8:44     ` Stephen Boyd [this message]

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=162798029780.3896750.11640439486576896470@swboyd.mtv.corp.google.com \
    --to=sboyd@kernel.org \
    --cc=aghayal@codeaurora.org \
    --cc=collinsd@codeaurora.org \
    --cc=kgunda@codeaurora.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=subbaram@codeaurora.org \
    --cc=tglx@linutronix.de \
    --subject='Re: [RESEND PATCH] spmi: spmi-pmic-arb: fix irq_set_type race condition' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).