xen: Convert kmap() to kmap_local_page()
Subsystems (3)
PATCH v2 1/1 xen: Convert kmap() to kmap_local_page()
kmap() is being deprecated and these usages are all local to the thread so there is no reason kmap_local_page() can't be used.
Replace kmap() calls with kmap_local_page().
---
changes in V2:
-edit commit subject
-edit commit message
--- drivers/xen/gntalloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/xen/gntalloc.c +2 -2
@@ -178,9 +178,9 @@ static void __del_gref(struct gntalloc_gref *gref)unsigned long addr;if (gref->notify.flags &UNMAP_NOTIFY_CLEAR_BYTE ) { -uint8_t *tmp =kmap (gref->page); +uint8_t *tmp =kmap_local_page (gref->page); tmp[gref->notify.pgoff] =0 ; -kunmap (gref->page); +kunmap_local (tmp); }if (gref->notify.flags &UNMAP_NOTIFY_SEND_EVENT ) {notify_remote_via_evtchn (gref->notify.event);
Signature · 1 line
2.35.2
Quoted 4 lines · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the thread so there is no reasonkmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().
OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@
e1 =
- kmap
+ kmap_local_page
(e2)
... when != x = e1 // not stored in any location and not passed to another function
when != f(...,e1,...)
when != x = e2
when != f(...,e2,...)-kunmap(e2)
+kunmap_local(e1)
julia
Quoted 31 lines
Signed-off-by Alaa Mohamed <[email protected]>---
changes in V2:
-edit commit subject
-edit commit message--- drivers/xen/gntalloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/xen/gntalloc.c +2
-3@@ -178,9 +178,9 @@ static void __del_gref(struct gntalloc_gref *gref)unsigned long addr;if (gref->notify.flags &UNMAP_NOTIFY_CLEAR_BYTE ) { -uint8_t *tmp =kmap (gref->page); +uint8_t *tmp =kmap_local_page (gref->page); tmp[gref->notify.pgoff] =0 ; -kunmap (gref->page); +kunmap_local (tmp); }if (gref->notify.flags &UNMAP_NOTIFY_SEND_EVENT ) {notify_remote_via_evtchn (gref->notify.event); --2.35.2
Quoted 14 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the thread so there is no reasonkmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@
Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed to
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax and semantics of expressions of Coccinelle. However, thanks Julia, this code looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been allocated within the same function where the kmap() call is.
In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with alloc_page()) and convert kmap() /kunmap() to use kmalloc() / kfree().
Fox example, this is done in the following patch:
commit 633b0616cfe0 ("x86/sgx: Remove unnecessary kmap() from sgx_ioc_enclave_init()") from Ira Weiny.
Can Coccinelle catch also those special cases where a page that is passed to kmap() is allocated within that same function (vs. being passed as argument to this function) and, if so, propose a replacement with kmalloc()?
Thanks,
Fabio
Quoted 34 lines · Fabio M. De Francesco
Quoted 14 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the thread so there is no reasonkmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed to
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax and semantics of expressions of Coccinelle. However, thanks Julia, this code looks good and can be very helpful.
Quoted 2 lines
Only a minor objection... it doesn't tell when 'e2' has been allocated within the same function where the
kmap()call is.
OK, thanks for pointing that out. That seems like a key point that should be mentioned in log messages. It's not even visible in the context lines around the patch.
Quoted 7 lines
In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/kunmap() to usekmalloc()/kfree().Fox example, this is done in the following patch:
Quoted 7 lines
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is passed to
kmap()is allocated within that same function (vs. being passed as argument to this function) and, if so, propose a replacement withkmalloc()?
Sure. I'll take a look at the precise example Alaa started with and see if I can come up with something.
thanks,
julia
Quoted 43 lines · Fabio M. De Francesco
Quoted 14 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the thread so there is no reasonkmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed to
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax and semantics of expressions of Coccinelle. However, thanks Julia, this code looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been allocated within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/kunmap() to usekmalloc()/kfree().Fox example, this is done in the following patch:
Quoted 7 lines
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is passed to
kmap()is allocated within that same function (vs. being passed as argument to this function) and, if so, propose a replacement withkmalloc()?
It looks complex in this case, because the allocation is in another function, and it is passed to another function.
julia
Quoted 8 lines
Thanks,
Fabio
Quoted 8 lines · On mercoledì 20 aprile 2022 15:40:10 CEST Julia Lawall wrote:
Quoted 5 lines · Fabio M. De Francesco
Quoted 4 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 1 line · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the
thread
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Quoted 3 lines
so there is no reason
kmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed to
another function
Quoted 6 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
julia
I've never spent sufficient time to understand properly the syntax and semantics of expressions of Coccinelle. However, thanks Julia, this
code
Quoted 7 lines
Quoted 7 lines
looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been allocated within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/
kunmap()
Quoted 8 lines
Quoted 8 lines
to use
kmalloc()/kfree().Fox example, this is done in the following patch:
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is
passed
Quoted 6 lines
Quoted 3 lines
to
kmap()is allocated within that same function (vs. being passed as argument to this function) and, if so, propose a replacement withkmalloc()?It looks complex in this case, because the allocation is in another function, and it is passed to another function.
This is not the special case I was talking about. In this case your code for Coccinelle tells the right proposal and it is exactly what Alaa did in her patch (which is good!).
I'm talking about other special cases like the one I pointed to with the link I provided. I'm sorry if my bad English made you think that Alaa's patch was one of those cases where the page is allocated within the same function where kmap() is.
I hope that now I've been clearer :)
Thanks,
Fabio
Quoted 66 lines · Fabio M. De Francesco
Quoted 8 lines · On mercoledì 20 aprile 2022 15:40:10 CEST Julia Lawall wrote:
Quoted 5 lines · Fabio M. De Francesco
Quoted 4 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 1 line · Alaa Mohamed
kmap()is being deprecated and these usages are all local to thethread
Quoted 22 lines
Quoted 22 lines
Quoted 10 lines
Quoted 3 lines
so there is no reason
kmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed to
another function
Quoted 6 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
julia
I've never spent sufficient time to understand properly the syntax and semantics of expressions of Coccinelle. However, thanks Julia, this
code
Quoted 7 lines
Quoted 7 lines
looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been allocated within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/
kunmap()Quoted 8 lines
Quoted 8 lines
to use
kmalloc()/kfree().Fox example, this is done in the following patch:
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is
passed
Quoted 6 lines
Quoted 3 lines
to
kmap()is allocated within that same function (vs. being passed as argument to this function) and, if so, propose a replacement withkmalloc()?It looks complex in this case, because the allocation is in another function, and it is passed to another function.
This is not the special case I was talking about. In this case your code for Coccinelle tells the right proposal and it is exactly what Alaa did in her patch (which is good!).
Quoted 6 lines
I'm talking about other special cases like the one I pointed to with the link I provided. I'm sorry if my bad English made you think that Alaa's patch was one of those cases where the page is allocated within the same function where
kmap()is.I hope that now I've been clearer :)
Ah, sorry for the misunderstanding. If you have an example, I can take a look and propose something for this special case.
julia
Quoted 5 lines · On mercoledì 20 aprile 2022 15:57:14 CEST Julia Lawall wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On mercoledì 20 aprile 2022 15:40:10 CEST Julia Lawall wrote:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Fabio M. De Francesco
Quoted 6 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
Quoted 2 lines
kmap()is being deprecated and these usages are all local to
the
Quoted 11 lines
Quoted 11 lines
thread
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
so there is no reason
kmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed
to
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax
and
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
semantics of expressions of Coccinelle. However, thanks Julia, this
code
Quoted 3 lines
Quoted 3 lines
looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been
allocated
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/
kunmap()Quoted 2 lines
Quoted 2 lines
to use
kmalloc()/kfree().
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Fox example, this is done in the following patch:
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is
passed
Quoted 1 line
Quoted 1 line
to
kmap()is allocated within that same function (vs. being passed
as
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
argument to this function) and, if so, propose a replacement with
kmalloc()?It looks complex in this case, because the allocation is in another function, and it is passed to another function.
This is not the special case I was talking about. In this case your
code
Quoted 1 line
Quoted 1 line
for Coccinelle tells the right proposal and it is exactly what Alaa did
in
Quoted 3 lines
Quoted 3 lines
her patch (which is good!).
I'm talking about other special cases like the one I pointed to with
the
Quoted 2 lines
Quoted 2 lines
link I provided. I'm sorry if my bad English made you think that Alaa's patch was one of those cases where the page is allocated within the
same
Quoted 8 lines
Quoted 3 lines
function where
kmap()is.I hope that now I've been clearer :)
Ah, sorry for the misunderstanding. If you have an example, I can take a look and propose something for this special case.
julia
Yes, I have the example that you are asking for. It's that commit 633b0616cfe0 from Ira Weiny.
Let me copy and paste it here for your convenience...
arch/x86/kernel/cpu/sgx/ renamed from arch/x86/kernel/cpu/sgx/ioctl.c +0 -0
ioctl.c
index 90a5caf76939..2e10367ea66c 100644
arch/x86/kernel/cpu/sgx/ioctl.c +0 -0
@@ -604,7 +604,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl,
void __user *arg)
{
struct sgx_sigstruct *sigstruct;
struct sgx_enclave_init init_arg;
- struct page *initp_page;
void *token;
int ret;@@ -615,11 +614,15 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
if (copy_from_user(&init_arg, arg, sizeof(init_arg)))
return -EFAULT;
- initp_page = alloc_page(GFP_KERNEL);
- if (!initp_page)
+ /*
+ * 'sigstruct' must be on a page boundary and 'token' on a 512 byte
+ * boundary. kmalloc() will give this alignment when allocating
+ * PAGE_SIZE bytes.
+ */
+ sigstruct = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!sigstruct)
return -ENOMEM;- sigstruct = kmap(initp_page);
token = (void *)((unsigned long)sigstruct + PAGE_SIZE / 2);
memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);@@ -645,8 +648,7 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
ret = sgx_encl_init(encl, sigstruct, token);
out:
- kunmap(initp_page);
- __free_page(initp_page);
+ kfree(sigstruct);
return ret;}
I think that Coccinelle might understand that "initp_page" is allocated in the same function where later it is kmap()'ed. But I'm not able to write a Coccinelle check to find out these kinds of special cases. In these cases the correct solution is not to use kmap_local_page(). Instead delete the alloc_page() and use kmalloc().
Thanks,
Fabio
Quoted 141 lines · Fabio M. De Francesco
Quoted 5 lines · On mercoledì 20 aprile 2022 15:57:14 CEST Julia Lawall wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On mercoledì 20 aprile 2022 15:40:10 CEST Julia Lawall wrote:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Fabio M. De Francesco
Quoted 6 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
Quoted 2 lines
kmap()is being deprecated and these usages are all local tothe
Quoted 11 lines
Quoted 11 lines
thread
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
so there is no reason
kmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed
to
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax
and
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
semantics of expressions of Coccinelle. However, thanks Julia, this
code
Quoted 3 lines
Quoted 3 lines
looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been
allocated
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/
kunmap()Quoted 2 lines
Quoted 2 lines
to use
kmalloc()/kfree().Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Fox example, this is done in the following patch:
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is
passed
Quoted 1 line
Quoted 1 line
to
kmap()is allocated within that same function (vs. being passedas
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
argument to this function) and, if so, propose a replacement with
kmalloc()?It looks complex in this case, because the allocation is in another function, and it is passed to another function.
This is not the special case I was talking about. In this case your
code
Quoted 1 line
Quoted 1 line
for Coccinelle tells the right proposal and it is exactly what Alaa did
in
Quoted 3 lines
Quoted 3 lines
her patch (which is good!).
I'm talking about other special cases like the one I pointed to with
the
Quoted 2 lines
Quoted 2 lines
link I provided. I'm sorry if my bad English made you think that Alaa's patch was one of those cases where the page is allocated within the
same
Quoted 8 lines
Quoted 3 lines
function where
kmap()is.I hope that now I've been clearer :)
Ah, sorry for the misunderstanding. If you have an example, I can take a look and propose something for this special case.
julia
Yes, I have the example that you are asking for. It's that commit
633b0616cfe0from Ira Weiny.Let me copy and paste it here for your convenience...
arch/x86/kernel/cpu/sgx/ renamed from arch/x86/kernel/cpu/sgx/ioctl.c +0
-0
ioctl.c
index90a5caf76939..2e10367ea66c100644arch/x86/kernel/cpu/sgx/ioctl.c +8
-6@@ -604,7 +604,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) {struct sgx_sigstruct *sigstruct;struct sgx_enclave_init init_arg; -struct page *initp_page;void *token;int ret;@@ -615,11 +614,15 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)if (copy_from_user (&init_arg, arg,sizeof (init_arg)))return -EFAULT ; - initp_page =alloc_page (GFP_KERNEL ); -if (!initp_page) +/* +* 'sigstruct' must be on a page boundary and 'token' on a 512 byte +* boundary. kmalloc() will give this alignment when allocating +* PAGE_SIZE bytes. +*/ + sigstruct =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!sigstruct)return -ENOMEM ; - sigstruct =kmap (initp_page); token = (void *)((unsigned long )sigstruct +PAGE_SIZE /2 );memset (token,0 ,SGX_LAUNCH_TOKEN_SIZE );@@ -645,8 +648,7 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) ret =sgx_encl_init (encl, sigstruct, token); out: -kunmap (initp_page); -__free_page (initp_page); +kfree (sigstruct);return ret; }
Quoted 6 lines
I think that Coccinelle might understand that "
initp_page" is allocated in the same function where later it is kmap()'ed. But I'm not able to write a Coccinelle check to find out these kinds of special cases. In these cases the correct solution is not to usekmap_local_page(). Instead delete thealloc_page()and usekmalloc().
Sorry about missing this thread last week...
I've lost the Coccinelle scripts I wrote before but the ones which helped were documented in patches I submitted when Coccinelle was used.
I think Coccinelle can help a lot. And probably a lot more than I know since I'm not an expert in the language either.
However, In addition to the example Fabio shows above here are a few other things to look out for when writing Coccinelle scripts.
1) The addition of mem*_page() functions means sometimes the entire kmap/kunmap can be removed. Check out the Coccinelle script for that.[1]
2) kunmap_local() has ordering rules which often requires some manual review.[2]
3) kmap/kunmap is often wrapped in other subsystem helper functions. I was not sure how to deal with that in Coccinelle. Julia is this easy in Coccinelle?[3]
Ira
[1]
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
[2]
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/
[3]
https://lore.kernel.org/lkml/[email protected]/
Quoted 7 lines
Thanks,
Fabio
Quoted 163 lines · Ira Weiny
Quoted 141 lines · Fabio M. De Francesco
Quoted 5 lines · On mercoledì 20 aprile 2022 15:57:14 CEST Julia Lawall wrote:
Quoted 2 lines · Fabio M. De Francesco
Quoted 1 line · On mercoledì 20 aprile 2022 15:40:10 CEST Julia Lawall wrote:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines · Fabio M. De Francesco
Quoted 6 lines · On mercoledì 20 aprile 2022 08:03:05 CEST Julia Lawall wrote:
Quoted 4 lines · Alaa Mohamed
Quoted 2 lines
kmap()is being deprecated and these usages are all local tothe
Quoted 11 lines
Quoted 11 lines
thread
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
so there is no reason
kmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().OK, so from a Coccinelle point of view, could we do
@@
expression e1,e2,x,f;
@@Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
e1 =
- kmap
+kmap_local_page(e2)... when != x = e1 // not stored in any location and not passed
to
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
another function
Quoted 8 lines
when != f(...,e1,...) when != x = e2 when != f(...,e2,...)(quoted diff) +1
-1-kunmap(e2) +kunmap_local(e1)julia
I've never spent sufficient time to understand properly the syntax
and
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
semantics of expressions of Coccinelle. However, thanks Julia, this
code
Quoted 3 lines
Quoted 3 lines
looks good and can be very helpful.
Only a minor objection... it doesn't tell when 'e2' has been
allocated
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
within the same function where the
kmap()call is.In the particular case that I cite above, I'd prefer to remove the allocation of the page (say with
alloc_page()) and convertkmap()/
kunmap()Quoted 2 lines
Quoted 2 lines
to use
kmalloc()/kfree().Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Fox example, this is done in the following patch:
commit
633b0616cfe0("x86/sgx: Remove unnecessarykmap()fromsgx_ioc_enclave_init()") from Ira Weiny.Can Coccinelle catch also those special cases where a page that is
passed
Quoted 1 line
Quoted 1 line
to
kmap()is allocated within that same function (vs. being passedas
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
argument to this function) and, if so, propose a replacement with
kmalloc()?It looks complex in this case, because the allocation is in another function, and it is passed to another function.
This is not the special case I was talking about. In this case your
code
Quoted 1 line
Quoted 1 line
for Coccinelle tells the right proposal and it is exactly what Alaa did
in
Quoted 3 lines
Quoted 3 lines
her patch (which is good!).
I'm talking about other special cases like the one I pointed to with
the
Quoted 2 lines
Quoted 2 lines
link I provided. I'm sorry if my bad English made you think that Alaa's patch was one of those cases where the page is allocated within the
same
Quoted 8 lines
Quoted 3 lines
function where
kmap()is.I hope that now I've been clearer :)
Ah, sorry for the misunderstanding. If you have an example, I can take a look and propose something for this special case.
julia
Yes, I have the example that you are asking for. It's that commit
633b0616cfe0from Ira Weiny.Let me copy and paste it here for your convenience...
arch/x86/kernel/cpu/sgx/ renamed from arch/x86/kernel/cpu/sgx/ioctl.c +0
-0
ioctl.c
index90a5caf76939..2e10367ea66c100644arch/x86/kernel/cpu/sgx/ioctl.c +8
-6@@ -604,7 +604,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) {struct sgx_sigstruct *sigstruct;struct sgx_enclave_init init_arg; -struct page *initp_page;void *token;int ret;@@ -615,11 +614,15 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)if (copy_from_user (&init_arg, arg,sizeof (init_arg)))return -EFAULT ; - initp_page =alloc_page (GFP_KERNEL ); -if (!initp_page) +/* +* 'sigstruct' must be on a page boundary and 'token' on a 512 byte +* boundary. kmalloc() will give this alignment when allocating +* PAGE_SIZE bytes. +*/ + sigstruct =kmalloc (PAGE_SIZE ,GFP_KERNEL ); +if (!sigstruct)return -ENOMEM ; - sigstruct =kmap (initp_page); token = (void *)((unsigned long )sigstruct +PAGE_SIZE /2 );memset (token,0 ,SGX_LAUNCH_TOKEN_SIZE );@@ -645,8 +648,7 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg) ret =sgx_encl_init (encl, sigstruct, token); out: -kunmap (initp_page); -__free_page (initp_page); +kfree (sigstruct);return ret; }Quoted 6 lines
I think that Coccinelle might understand that "
initp_page" is allocated in the same function where later it is kmap()'ed. But I'm not able to write a Coccinelle check to find out these kinds of special cases. In these cases the correct solution is not to usekmap_local_page(). Instead delete thealloc_page()and usekmalloc().Sorry about missing this thread last week...
I've lost the Coccinelle scripts I wrote before but the ones which helped were documented in patches I submitted when Coccinelle was used.
I think Coccinelle can help a lot. And probably a lot more than I know since I'm not an expert in the language either.
However, In addition to the example Fabio shows above here are a few other things to look out for when writing Coccinelle scripts.
1) The addition of mem*_page() functions means sometimes the entire kmap/kunmap can be removed. Check out the Coccinelle script for that.[1]
Quoted 6 lines
2)
kunmap_local()has ordering rules which often requires some manual review.[2]3) kmap/kunmap is often wrapped in other subsystem helper functions. I was not sure how to deal with that in Coccinelle. Julia is this easy in Coccinelle?[3]
Thanks for the pointers.
[3] would depend on how much risk you are willing to take. The arguments are the same, except for the array index, for example. But one may prefer to be sure that nothing complex happens between the two calls.
julia
Boris Ostrovsky
AppliedQuoted 6 lines · Alaa Mohamed
kmap()is being deprecated and these usages are all local to the thread so there is no reasonkmap_local_page()can't be used.Replace
kmap()calls withkmap_local_page().Signed-off-by Alaa Mohamed <[email protected]>
Applied to for-linus-5.18. (Juergen, I kept your R-b from v1)
-boris