* [PATCH v2] NVMe: Use put_unaligned_be64
@ 2015-01-10 8:52 Vaishali Thakkar
2015-01-12 19:06 ` Matthew Wilcox
0 siblings, 1 reply; 2+ messages in thread
From: Vaishali Thakkar @ 2015-01-10 8:52 UTC (permalink / raw)
To: matthew.r.wilcox; +Cc: linux-kernel, linux-nvme, axboe
This patch introduces the use of function put_unaligned_be64.
This is done using Coccinelle and semantic patch used is as follows:
@a@
typedef u64, __be64, uint64_t;
{u64,__be64,uint64_t} e64;
identifier tmp;
expression ptr;
expression y,e;
type T;
@@
- tmp = cpu_to_be64(y);
<+... when != tmp
(
- memcpy(ptr, (T)&tmp, \(8\|sizeof(u64)\|sizeof(__be64)\|sizeof(uint64_t)\|sizeof(e64)\));
+ put_unaligned_be64(y,ptr);
|
- memcpy(ptr, (T)&tmp, ...);
+ put_unaligned_be64(y,ptr);
)
...+>
? tmp = e
@@ type T; identifier a.tmp; @@
- T tmp;
...when != tmp
Signed-off-by: Vaishali Thakkar <vthakkar1994@gmail.com>
--
Changes in v2:
-Shorten commit message and change subject line
-<asm/unaligned.h> has arch-specific knowlege of which of
the implementations needs to be used. So, include it instaed
of <linux/unaligned/access_ok.h>.
drivers/block/nvme-scsi.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/drivers/block/nvme-scsi.c b/drivers/block/nvme-scsi.c
index 5e78568..12893ef 100644
--- a/drivers/block/nvme-scsi.c
+++ b/drivers/block/nvme-scsi.c
@@ -43,6 +43,7 @@
#include <linux/types.h>
#include <scsi/sg.h>
#include <scsi/scsi.h>
+#include <asm/unaligned.h>
static int sg_version_num = 30534; /* 2 digits for each component */
@@ -1417,7 +1418,6 @@ static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
u64 rlba;
u8 prot_en;
u8 p_type_lut[4] = {0, 0, 1, 2};
- __be64 tmp_rlba;
__be32 tmp_rlba_32;
__be32 tmp_len;
@@ -1434,9 +1434,8 @@ static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
memcpy(response, &tmp_rlba_32, sizeof(u32));
memcpy(&response[4], &tmp_len, sizeof(u32));
} else {
- tmp_rlba = cpu_to_be64(rlba);
tmp_len = cpu_to_be32(lba_length);
- memcpy(response, &tmp_rlba, sizeof(u64));
+ put_unaligned_be64(rlba, response);
memcpy(&response[8], &tmp_len, sizeof(u32));
response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en;
/* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
--
1.9.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v2] NVMe: Use put_unaligned_be64
2015-01-10 8:52 [PATCH v2] NVMe: Use put_unaligned_be64 Vaishali Thakkar
@ 2015-01-12 19:06 ` Matthew Wilcox
0 siblings, 0 replies; 2+ messages in thread
From: Matthew Wilcox @ 2015-01-12 19:06 UTC (permalink / raw)
To: Vaishali Thakkar; +Cc: matthew.r.wilcox, axboe, linux-kernel, linux-nvme
On Sat, Jan 10, 2015 at 02:22:59PM +0530, Vaishali Thakkar wrote:
> This patch introduces the use of function put_unaligned_be64.
>
> This is done using Coccinelle and semantic patch used is as follows:
I appreciate you're using an automated tool to find the problem ...
> diff --git a/drivers/block/nvme-scsi.c b/drivers/block/nvme-scsi.c
> index 5e78568..12893ef 100644
> --- a/drivers/block/nvme-scsi.c
> +++ b/drivers/block/nvme-scsi.c
> @@ -1417,7 +1418,6 @@ static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
> u64 rlba;
> u8 prot_en;
> u8 p_type_lut[4] = {0, 0, 1, 2};
> - __be64 tmp_rlba;
> __be32 tmp_rlba_32;
> __be32 tmp_len;
>
> @@ -1434,9 +1434,8 @@ static void nvme_trans_fill_read_cap(u8 *response, struct nvme_id_ns *id_ns,
> memcpy(response, &tmp_rlba_32, sizeof(u32));
> memcpy(&response[4], &tmp_len, sizeof(u32));
> } else {
> - tmp_rlba = cpu_to_be64(rlba);
> tmp_len = cpu_to_be32(lba_length);
> - memcpy(response, &tmp_rlba, sizeof(u64));
> + put_unaligned_be64(rlba, response);
> memcpy(&response[8], &tmp_len, sizeof(u32));
> response[12] = (p_type_lut[id_ns->dps & 0x3] << 1) | prot_en;
> /* P_I_Exponent = 0x0 | LBPPBE = 0x0 */
... but can't you look at the code you're modifying and notice that
maybe there's an opportunity to do better? The two things I notice when
looking at this code:
- There are also 32-bit variables being used in the same way, so a
complete patch would also eliminate them.
- The 'response' buffer is allocated from kmalloc, so it's aligned, and
these fields are aligned relative to the start of the buffer,
so we know they're aligned. So we should be using something in
the cpu_to_be64p family to store the value to the fields, not
put_unaligned_be64().
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-01-12 19:06 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-10 8:52 [PATCH v2] NVMe: Use put_unaligned_be64 Vaishali Thakkar
2015-01-12 19:06 ` Matthew Wilcox
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox