From: Christoph Hellwig <hch@lst.de>
To: LEROY Christophe <christophe.leroy2@cs-soprasteria.com>
Cc: Christoph Hellwig <hch@lst.de>,
Christian Lamparter <christian.lamparter@isd.uni-stuttgart.de>,
Benjamin Herrenschmidt <benh@kernel.crashing.org>,
Paul Mackerras <paulus@samba.org>,
Michael Ellerman <mpe@ellerman.id.au>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"linuxppc-dev@lists.ozlabs.org" <linuxppc-dev@lists.ozlabs.org>,
Stan Johnson <userm57@yahoo.com>,
Finn Thain <fthain@linux-m68k.org>
Subject: Re: [PATCH v2] powerpc: warn on emulation of dcbz instruction in kernel mode
Date: Thu, 22 Aug 2024 09:14:43 +0200 [thread overview]
Message-ID: <20240822071443.GA6395@lst.de> (raw)
In-Reply-To: <e6acf664-5ebd-4273-9330-cbec283ede23@cs-soprasteria.com>
On Thu, Aug 22, 2024 at 06:39:33AM +0000, LEROY Christophe wrote:
> powerpc has a magic instruction 'dcbz' which clears a full cacheline in
> one go. It is far more efficient than a loop to store zeros, and since
> 2015 memset(0) has been implemented with that instruction (commit
> 5b2a32e80634 ("powerpc/32: memset(0): use cacheable_memzero"))
>
> But that instruction generates an alignment exception when used on
> non-cached memory (whether it is RAM or not doesn't matter). It is then
> emulated by the kernel but it of course leads to a serious performance
> degradation, hence the warning added by commit cbe654c77961 ("powerpc:
> warn on emulation of dcbz instruction in kernel mode"). Until now it
> helped identify and fix use of memset() on IO memory.
>
> But if memset() is expected to be used with non-cached RAM, then I don't
> know what to do. Any suggestion ?
I'd suggest two things:
1) remove the warning. The use case is perfectly valid and everything
using uncached memory is already slow, so people will just have to
deal with it. Maybe offer a trace point instead if people care about
it.
2) figure out a way to avoid this case in the dma-coherent allocator,
which is probably the only case where it happens frequently
(a few drivers also zero or re-zero coherent memory, but most of the
time that is cargo cult programming and not actually needed)
For 2 I can think of two options:
a) provide a arch hook for zeroing the dma memory that defaults to
memset, but which powerpc can override
a) figure out a way to clear the memory before marking it uncached
if we can
a) it obviously easier to verify, but b) is probably going to give
way better performance.
Below is an untested implementation of b) for dma-direct, I just need to
find out if there is any architecture that requires the memory to be
zeroed after it іt has been remapped. The iommu drivers might also
need similar treatment.
diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c
index 4480a3cd92e087..66e94b32ab0081 100644
--- a/kernel/dma/direct.c
+++ b/kernel/dma/direct.c
@@ -275,6 +275,9 @@ void *dma_direct_alloc(struct device *dev, size_t size,
if (force_dma_unencrypted(dev))
prot = pgprot_decrypted(prot);
+ if (!PageHighMem(page))
+ memset(page_address(page), 0, size);
+
/* remove any dirty cache lines on the kernel alias */
arch_dma_prep_coherent(page, size);
@@ -283,14 +286,15 @@ void *dma_direct_alloc(struct device *dev, size_t size,
__builtin_return_address(0));
if (!ret)
goto out_free_pages;
+ if (PageHighMem(page))
+ memset(ret, 0, size);
} else {
ret = page_address(page);
if (dma_set_decrypted(dev, ret, size))
goto out_leak_pages;
+ memset(ret, 0, size);
}
- memset(ret, 0, size);
-
if (set_uncached) {
arch_dma_prep_coherent(page, size);
ret = arch_dma_set_uncached(ret, size);
next prev parent reply other threads:[~2024-08-22 7:16 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 14:52 [PATCH v2] powerpc: warn on emulation of dcbz instruction in kernel mode Christophe Leroy
2021-11-02 10:11 ` Michael Ellerman
2024-08-21 22:39 ` Christian Lamparter
2024-08-22 5:25 ` LEROY Christophe
2024-08-22 5:32 ` Christoph Hellwig
2024-08-22 6:39 ` LEROY Christophe
2024-08-22 7:14 ` Christoph Hellwig [this message]
2024-08-22 9:53 ` Benjamin Herrenschmidt
2024-08-22 18:19 ` Christian Lamparter
2024-08-23 8:07 ` Christoph Hellwig
2024-08-23 13:06 ` Segher Boessenkool
2024-08-23 13:54 ` Christoph Hellwig
2024-08-23 19:19 ` Segher Boessenkool
2024-08-23 23:43 ` Christian Lamparter
2024-08-24 9:01 ` LEROY Christophe
2024-08-24 17:17 ` Segher Boessenkool
2024-08-27 7:29 ` Christoph Hellwig
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=20240822071443.GA6395@lst.de \
--to=hch@lst.de \
--cc=benh@kernel.crashing.org \
--cc=christian.lamparter@isd.uni-stuttgart.de \
--cc=christophe.leroy2@cs-soprasteria.com \
--cc=fthain@linux-m68k.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mpe@ellerman.id.au \
--cc=paulus@samba.org \
--cc=userm57@yahoo.com \
/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).