tpm: Detach page allocation from tpm_buf
Subsystems (2)
v3 cover tpm: Detach page allocation from tpm_buf
As has been seen recently, binding the buffer allocation and tpm_buf together is sometimes far from optimal. The buffer might come from the caller namely when tpm_send() is used by another subsystem. In addition we can stability in call sites w/o rollback (e.g. power events)>
Take allocation out of the tpm_buf framework and make it purely a wrapper for the data buffer.
Jarkko Sakkinen (2):
tpm: Use GFP_KERNEL for allocating struct tpm_buf
tpm: Detach page allocation from tpm_buf
drivers/char/tpm/tpm-sysfs.c | 19 ++- drivers/char/tpm/tpm.h | 40 ++--- drivers/char/tpm/tpm1-cmd.c | 114 +++++++++---- drivers/char/tpm/tpm2-cmd.c | 265 +++++++++++++++++++----------- drivers/char/tpm/tpm2-space.c | 64 +++++--- drivers/char/tpm/tpm_vtpm_proxy.c | 24 +-- 6 files changed, 333 insertions(+), 193 deletions(-)
Signature · 1 line
2.20.1
PATCH v3 1/2 tpm: Use GFP_KERNEL for allocating struct tpm_buf
Switch from GFP_HIGHUSER to GFP_KERNEL. On 32-bit platforms kmap() space could be unnecessarily wasted because of using GFP_HIGHUSER by taking a page of from the highmem.
--- drivers/char/tpm/tpm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/tpm.h +1 -1
@@ -297,7 +297,7 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); + buf->data_page =alloc_page (GFP_KERNEL );if (!buf->data_page)return -ENOMEM ;
Signature · 1 line
2.20.1
Re: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
Quoted 25 lines · Jarkko Sakkinen
Switch from
GFP_HIGHUSERtoGFP_KERNEL. On 32-bit platformskmap()space
could be unnecessarily wasted because of usingGFP_HIGHUSERby taking a
page of from the highmem.Suggested-by James Bottomley <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>--- drivers/char/tpm/tpm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/tpm/tpm.h +1
-1@@ -297,7 +297,7 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); + buf->data_page =alloc_page (GFP_KERNEL );if (!buf->data_page)
Quoted 1 line
return
-ENOMEM;
The kmap/kunmap needs removing as well, and now the data_page field isn't necessary, so it can go. I think the result should be something like the below (uncompiled and untested).
James
---
drivers/char/tpm/tpm.h +3 -6
@@ -284,7 +284,6 @@ enum tpm_buf_flags { };struct tpm_buf { -struct page *data_page;unsigned int flags;u8 *data; };
@@ -300,20 +299,18 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); -if (!buf->data_page) + buf->data = (u8 *)__get_free_page (GFP_KERNEL ); +if (!buf->data)return -ENOMEM ; buf->flags =0 ; - buf->data =kmap (buf->data_page);tpm_buf_reset (buf, tag, ordinal);return 0 ; }static inline void tpm_buf_destroy (struct tpm_buf *buf) { -kunmap (buf->data_page); -__free_page (buf->data_page); +free_page (buf->data); }static inline u32 tpm_buf_length (struct tpm_buf *buf)
Re: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
Quoted 64 lines · James Bottomley
Quoted 25 lines · Jarkko Sakkinen
Switch from
GFP_HIGHUSERtoGFP_KERNEL. On 32-bit platformskmap()space
could be unnecessarily wasted because of usingGFP_HIGHUSERby taking a
page of from the highmem.Suggested-by James Bottomley <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>--- drivers/char/tpm/tpm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/char/tpm/tpm.h +1
-1@@ -297,7 +297,7 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); + buf->data_page =alloc_page (GFP_KERNEL );if (!buf->data_page)Quoted 1 line
return
-ENOMEM;The kmap/kunmap needs removing as well, and now the
data_pagefield isn't necessary, so it can go. I think the result should be something like the below (uncompiled and untested).James
---
drivers/char/tpm/tpm.h +2
-4@@ -284,7 +284,6 @@ enum tpm_buf_flags { };struct tpm_buf { -struct page *data_page;unsigned int flags;u8 *data; };@@ -300,20 +299,18 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); -if (!buf->data_page) + buf->data = (u8 *)__get_free_page (GFP_KERNEL ); +if (!buf->data)return -ENOMEM ; buf->flags =0 ; - buf->data =kmap (buf->data_page);tpm_buf_reset (buf, tag, ordinal);return 0 ;
drivers/char/tpm/tpm.h static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) · line 316
Quoted 10 lines
drivers/char/tpm/tpm.h +1
-2}static inline void tpm_buf_destroy (struct tpm_buf *buf) { -kunmap (buf->data_page); -__free_page (buf->data_page); +free_page (buf->data); }static inline u32 tpm_buf_length (struct tpm_buf *buf)
Care to make this a proper patch? Much better idea to do it this way, agreed.
/Jarkko
Re: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
From: James Bottomley <[email protected]>
Subject: [PATCH] tpm: use GFP kernel for tpm_buf allocations
The current code uses GFP_HIGHMEM, which is wrong because GFP_HIGHMEM (on 32 bit systems) is memory ordinarily inaccessible to the kernel and should only be used for allocations affecting userspace. In order to make highmem visible to the kernel on 32 bit it has to be kmapped, which consumes valuable entries in the kmap region. Since the tpm_buf is only ever used in the kernel, switch to using a GFP_KERNEL allocation so as not to waste kmap space on 32 bits.
--- drivers/char/tpm/tpm.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm.h +3 -6
@@ -284,7 +284,6 @@ enum tpm_buf_flags { };struct tpm_buf { -struct page *data_page;unsigned int flags;u8 *data; };
@@ -300,20 +299,18 @@ static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->data_page =alloc_page (GFP_HIGHUSER ); -if (!buf->data_page) + buf->data = (u8 *)__get_free_page (GFP_KERNEL ); +if (!buf->data)return -ENOMEM ; buf->flags =0 ; - buf->data =kmap (buf->data_page);tpm_buf_reset (buf, tag, ordinal);return 0 ; }static inline void tpm_buf_destroy (struct tpm_buf *buf) { -kunmap (buf->data_page); -__free_page (buf->data_page); +free_page (buf->data); }static inline u32 tpm_buf_length (struct tpm_buf *buf)
Signature · 1 line
2.16.4
Jerry Snitselaar
Reviewed-byRe: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
Quoted 11 lines · James Bottomley
From: James Bottomley <[email protected]>
Subject: [PATCH] tpm: use GFP kernel fortpm_bufallocationsThe current code uses
GFP_HIGHMEM, which is wrong becauseGFP_HIGHMEM(on 32 bit systems) is memory ordinarily inaccessible to the kernel and should only be used for allocations affecting userspace. In order to make highmem visible to the kernel on 32 bit it has to be kmapped, which consumes valuable entries in the kmap region. Since thetpm_bufis only ever used in the kernel, switch to using aGFP_KERNELallocation so as not to waste kmap space on 32 bits.
Quoted 3 lines
Fixes a74f8b36352e (tpm: introduce tpm_buf)Signed-off-by James Bottomley <[email protected]>---
Jarkko Sakkinen
Reviewed-byRe: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
In reply to James BottomleyQuoted 11 lines · James Bottomley
From: James Bottomley <[email protected]>
Subject: [PATCH] tpm: use GFP kernel fortpm_bufallocationsThe current code uses
GFP_HIGHMEM, which is wrong becauseGFP_HIGHMEM(on 32 bit systems) is memory ordinarily inaccessible to the kernel and should only be used for allocations affecting userspace. In order to make highmem visible to the kernel on 32 bit it has to be kmapped, which consumes valuable entries in the kmap region. Since thetpm_bufis only ever used in the kernel, switch to using aGFP_KERNELallocation so as not to waste kmap space on 32 bits.
Quoted 2 lines
Fixes a74f8b36352e (tpm: introduce tpm_buf)Signed-off-by James Bottomley <[email protected]>
Thanks a lot. Makes a lot more sense than the patch that I sent.
/Jarkko
Jarkko Sakkinen
AppliedRe: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
In reply to James BottomleyQuoted 11 lines · James Bottomley
From: James Bottomley <[email protected]>
Subject: [PATCH] tpm: use GFP kernel fortpm_bufallocationsThe current code uses
GFP_HIGHMEM, which is wrong becauseGFP_HIGHMEM(on 32 bit systems) is memory ordinarily inaccessible to the kernel and should only be used for allocations affecting userspace. In order to make highmem visible to the kernel on 32 bit it has to be kmapped, which consumes valuable entries in the kmap region. Since thetpm_bufis only ever used in the kernel, switch to using aGFP_KERNELallocation so as not to waste kmap space on 32 bits.
Quoted 2 lines
Fixes a74f8b36352e (tpm: introduce tpm_buf)Signed-off-by James Bottomley <[email protected]>
Pushed to master branch.
/Jarkko
Re: [PATCH v3 1/2] tpm: Use GFP_KERNEL for allocating struct tpm_buf
Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · James Bottomley
From: James Bottomley <[email protected]>
Subject: [PATCH] tpm: use GFP kernel fortpm_bufallocationsThe current code uses
GFP_HIGHMEM, which is wrong becauseGFP_HIGHMEM(on 32 bit systems) is memory ordinarily inaccessible to the kernel and should only be used for allocations affecting userspace. In order to make highmem visible to the kernel on 32 bit it has to be kmapped, which consumes valuable entries in the kmap region. Since thetpm_bufis only ever used in the kernel, switch to using aGFP_KERNELallocation so as not to waste kmap space on 32 bits.
Quoted 5 lines
Quoted 3 lines
Fixes a74f8b36352e (tpm: introduce tpm_buf)Signed-off-by James Bottomley <[email protected]om>
Pushed to master branch.
Thanks. 0day spotted a problem with the use of free_page() so I've sent a v2.
James
PATCH v3 2/2 tpm: Detach page allocation from tpm_buf
In reply to Jarkko SakkinenAs has been seen recently, binding the buffer allocation and tpm_buf together is sometimes far from optimal. The buffer might come from the caller namely when tpm_send() is used by another subsystem. In addition we can stability in call sites w/o rollback (e.g. power events)>
Take allocation out of the tpm_buf framework and make it purely a wrapper for the data buffer.
--- drivers/char/tpm/tpm-sysfs.c | 19 ++- drivers/char/tpm/tpm.h | 40 ++--- drivers/char/tpm/tpm1-cmd.c | 114 +++++++++---- drivers/char/tpm/tpm2-cmd.c | 265 +++++++++++++++++++----------- drivers/char/tpm/tpm2-space.c | 64 +++++--- drivers/char/tpm/tpm_vtpm_proxy.c | 24 +-- 6 files changed, 333 insertions(+), 193 deletions(-)
drivers/char/tpm/tpm-sysfs.c +13 -6
@@ -32,21 +32,26 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf; -struct tpm_readpubek_out *out; -int i; -char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev); +struct tpm_readpubek_out *out; +struct page *data_page; +struct tpm_buf tpm_buf;char anti_replay[20 ]; +char *str = buf; +int i;memset (&anti_replay,0 ,sizeof (anti_replay));if (tpm_try_get_ops (chip))return 0 ; -if (tpm_buf_init (&tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK )) + data_page =alloc_page (GFP_KERNEL ); +if (!data_page) goto out_ops; +tpm_buf_reset (&tpm_buf,kmap (data_page),PAGE_SIZE ,TPM_TAG_RQU_COMMAND , +TPM_ORD_READPUBEK ); +tpm_buf_append (&tpm_buf, anti_replay,sizeof (anti_replay)); if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE,
@@ -83,7 +88,9 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr, } out_buf: -tpm_buf_destroy (&tpm_buf); +kunmap (data_page); +__free_page (data_page); + out_ops:tpm_put_ops (chip);return str - buf;
drivers/char/tpm/tpm.h +17 -23
@@ -281,36 +281,30 @@ enum tpm_buf_flags { };struct tpm_buf { -struct page *data_page; -unsigned int flags;u8 *data; +unsigned int size; +unsigned int flags; }; -static inline void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) +static inline void tpm_buf_reset (struct tpm_buf *buf,u8 *data, +unsigned int size,u16 tag,u32 ordinal) { -struct tpm_header *head = (struct tpm_header *)buf->data; +struct tpm_header *head = (struct tpm_header *)data; - head->tag =cpu_to_be16 (tag); - head->length =cpu_to_be32 (sizeof (*head)); - head->ordinal =cpu_to_be32 (ordinal); -} - -static inline int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) -{ - buf->data_page =alloc_page (GFP_KERNEL ); -if (!buf->data_page) -return -ENOMEM ; +/* sanity check */ +if (size <TPM_HEADER_SIZE ) { +WARN (1 ,"tpm_buf: overflow\n" ); + buf->flags |=TPM_BUF_OVERFLOW ; +return ; + } + buf->data = data; + buf->size = size; buf->flags =0 ; - buf->data =kmap (buf->data_page); -tpm_buf_reset (buf, tag, ordinal); -return 0 ; -} -static inline void tpm_buf_destroy (struct tpm_buf *buf) -{ -kunmap (buf->data_page); -__free_page (buf->data_page); + head->tag =cpu_to_be16 (tag); + head->length =cpu_to_be32 (sizeof (*head)); + head->ordinal =cpu_to_be32 (ordinal); }static inline u32 tpm_buf_length (struct tpm_buf *buf)
@@ -338,7 +332,7 @@ static inline void tpm_buf_append(struct tpm_buf *buf,if (buf->flags &TPM_BUF_OVERFLOW )return ; -if ((len + new_len) >PAGE_SIZE ) { +if ((len + new_len) > buf->size) {WARN (1 ,"tpm_buf: overflow\n" ); buf->flags |=TPM_BUF_OVERFLOW ;return ;
drivers/char/tpm/tpm1-cmd.c +78 -36
238 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +173 -92
530 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +38 -26
122 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +14 -10
@@ -394,19 +394,23 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)static int vtpm_proxy_request_locality (struct tpm_chip *chip,int locality) { +struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); +const struct tpm_header *header; +struct page *data_page;struct tpm_buf buf;int rc; -const struct tpm_header *header; -struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + + data_page =alloc_page (GFP_KERNEL ); +if (!data_page) +return -ENOMEM ;if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS , -TPM2_CC_SET_LOCALITY ); +tpm_buf_reset (&buf,kmap (data_page),PAGE_SIZE , +TPM2_ST_SESSIONS ,TPM2_CC_SET_LOCALITY );else - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND , -TPM_ORD_SET_LOCALITY ); -if (rc) -return rc; +tpm_buf_reset (&buf,kmap (data_page),PAGE_SIZE , +TPM_TAG_RQU_COMMAND ,TPM_ORD_SET_LOCALITY ); +tpm_buf_append_u8 (&buf, locality); proxy_dev->state |=STATE_DRIVER_COMMAND ;
@@ -426,8 +430,8 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality) locality =-1 ; out: -tpm_buf_destroy (&buf); - +kunmap (data_page); +__free_page (data_page);return locality; }
Signature · 1 line
2.20.1
James Bottomley
maintainerQuoted 2 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buftogether is sometimes far from optimal.
Can you elaborate on this a bit more? I must have missed the discussion.
Quoted 6 lines
The buffer might come from the caller namely when
tpm_send()is used
by another subsystem. In addition we can stability in call sites w/o rollback (e.g. power events)>Take allocation out of the
tpm_bufframework and make it purely a wrapper for the data buffer.
What you're doing here is taking a single object with a single lifetime and creating two separate objects with separate lifetimes and a dependency. The problem with doing that is that it always creates subtle and hard to debug corner cases where the dependency gets violated, so it's usually better to simplify the object lifetimes by reducing the dependencies and combining as many dependent objects as possible into a single object with one lifetime. Bucking this trend for a good reason is OK, but I think a better reason than "is sometimes far from optimal" is needed.
James
Mimi Zohar
maintainerQuoted 6 lines · James Bottomley
Quoted 2 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buftogether is sometimes far from optimal.Can you elaborate on this a bit more? I must have missed the discussion.
Refer to e13cd21ffd50 ("tpm: Wrap the buffer from the caller to tpm_buf in tpm_send()") for the details.
Mimi
James Bottomley
maintainerQuoted 11 lines · Mimi Zohar
Quoted 7 lines · James Bottomley
Quoted 3 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buf
together is sometimes far from optimal.Can you elaborate on this a bit more? I must have missed the discussion.
Refer to
e13cd21ffd50("tpm: Wrap the buffer from the caller totpm_bufintpm_send()") for the details.
Yes, I get that, but to my mind that calls for moving the tpm_init/destroy_buf into the callers of tpm_send (which, for the most part, already exist), which means there's no need to separate the buf and data lifetimes.
James
Quoted 13 lines · James Bottomley
Quoted 11 lines · Mimi Zohar
Quoted 7 lines · James Bottomley
Quoted 3 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buf
together is sometimes far from optimal.Can you elaborate on this a bit more? I must have missed the discussion.
Refer to
e13cd21ffd50("tpm: Wrap the buffer from the caller totpm_bufintpm_send()") for the details.
Quoted 7 lines
Yes, I get that, but to my mind that calls for moving the tpm_init/destroy_buf into the callers of
tpm_send(which, for the most part, already exist), which means there's no need to separate the buf and data lifetimes.James
Sumit has been working on a patchset that does this. His patchset converts both the asymmetric keys and trusted keys code to using the tpm_buf manipulation functions.
Quoted 19 lines · Jerry Snitselaar
Quoted 18 lines · James Bottomley
Quoted 11 lines · Mimi Zohar
Quoted 7 lines · James Bottomley
Quoted 3 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buf
together is sometimes far from optimal.Can you elaborate on this a bit more? I must have missed the discussion.
Refer to
e13cd21ffd50("tpm: Wrap the buffer from the caller totpm_bufintpm_send()") for the details.Yes, I get that, but to my mind that calls for moving the tpm_init/destroy_buf into the callers of
tpm_send(which, for the most part, already exist), which means there's no need to separate the buf and data lifetimes.
Quoted 6 lines
Quoted 2 lines
James
Sumit has been working on a patchset that does this. His patchset converts both the asymmetric keys and trusted keys code to using the
tpm_bufmanipulation functions.
And it is also in a shape that it can soon be merged (within few iterations at most).
/Jarkko
Quoted 14 lines · James Bottomley
Quoted 2 lines · Jarkko Sakkinen
As has been seen recently, binding the buffer allocation and
tpm_buftogether is sometimes far from optimal.Can you elaborate on this a bit more? I must have missed the discussion.
Quoted 6 lines
The buffer might come from the caller namely when
tpm_send()is used
by another subsystem. In addition we can stability in call sites w/o rollback (e.g. power events)>Take allocation out of the
tpm_bufframework and make it purely a wrapper for the data buffer.
Quoted 9 lines
What you're doing here is taking a single object with a single lifetime and creating two separate objects with separate lifetimes and a dependency. The problem with doing that is that it always creates subtle and hard to debug corner cases where the dependency gets violated, so it's usually better to simplify the object lifetimes by reducing the dependencies and combining as many dependent objects as possible into a single object with one lifetime. Bucking this trend for a good reason is OK, but I think a better reason than "is sometimes far from optimal" is needed.
Right, I see your point. We can just say instead in a comment that tpm_buf_init() is optional if you need to allocate the buffer and do not provide your own.
Thanks for the remark. I have to agree with this.
/Jarkko