netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vadim Fedorenko <vadim.fedorenko@linux.dev>
To: Jiri Slaby <jirislaby@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Vadim Fedorenko <vadfed@meta.com>
Cc: Jakub Kicinski <kuba@kernel.org>,
	Jonathan Lemon <jonathan.lemon@gmail.com>,
	netdev@vger.kernel.org
Subject: Re: [PATCH net v2 1/2] ptp: ocp: adjust sysfs entries to expose tty information
Date: Wed, 14 Aug 2024 09:37:17 +0100	[thread overview]
Message-ID: <6a28249c-be3a-498a-8a48-af853350c5d8@linux.dev> (raw)
In-Reply-To: <dc9df0fd-6344-49ad-87c6-8e5c63857bd6@kernel.org>

On 14/08/2024 06:00, Jiri Slaby wrote:
> On 13. 08. 24, 20:24, Vadim Fedorenko wrote:
>> On 13/08/2024 10:33, Greg Kroah-Hartman wrote:
>>> On Mon, Aug 05, 2024 at 03:04:59PM -0700, Vadim Fedorenko wrote:
>>>> Starting v6.8 the serial port subsystem changed the hierarchy of 
>>>> devices
>>>> and symlinks are not working anymore. Previous discussion made it clear
>>>> that the idea of symlinks for tty devices was wrong by design. 
>>>> Implement
>>>> additional attributes to expose the information. Fixes tag points to 
>>>> the
>>>> commit which introduced the change.
>>>>
>>>> Fixes: b286f4e87e32 ("serial: core: Move tty and serdev to be 
>>>> children of serial core port device")
>>>> Signed-off-by: Vadim Fedorenko <vadfed@meta.com>
>>>> ---
>>>>   drivers/ptp/ptp_ocp.c | 68 
>>>> +++++++++++++++++++++++++++++++++----------
>>>>   1 file changed, 52 insertions(+), 16 deletions(-)
>>>>
>>>> diff --git a/drivers/ptp/ptp_ocp.c b/drivers/ptp/ptp_ocp.c
>>>> index ee2ced88ab34..7a5026656452 100644
>>>> --- a/drivers/ptp/ptp_ocp.c
>>>> +++ b/drivers/ptp/ptp_ocp.c
>>>> @@ -3346,6 +3346,55 @@ static EXT_ATTR_RO(freq, frequency, 1);
>>>>   static EXT_ATTR_RO(freq, frequency, 2);
>>>>   static EXT_ATTR_RO(freq, frequency, 3);
>>>> +static ssize_t
>>>> +ptp_ocp_tty_show(struct device *dev, struct device_attribute *attr, 
>>>> char *buf)
>>>> +{
>>>> +    struct dev_ext_attribute *ea = to_ext_attr(attr);
>>>> +    struct ptp_ocp *bp = dev_get_drvdata(dev);
>>>> +    struct ptp_ocp_serial_port *port;
>>>> +
>>>> +    port = (void *)((uintptr_t)bp + (uintptr_t)ea->var);
>>>
>>> That's insane pointer math, how do we know this is correct?
>>>
>>>> +    return sysfs_emit(buf, "ttyS%d", port->line);
>>>> +}
>>>> +
>>>> +static umode_t
>>>> +ptp_ocp_timecard_tty_is_visible(struct kobject *kobj, struct 
>>>> attribute *attr, int n)
>>>> +{
>>>> +    struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
>>>> +    struct ptp_ocp_serial_port *port;
>>>> +    struct device_attribute *dattr;
>>>> +    struct dev_ext_attribute *ea;
>>>> +
>>>> +    if (strncmp(attr->name, "tty", 3))
>>>> +        return attr->mode;
>>>> +
>>>> +    dattr = container_of(attr, struct device_attribute, attr);
>>>> +    ea = container_of(dattr, struct dev_ext_attribute, attr);
>>>> +    port = (void *)((uintptr_t)bp + (uintptr_t)ea->var);
>>>
>>> That's crazy pointer math, how are you ensured that it is correct?  Why
>>> isn't there a container_of() thing here instead?
>>
>> Well, container_of cannot be used here because the attributes are static
>> while the function reads dynamic instance. The only values that are
>> populated into the attributes of the group are offsets.
>> But I can convert it to a helper which will check that the offset 
>> provided is the real offset of the structure we expect. And it could 
>> be reused in both "is_visible" and "show" functions.
> 
> Strong NACK against this approach.
> 
> What about converting those 4 ports into an array and adding an enum { 
> PORT_GNSS, POTR_GNSS2, PORT_MAC, PORT_NMEA }?

Why is it a problem? I don't see big difference between these 2
implementations:

struct ptp_ocp_serial_port *get_port(struct ptp_ocp *bp, void *offset)
{
	switch((uintptr_t)offset) {
		case offsetof(struct ptp_ocp, gnss_port):
			return &bp->gnss_port;
		case offsetof(struct ptp_ocp, gnss2_port):
			return &bp->gnss2_port;
		case offsetof(struct ptp_ocp, mac_port):
			return &bp->mac_port;
		case offsetof(struct ptp_ocp, nmea_port):
			return &bp->nmea_port;
	}
	return NULL;
}

and:

struct ptp_ocp_serial_port *get_port(struct ptp_ocp *bp, void *offset)
{
	switch((enum port_type)offset) {
		case PORT_GNSS:
			return &bp->tty_port[PORT_GNSS];
		case PORT_GNSS2:
			return &bp->tty_port[PORT_GNSS2];
		case PORT_MAC:
			return &bp->tty_port[PORT_MAC];
		case PORT_NMEA:
			return &bp->tty_port[PORT_NMEA];
	}
	return NULL;
}

The second option will require more LoC to change the initialization
part of the driver, but will not simplify the access.
If you suggest to use enum value directly, without the check, then
it will not solve the problem of checking the boundary, which Greg
refers to AFAIU.

Thanks!

  reply	other threads:[~2024-08-14  8:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-05 22:04 [PATCH net v2 1/2] ptp: ocp: adjust sysfs entries to expose tty information Vadim Fedorenko
2024-08-05 22:05 ` [PATCH net v2 2/2] docs: ABI: update OCP TimeCard sysfs entries Vadim Fedorenko
2024-08-08  9:13   ` Simon Horman
2024-08-13  9:33 ` [PATCH net v2 1/2] ptp: ocp: adjust sysfs entries to expose tty information Greg Kroah-Hartman
2024-08-13 18:24   ` Vadim Fedorenko
2024-08-14  5:00     ` Jiri Slaby
2024-08-14  8:37       ` Vadim Fedorenko [this message]
2024-08-14 10:30         ` Jiri Slaby
2024-08-15 13:40           ` Vadim Fedorenko

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=6a28249c-be3a-498a-8a48-af853350c5d8@linux.dev \
    --to=vadim.fedorenko@linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=jonathan.lemon@gmail.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=vadfed@meta.com \
    /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 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).