From: Ira Weiny <ira.weiny@intel.com>
To: Dan Williams <dan.j.williams@intel.com>
Cc: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>,
"hpa@zytor.com" <hpa@zytor.com>,
"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
"Yu, Fenghua" <fenghua.yu@intel.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH V8 36/44] memremap_pages: Reserve a PKS PKey for eventual use by PMEM
Date: Tue, 8 Feb 2022 14:48:03 -0800 [thread overview]
Message-ID: <20220208224803.GI785175@iweiny-DESK2.sc.intel.com> (raw)
In-Reply-To: <CAPcyv4hcKsy1Z78OyhPK+gPCqqEsbr9fuNRzOBNah03guDzktA@mail.gmail.com>
On Sat, Feb 05, 2022 at 12:19:27AM -0800, Dan Williams wrote:
> On Fri, Feb 4, 2022 at 9:40 PM Ira Weiny <ira.weiny@intel.com> wrote:
> >
> > On Fri, Feb 04, 2022 at 09:12:11AM -0800, Dan Williams wrote:
> > > On Tue, Feb 1, 2022 at 10:35 AM Edgecombe, Rick P
> > > <rick.p.edgecombe@intel.com> wrote:
> > > >
> > > > On Thu, 2022-01-27 at 09:54 -0800, ira.weiny@intel.com wrote:
> > > > > enum pks_pkey_consumers {
> > > > > - PKS_KEY_DEFAULT = 0, /* Must be 0 for default PTE
> > > > > values */
> > > > > - PKS_KEY_TEST = 1,
> > > > > - PKS_KEY_NR_CONSUMERS = 2,
> > > > > + PKS_KEY_DEFAULT = 0, /* Must be 0 for default
> > > > > PTE values */
> > > > > + PKS_KEY_TEST = 1,
> > > > > + PKS_KEY_PGMAP_PROTECTION = 2,
> > > > > + PKS_KEY_NR_CONSUMERS = 3,
> > > > > };
> > > >
> > > > The c spec says that any enum member that doesn't have an "=" will be
> > > > one more than the previous member. As a consequence you can leave the
> > > > "=" off PKS_KEY_NR_CONSUMERS and it will get auto adjusted when you add
> > > > more like this.
> > > >
> > > > I know we've gone around and around on this, but why also specify the
> > > > value for each key? They should auto increment and the first one is
> > > > guaranteed to be zero.
> >
> > Because it was easier to ensure that the init value had all the defaults
> > covered.
> >
> > > >
> > > > Otherwise this doesn't use any of the features of "enum", it's just a
> > > > verbose series of const int's.
> >
> > True but does this really matter?
> >
> > >
> > > Going further, this can also build in support for dynamically (at
> > > build time) freeing keys based on config, something like:
> > >
> > > enum {
> > > #if IS_ENABLED(CONFIG_PKS_TEST)
> > > PKS_KEY_TEST,
> > > #endif
> > > #if IS_ENABLED(CONFIG_DEVMAP_PROTECTION)
> > > PKS_KEY_PGMAP_PROTECTION,
> > > #endif
> > > PKS_KEY_NR_CONSUMERS,
> > > }
> >
> > This is all well and good until you get to the point of trying to define the
> > initial MSR value.
> >
> > What Rick proposes without the Kconfig check is easier than this. But to do
> > what both you and Rick suggest this is the best crap I've been able to come up
> > with that actually works...
> >
> >
> > /* pkeys_common.h */
> > #define PKR_AD_BIT 0x1u
> > #define PKR_WD_BIT 0x2u
> > #define PKR_BITS_PER_PKEY 2
> >
> > #define PKR_PKEY_SHIFT(pkey) (pkey * PKR_BITS_PER_PKEY)
> >
> > #define PKR_KEY_INIT_RW(pkey) (0 << PKR_PKEY_SHIFT(pkey))
> > #define PKR_KEY_INIT_AD(pkey) (PKR_AD_BIT << PKR_PKEY_SHIFT(pkey))
> > #define PKR_KEY_INIT_WD(pkey) (PKR_WD_BIT << PKR_PKEY_SHIFT(pkey))
> >
> >
> > /* pks-keys.h */
> > #define PKR_KEY_MASK(pkey) (0xffffffff & ~((PKR_WD_BIT|PKR_AD_BIT) << PKR_PKEY_SHIFT(pkey)))
> >
> > enum pks_pkey_consumers {
> > PKS_KEY_DEFAULT = 0, /* Must be 0 for default PTE values */
> > #if IS_ENABLED(CONFIG_PKS_TEST)
> > PKS_KEY_TEST,
> > #endif
> > #if IS_ENABLED(CONFIG_DEVMAP_ACCESS_PROTECTION)
> > PKS_KEY_PGMAP_PROTECTION,
> > #endif
> > PKS_KEY_NR_CONSUMERS
> > };
> >
> > #define PKS_DEFAULT_VALUE PKR_KEY_INIT_RW(PKS_KEY_DEFAULT)
> > #define PKS_DEFAULT_MASK PKR_KEY_MASK(PKS_KEY_DEFAULT)
> >
> > #if IS_ENABLED(CONFIG_PKS_TEST)
> > #define PKS_TEST_VALUE PKR_KEY_INIT_AD(PKS_KEY_TEST)
> > #define PKS_TEST_MASK PKR_KEY_MASK(PKS_KEY_TEST)
> > #else
> > /* Just define another default value to fool the CPP */
> > #define PKS_TEST_VALUE PKR_KEY_INIT_RW(0)
> > #define PKS_TEST_MASK PKR_KEY_MASK(0)
> > #endif
> >
> > #if IS_ENABLED(CONFIG_DEVMAP_ACCESS_PROTECTION)
> > #define PKS_PGMAP_VALUE PKR_KEY_INIT_AD(PKS_KEY_PGMAP_PROTECTION)
> > #define PKS_PGMAP_MASK PKR_KEY_MASK(PKS_KEY_PGMAP_PROTECTION)
> > #else
> > /* Just define another default value to fool the CPP */
> > #define PKS_PGMAP_VALUE PKR_KEY_INIT_RW(0)
> > #define PKS_PGMAP_MASK PKR_KEY_MASK(0)
> > #endif
> >
> > #define PKS_INIT_VALUE ((0xFFFFFFFF & \
> > (PKS_DEFAULT_MASK & \
> > PKS_TEST_MASK & \
> > PKS_PGMAP_MASK \
> > )) | \
> > (PKS_DEFAULT_VALUE | \
> > PKS_TEST_VALUE | \
> > PKS_PGMAP_VALUE \
> > ) \
> > )
> >
> >
> > I find the above much harder to parse and of little value. I'm pretty sure
> > that someone adding a key is much more likely to get the macro maze wrong.
> > Reviewing a patch to add a key would be much more difficult as well, IMHO.
> >
> > I'm a bit tired of this back and forth trying to implement this for features
> > which may never exist. In all my discussions I don't think we have reached
> > more than 10 use cases in our wildest dreams and only 4 have even been
> > attempted with real code including the PKS test.
> >
> > So I'm just a bit frustrated with the effort we have put in to try and do
> > dynamic or even compile time dynamic keys.
> >
> > Anyway I'll think on it more. But I'm inclined to leave it alone right now.
> > What I have is easy to review for correctness and only takes a bit of effort to
> > actually use.
>
> Sorry, for the thrash Ira, I felt compelled to put some skin in the
> game and came up with the below. Ignore the names they are just to
> show the idea:
>
> #define KEYVAL(prev, config) (prev + __is_defined(config))
> #define KEYDEFAULT(key, default, config) ((default << (key * 2)) *
> __is_defined(config))
> #define KEYX KEYVAL(0, ENABLE_X)
> #define KEYY KEYVAL(KEYX, ENABLE_Y)
> #define KEYZ KEYVAL(KEYY, ENABLE_Z)
> #define KEYMAX KEYVAL(KEYZ, 1)
>
> #define KEY0_INIT 0x1
> #define KEYX_INIT KEYDEFAULT(KEYX, 2, ENABLE_X)
> #define KEYY_INIT KEYDEFAULT(KEYY, 2, ENABLE_Y)
> #define KEYZ_INIT KEYDEFAULT(KEYZ, 2, ENABLE_Z)
>
> #define ALL_AD 0x55555555
> #define DEFAULT (ALL_AD & (GENMASK(31, KEYMAX * 2))) | KEYX_INIT |
> KEYY_INIT | KEYZ_INIT | KEY0_INIT
>
> The idea is that this relies on a defined key order, but still
> dynamically assigns key values at compile time. It's not as simple as
> an enum, but it still seems readable to me.
>
> The definition is done in 2 parts key slot numbers (KEYVAL()) and the
> values they need to inject into the combined global init mask
> (KEYDEFAULT()). The KEYVAL() section above defines the key order where
> each key gets an incremented value in the order, but only if the
> previous key was defined. KEYDEFAULT() defines the init value and
> optionally zeros out the init value if the config is disabled. Finally
> the DEFAULT mask is initialized to all 5s if there are zero keys
> defined, but otherwise masks 2-bits per defined key + 1 and ORs in the
> corresponding key init values. This uses some of the magic behind the
> IS_ENABLED() macro to turn undefined symbols into 0s.
>
> It seems to work, for example if I just define KEYX and KEYZ,
>
> #define ENABLE_X 1
> #define ENABLE_Z 1
>
> printf("X: %d:%#x Y: %d:%#x Z: %d:%#x MAX: %d DEFAULT: %#x\n", KEYX,
> KEYX_INIT, KEYY, KEYY_INIT, KEYZ, KEYZ_INIT, KEYMAX, DEFAULT);
>
> # ./a.out
> X: 1:0x8 Y: 1:0 Z: 2:0x20 MAX: 3 DEFAULT: 0x55555569
Yes this seems to work. I still think it is a bit clunky, but after
documenting it I think it is clear enough. I also played with making it a bit
more straight forward with the macro names.
Here is the doc:
/**
* DOC: PKS_KEY_ALLOCATION
*
* Users reserve a key value in 5 steps.
* 1) Use PKS_NEW_KEY to create a new key
* 2) Ensure that the last key value is used in the PKS_NEW_KEY macro
* 3) Adjust PKS_KEY_MAX to use the newly defined key value
* 4) Use PKS_DECLARE_INIT_VALUE to define an initial value
* 5) Add the new PKS default value to PKS_INIT_VALUE
*
* The PKS_NEW_KEY and PKS_DECLARE_INIT_VALUE macros require the Kconfig
* option to be specified to automatically adjust the number of keys used.
*
* PKS_KEY_DEFAULT must remain 0 (prev = 0) key with a default of
* PKS_DECLARE_DEFAULT_RW support non-pks protected pages.
*
* For example to configure a key for 'MY_FEATURE' with a default of Write
* Disabled.
*
* .. code-block:: c
*
* #define PKS_KEY_DEFAULT PKS_NEW_KEY(0, 1)
*
* // 1) Add PKS_KEY_MY_FEATURE
* // 2) Be sure to use the last defined key in the macro
* #define PKS_KEY_MY_FEATURE PKS_NEW_KEY(PKS_KEY_DEFAULT, CONFIG_MY_FEATURE)
*
* // 3) Adjust PKS_KEY_MAX
* #define PKS_KEY_MAX PKS_NEW_KEY(PKS_KEY_MY_FEATURE, 1)
*
*
* #define PKS_KEY_DEFAULT_INIT PKS_DECLARE_INIT_VALUE(PKS_KEY_DEFAULT, RW, 1)
*
* // 4) Define initial value
* #define PKS_KEY_MY_FEATURE_INIT PKS_DECLARE_INIT_VALUE(PKS_KEY_MY_FEATURE, WD, CONFIG_MY_FEATURE)
*
* // 5) Add initial value to PKS_INIT_VALUE
* #define PKS_INIT_VALUE ((PKS_ALL_AD & (GENMASK(31, PKS_KEY_MAX * PKR_BITS_PER_PKEY))) | \
* PKS_KEY_DEFAULT_INIT | \
* PKS_KEY_MY_FEATURE_INIT | \
* )
*/
Let me know if this is clear enough?
Ira
next prev parent reply other threads:[~2022-02-08 22:53 UTC|newest]
Thread overview: 145+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-27 17:54 [PATCH V8 00/44] PKS/PMEM: Add Stray Write Protection ira.weiny
2022-01-27 17:54 ` [PATCH V8 01/44] entry: Create an internal irqentry_exit_cond_resched() call ira.weiny
2022-01-27 17:54 ` [PATCH V8 02/44] Documentation/protection-keys: Clean up documentation for User Space pkeys ira.weiny
2022-01-28 22:39 ` Dave Hansen
2022-02-01 23:49 ` Ira Weiny
2022-02-01 23:54 ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 03/44] x86/pkeys: Create pkeys_common.h ira.weiny
2022-01-28 22:43 ` Dave Hansen
2022-02-02 1:00 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 04/44] x86/pkeys: Add additional PKEY helper macros ira.weiny
2022-01-28 22:47 ` Dave Hansen
2022-02-02 20:21 ` Ira Weiny
2022-02-02 20:26 ` Dave Hansen
2022-02-02 20:28 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 05/44] x86/fpu: Refactor arch_set_user_pkey_access() ira.weiny
2022-01-28 22:50 ` Dave Hansen
2022-02-02 20:22 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 06/44] mm/pkeys: Add Kconfig options for PKS ira.weiny
2022-01-28 22:54 ` Dave Hansen
2022-01-28 23:10 ` Ira Weiny
2022-01-28 23:51 ` Dave Hansen
2022-02-04 19:08 ` Ira Weiny
2022-02-09 5:34 ` Ira Weiny
2022-02-14 19:20 ` Dave Hansen
2022-02-14 23:03 ` Ira Weiny
2022-01-29 0:06 ` Dave Hansen
2022-02-04 19:14 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 07/44] x86/pkeys: Add PKS CPU feature bit ira.weiny
2022-01-28 23:05 ` Dave Hansen
2022-02-04 19:21 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 08/44] x86/fault: Adjust WARN_ON for PKey fault ira.weiny
2022-01-28 23:10 ` Dave Hansen
2022-02-04 20:06 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 09/44] x86/pkeys: Enable PKS on cpus which support it ira.weiny
2022-01-28 23:18 ` Dave Hansen
2022-01-28 23:41 ` Ira Weiny
2022-01-28 23:53 ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 10/44] Documentation/pkeys: Add initial PKS documentation ira.weiny
2022-01-28 23:57 ` Dave Hansen
2022-01-27 17:54 ` [PATCH V8 11/44] mm/pkeys: Define static PKS key array and default values ira.weiny
2022-01-29 0:02 ` Dave Hansen
2022-02-04 23:54 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 12/44] mm/pkeys: Define PKS page table macros ira.weiny
2022-01-27 17:54 ` [PATCH V8 13/44] mm/pkeys: Add initial PKS Test code ira.weiny
2022-01-31 19:30 ` Edgecombe, Rick P
2022-02-09 23:44 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 14/44] x86/pkeys: Introduce pks_write_pkrs() ira.weiny
2022-01-29 0:12 ` Dave Hansen
2022-01-29 0:16 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 15/44] x86/pkeys: Preserve the PKS MSR on context switch ira.weiny
2022-01-29 0:22 ` Dave Hansen
2022-02-11 6:10 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 16/44] mm/pkeys: Introduce pks_mk_readwrite() ira.weiny
2022-01-31 23:10 ` Edgecombe, Rick P
2022-02-18 2:22 ` Ira Weiny
2022-02-01 17:40 ` Dave Hansen
2022-02-18 4:39 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 17/44] mm/pkeys: Introduce pks_mk_noaccess() ira.weiny
2022-01-27 17:54 ` [PATCH V8 18/44] x86/fault: Add a PKS test fault hook ira.weiny
2022-01-31 19:56 ` Edgecombe, Rick P
2022-02-11 20:40 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 19/44] mm/pkeys: PKS Testing, add pks_mk_*() tests ira.weiny
2022-02-01 17:45 ` Dave Hansen
2022-02-18 5:34 ` Ira Weiny
2022-02-18 15:28 ` Dave Hansen
2022-02-18 17:25 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 20/44] mm/pkeys: Add PKS test for context switching ira.weiny
2022-02-01 17:43 ` Edgecombe, Rick P
2022-02-22 21:42 ` Ira Weiny
2022-02-01 17:47 ` Edgecombe, Rick P
2022-02-01 19:52 ` Edgecombe, Rick P
2022-02-18 6:03 ` Ira Weiny
2022-02-18 6:02 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 21/44] x86/entry: Add auxiliary pt_regs space ira.weiny
2022-01-27 17:54 ` [PATCH V8 22/44] entry: Pass pt_regs to irqentry_exit_cond_resched() ira.weiny
2022-01-27 17:54 ` [PATCH V8 23/44] entry: Add architecture auxiliary pt_regs save/restore calls ira.weiny
2022-01-27 17:54 ` [PATCH V8 24/44] x86/entry: Define arch_{save|restore}_auxiliary_pt_regs() ira.weiny
2022-01-27 17:54 ` [PATCH V8 25/44] x86/pkeys: Preserve PKRS MSR across exceptions ira.weiny
2022-01-27 17:54 ` [PATCH V8 26/44] x86/fault: Print PKS MSR on fault ira.weiny
2022-02-01 18:13 ` Edgecombe, Rick P
2022-02-18 6:01 ` Ira Weiny
2022-02-18 17:28 ` Edgecombe, Rick P
2022-02-18 20:20 ` Dave Hansen
2022-02-18 20:54 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 27/44] mm/pkeys: Add PKS exception test ira.weiny
2022-01-27 17:54 ` [PATCH V8 28/44] mm/pkeys: Introduce pks_update_exception() ira.weiny
2022-01-27 17:54 ` [PATCH V8 29/44] mm/pkeys: Introduce PKS fault callbacks ira.weiny
2022-01-27 17:54 ` [PATCH V8 30/44] mm/pkeys: Test setting a PKS key in a custom fault callback ira.weiny
2022-02-01 0:55 ` Edgecombe, Rick P
2022-03-01 15:39 ` Ira Weiny
2022-02-01 17:42 ` Edgecombe, Rick P
2022-02-11 20:44 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 31/44] mm/pkeys: Add pks_available() ira.weiny
2022-01-27 17:54 ` [PATCH V8 32/44] memremap_pages: Add Kconfig for DEVMAP_ACCESS_PROTECTION ira.weiny
2022-02-04 15:49 ` Dan Williams
2022-01-27 17:54 ` [PATCH V8 33/44] memremap_pages: Introduce pgmap_protection_available() ira.weiny
2022-02-04 16:19 ` Dan Williams
2022-02-28 16:59 ` Ira Weiny
2022-03-01 15:56 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 34/44] memremap_pages: Introduce a PGMAP_PROTECTION flag ira.weiny
2022-01-27 17:54 ` [PATCH V8 35/44] memremap_pages: Introduce devmap_protected() ira.weiny
2022-01-27 17:54 ` [PATCH V8 36/44] memremap_pages: Reserve a PKS PKey for eventual use by PMEM ira.weiny
2022-02-01 18:35 ` Edgecombe, Rick P
2022-02-04 17:12 ` Dan Williams
2022-02-05 5:40 ` Ira Weiny
2022-02-05 8:19 ` Dan Williams
2022-02-06 18:14 ` Dan Williams
2022-02-08 22:48 ` Ira Weiny [this message]
2022-02-08 23:22 ` Dan Williams
2022-02-08 23:42 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 37/44] memremap_pages: Set PKS PKey in PTEs if PGMAP_PROTECTIONS is requested ira.weiny
2022-02-04 17:41 ` Dan Williams
2022-03-01 18:15 ` Ira Weiny
2022-01-27 17:54 ` [PATCH V8 38/44] memremap_pages: Define pgmap_mk_{readwrite|noaccess}() calls ira.weiny
2022-02-04 18:35 ` Dan Williams
2022-02-05 0:09 ` Ira Weiny
2022-02-05 0:19 ` Dan Williams
2022-02-05 0:25 ` Dan Williams
2022-02-05 0:27 ` Dan Williams
2022-02-05 5:55 ` Ira Weiny
2022-02-05 6:28 ` Dan Williams
2022-02-22 22:05 ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 39/44] memremap_pages: Add memremap.pks_fault_mode ira.weiny
2022-02-01 1:16 ` Edgecombe, Rick P
2022-03-02 0:20 ` Ira Weiny
2022-02-04 19:01 ` Dan Williams
2022-03-02 2:00 ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 40/44] memremap_pages: Add pgmap_protection_flag_invalid() ira.weiny
2022-02-01 1:37 ` Edgecombe, Rick P
2022-03-02 2:01 ` Ira Weiny
2022-02-04 19:18 ` Dan Williams
2022-01-27 17:55 ` [PATCH V8 41/44] kmap: Ensure kmap works for devmap pages ira.weiny
2022-02-04 21:07 ` Dan Williams
2022-03-01 19:45 ` Ira Weiny
2022-03-01 19:50 ` Ira Weiny
2022-03-01 20:05 ` Dan Williams
2022-03-01 23:03 ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 42/44] dax: Stray access protection for dax_direct_access() ira.weiny
2022-02-04 5:19 ` Dan Williams
2022-03-01 18:13 ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 43/44] nvdimm/pmem: Enable stray access protection ira.weiny
2022-02-04 21:10 ` Dan Williams
2022-03-01 18:18 ` Ira Weiny
2022-01-27 17:55 ` [PATCH V8 44/44] devdax: " ira.weiny
2022-02-04 21:12 ` Dan Williams
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=20220208224803.GI785175@iweiny-DESK2.sc.intel.com \
--to=ira.weiny@intel.com \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=fenghua.yu@intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=rick.p.edgecombe@intel.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
all inboxes | Powered by JetHome®