* [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions
@ 2016-06-10 15:00 Peter Maydell
2016-06-10 15:04 ` Eric Blake
2016-06-13 9:34 ` Paolo Bonzini
0 siblings, 2 replies; 3+ messages in thread
From: Peter Maydell @ 2016-06-10 15:00 UTC (permalink / raw)
To: qemu-devel; +Cc: patches, Paolo Bonzini
The *_to_cpup() functions are not very useful, as they simply do
a pointer dereference and then a *_to_cpu(). Instead use either:
* ld*_*_p(), if the data is at an address that might not be
correctly aligned for the load
* a local dereference and *_to_cpu(), if the pointer is
the correct type and known to be correctly aligned
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
The motivation is to be able to drop *_to_cpup() entirely;
we don't have many places that use it.
---
nbd/client.c | 8 ++++----
nbd/server.c | 10 +++++-----
qemu-nbd.c | 4 ++--
3 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/nbd/client.c b/nbd/client.c
index 31b88f3..bb8981f 100644
--- a/nbd/client.c
+++ b/nbd/client.c
@@ -572,7 +572,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
error_setg(errp, "Failed to read export flags");
goto fail;
}
- *flags = be32_to_cpup(flags);
+ *flags = be32_to_cpu(*flags);
} else {
error_setg(errp, "Bad magic received");
goto fail;
@@ -726,9 +726,9 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply)
[ 7 .. 15] handle
*/
- magic = be32_to_cpup((uint32_t*)buf);
- reply->error = be32_to_cpup((uint32_t*)(buf + 4));
- reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
+ magic = ldl_be_p(buf);
+ reply->error = ldl_be_p(buf + 4);
+ reply->handle = ldq_be_p(buf + 8);
reply->error = nbd_errno_to_system_errno(reply->error);
diff --git a/nbd/server.c b/nbd/server.c
index b2cfeb9..91471f1 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -646,11 +646,11 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, struct nbd_request *request)
[24 .. 27] len
*/
- magic = be32_to_cpup((uint32_t*)buf);
- request->type = be32_to_cpup((uint32_t*)(buf + 4));
- request->handle = be64_to_cpup((uint64_t*)(buf + 8));
- request->from = be64_to_cpup((uint64_t*)(buf + 16));
- request->len = be32_to_cpup((uint32_t*)(buf + 24));
+ magic = ldl_be_p(buf);
+ request->type = ldl_be_p(buf + 4);
+ request->handle = ldq_be_p(buf + 8);
+ request->from = ldq_be_p(buf + 16);
+ request->len = ldl_be_p(buf + 24);
TRACE("Got request: "
"{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
diff --git a/qemu-nbd.c b/qemu-nbd.c
index 6554f0a..9519db3 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -154,8 +154,8 @@ static void read_partition(uint8_t *p, struct partition_record *r)
r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
r->end_sector = p[6] & 0x3f;
- r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
- r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
+ r->start_sector_abs = ldl_le_p(p + 8);
+ r->nb_sectors_abs = ldl_le_p(p + 12);
}
static int find_partition(BlockBackend *blk, int partition,
--
1.9.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions
2016-06-10 15:00 [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions Peter Maydell
@ 2016-06-10 15:04 ` Eric Blake
2016-06-13 9:34 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Eric Blake @ 2016-06-10 15:04 UTC (permalink / raw)
To: Peter Maydell, qemu-devel; +Cc: Paolo Bonzini, patches
[-- Attachment #1: Type: text/plain, Size: 889 bytes --]
On 06/10/2016 09:00 AM, Peter Maydell wrote:
> The *_to_cpup() functions are not very useful, as they simply do
> a pointer dereference and then a *_to_cpu(). Instead use either:
> * ld*_*_p(), if the data is at an address that might not be
> correctly aligned for the load
> * a local dereference and *_to_cpu(), if the pointer is
> the correct type and known to be correctly aligned
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> The motivation is to be able to drop *_to_cpup() entirely;
> we don't have many places that use it.
> ---
> nbd/client.c | 8 ++++----
> nbd/server.c | 10 +++++-----
> qemu-nbd.c | 4 ++--
> 3 files changed, 11 insertions(+), 11 deletions(-)
>
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions
2016-06-10 15:00 [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions Peter Maydell
2016-06-10 15:04 ` Eric Blake
@ 2016-06-13 9:34 ` Paolo Bonzini
1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2016-06-13 9:34 UTC (permalink / raw)
To: Peter Maydell, qemu-devel; +Cc: patches
On 10/06/2016 17:00, Peter Maydell wrote:
> The *_to_cpup() functions are not very useful, as they simply do
> a pointer dereference and then a *_to_cpu(). Instead use either:
> * ld*_*_p(), if the data is at an address that might not be
> correctly aligned for the load
> * a local dereference and *_to_cpu(), if the pointer is
> the correct type and known to be correctly aligned
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> The motivation is to be able to drop *_to_cpup() entirely;
> we don't have many places that use it.
Good idea indeed. Queued both patches.
Paolo
> ---
> nbd/client.c | 8 ++++----
> nbd/server.c | 10 +++++-----
> qemu-nbd.c | 4 ++--
> 3 files changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/nbd/client.c b/nbd/client.c
> index 31b88f3..bb8981f 100644
> --- a/nbd/client.c
> +++ b/nbd/client.c
> @@ -572,7 +572,7 @@ int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint32_t *flags,
> error_setg(errp, "Failed to read export flags");
> goto fail;
> }
> - *flags = be32_to_cpup(flags);
> + *flags = be32_to_cpu(*flags);
> } else {
> error_setg(errp, "Bad magic received");
> goto fail;
> @@ -726,9 +726,9 @@ ssize_t nbd_receive_reply(QIOChannel *ioc, struct nbd_reply *reply)
> [ 7 .. 15] handle
> */
>
> - magic = be32_to_cpup((uint32_t*)buf);
> - reply->error = be32_to_cpup((uint32_t*)(buf + 4));
> - reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
> + magic = ldl_be_p(buf);
> + reply->error = ldl_be_p(buf + 4);
> + reply->handle = ldq_be_p(buf + 8);
>
> reply->error = nbd_errno_to_system_errno(reply->error);
>
> diff --git a/nbd/server.c b/nbd/server.c
> index b2cfeb9..91471f1 100644
> --- a/nbd/server.c
> +++ b/nbd/server.c
> @@ -646,11 +646,11 @@ static ssize_t nbd_receive_request(QIOChannel *ioc, struct nbd_request *request)
> [24 .. 27] len
> */
>
> - magic = be32_to_cpup((uint32_t*)buf);
> - request->type = be32_to_cpup((uint32_t*)(buf + 4));
> - request->handle = be64_to_cpup((uint64_t*)(buf + 8));
> - request->from = be64_to_cpup((uint64_t*)(buf + 16));
> - request->len = be32_to_cpup((uint32_t*)(buf + 24));
> + magic = ldl_be_p(buf);
> + request->type = ldl_be_p(buf + 4);
> + request->handle = ldq_be_p(buf + 8);
> + request->from = ldq_be_p(buf + 16);
> + request->len = ldl_be_p(buf + 24);
>
> TRACE("Got request: "
> "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
> diff --git a/qemu-nbd.c b/qemu-nbd.c
> index 6554f0a..9519db3 100644
> --- a/qemu-nbd.c
> +++ b/qemu-nbd.c
> @@ -154,8 +154,8 @@ static void read_partition(uint8_t *p, struct partition_record *r)
> r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
> r->end_sector = p[6] & 0x3f;
>
> - r->start_sector_abs = le32_to_cpup((uint32_t *)(p + 8));
> - r->nb_sectors_abs = le32_to_cpup((uint32_t *)(p + 12));
> + r->start_sector_abs = ldl_le_p(p + 8);
> + r->nb_sectors_abs = ldl_le_p(p + 12);
> }
>
> static int find_partition(BlockBackend *blk, int partition,
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-06-13 9:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-10 15:00 [Qemu-devel] [PATCH] nbd: Don't use *_to_cpup() functions Peter Maydell
2016-06-10 15:04 ` Eric Blake
2016-06-13 9:34 ` Paolo Bonzini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).