x86: Add support for the pcommit instruction
PATCH v4 1/1 x86: Add support for the pcommit instruction
Add support for the new pcommit (persistent commit) instruction. This instruction was announced in the document "Intel Architecture Instruction Set Extensions Programming Reference" with reference number 319433-022.
https://software.intel.com/sites/default/files/managed/0d/53/319433-022.pdf
The pcommit instruction ensures that data that has been flushed from the processor's cache hierarchy with clwb, clflushopt or clflush is accepted to memory and is durable on the DIMM. The primary use case for this is persistent memory.
This function shows how to properly use clwb/clflushopt/clflush and pcommit with appropriate fencing:
void flush_and_commit_buffer(void *vaddr, unsigned int size)
{
void *vend = vaddr + size - 1;
for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
clwb(vaddr);
/* Flush any possible final partial cacheline */
clwb(vend);
/*
* sfence to order clwb/clflushopt/clflush cache flushes
* mfence via mb() also works
*/
wmb();
/* pcommit and the required sfence for ordering */
pcommit_sfence();
}After this function completes the data pointed to by vaddr is has been accepted to memory and will be durable if the vaddr points to persistent memory.
Pcommit must always be ordered by an mfence or sfence, so to help simplify things we include both the pcommit and the required sfence in the alternatives generated by pcommit_sfence(). The other option is to keep them separated, but on platforms that don't support pcommit this would then turn into:
void flush_and_commit_buffer(void *vaddr, unsigned int size)
{
void *vend = vaddr + size - 1;
for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
clwb(vaddr);
/* Flush any possible final partial cacheline */
clwb(vend);
/*
* sfence to order clwb/clflushopt/clflush cache flushes
* mfence via mb() also works
*/
wmb();
nop(); /* from pcommit(), via alternatives */
/*
* sfence to order pcommit
* mfence via mb() also works
*/
wmb();
}This is still correct, but now you've got two fences separated by only a nop. With the commit and the fence together in pcommit_sfence() you avoid the final unneeded fence.
--- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/include/asm/special_insns.h | 10 ++++++++++ 2 files changed, 11 insertions(+)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -230,6 +230,7 @@#define X86_FEATURE_RDSEED ( 9*32+18)/* The RDSEED instruction */ #define X86_FEATURE_ADX ( 9*32+19)/* The ADCX and ADOX instructions */ #define X86_FEATURE_SMAP ( 9*32+20)/* Supervisor Mode Access Prevention */ +#define X86_FEATURE_PCOMMIT ( 9*32+22)/* PCOMMIT instruction */ #define X86_FEATURE_CLFLUSHOPT ( 9*32+23)/* CLFLUSHOPT instruction */ #define X86_FEATURE_AVX512PF ( 9*32+26)/* AVX-512 Prefetch */ #define X86_FEATURE_AVX512ER ( 9*32+27)/* AVX-512 Exponential and Reciprocal */
arch/x86/include/asm/special_insns.h +10 -0
@@ -4,6 +4,8 @@#ifdef __KERNEL__ +#include <asm/nops.h> +static inline void native_clts (void ) { asmvolatile ("clts" );
@@ -199,6 +201,14 @@ static inline void clflushopt(volatile void *__p) "+m" (*(volatile char __force *)__p)); } +static inline void pcommit_sfence (void ) +{ +alternative (ASM_NOP7 , +".byte 0x66, 0x0f, 0xae, 0xf8\n\t" /* pcommit */ +"sfence" , +X86_FEATURE_PCOMMIT ); +} +#define nop () asm volatile ("nop")
Signature · 1 line
1.9.3
tip-bot for Ross Zwisler
merge bot[tip:x86/asm] x86/asm: Add support for the pcommit instruction
x86/asm: Add support for the pcommit instruction
Commit-ID: 719d359dc7b6be3e43d6661f192ceb980b10ee26 Gitweb: http://git.kernel.org/tip/719d359dc7b6be3e43d6661f192ceb980b10ee26 Author: Ross Zwisler <[email protected]> AuthorDate: Thu, 19 Feb 2015 10:37:28 -0700 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 20 Feb 2015 09:43:36 +0100
x86/asm: Add support for the pcommit instruction
Add support for the new pcommit (persistent commit) instruction. This instruction was announced in the document "Intel Architecture Instruction Set Extensions Programming Reference" with reference number 319433-022:
https://software.intel.com/sites/default/files/managed/0d/53/319433-022.pdf
The pcommit instruction ensures that data that has been flushed from the processor's cache hierarchy with clwb, clflushopt or clflush is accepted to memory and is durable on the DIMM. The primary use case for this is persistent memory.
This function shows how to properly use clwb/clflushopt/clflush and pcommit with appropriate fencing:
void flush_and_commit_buffer(void *vaddr, unsigned int size)
{
void *vend = vaddr + size - 1;
for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
clwb(vaddr);
/* Flush any possible final partial cacheline */
clwb(vend);
/*
* sfence to order clwb/clflushopt/clflush cache flushes
* mfence via mb() also works
*/
wmb();
/* pcommit and the required sfence for ordering */
pcommit_sfence();
}After this function completes the data pointed to by vaddr is has been accepted to memory and will be durable if the vaddr points to persistent memory.
Pcommit must always be ordered by an mfence or sfence, so to help simplify things we include both the pcommit and the required sfence in the alternatives generated by pcommit_sfence(). The other option is to keep them separated, but on platforms that don't support pcommit this would then turn into:
void flush_and_commit_buffer(void *vaddr, unsigned int size)
{
void *vend = vaddr + size - 1;
for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size)
clwb(vaddr);
/* Flush any possible final partial cacheline */
clwb(vend);
/*
* sfence to order clwb/clflushopt/clflush cache flushes
* mfence via mb() also works
*/
wmb();
nop(); /* from pcommit(), via alternatives */
/*
* sfence to order pcommit
* mfence via mb() also works
*/
wmb();
}This is still correct, but now you've got two fences separated by only a nop. With the commit and the fence together in pcommit_sfence() you avoid the final unneeded fence.
--- arch/x86/include/asm/cpufeature.h | 1 + arch/x86/include/asm/special_insns.h | 10 ++++++++++ 2 files changed, 11 insertions(+)
arch/x86/include/asm/cpufeature.h +1 -0
@@ -231,6 +231,7 @@#define X86_FEATURE_RDSEED ( 9*32+18)/* The RDSEED instruction */ #define X86_FEATURE_ADX ( 9*32+19)/* The ADCX and ADOX instructions */ #define X86_FEATURE_SMAP ( 9*32+20)/* Supervisor Mode Access Prevention */ +#define X86_FEATURE_PCOMMIT ( 9*32+22)/* PCOMMIT instruction */ #define X86_FEATURE_CLFLUSHOPT ( 9*32+23)/* CLFLUSHOPT instruction */ #define X86_FEATURE_AVX512PF ( 9*32+26)/* AVX-512 Prefetch */ #define X86_FEATURE_AVX512ER ( 9*32+27)/* AVX-512 Exponential and Reciprocal */
arch/x86/include/asm/special_insns.h +10 -0
@@ -4,6 +4,8 @@#ifdef __KERNEL__ +#include <asm/nops.h> +static inline void native_clts (void ) { asmvolatile ("clts" );
@@ -199,6 +201,14 @@ static inline void clflushopt(volatile void *__p) "+m" (*(volatile char __force *)__p)); } +static inline void pcommit_sfence (void ) +{ +alternative (ASM_NOP7 , +".byte 0x66, 0x0f, 0xae, 0xf8\n\t" /* pcommit */ +"sfence" , +X86_FEATURE_PCOMMIT ); +} +#define nop () asm volatile ("nop")
H. Peter Anvin
reviewerRe: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 15 lines · tip-bot for Ross Zwisler
This function shows how to properly use clwb/clflushopt/clflush and pcommit with appropriate fencing:
void
flush_and_commit_buffer(void *vaddr, unsigned int size){ void *vend = vaddr + size - 1; for (; vaddr < vend; vaddr += boot_cpu_data.x86_clflush_size) clwb(vaddr); /* Flush any possible final partial cacheline */ clwb(vend); /*
Quoted 9 lines
* sfence to order clwb/clflushopt/clflush cache flushes * mfence via mb() also works */ wmb(); /* pcommit and the required sfence for ordering */ pcommit_sfence(); }
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Totally untested, yadda yadda...
void flush_and_commit_buffer(void *vaddr, unsigned int size)
{
unsigned long clflush_mask = boot_cpu_data.x86_clflush_size - 1;
char *vend = (char *)vaddr + size;
char *p;for (p = (char *)((unsigned long)vaddr & ~clflush_mask); p < vend; p += boot_cpu_data.x86_clflush_size)
clwb(vaddr);
/*
* sfence to order clwb/clflushopt/clflush cache flushes
* mfence via mb() also works
*/
wmb();
/* pcommit and the required sfence for ordering */ pcommit_sfence();
}
Re: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 2 lines · H. Peter Anvin
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Btw, should we hold down all those suggested usages somewhere in Documentation/x86/ as commit messages are generally harder to find?
Signature · 5 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.
--H. Peter Anvin
reviewerRe: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Btw, should we hold down all those suggested usages somewhere in
Documentation/x86/as commit messages are generally harder to find?
Yes, and commit messages can't be corrected after the fact.
-hpa
H. Peter Anvin
reviewerRe: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 11 lines · H. Peter Anvin
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Btw, should we hold down all those suggested usages somewhere in
Documentation/x86/as commit messages are generally harder to find?Yes, and commit messages can't be corrected after the fact.
Either that, or perhaps even better in kerneldoc comments in the source itself.
-hpa
Re: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 10 lines · H. Peter Anvin
Quoted 9 lines · H. Peter Anvin
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Btw, should we hold down all those suggested usages somewhere in
Documentation/x86/as commit messages are generally harder to find?
Quoted 5 lines
Quoted 2 lines
Yes, and commit messages can't be corrected after the fact.
Either that, or perhaps even better in kerneldoc comments in the source itself.
Either sounds like a good idea to me. :) Let me know which you'd prefer and I'll get something written up.
- Ross
H. Peter Anvin
reviewerRe: [tip:x86/asm] x86/asm: Add support for the pcommit instruction
Quoted 11 lines · Ross Zwisler
Quoted 10 lines · H. Peter Anvin
Quoted 9 lines · H. Peter Anvin
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin
That may cause the same line to be flushed twice. I would suggest, instead, also removing the arithmetic on void *:
Btw, should we hold down all those suggested usages somewhere in
Documentation/x86/as commit messages are generally harder to find?
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Yes, and commit messages can't be corrected after the fact.
Either that, or perhaps even better in kerneldoc comments in the source itself.
Either sounds like a good idea to me. :) Let me know which you'd prefer and I'll get something written up.
Kerneldoc would be better, as it is more likely to be maintained in proximity for the source.
-hpa