diff mbox series

net, uapi: remove inclusion of arpa/inet.h

Message ID 20220329212946.2861648-1-ndesaulniers@google.com (mailing list archive)
State Superseded
Headers show
Series net, uapi: remove inclusion of arpa/inet.h | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Nick Desaulniers March 29, 2022, 9:29 p.m. UTC
Testing out CONFIG_UAPI_HEADER_TEST=y with a prebuilt Bionic sysroot
from Android's SDK, I encountered an error:

  HDRTEST usr/include/linux/fsi.h
In file included from <built-in>:1:
In file included from ./usr/include/linux/tipc_config.h:46:
prebuilts/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arpa/inet.h:39:1:
error: unknown type name 'in_addr_t'
in_addr_t inet_addr(const char* __s);
^

This is because Bionic has a bug in its inclusion chain. I sent a patch
to fix that, but looking closer at include/uapi/linux/tipc_config.h,
there's a comment that it includes arpa/inet.h for ntohs; but ntohs is
already defined in include/linux/byteorder/generic.h which is already
included in include/uapi/linux/tipc_config.h. There are no __KERNEL__
guards on include/linux/byteorder/generic.h's definition of ntohs. So
besides fixing Bionic, it looks like we can additionally remove this
unnecessary header inclusion.

Link: https://android-review.googlesource.com/c/platform/bionic/+/2048127
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
---
 include/uapi/linux/tipc_config.h | 4 ----
 1 file changed, 4 deletions(-)


base-commit: 5efabdadcf4a5b9a37847ecc85ba71cf2eff0fcf
prerequisite-patch-id: 0c2abf2af8051f4b37a70ef11b7d2fc2a3ec7181

Comments

Nick Desaulniers March 29, 2022, 9:50 p.m. UTC | #1
On Tue, Mar 29, 2022 at 2:29 PM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> Testing out CONFIG_UAPI_HEADER_TEST=y with a prebuilt Bionic sysroot
> from Android's SDK, I encountered an error:
>
>   HDRTEST usr/include/linux/fsi.h
> In file included from <built-in>:1:
> In file included from ./usr/include/linux/tipc_config.h:46:
> prebuilts/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arpa/inet.h:39:1:
> error: unknown type name 'in_addr_t'
> in_addr_t inet_addr(const char* __s);
> ^

Oh, this might not quite be the correct fix for this particular issue.

Cherry picking this diff back into my Android kernel tree, I now observe:
  HDRTEST usr/include/linux/tipc_config.h
In file included from <built-in>:1:
./usr/include/linux/tipc_config.h:268:4: error: implicit declaration
of function 'ntohs' [-Werror,-Wimplicit-function-declaration]
                (ntohs(((struct tlv_desc *)tlv)->tlv_len) <= space);
                 ^

Is there more than one byteorder.h? Oh, I see what I did; ntohs is defined in
linux/byteorder/generic.h
not
usr/include/asm/byteorder.h
Sorry, I saw `byteorder` and mixed up the path with the filename.

So rather than just remove the latter, I should additionally be adding
the former. Though that then produces

common/include/linux/byteorder/generic.h:146:9: error: implicit
declaration of function '__cpu_to_le16'
[-Werror,-Wimplicit-function-declaration]
        *var = cpu_to_le16(le16_to_cpu(*var) + val);
               ^

Oh?

>
> This is because Bionic has a bug in its inclusion chain. I sent a patch
> to fix that, but looking closer at include/uapi/linux/tipc_config.h,
> there's a comment that it includes arpa/inet.h for ntohs; but ntohs is
> already defined in include/linux/byteorder/generic.h which is already
> included in include/uapi/linux/tipc_config.h. There are no __KERNEL__
> guards on include/linux/byteorder/generic.h's definition of ntohs. So
> besides fixing Bionic, it looks like we can additionally remove this
> unnecessary header inclusion.
>
> Link: https://android-review.googlesource.com/c/platform/bionic/+/2048127
> Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
> ---
>  include/uapi/linux/tipc_config.h | 4 ----
>  1 file changed, 4 deletions(-)
>
> diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
> index 4dfc05651c98..b38374d5f192 100644
> --- a/include/uapi/linux/tipc_config.h
> +++ b/include/uapi/linux/tipc_config.h
> @@ -43,10 +43,6 @@
>  #include <linux/tipc.h>
>  #include <asm/byteorder.h>
>
> -#ifndef __KERNEL__
> -#include <arpa/inet.h> /* for ntohs etc. */
> -#endif
> -
>  /*
>   * Configuration
>   *
>
> base-commit: 5efabdadcf4a5b9a37847ecc85ba71cf2eff0fcf
> prerequisite-patch-id: 0c2abf2af8051f4b37a70ef11b7d2fc2a3ec7181
> --
> 2.35.1.1021.g381101b075-goog
>
Nick Desaulniers March 29, 2022, 10:09 p.m. UTC | #2
On Tue, Mar 29, 2022 at 2:50 PM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Tue, Mar 29, 2022 at 2:29 PM Nick Desaulniers
> <ndesaulniers@google.com> wrote:
> >
> > Testing out CONFIG_UAPI_HEADER_TEST=y with a prebuilt Bionic sysroot
> > from Android's SDK, I encountered an error:
> >
> >   HDRTEST usr/include/linux/fsi.h
> > In file included from <built-in>:1:
> > In file included from ./usr/include/linux/tipc_config.h:46:
> > prebuilts/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arpa/inet.h:39:1:
> > error: unknown type name 'in_addr_t'
> > in_addr_t inet_addr(const char* __s);
> > ^
>
> Oh, this might not quite be the correct fix for this particular issue.
>
> Cherry picking this diff back into my Android kernel tree, I now observe:
>   HDRTEST usr/include/linux/tipc_config.h
> In file included from <built-in>:1:
> ./usr/include/linux/tipc_config.h:268:4: error: implicit declaration
> of function 'ntohs' [-Werror,-Wimplicit-function-declaration]
>                 (ntohs(((struct tlv_desc *)tlv)->tlv_len) <= space);
>                  ^
>
> Is there more than one byteorder.h? Oh, I see what I did; ntohs is defined in
> linux/byteorder/generic.h
> not
> usr/include/asm/byteorder.h
> Sorry, I saw `byteorder` and mixed up the path with the filename.
>
> So rather than just remove the latter, I should additionally be adding
> the former. Though that then produces
>
> common/include/linux/byteorder/generic.h:146:9: error: implicit
> declaration of function '__cpu_to_le16'
> [-Werror,-Wimplicit-function-declaration]
>         *var = cpu_to_le16(le16_to_cpu(*var) + val);
>                ^
>
> Oh?

Should there be a definition of ntohs (and friends) under
include/uapi/linux/ somewhere, rather than have the kernel header
include/uapi/linux/tipc_config.h depend on the libc header
arpa/inet.h?

It looks like we already have
include/uapi/linux/byteorder/{big|little}_endian.h to define
__be16_to_cpu and friends, just no definition of ntohs under
include/uapi/linux/.

Also, it looks like include/uapi/linux/ has definitions for
__constant_ntohs in
include/uapi/linux/byteorder/{big|little}_endian.h.  Should those 2
headers also define ntohs (and friends) unprefixed, i.e. the versions
that don't depend on constant expressions?

(I wish there was an entry in MAINTAINERS for UAPI questions like this...)

>
> >
> > This is because Bionic has a bug in its inclusion chain. I sent a patch
> > to fix that, but looking closer at include/uapi/linux/tipc_config.h,
> > there's a comment that it includes arpa/inet.h for ntohs; but ntohs is
> > already defined in include/linux/byteorder/generic.h which is already
> > included in include/uapi/linux/tipc_config.h. There are no __KERNEL__
> > guards on include/linux/byteorder/generic.h's definition of ntohs. So
> > besides fixing Bionic, it looks like we can additionally remove this
> > unnecessary header inclusion.
> >
> > Link: https://android-review.googlesource.com/c/platform/bionic/+/2048127
> > Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
> > ---
> >  include/uapi/linux/tipc_config.h | 4 ----
> >  1 file changed, 4 deletions(-)
> >
> > diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
> > index 4dfc05651c98..b38374d5f192 100644
> > --- a/include/uapi/linux/tipc_config.h
> > +++ b/include/uapi/linux/tipc_config.h
> > @@ -43,10 +43,6 @@
> >  #include <linux/tipc.h>
> >  #include <asm/byteorder.h>
> >
> > -#ifndef __KERNEL__
> > -#include <arpa/inet.h> /* for ntohs etc. */
> > -#endif
> > -
> >  /*
> >   * Configuration
> >   *
> >
> > base-commit: 5efabdadcf4a5b9a37847ecc85ba71cf2eff0fcf
> > prerequisite-patch-id: 0c2abf2af8051f4b37a70ef11b7d2fc2a3ec7181
> > --
> > 2.35.1.1021.g381101b075-goog
> >
>
>
> --
> Thanks,
> ~Nick Desaulniers
Nick Desaulniers March 29, 2022, 10:26 p.m. UTC | #3
On Tue, Mar 29, 2022 at 3:09 PM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Tue, Mar 29, 2022 at 2:50 PM Nick Desaulniers
> <ndesaulniers@google.com> wrote:
> >
> > On Tue, Mar 29, 2022 at 2:29 PM Nick Desaulniers
> > <ndesaulniers@google.com> wrote:
> > >
> > > Testing out CONFIG_UAPI_HEADER_TEST=y with a prebuilt Bionic sysroot
> > > from Android's SDK, I encountered an error:
> > >
> > >   HDRTEST usr/include/linux/fsi.h
> > > In file included from <built-in>:1:
> > > In file included from ./usr/include/linux/tipc_config.h:46:
> > > prebuilts/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arpa/inet.h:39:1:
> > > error: unknown type name 'in_addr_t'
> > > in_addr_t inet_addr(const char* __s);
> > > ^
> >
> > Oh, this might not quite be the correct fix for this particular issue.
> >
> > Cherry picking this diff back into my Android kernel tree, I now observe:
> >   HDRTEST usr/include/linux/tipc_config.h
> > In file included from <built-in>:1:
> > ./usr/include/linux/tipc_config.h:268:4: error: implicit declaration
> > of function 'ntohs' [-Werror,-Wimplicit-function-declaration]
> >                 (ntohs(((struct tlv_desc *)tlv)->tlv_len) <= space);
> >                  ^
> >
> > Is there more than one byteorder.h? Oh, I see what I did; ntohs is defined in
> > linux/byteorder/generic.h
> > not
> > usr/include/asm/byteorder.h
> > Sorry, I saw `byteorder` and mixed up the path with the filename.
> >
> > So rather than just remove the latter, I should additionally be adding
> > the former. Though that then produces
> >
> > common/include/linux/byteorder/generic.h:146:9: error: implicit
> > declaration of function '__cpu_to_le16'
> > [-Werror,-Wimplicit-function-declaration]
> >         *var = cpu_to_le16(le16_to_cpu(*var) + val);
> >                ^
> >
> > Oh?
>
> Should there be a definition of ntohs (and friends) under
> include/uapi/linux/ somewhere, rather than have the kernel header
> include/uapi/linux/tipc_config.h depend on the libc header
> arpa/inet.h?
>
> It looks like we already have
> include/uapi/linux/byteorder/{big|little}_endian.h to define
> __be16_to_cpu and friends, just no definition of ntohs under
> include/uapi/linux/.
>
> Also, it looks like include/uapi/linux/ has definitions for
> __constant_ntohs in
> include/uapi/linux/byteorder/{big|little}_endian.h.  Should those 2
> headers also define ntohs (and friends) unprefixed, i.e. the versions
> that don't depend on constant expressions?

I think the answer is yes; in addition to the diff in this patch, the
following seems to be working:
```
diff --git a/include/uapi/linux/byteorder/little_endian.h
b/include/uapi/linux/byteorder/little_endian.h
index cd98982e7523..c14f2c3728e2 100644
--- a/include/uapi/linux/byteorder/little_endian.h
+++ b/include/uapi/linux/byteorder/little_endian.h
@@ -103,5 +103,8 @@ static __always_inline __u16 __be16_to_cpup(const __be16 *p)
 #define __cpu_to_be16s(x) __swab16s((x))
 #define __be16_to_cpus(x) __swab16s((x))

+#define htonl(x) __cpu_to_be32(x)
+#define htons(x) __cpu_to_be16(x)
+#define ntohs(x) __be16_to_cpu(x)

 #endif /* _UAPI_LINUX_BYTEORDER_LITTLE_ENDIAN_H */
```
I think if I flush out the rest for both endiannesses, then we should
be in good shape?

>
> (I wish there was an entry in MAINTAINERS for UAPI questions like this...)
>
> >
> > >
> > > This is because Bionic has a bug in its inclusion chain. I sent a patch
> > > to fix that, but looking closer at include/uapi/linux/tipc_config.h,
> > > there's a comment that it includes arpa/inet.h for ntohs; but ntohs is
> > > already defined in include/linux/byteorder/generic.h which is already
> > > included in include/uapi/linux/tipc_config.h. There are no __KERNEL__
> > > guards on include/linux/byteorder/generic.h's definition of ntohs. So
> > > besides fixing Bionic, it looks like we can additionally remove this
> > > unnecessary header inclusion.
> > >
> > > Link: https://android-review.googlesource.com/c/platform/bionic/+/2048127
> > > Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
> > > ---
> > >  include/uapi/linux/tipc_config.h | 4 ----
> > >  1 file changed, 4 deletions(-)
> > >
> > > diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
> > > index 4dfc05651c98..b38374d5f192 100644
> > > --- a/include/uapi/linux/tipc_config.h
> > > +++ b/include/uapi/linux/tipc_config.h
> > > @@ -43,10 +43,6 @@
> > >  #include <linux/tipc.h>
> > >  #include <asm/byteorder.h>
> > >
> > > -#ifndef __KERNEL__
> > > -#include <arpa/inet.h> /* for ntohs etc. */
> > > -#endif
> > > -
> > >  /*
> > >   * Configuration
> > >   *
> > >
> > > base-commit: 5efabdadcf4a5b9a37847ecc85ba71cf2eff0fcf
> > > prerequisite-patch-id: 0c2abf2af8051f4b37a70ef11b7d2fc2a3ec7181
> > > --
> > > 2.35.1.1021.g381101b075-goog
> > >
> >
> >
> > --
> > Thanks,
> > ~Nick Desaulniers
>
>
>
> --
> Thanks,
> ~Nick Desaulniers
Nick Desaulniers March 29, 2022, 11:03 p.m. UTC | #4
On Tue, Mar 29, 2022 at 3:09 PM Nick Desaulniers
<ndesaulniers@google.com> wrote:
>
> On Tue, Mar 29, 2022 at 2:50 PM Nick Desaulniers
> <ndesaulniers@google.com> wrote:
> >
> > On Tue, Mar 29, 2022 at 2:29 PM Nick Desaulniers
> > <ndesaulniers@google.com> wrote:
> > >
> > > Testing out CONFIG_UAPI_HEADER_TEST=y with a prebuilt Bionic sysroot
> > > from Android's SDK, I encountered an error:
> > >
> > >   HDRTEST usr/include/linux/fsi.h
> > > In file included from <built-in>:1:
> > > In file included from ./usr/include/linux/tipc_config.h:46:
> > > prebuilts/ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arpa/inet.h:39:1:
> > > error: unknown type name 'in_addr_t'
> > > in_addr_t inet_addr(const char* __s);
> > > ^
> >
> > Oh, this might not quite be the correct fix for this particular issue.
> >
> > Cherry picking this diff back into my Android kernel tree, I now observe:
> >   HDRTEST usr/include/linux/tipc_config.h
> > In file included from <built-in>:1:
> > ./usr/include/linux/tipc_config.h:268:4: error: implicit declaration
> > of function 'ntohs' [-Werror,-Wimplicit-function-declaration]
> >                 (ntohs(((struct tlv_desc *)tlv)->tlv_len) <= space);
> >                  ^
> >
> > Is there more than one byteorder.h? Oh, I see what I did; ntohs is defined in
> > linux/byteorder/generic.h
> > not
> > usr/include/asm/byteorder.h
> > Sorry, I saw `byteorder` and mixed up the path with the filename.
> >
> > So rather than just remove the latter, I should additionally be adding
> > the former. Though that then produces
> >
> > common/include/linux/byteorder/generic.h:146:9: error: implicit
> > declaration of function '__cpu_to_le16'
> > [-Werror,-Wimplicit-function-declaration]
> >         *var = cpu_to_le16(le16_to_cpu(*var) + val);
> >                ^
> >
> > Oh?
>
> Should there be a definition of ntohs (and friends) under
> include/uapi/linux/ somewhere, rather than have the kernel header
> include/uapi/linux/tipc_config.h depend on the libc header
> arpa/inet.h?
>
> It looks like we already have
> include/uapi/linux/byteorder/{big|little}_endian.h to define
> __be16_to_cpu and friends, just no definition of ntohs under
> include/uapi/linux/.
>
> Also, it looks like include/uapi/linux/ has definitions for
> __constant_ntohs in
> include/uapi/linux/byteorder/{big|little}_endian.h.  Should those 2
> headers also define ntohs (and friends) unprefixed, i.e. the versions
> that don't depend on constant expressions?
>
> (I wish there was an entry in MAINTAINERS for UAPI questions like this...)

I also think that Documentation/kbuild/headers_install.rst should
probably include text from
https://lore.kernel.org/all/Pine.LNX.4.58.0411281710490.22796@ppc970.osdl.org/
or something along the lines of "kernel headers MUST NOT depend on
userspace headers; any instance of that in tree is a bug that should
be fixed."

For people I just cc'ed, here's the lore link to this thread:
https://lore.kernel.org/lkml/20220329212946.2861648-1-ndesaulniers@google.com/

>
> >
> > >
> > > This is because Bionic has a bug in its inclusion chain. I sent a patch
> > > to fix that, but looking closer at include/uapi/linux/tipc_config.h,
> > > there's a comment that it includes arpa/inet.h for ntohs; but ntohs is
> > > already defined in include/linux/byteorder/generic.h which is already
> > > included in include/uapi/linux/tipc_config.h. There are no __KERNEL__
> > > guards on include/linux/byteorder/generic.h's definition of ntohs. So
> > > besides fixing Bionic, it looks like we can additionally remove this
> > > unnecessary header inclusion.
> > >
> > > Link: https://android-review.googlesource.com/c/platform/bionic/+/2048127
> > > Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
> > > ---
> > >  include/uapi/linux/tipc_config.h | 4 ----
> > >  1 file changed, 4 deletions(-)
> > >
> > > diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
> > > index 4dfc05651c98..b38374d5f192 100644
> > > --- a/include/uapi/linux/tipc_config.h
> > > +++ b/include/uapi/linux/tipc_config.h
> > > @@ -43,10 +43,6 @@
> > >  #include <linux/tipc.h>
> > >  #include <asm/byteorder.h>
> > >
> > > -#ifndef __KERNEL__
> > > -#include <arpa/inet.h> /* for ntohs etc. */
> > > -#endif
> > > -
> > >  /*
> > >   * Configuration
> > >   *
> > >
> > > base-commit: 5efabdadcf4a5b9a37847ecc85ba71cf2eff0fcf
> > > prerequisite-patch-id: 0c2abf2af8051f4b37a70ef11b7d2fc2a3ec7181
> > > --
> > > 2.35.1.1021.g381101b075-goog
> > >
> >
> >
> > --
> > Thanks,
> > ~Nick Desaulniers
>
>
>
> --
> Thanks,
> ~Nick Desaulniers
Arnd Bergmann March 30, 2022, 6:08 a.m. UTC | #5
On Wed, Mar 30, 2022 at 12:26 AM Nick Desaulniers
<ndesaulniers@google.com> wrote:
> On Tue, Mar 29, 2022 at 3:09 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
> ```
> diff --git a/include/uapi/linux/byteorder/little_endian.h
> b/include/uapi/linux/byteorder/little_endian.h
> index cd98982e7523..c14f2c3728e2 100644
> --- a/include/uapi/linux/byteorder/little_endian.h
> +++ b/include/uapi/linux/byteorder/little_endian.h
> @@ -103,5 +103,8 @@ static __always_inline __u16 __be16_to_cpup(const __be16 *p)
>  #define __cpu_to_be16s(x) __swab16s((x))
>  #define __be16_to_cpus(x) __swab16s((x))
>
> +#define htonl(x) __cpu_to_be32(x)
> +#define htons(x) __cpu_to_be16(x)
> +#define ntohs(x) __be16_to_cpu(x)
>

This is unfortunately a namespace violation, you can't define things in uapi
headers that conflict with standard library interfaces.

         Arnd
Arnd Bergmann March 30, 2022, 6:11 a.m. UTC | #6
On Wed, Mar 30, 2022 at 8:08 AM Arnd Bergmann <arnd@arndb.de> wrote:
> On Wed, Mar 30, 2022 at 12:26 AM Nick Desaulniers
> <ndesaulniers@google.com> wrote:
> > On Tue, Mar 29, 2022 at 3:09 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
> > ```
> > diff --git a/include/uapi/linux/byteorder/little_endian.h
> > b/include/uapi/linux/byteorder/little_endian.h
> > index cd98982e7523..c14f2c3728e2 100644
> > --- a/include/uapi/linux/byteorder/little_endian.h
> > +++ b/include/uapi/linux/byteorder/little_endian.h
> > @@ -103,5 +103,8 @@ static __always_inline __u16 __be16_to_cpup(const __be16 *p)
> >  #define __cpu_to_be16s(x) __swab16s((x))
> >  #define __be16_to_cpus(x) __swab16s((x))
> >
> > +#define htonl(x) __cpu_to_be32(x)
> > +#define htons(x) __cpu_to_be16(x)
> > +#define ntohs(x) __be16_to_cpu(x)
> >
>
> This is unfortunately a namespace violation, you can't define things in uapi
> headers that conflict with standard library interfaces.

nevermind, I just saw the thread continues and you arrived at a good solution
withusing the __cpu_to_be16() etc directly.

       Arnd
diff mbox series

Patch

diff --git a/include/uapi/linux/tipc_config.h b/include/uapi/linux/tipc_config.h
index 4dfc05651c98..b38374d5f192 100644
--- a/include/uapi/linux/tipc_config.h
+++ b/include/uapi/linux/tipc_config.h
@@ -43,10 +43,6 @@ 
 #include <linux/tipc.h>
 #include <asm/byteorder.h>
 
-#ifndef __KERNEL__
-#include <arpa/inet.h> /* for ntohs etc. */
-#endif
-
 /*
  * Configuration
  *