stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Halil Pasic <pasic@linux.ibm.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: "Toke Høiland-Jørgensen" <toke@toke.dk>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Christoph Hellwig" <hch@lst.de>,
	"Maxime Bizon" <mbizon@freebox.fr>,
	"Oleksandr Natalenko" <oleksandr@natalenko.name>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Kalle Valo" <kvalo@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Olha Cherevyk" <olha.cherevyk@gmail.com>,
	iommu <iommu@lists.linux-foundation.org>,
	linux-wireless <linux-wireless@vger.kernel.org>,
	Netdev <netdev@vger.kernel.org>,
	"Linux Kernel Mailing List" <linux-kernel@vger.kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	stable <stable@vger.kernel.org>,
	"Halil Pasic" <pasic@linux.ibm.com>
Subject: Re: [REGRESSION] Recent swiotlb DMA_FROM_DEVICE fixes break ath9k-based AP
Date: Mon, 28 Mar 2022 01:37:31 +0200	[thread overview]
Message-ID: <20220328013731.017ae3e3.pasic@linux.ibm.com> (raw)
In-Reply-To: <CAHk-=whUJ=tMEgP3KiWwk0pzmHn+1QORUu50syE+zOGk4UnFog@mail.gmail.com>

On Sat, 26 Mar 2022 22:06:15 -0700
Linus Torvalds <torvalds@linux-foundation.org> wrote:

> On Sat, Mar 26, 2022 at 8:49 PM Halil Pasic <pasic@linux.ibm.com> wrote:
> >
> > I agree it CPU modified buffers *concurrently* with DMA can never work,
> > and I believe the ownership model was conceived to prevent this
> > situation.  
> 
> But that just means that the "ownership" model is garbage, and cannot
> handle this REAL LIFE situation.
> 
> Here's the deal: if somebody makes a model that is counter-factual,
> you have exactly two choices:
> 
>  - fix the damn model
> 
>  - live in a la-la-land that isn't reality
> 
> Which choice do you pick?

I pix "fix the dam model". This email of mine was supposed to discuss how
the model can be fixed:
https://www.spinics.net/lists/linux-wireless/msg222442.html

> 
> And I'll be brutally honest: if you pick the la-la-land one, I don't
> think we can really discuss this any more.

I completely agree. Never intended to pick the la-la-land one.

> 
> > But a CPU can modify the buffer *after* DMA has written to
> > it, while the mapping is still alive.  
> 
> Yes.
> 
> But you are making ALL your arguments based on that "ownership" model
> that we now know is garbage.

Sorry, I'm not very knowledgeable when it comes all the different
hardware out there.

> 
> If you make your arguments based on garbage, the end result _might_
> work just by happenstance, but the arguments sure aren't very
> convincing, are they?

No it is not. I have to admit, I did see some value in talking about the
model that is described by the current documentation for two reasons:
1) Not everybody has great knowledge of all the hardware out there, and
there might be people other than me, who based their work on that broken
model. And thus wrote code that is correct within the broken model,
but not correct within the fixed mode. Does not see to be the case here,
but I was not able to tell.
2) To fix the model, we have to understand both reality and the model.
Or we have to replace it with an entirely new one.

> 
> So let's make it really clear that the arguments must not be based on
> some "ownership" model that you just admitted cannot handle the very
> real case of real and common hardware.
> 
> Ok?

Sure. It was never my intention to base my argument on false assumptions.

> 
> >  For example one could do one
> > partial read from the device, *after* the DMA is done,
> > sync_for_cpu(DMA_FROM_DEVICE), examine, then zero out the entire buffer,
> > sync_for_device(DMA_FROM_DEVICE)  
> 
> So the result you want to get to I can believe in, but your sequence
> of getting there is untenable, since it depends on breaking other
> cases that are more important than your utterly broken hardware that
> you don't even know how much data it filled.
> 

I agree, and that is is the very reason I said, I'm not against the
partial revert (see
https://www.spinics.net/lists/linux-wireless/msg222326.html)

Hey, I don't even know if there is a single usage of what I described.
In fact I asked the community is this even something legit. What I know,
is that the current (broken) model does allow the scenario.

> And I fundamentally also happen to think that since the CPU just wrote
> that buffer, and *that* write is what you want to sync with the
> device, then that DMA_FROM_DEVICE was just pure fantasy to begin with.
> 

Not sync with the device, but with the memory. And it is supposed to
happen after the DMA has completed, and not while the DMA is ongoing.

But I'm clearly not knowledgeable enough to have this discussion. I'm
afraid if I continue, I will just drag the community down.

> So that code sequence you quote is wrong. You are literally trying to
> re-introduce the semantics that we already know broke the ath9k
> driver.
> 
> Instead, let me give you a couple of alternative scenarios.
> 
> Alternative 1:
> 
>  - ok, so the CPU wrote zeroes to the area, and we want to tell the
> DMA mapping code that it needs to make that CPU write visible to the
> device.

Not make visible to the device but make actually it RAM instead of 
remaining in cache. My most basic mental model is:

+----------------+
|                |        +---------+         +--------+
|     +--------+ |        |         |   DMA   |        |
| CPU | CACHE  | |  <---> |   RAM   |  <----> | DEVICE |
|     +--------+ |        |         |         |        |
|                |        +---------+         +--------+
+----------------+

> 
>  - Ahh, you mean "sync_for_device(DMA_TO_DEVICE)"?
> 
>  - Yes.
> 
>    The "for_device()" tells us that afterwards, the device can access
> the memory (we synced it for the device).
> 
>    And the DMA_TO_DEVICE tells us that we're transferring the zeroes
> we wrote on the CPU to the device bounce buffer.
> 
>    Now the device got those zeroes, and it can overwrite them
> (partially), and everything is fine
> 
>  - And then we need to use "sync_for_cpu(DMA_FROM_DEVICE)" when we
> want to see the result once it's all done?
> 
>  - Yes.
> 
>  - Splendid. Except I don't like how you mix DMA_TO_DEVICE and
> DMA_FROM_DEVICE and you made the dma_alloc() not use DMA_BIDIRECTIONAL
> 
>  - Yeah, that's ugly, but it matches reality, and it would "just work" today.
> 

It is certainly an option. The tricky part is that one is supposed to use
DMA_TO_DEVICE even if the device does not read RAM but only writes it,
and thus the direction of the data flow of the direct memory access (DMA)
is from the device to the memory (RAM).

> Alternative 2:
> 
>  - Ok, so the CPU doesn't even want to write to the area AT ALL, but
> we know we have a broken device that might not fill all of the bounce
> buffer, and we don't want to see old stale bounce buffer contents that
> could come from some other operation entirely and leak sensitive data
> that wasn't for us.
> 
>  - Ahh, so you really want to just clear the bounce buffer before IO?
> 
>  - Yes. So let's introduce a "clear_bounce_buffer()" operation before
> starting DMA. Let's call it "sync_for_device(DMA_NONE)" and teach the
> non-bounce-buffer dmas mapping entities to just ignore it, because
> they don't have a bounce buffer that can contain stale data.
> 
>  - Sounds good.
> 

It is an option.

> Alternative 3:
> 
>  - Ok, you have me stumped. I can't come up with something else sane.
> 

I have tired to in
https://www.spinics.net/lists/linux-wireless/msg222442.html

> Anyway, notice what's common about these alternatives? They are based
> not on some "we have a broken model", but on trying to solve the
> actual real-life problem case.

Yes.
> 
> I'm more than happy to hear other alternatives.
> 
> But the alternative I am _not_ willing to entertain is "Yeah, we have
> a model of ownership, and that can't handle ath9k because that one
> wants to do CPU reads while DMA is possibly active, so ath9k is
> broken".
> 
> Can we please agree on that?
> 

Yes.

AFAIU the main reason we have should postulate the ath9k code is correct,
is that it would be prohibitively expensive not to do so. Because we
can comparatively easily change ath9k, but we can't be sure about other
uses of sync_for_device unless we audit all of them. That does make
perfect sense for me.

IMHO that also means that the whole "ownership" concept is beyond saving,
and that we should rewrite the corresponding parts of the documentation.
Thus my effort at saving it was misguided.

Give how this unfolded, I intend to let the more knowledgeable people
hash out the new model, and avoiding dragging down the community by
being too vocal about my opinion.

For the record, I believe that the partial revert proposed here
https://www.spinics.net/lists/linux-wireless/msg222300.html
would have been a wiser choice, than a complete revert of commit
aa6f8dcbab47 ("swiotlb: rework "fix info leak with DMA_FROM_DEVICE"").
The reason why is in the commit message, and was also pointed out by
Robin.

Regards,
Halil

  parent reply	other threads:[~2022-03-27 23:38 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-23  7:19 [REGRESSION] Recent swiotlb DMA_FROM_DEVICE fixes break ath9k-based AP Oleksandr Natalenko
2022-03-23  7:28 ` Kalle Valo
2022-03-23 17:27 ` Linus Torvalds
2022-03-23 19:06   ` Robin Murphy
2022-03-23 19:16     ` Linus Torvalds
2022-03-23 20:54       ` Robin Murphy
2022-03-24  5:57         ` Christoph Hellwig
2022-03-24 10:25           ` Oleksandr Natalenko
2022-03-24 11:05             ` Robin Murphy
2022-03-24 14:27               ` Toke Høiland-Jørgensen
2022-03-24 16:29                 ` Maxime Bizon
2022-03-24 16:31                   ` Christoph Hellwig
2022-03-24 16:52                     ` Robin Murphy
2022-03-24 17:07                       ` Toke Høiland-Jørgensen
2022-03-24 19:26                         ` Linus Torvalds
2022-03-24 21:14                           ` Toke Høiland-Jørgensen
2022-03-25 10:25                           ` Maxime Bizon
2022-03-25 11:27                             ` Robin Murphy
2022-03-25 23:38                               ` Halil Pasic
2022-03-26 16:05                                 ` Toke Høiland-Jørgensen
2022-03-26 18:38                                   ` Linus Torvalds
2022-03-26 22:38                                     ` David Laight
2022-03-26 22:41                                       ` Linus Torvalds
2022-03-25 16:25                             ` Toke Høiland-Jørgensen
2022-03-25 16:45                               ` Robin Murphy
2022-03-25 18:13                                 ` Toke Høiland-Jørgensen
2022-03-25 18:30                             ` Linus Torvalds
2022-03-25 19:14                               ` Robin Murphy
2022-03-25 19:21                                 ` Linus Torvalds
2022-03-25 19:26                               ` Oleksandr Natalenko
2022-03-25 19:27                                 ` Linus Torvalds
2022-03-25 19:35                                   ` Oleksandr Natalenko
2022-03-25 20:37                               ` Johannes Berg
2022-03-25 20:47                                 ` Linus Torvalds
2022-03-25 21:13                                   ` Johannes Berg
2022-03-25 21:40                                     ` David Laight
2022-03-25 21:56                                     ` Linus Torvalds
2022-03-25 22:41                                       ` David Laight
2022-03-27  3:15                                     ` Halil Pasic
2022-03-28  9:48                                       ` Johannes Berg
2022-03-28  9:50                                         ` Johannes Berg
2022-03-28  9:57                                           ` Johannes Berg
2022-03-27  3:48                           ` Halil Pasic
2022-03-27  5:06                             ` Linus Torvalds
2022-03-27  5:21                               ` Linus Torvalds
2022-03-27 15:24                                 ` David Laight
2022-03-27 19:23                                   ` Linus Torvalds
2022-03-27 20:04                                     ` Linus Torvalds
2022-03-27 23:52                                 ` Halil Pasic
2022-03-28  0:30                                   ` Linus Torvalds
2022-03-28 12:02                                     ` Halil Pasic
2022-03-27 23:37                               ` Halil Pasic [this message]
2022-03-28  0:37                                 ` Linus Torvalds
2022-03-25  7:12                         ` Oleksandr Natalenko
2022-03-25  9:21                           ` Thorsten Leemhuis
2022-03-24 18:31                       ` Halil Pasic
2022-03-25 16:31                         ` Christoph Hellwig
2022-03-24 18:02         ` Halil Pasic
2022-03-25 15:25           ` Halil Pasic
2022-03-25 16:23             ` Robin Murphy
2022-03-25 16:32           ` Christoph Hellwig
2022-03-25 18:15             ` Toke Høiland-Jørgensen
2022-03-25 18:42               ` Robin Murphy
2022-03-25 18:46                 ` Linus Torvalds
2022-03-28  6:37                   ` Christoph Hellwig
2022-03-28  8:15                     ` David Laight
2022-03-30 12:11                     ` Halil Pasic
2022-03-24  8:55   ` Oleksandr Natalenko

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=20220328013731.017ae3e3.pasic@linux.ibm.com \
    --to=pasic@linux.ibm.com \
    --cc=davem@davemloft.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux-foundation.org \
    --cc=kuba@kernel.org \
    --cc=kvalo@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=mbizon@freebox.fr \
    --cc=netdev@vger.kernel.org \
    --cc=oleksandr@natalenko.name \
    --cc=olha.cherevyk@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=stable@vger.kernel.org \
    --cc=toke@toke.dk \
    --cc=torvalds@linux-foundation.org \
    /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 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).