From: "Michael S. Tsirkin" <mst@redhat.com>
To: Feng Liu <feliu@nvidia.com>
Cc: "virtualization@lists.linux-foundation.org"
<virtualization@lists.linux-foundation.org>,
"feng.liu.kernel@gmail.com" <feng.liu.kernel@gmail.com>,
Jiri Pirko <jiri@nvidia.com>, Bodong Wang <bodong@nvidia.com>,
Gavin Li <gavinl@nvidia.com>
Subject: Re: [PATCH 3/3] virtio_ring: Use const to annotate read-only pointer params
Date: Wed, 8 Mar 2023 11:49:51 -0500 [thread overview]
Message-ID: <20230308114919-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20230308110235-mutt-send-email-mst@kernel.org>
On Wed, Mar 08, 2023 at 11:26:04AM -0500, Michael S. Tsirkin wrote:
> On Wed, Mar 08, 2023 at 10:59:57AM -0500, Feng Liu wrote:
> >
> >
> > On 2023-03-08 a.m.9:13, Michael S. Tsirkin wrote:
> > > External email: Use caution opening links or attachments
> > >
> > >
> > > On Tue, Mar 07, 2023 at 09:17:55PM +0000, Feng Liu wrote:
> > > > On 2023-03-07 04:14, David Edmondson wrote:
> > > > > External email: Use caution opening links or attachments
> > > > >
> > > > >
> > > > > Feng Liu via Virtualization <virtualization@lists.linux-foundation.org>
> > > > > writes:
> > > > >
> > > > > > Add const to make the read-only pointer parameters clear, similar to
> > > > > > many existing functions.
> > > > >
> > > > > In many of the modified functions the local variable that is a cast of
> > > > > the argument could also be const. Is there a reason not to do both at
> > > > > the same time?
> > > > >
> > > >
> > > > Hi,David
> > > >
> > > > In order to prevent the content of a pointer parameter from being
> > > > modified and increase the readability of the function, it is recommended
> > > > to add the 'const' keyword to the parameter. This is not necessary for
> > > > local variables and non-pointer parameters, as they are only stored on
> > > > the stack and do not affect the original value or structure member
> > > > passed into the function. Therefore, in this case, the 'const' keyword
> > > > is only added to pointer parameters.
> > >
> > > This makes no sense to me. If ytou cast away the const then it is
> > > pointless.
> > >
> >
> > Hi, Michael
> >
> > I really don't quite understand your point of view.
> > Is a local variable that needs to be add const? Can you help to point
> > out the specific problem/point ?
>
> I just repeated what David said. Basically most of these functions use
> to_vvq which uses container_of which in turn loses const qualifier.
> So your change is poinless since rest of code accesses vq through
> to_vvq.
>
> What to do? I don't like the idea of to_vvq_const.
> So I propose a version of container_of using _Generic
> which preserves the const qualifier.
>
>
> #define container_of(ptr, type, member) \
> ({ \
> const void *__mptr = (ptr); \
> static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> __same_type(*(ptr), void), \
> "pointer type mismatch in container_of()"); \
> _Generic((ptr), \
> typeof(&((const type *)0)->member): \
> (const type *)(__mptr - offsetof(type, member)), \
> default: \
> (type *)(__mptr - offsetof(type, member))); \
> })
>
>
> I'll hack it up in a day or two and post.
>
Oh wait a second. There's already container_of_const.
So just use it in to_vvq.
--
MST
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
next prev parent reply other threads:[~2023-03-08 16:50 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-07 3:57 [PATCH 0/3] virtio_ring: Clean up code for virtio ring and pci Feng Liu via Virtualization
2023-03-07 3:57 ` [PATCH 1/3] virtio_pci_modern: Remove unnecessary num zero check Feng Liu via Virtualization
2023-03-07 9:10 ` David Edmondson
2023-03-08 5:52 ` Jason Wang
2023-03-08 6:57 ` Michael S. Tsirkin
2023-03-08 14:23 ` Michael S. Tsirkin
2023-03-08 14:33 ` Feng Liu via Virtualization
2023-03-07 3:57 ` [PATCH 2/3] virtio_ring: Avoid using inline for small functions Feng Liu via Virtualization
2023-03-07 9:11 ` David Edmondson
2023-03-08 5:55 ` Jason Wang
2023-03-07 3:57 ` [PATCH 3/3] virtio_ring: Use const to annotate read-only pointer params Feng Liu via Virtualization
2023-03-07 9:14 ` David Edmondson
2023-03-07 21:17 ` Feng Liu via Virtualization
2023-03-08 14:13 ` Michael S. Tsirkin
2023-03-08 15:59 ` Feng Liu via Virtualization
2023-03-08 16:25 ` Michael S. Tsirkin
2023-03-08 16:44 ` Feng Liu via Virtualization
2023-03-08 16:49 ` Michael S. Tsirkin [this message]
2023-03-08 17:26 ` Feng Liu via Virtualization
2023-03-08 5:58 ` Jason Wang
2023-03-08 14:07 ` Feng Liu via Virtualization
2023-03-08 14:13 ` Feng Liu via Virtualization
2023-03-08 14:16 ` Michael S. Tsirkin
2023-03-08 14:19 ` Feng Liu via Virtualization
2023-03-08 14:28 ` Michael S. Tsirkin
2023-03-08 14:40 ` Feng Liu via Virtualization
2023-03-08 14:47 ` Michael S. Tsirkin
2023-03-08 15:47 ` Feng Liu via Virtualization
[not found] ` <ZAmlwyVfz+IK1b6T@nanopsycho>
2023-03-09 14:27 ` Michael S. Tsirkin
2023-03-08 6:59 ` Michael S. Tsirkin
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=20230308114919-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=bodong@nvidia.com \
--cc=feliu@nvidia.com \
--cc=feng.liu.kernel@gmail.com \
--cc=gavinl@nvidia.com \
--cc=jiri@nvidia.com \
--cc=virtualization@lists.linux-foundation.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.