Skip to contentSkip to conversation

ieee1275: alloc-mem and free-mem

PATCH In review Stanislav Kholmanskikh 3 participants 16 messages Apr 12, 2016 – May 11, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ieee1275: alloc-mem and free-mem

Add wrappers for memory allocation using alloc-mem and free-mem commands from the User Interface.

---
 grub-core/kern/ieee1275/openfw.c |   68 ++++++++++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h |    2 +
 2 files changed, 70 insertions(+), 0 deletions(-)
grub-core/kern/ieee1275/openfw.c +68 -0
index ddb7783..35225ec 100644
@@ -561,3 +561,71 @@ grub_ieee1275_canonicalise_devname (const char *path)
   return NULL;
 }
 
+/* Allocate memory with alloc-mem */
+void *
+grub_ieee1275_alloc_mem (grub_size_t len)
+{
+  struct alloc_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t catch;
+    grub_ieee1275_cell_t result;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
+      return NULL;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "alloc-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1
+      || args.catch)
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("alloc-mem failed"));
+      return NULL;
+    }
+  else
+    return (void *)args.result;
+}
+
+/* Free memory allocated by alloc-mem */
+grub_err_t
+grub_ieee1275_free_mem (void *addr, grub_size_t len)
+{
+  struct free_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t addr;
+    grub_ieee1275_cell_t catch;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
+      return grub_errno;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 3, 1);
+  args.addr = (grub_ieee1275_cell_t)addr;
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "free-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN(&args) == -1
+      || args.catch)
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("free-mem failed"));
+      return grub_errno;
+    }
+
+  return GRUB_ERR_NONE;
+}
include/grub/ieee1275/ieee1275.h +2 -0
index 8e42513..91510b3 100644
@@ -234,6 +234,8 @@ int EXPORT_FUNC(grub_ieee1275_devalias_next) (struct grub_ieee1275_devalias *ali
 void EXPORT_FUNC(grub_ieee1275_children_peer) (struct grub_ieee1275_devalias *alias);
 void EXPORT_FUNC(grub_ieee1275_children_first) (const char *devpath,
 						struct grub_ieee1275_devalias *alias);
+void *EXPORT_FUNC(grub_ieee1275_alloc_mem) (grub_size_t len);
+grub_err_t EXPORT_FUNC(grub_ieee1275_free_mem) (void * addr, grub_size_t len);
 
 #define FOR_IEEE1275_DEVALIASES(alias) for (grub_ieee1275_devalias_init_iterator (&(alias)); grub_ieee1275_devalias_next (&(alias));)
 
Signature · 1 line
1.7.1
#1 lore raw

PATCH 2/2 ofnet: implement a receive buffer

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +58 -42
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
+
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
   if (!nb)
     return NULL;
+
   /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
      by 4. So that IP header is aligned on 4 bytes. */
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }
 
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
     {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
     }
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;
 }
 
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }
+
   card->driver = NULL;
   card->data = ofdata;
   card->flags = 0;
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->driver = &ofdriver;
   grub_net_card_register (card);
   return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;
 }
 
 static void
Signature · 1 line
1.7.1
#2 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Hi.

Could anybody have a look at this series, please?

Thanks.

Quoted 161 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

   nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

---
  grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
  1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +58 -42
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
    grub_uint64_t start_time;
    struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
+
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
    if (!nb)
      return NULL;
+
    /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
       by 4. So that IP header is aligned on 4 bytes. */
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }
 
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
      {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
      }
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;
  }
 
  static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
    }
  }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
  static int
  search_net_devices (struct grub_ieee1275_devalias *alias)
  {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
    card->default_address = lla;
 
    card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
    if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
      {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
      }
+
    card->driver = NULL;
    card->data = ofdata;
    card->flags = 0;
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
    card->driver = &ofdriver;
    grub_net_card_register (card);
    return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;
  }
 
  static void
#3 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Hi.

Quoted 3 lines · Stanislav Kholmanskikh

Hi.

Could anybody have a look at this series, please?

Ping.

Quoted 182 lines

Thanks.

Quoted 171 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

   nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c,
and makes get_card_packet() allocate a netbuff of the received data size.

---
  grub-core/net/drivers/ieee1275/ofnet.c |  100
++++++++++++++++++-------------
  1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +10 -1
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
    grub_uint64_t start_time;
    struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf,
 dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time
 < 200));
+
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
    if (!nb)
      return NULL;
+
    /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is

divisible
by 4. So that IP header is aligned on 4 bytes. */

grub-core/net/drivers/ieee1275/ofnet.c +5 -4
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }
 
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64,

&actual);
- while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c +25 -5
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
      {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
      }
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;
  }
 
  static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char
 *devpath, char **device, char **path,
    }
  }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
  static int
  search_net_devices (struct grub_ieee1275_devalias *alias)
  {
grub-core/net/drivers/ieee1275/ofnet.c +11 -31
@@ -409,41 +438,21 @@ search_net_devices (struct
 grub_ieee1275_devalias *alias)
    card->default_address = lla;
 
    card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-    struct grub_ieee1275_common_hdr common;
-    grub_ieee1275_cell_t method;
-    grub_ieee1275_cell_t len;
-    grub_ieee1275_cell_t catch;
-    grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-      || args.catch)
-    {
-      card->txbuf = 0;
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-    }
-      else
-    card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
    if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
      {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
      }
+
    card->driver = NULL;
    card->data = ofdata;
    card->flags = 0;
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias
 *alias)
    card->driver = &ofdriver;
    grub_net_card_register (card);
    return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;
  }
 
Quoted 2 lines

static void

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Hi.

I still believe this series may be useful for the community. So here is one more ping.

Thanks.

Quoted 198 lines · Stanislav Kholmanskikh

Hi.

Quoted 3 lines · Stanislav Kholmanskikh

Hi.

Could anybody have a look at this series, please?

Ping.

Quoted 177 lines

Thanks.

Quoted 173 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

   nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c,
and makes get_card_packet() allocate a netbuff of the received data size.

Signed-off-by: Stanislav Kholmanskikh
<[email protected]>

---
  grub-core/net/drivers/ieee1275/ofnet.c |  100
++++++++++++++++++-------------
  1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +10 -1
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
    grub_uint64_t start_time;
    struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf,
 dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time
 < 200));
+
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
    if (!nb)
      return NULL;
+
    /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is

divisible
by 4. So that IP header is aligned on 4 bytes. */

grub-core/net/drivers/ieee1275/ofnet.c +5 -4
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }
 
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64,

&actual);
- while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c +25 -5
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
      {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
      }
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;
  }
 
  static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char
 *devpath, char **device, char **path,
    }
  }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
  static int
  search_net_devices (struct grub_ieee1275_devalias *alias)
  {
grub-core/net/drivers/ieee1275/ofnet.c +11 -31
@@ -409,41 +438,21 @@ search_net_devices (struct
 grub_ieee1275_devalias *alias)
    card->default_address = lla;
 
    card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-    struct grub_ieee1275_common_hdr common;
-    grub_ieee1275_cell_t method;
-    grub_ieee1275_cell_t len;
-    grub_ieee1275_cell_t catch;
-    grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-      || args.catch)
-    {
-      card->txbuf = 0;
-      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-    }
-      else
-    card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
    if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
      {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
      }
+
    card->driver = NULL;
    card->data = ofdata;
    card->flags = 0;
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias
 *alias)
    card->driver = &ofdriver;
    grub_net_card_register (card);
    return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;
  }
 
Quoted 7 lines
Quoted 2 lines

static void

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

In reply to Stanislav Kholmanskikh
Quoted 11 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

Quoted 10 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 8 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -0
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
   if (!nb)

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 106

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +6 -1
     return NULL;
+
   /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
      by 4. So that IP header is aligned on 4 bytes. */
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }

This smells like separate fix not belonging to this patch.

Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -4
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 114

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -3
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
     {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
     }

Why not...

if (!grub_netbuff_put (nb, actual))

    return nb;

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 116

Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;

...then you do not need these changes too...

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 115

Quoted 1 line
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 }

It looks that everything below belongs to patch #1...

Quoted 80 lines
grub-core/net/drivers/ieee1275/ofnet.c +29 -31
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }
+
   card->driver = NULL;
   card->data = ofdata;
   card->flags = 0;

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 473

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->driver = &ofdriver;
   grub_net_card_register (card);
   return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;

...and without full explanation why in #1 commit message it is not obvious for what this change is really needed...

Daniel

#6 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

Quoted 5 lines
Quoted 2 lines

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Quoted 14 lines
Quoted 14 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

Quoted 2 lines

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

so, yes, it's capable to handle any valid packet size.

Quoted 10 lines
Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -0
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
   if (!nb)
     return NULL;
+
   /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
      by 4. So that IP header is aligned on 4 bytes. */
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 106

Quoted 6 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -1
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }

This smells like separate fix not belonging to this patch.

Ok. I can move this change into a separate patch.

Quoted 16 lines
Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -4
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 114

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -3
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
     {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
     }
Quoted 1 line

Why not...

Ok.

Quoted 9 lines

if (!grub_netbuff_put (nb, actual))

    return nb;

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 116

Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;
Quoted 5 lines

...then you do not need these changes too...

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 115

Quoted 1 line
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 }

It looks that everything below belongs to patch #1...

No. Patch 1 is about two supplementary funcions for "alloc-mem", "free-mem". The changes below setup the transmit/receive buffers for a network card. The changes above use this receive buffer. So, in my opinion, this all is logically coupled and should be in one patch.

Quoted 102 lines
Quoted 81 lines
grub-core/net/drivers/ieee1275/ofnet.c +29 -31
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }
+
   card->driver = NULL;
   card->data = ofdata;
   card->flags = 0;

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 473

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
@@ -455,6 +464,13 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->driver = &ofdriver;
   grub_net_card_register (card);
   return 0;
+
+ fail:
+  grub_free (ofdata->path);
+  grub_free (ofdata);
+  grub_free (card);
+  grub_print_error ();
+  return 1;

...and without full explanation why in #1 commit message it is not obvious for what this change is really needed...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Quoted 11 lines · Stanislav Kholmanskikh
Quoted 10 lines · Daniel Kiper
Quoted 9 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Please do.

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

Quoted 2 lines

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

OK but if you move such code around anyway do not leave it unreadable. Improve it by constants or comments.

Quoted 7 lines
Quoted 1 line

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

so, yes, it's capable to handle any valid packet size.

Great but why this numbers?

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -0
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
   if (!nb)
     return NULL;
+
   /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
      by 4. So that IP header is aligned on 4 bytes. */
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 106

Quoted 6 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -1
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }

This smells like separate fix not belonging to this patch.

Quoted 1 line

Ok. I can move this change into a separate patch.

Thanks a lot!

Quoted 30 lines
Quoted 15 lines
Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -4
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 114

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -3
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
     {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
     }
Quoted 1 line

Why not...

Ok.

Quoted 11 lines

if (!grub_netbuff_put (nb, actual))

    return nb;

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 116

Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;

...then you do not need these changes too...

Quoted 8 lines
Quoted 3 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 115

Quoted 1 line
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 }

It looks that everything below belongs to patch #1...

No. Patch 1 is about two supplementary funcions for "alloc-mem", "free-mem". The changes below setup the transmit/receive buffers for a network card. The changes above use this receive buffer. So, in my opinion, this all is logically coupled and should be in one patch.

I have checked code below once again. First of all I think that grub_ieee1275_alloc_mem() and grub_ieee1275_free_mem() have to live in this file not in grub-core/kern/ieee1275/openfw.c. I see no other callers for both of them. Additionally, patch #1 should introduce grub_ieee1275_alloc_mem(), ofnet_alloc_netbuf() and search_net_devices() should call ofnet_alloc_netbuf(). No more no less. Do not forget to mention in commit message why patch #1 is needed. Then patch #2 should introduce rest of the code below.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
Quoted 7 lines
Quoted 7 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, · line 314

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);

It looks that it should be grub_zalloc() instead of grub_malloc() here.

Quoted 55 lines
Quoted 55 lines
Quoted 55 lines
grub-core/net/drivers/ieee1275/ofnet.c +17 -28
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
Quoted 8 lines
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 454

Quoted 8 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -3
-      grub_free (card);
-      grub_print_error ();
-      return 0;
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }

Should not we free card->rcvbuf and/or card->txbuf if module is unloaded or something like that?

Daniel

#8 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Stanislav Kholmanskikh
Quoted 13 lines · Daniel Kiper
Quoted 11 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Please do.

Ok. I'll check what I can do here.

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 11 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 8 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

Quoted 5 lines
Quoted 2 lines

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

OK but if you move such code around anyway do not leave it unreadable. Improve it by constants or comments.

May I use a macro for this

#define READ_TIMEOUT 200

?

Quoted 10 lines
Quoted 8 lines
Quoted 2 lines

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

so, yes, it's capable to handle any valid packet size.

Great but why this numbers?

I have to admit that I can't answer to your question. :( I copied this stuff from efi (for the receive buffer). The transmit buffer was already of this size.

Quoted 62 lines
Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -0
+  if (actual <= 0)
+    return NULL;
+
+  nb = grub_netbuff_alloc (actual + 2);
   if (!nb)
     return NULL;
+
   /* Reserve 2 bytes so that 2 + 14/18 bytes of ethernet header is divisible
      by 4. So that IP header is aligned on 4 bytes. */
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 105

Quoted 6 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -1
-  grub_netbuff_reserve (nb, 2);
+  if (grub_netbuff_reserve (nb, 2))
+    {
+      grub_netbuff_free (nb);
+      return NULL;
+    }

This smells like separate fix not belonging to this patch.

Quoted 1 line

Ok. I can move this change into a separate patch.

Thanks a lot!

Quoted 30 lines
Quoted 15 lines
Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -4
-  start_time = grub_get_time_ms ();
-  do
-    rc = grub_ieee1275_read (data->handle, nb->data, dev->mtu + 64, &actual);
-  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 112

Quoted 10 lines
grub-core/net/drivers/ieee1275/ofnet.c +5 -3
-  if (actual > 0)
+  grub_memcpy (nb->data, dev->rcvbuf, actual);
+
+  if (grub_netbuff_put (nb, actual))
     {
-      grub_netbuff_put (nb, actual);
-      return nb;
+      grub_netbuff_free (nb);
+      return NULL;
     }
Quoted 1 line

Why not...

Ok.

Quoted 11 lines

if (!grub_netbuff_put (nb, actual))

    return nb;

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 114

Quoted 4 lines
grub-core/net/drivers/ieee1275/ofnet.c +2 -2
-  grub_netbuff_free (nb);
-  return NULL;
+
+  return nb;

...then you do not need these changes too...

Quoted 8 lines
Quoted 3 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 115

Quoted 1 line
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 }

It looks that everything below belongs to patch #1...

No. Patch 1 is about two supplementary funcions for "alloc-mem", "free-mem". The changes below setup the transmit/receive buffers for a network card. The changes above use this receive buffer. So, in my opinion, this all is logically coupled and should be in one patch.

Quoted 8 lines

I have checked code below once again. First of all I think that grub_ieee1275_alloc_mem() and grub_ieee1275_free_mem() have to live in this file not in grub-core/kern/ieee1275/openfw.c. I see no other callers for both of them. Additionally, patch #1 should introduce grub_ieee1275_alloc_mem(), ofnet_alloc_netbuf() and search_net_devices() should call ofnet_alloc_netbuf(). No more no less. Do not forget to mention in commit message why patch #1 is needed. Then patch #2 should introduce rest of the code below.

Ok.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, · line 314

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);

It looks that it should be grub_zalloc() instead of grub_malloc() here.

I have two reasons why I don't use grub_zalloc() here:

1. The buffer allocated with this function is written/read many times while grub is working. We write some amount of bytes to the buffer, and then read this amount of bytes. So I don't see why zeroing the buffer on allocation should matter.

2. In IEEE1275-1994 I do not see an explicit notice that memory allocated with alloc-mem is zeroed. So for consistence of ofnet_alloc_netbuf() I do not call grub_zalloc() there.

Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
grub-core/net/drivers/ieee1275/ofnet.c +17 -31
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 454

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -0
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }

Should not we free card->rcvbuf and/or card->txbuf if module is unloaded or something like that?

Yes, I think so. Thanks for pointing at this.

It's interesting that none of uboot, efi, ieee1275 drivers seems to care of freeing the card data structure on module unload. All they do is similar to:

FOR_NET_CARDS_SAFE (card, next)
if (the card is handled by us)

    grub_net_card_unregister (card);

whereas from grub-core/net/net.c I don't see that
grub_net_card_unregister() frees memory.

It seems that the job of freeing card's memory is expected to be handled in drivers and none of the drivers care about it, excluding pxe, where 'grub_pxe_card' is statically allocated. Or am I missing something?

As for ieee1275 I'm thinking about something like:

(quoted diff) +0 -0

b/grub-core/net/drivers/ieee1275/ofnet.c
index 6bd3b92..12a4289 100644

grub-core/net/drivers/ieee1275/ofnet.c +20 -1
@@ -473,9 +473,28 @@ GRUB_MOD_INIT(ofnet)
 GRUB_MOD_FINI(ofnet)
 {
   struct grub_net_card *card, *next;
+  struct grub_ofnetcard_data *ofdata;
 
   FOR_NET_CARDS_SAFE (card, next)
     if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
-      grub_net_card_unregister (card);
+      {
+       grub_net_card_unregister (card);
+
+       /*
+        * The fact that we are here means the card was successfully
+        * initialized in the past, so all the below pointers are valid,
+        * and we may free associated memory without checks.
+        */
+
+       ofdata = (struct grub_ofnetcard_data *) card->data;
+       grub_free (ofdata->path);
+       grub_free (ofdata->suffix);
+       grub_free (ofdata);
+
+       ofnet_free_netbuf (card->txbuf, card->txbufsize);
+       ofnet_free_netbuf (card->rcvbuf, card->rcvbufsize);
+
+       grub_free (card);
+      }
   grub_ieee1275_net_config = 0;
 }

(not tested)

I think it deserves a separate patch. In one patch we are adding the receive buffer, and in another we are making the ieee1275 driver to free all card resources on unload.

Quoted 8 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#9 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Quoted 16 lines · Stanislav Kholmanskikh
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Stanislav Kholmanskikh
Quoted 13 lines · Daniel Kiper
Quoted 11 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Please do.

Ok. I'll check what I can do here.

Great! Thnaks!

Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
Quoted 8 lines

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

Quoted 8 lines
Quoted 2 lines

OK but if you move such code around anyway do not leave it unreadable. Improve it by constants or comments.

May I use a macro for this

#define READ_TIMEOUT 200

?

It seems to me that it appears in one place, so, comment would be better here. Unfortunately, it looks that there is no explanation for that value in commit message... Ehhh... Could you check mail archive? If there is no such thing there then let's leave it as it. Though I do not like it.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 1 line

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

so, yes, it's capable to handle any valid packet size.

Great but why this numbers?

I have to admit that I can't answer to your question. :( I copied this stuff from efi (for the receive buffer). The transmit buffer was already of this size.

Ugh... Same as above...

[...]

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, · line 314

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);

It looks that it should be grub_zalloc() instead of grub_malloc() here.

Quoted 5 lines

I have two reasons why I don't use grub_zalloc() here:

1. The buffer allocated with this function is written/read many times while grub is working. We write some amount of bytes to the buffer, and then read this amount of bytes. So I don't see why zeroing the buffer on

I suppose that "this" == "same" here...

Quoted 5 lines

allocation should matter.

2. In IEEE1275-1994 I do not see an explicit notice that memory allocated with alloc-mem is zeroed. So for consistence of ofnet_alloc_netbuf() I do not call grub_zalloc() there.

Yep, I am aware of that. However, I am asking because you change currently exiting code behavior which uses grub_zalloc(). Maybe we should leave it as is (I am thinking about grub_zalloc()) but it is not very strong must. If you choose grub_malloc() please explain in commit message why you did it and why it is safe here.

Quoted 122 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
Quoted 58 lines
grub-core/net/drivers/ieee1275/ofnet.c +17 -31
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 454

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -0
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }

Should not we free card->rcvbuf and/or card->txbuf if module is unloaded or something like that?

Yes, I think so. Thanks for pointing at this.

It's interesting that none of uboot, efi, ieee1275 drivers seems to care of freeing the card data structure on module unload. All they do is similar to:

FOR_NET_CARDS_SAFE (card, next)
if (the card is handled by us)

    grub_net_card_unregister (card);

whereas from grub-core/net/net.c I don't see that
grub_net_card_unregister() frees memory.

It seems that the job of freeing card's memory is expected to be handled in drivers and none of the drivers care about it, excluding pxe, where 'grub_pxe_card' is statically allocated. Or am I missing something?

As for ieee1275 I'm thinking about something like:

grub-core/net/drivers/ieee1275/ofnet.c +20 -1
index 6bd3b92..12a4289 100644
@@ -473,9 +473,28 @@ GRUB_MOD_INIT(ofnet)
 GRUB_MOD_FINI(ofnet)
 {
   struct grub_net_card *card, *next;
+  struct grub_ofnetcard_data *ofdata;
 
   FOR_NET_CARDS_SAFE (card, next)
     if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
-      grub_net_card_unregister (card);
+      {
+       grub_net_card_unregister (card);
+
+       /*
+        * The fact that we are here means the card was successfully
+        * initialized in the past, so all the below pointers are valid,
+        * and we may free associated memory without checks.
+        */
+
+       ofdata = (struct grub_ofnetcard_data *) card->data;
+       grub_free (ofdata->path);
+       grub_free (ofdata->suffix);
+       grub_free (ofdata);
+
+       ofnet_free_netbuf (card->txbuf, card->txbufsize);
+       ofnet_free_netbuf (card->rcvbuf, card->rcvbufsize);
+
+       grub_free (card);
+      }
   grub_ieee1275_net_config = 0;
 }
Quoted 5 lines

(not tested)

I think it deserves a separate patch. In one patch we are adding the receive buffer, and in another we are making the ieee1275 driver to free all card resources on unload.

Make sense for me. Could you do the same thing for other modules (at least for those mentioned by you) too? Of course in separate patches.

Daniel

#10 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

Quoted 58 lines · Daniel Kiper
Quoted 16 lines · Stanislav Kholmanskikh
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Stanislav Kholmanskikh
Quoted 13 lines · Daniel Kiper
Quoted 11 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Please do.

Ok. I'll check what I can do here.

Great! Thnaks!

Quoted 30 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 10 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 8 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

Quoted 5 lines
Quoted 2 lines

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

OK but if you move such code around anyway do not leave it unreadable. Improve it by constants or comments.

May I use a macro for this

Quoted 8 lines
Quoted 3 lines

#define READ_TIMEOUT 200

?

It seems to me that it appears in one place, so, comment would be better here. Unfortunately, it looks that there is no explanation for that value in commit message... Ehhh... Could you check mail archive? If there is no such thing there then let's leave it as it. Though I do not like it.

Ok. I'll check the mail archive as well.

Quoted 48 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

so, yes, it's capable to handle any valid packet size.

Great but why this numbers?

I have to admit that I can't answer to your question. :( I copied this stuff from efi (for the receive buffer). The transmit buffer was already of this size.

Ugh... Same as above...

[...]

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
 static struct grub_net_card_driver ofdriver =
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, · line 314

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);

It looks that it should be grub_zalloc() instead of grub_malloc() here.

Quoted 5 lines

I have two reasons why I don't use grub_zalloc() here:

1. The buffer allocated with this function is written/read many times while grub is working. We write some amount of bytes to the buffer, and then read this amount of bytes. So I don't see why zeroing the buffer on

I suppose that "this" == "same" here...

Quoted 5 lines

allocation should matter.

2. In IEEE1275-1994 I do not see an explicit notice that memory allocated with alloc-mem is zeroed. So for consistence of ofnet_alloc_netbuf() I do not call grub_zalloc() there.

Quoted 5 lines

Yep, I am aware of that. However, I am asking because you change currently exiting code behavior which uses grub_zalloc(). Maybe we should leave it as is (I am thinking about grub_zalloc()) but it is not very strong must. If you choose grub_malloc() please explain in commit message why you did it and why it is safe here.

Well, let it be grub_zalloc() then.

Quoted 123 lines
Quoted 123 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
grub-core/net/drivers/ieee1275/ofnet.c +17 -31
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 454

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -0
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }

Should not we free card->rcvbuf and/or card->txbuf if module is unloaded or something like that?

Yes, I think so. Thanks for pointing at this.

It's interesting that none of uboot, efi, ieee1275 drivers seems to care of freeing the card data structure on module unload. All they do is similar to:

FOR_NET_CARDS_SAFE (card, next)
if (the card is handled by us)

    grub_net_card_unregister (card);

whereas from grub-core/net/net.c I don't see that
grub_net_card_unregister() frees memory.

It seems that the job of freeing card's memory is expected to be handled in drivers and none of the drivers care about it, excluding pxe, where 'grub_pxe_card' is statically allocated. Or am I missing something?

As for ieee1275 I'm thinking about something like:

grub-core/net/drivers/ieee1275/ofnet.c +20 -1
index 6bd3b92..12a4289 100644
@@ -473,9 +473,28 @@ GRUB_MOD_INIT(ofnet)
 GRUB_MOD_FINI(ofnet)
 {
   struct grub_net_card *card, *next;
+  struct grub_ofnetcard_data *ofdata;
 
   FOR_NET_CARDS_SAFE (card, next)
     if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
-      grub_net_card_unregister (card);
+      {
+       grub_net_card_unregister (card);
+
+       /*
+        * The fact that we are here means the card was successfully
+        * initialized in the past, so all the below pointers are valid,
+        * and we may free associated memory without checks.
+        */
+
+       ofdata = (struct grub_ofnetcard_data *) card->data;
+       grub_free (ofdata->path);
+       grub_free (ofdata->suffix);
+       grub_free (ofdata);
+
+       ofnet_free_netbuf (card->txbuf, card->txbufsize);
+       ofnet_free_netbuf (card->rcvbuf, card->rcvbufsize);
+
+       grub_free (card);
+      }
   grub_ieee1275_net_config = 0;
 }
Quoted 8 lines
Quoted 5 lines

(not tested)

I think it deserves a separate patch. In one patch we are adding the receive buffer, and in another we are making the ieee1275 driver to free all card resources on unload.

Make sense for me. Could you do the same thing for other modules (at least for those mentioned by you) too? Of course in separate patches.

I'll do this for ieee1275. As for efi/uboot, I think I can also do this, but later, since testing this may take some time. I'd prefer to play with efi/uboot after finishing with this series :)

Regarding this series. It seems we have all questions answered and the ball is on my side. I'll try to come up with V2 someday next week.

Thank you for your time reviewing this!

Quoted 8 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#11 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

On Wed, Nov 23, 2016 at 06:09:52PM +0300, Stanislav Kholmanskikh wrote:

[...]

Quoted 3 lines

I'll do this for ieee1275. As for efi/uboot, I think I can also do this, but later, since testing this may take some time. I'd prefer to play with efi/uboot after finishing with this series :)

No problem.

Quoted 2 lines

Regarding this series. It seems we have all questions answered and the ball is on my side. I'll try to come up with V2 someday next week.

Great!

Quoted 1 line

Thank you for your time reviewing this!

You are welcome!

Daniel

#12 lore raw

Re: [PATCH 2/2] ofnet: implement a receive buffer

In reply to Stanislav Kholmanskikh
Quoted 63 lines · Stanislav Kholmanskikh
Quoted 60 lines · Daniel Kiper
Quoted 16 lines · Stanislav Kholmanskikh
Quoted 15 lines · Daniel Kiper
Quoted 14 lines · Stanislav Kholmanskikh
Quoted 13 lines · Daniel Kiper
Quoted 11 lines · Stanislav Kholmanskikh

get_card_packet() from ofnet.c allocates a netbuff based on the device's MTU:

  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);

In the case when the MTU is large, and the received packet is relatively small, this leads to allocation of significantly more memory, than it's required. An example could be transmission of TFTP packets with 0x400 blksize via a network card with 0x10000 MTU.

This patch implements a per-card receive buffer in a way similar to efinet.c, and makes get_card_packet() allocate a netbuff of the received data size.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Have you checked performance impact of this patch? It should not be meaningful but it is good to know.

No. I didn't do performance testing.

Please do.

Ok. I'll check what I can do here.

Great! Thnaks!

Quoted 32 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 10 lines
---
 grub-core/net/drivers/ieee1275/ofnet.c |  100 ++++++++++++++++++-------------
 1 files changed, 58 insertions(+), 42 deletions(-)
grub-core/net/drivers/ieee1275/ofnet.c +0 -0
index 6bd3b92..09ec77e 100644

grub-core/net/drivers/ieee1275/ofnet.c get_card_packet (struct grub_net_card *dev) · line 91

Quoted 8 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -1
@@ -85,24 +85,35 @@ get_card_packet (struct grub_net_card *dev)
   grub_uint64_t start_time;
   struct grub_net_buff *nb;
 
-  nb = grub_netbuff_alloc (dev->mtu + 64 + 2);
+  start_time = grub_get_time_ms ();
+  do
+    rc = grub_ieee1275_read (data->handle, dev->rcvbuf, dev->rcvbufsize, &actual);
+  while ((actual <= 0 || rc < 0) && (grub_get_time_ms () - start_time < 200));

Why 200? Please avoid using plain numbers if possible. Use constants. If it does not make sense then put comment which explains why this figure not another.

Quoted 5 lines
Quoted 2 lines

The whole 'do while' construction is from the existing code, I only modify the destination for the grub_ieee1275_read() call.

OK but if you move such code around anyway do not leave it unreadable. Improve it by constants or comments.

May I use a macro for this

#define READ_TIMEOUT 200

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

?

It seems to me that it appears in one place, so, comment would be better here. Unfortunately, it looks that there is no explanation for that value in commit message... Ehhh... Could you check mail archive? If there is no such thing there then let's leave it as it. Though I do not like it.

Ok. I'll check the mail archive as well.

What I found is that this timeout of 200 ms was introduced by commit:

commit 0f231af8ae44b6e4efe6b25794db21fbfd270718

Author: Manoel Rebelo Abranches <[email protected]>
Date:   Tue May 10 09:50:18 2011 -0300

Implement timeout when receiving packets.

It seems this commit has roots here:

http://lists.gnu.org/archive/html/grub-devel/2011-05/msg00041.html

where my search stops.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

Additionally, are we sure that whole packet can be always stored in dev->rcvbuf?

Code in search_net_devices() allocates the buffer to be of size:

ALIGN_UP (card->mtu, 64) + 256;

so, yes, it's capable to handle any valid packet size.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Great but why this numbers?

I have to admit that I can't answer to your question. :( I copied this stuff from efi (for the receive buffer). The transmit buffer was already of this size.

Ugh... Same as above...

It seems that commit:

commit 3e7472395127dc231c0f7139600b0465f68d0095

Author: Vladimir 'phcoder' Serbinenko <[email protected]>
Date:   Sat Jun 9 11:00:18 2012 +0200

Keep TX and RX buffers on EFI rather than always allocate new ones.

* include/grub/net.h (grub_net_card_driver): Allow driver to modify card. All users updated.
(grub_net_card): New members txbuf, rcvbuf, rcvbufsize and txbusy.
* grub-core/net/drivers/efi/efinet.c (send_card_buffer): Reuse
buffer.
(get_card_packet): Likewise.
(grub_efinet_findcards): Init new fields.

was the first one to use ALIGN_UP (card->mtu, 64) + 256 for the receive buffer. Before that the buffer size was hard coded to 1536.

I could not find an email message describing this change...

Quoted 195 lines
Quoted 32 lines

[...]

Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

static struct grub_net_card_driver ofdriver =

grub-core/net/drivers/ieee1275/ofnet.c +0 -0
@@ -294,6 +305,24 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path,
   }
 }
 
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

grub-core/net/drivers/ieee1275/ofnet.c grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, · line 314

Quoted 7 lines
grub-core/net/drivers/ieee1275/ofnet.c +7 -0
+static void *
+ofnet_alloc_netbuf (grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    return grub_ieee1275_alloc_mem (len);
+  else
+    return grub_malloc (len);

It looks that it should be grub_zalloc() instead of grub_malloc() here.

Quoted 5 lines

I have two reasons why I don't use grub_zalloc() here:

1. The buffer allocated with this function is written/read many times while grub is working. We write some amount of bytes to the buffer, and then read this amount of bytes. So I don't see why zeroing the buffer on

I suppose that "this" == "same" here...

Quoted 5 lines

allocation should matter.

2. In IEEE1275-1994 I do not see an explicit notice that memory allocated with alloc-mem is zeroed. So for consistence of ofnet_alloc_netbuf() I do not call grub_zalloc() there.

Quoted 5 lines

Yep, I am aware of that. However, I am asking because you change currently exiting code behavior which uses grub_zalloc(). Maybe we should leave it as is (I am thinking about grub_zalloc()) but it is not very strong must. If you choose grub_malloc() please explain in commit message why you did it and why it is safe here.

Well, let it be grub_zalloc() then.

Quoted 123 lines
Quoted 123 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
Quoted 59 lines
grub-core/net/drivers/ieee1275/ofnet.c +17 -31
+}
+
+static void
+ofnet_free_netbuf (void *addr, grub_size_t len)
+{
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
+    grub_ieee1275_free_mem (addr, len);
+  else
+    grub_free (addr);
+}
+
 static int
 search_net_devices (struct grub_ieee1275_devalias *alias)
 {
@@ -409,41 +438,21 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
   card->default_address = lla;
 
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
+  card->rcvbufsize = ALIGN_UP (card->mtu, 64) + 256;
 
-  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
-    {
-      struct alloc_args
-      {
-	struct grub_ieee1275_common_hdr common;
-	grub_ieee1275_cell_t method;
-	grub_ieee1275_cell_t len;
-	grub_ieee1275_cell_t catch;
-	grub_ieee1275_cell_t result;
-      }
-      args;
-      INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
-      args.len = card->txbufsize;
-      args.method = (grub_ieee1275_cell_t) "alloc-mem";
-
-      if (IEEE1275_CALL_ENTRY_FN (&args) == -1
-	  || args.catch)
-	{
-	  card->txbuf = 0;
-	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
-	}
-      else
-	card->txbuf = (void *) args.result;
-    }
-  else
-    card->txbuf = grub_zalloc (card->txbufsize);
+  card->txbuf = ofnet_alloc_netbuf (card->txbufsize);
   if (!card->txbuf)
+    goto fail;
+
+  card->rcvbuf = ofnet_alloc_netbuf (card->rcvbufsize);
+  if (!card->rcvbuf)
     {
-      grub_free (ofdata->path);
-      grub_free (ofdata);
-      grub_free (card);
-      grub_print_error ();
-      return 0;
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

grub-core/net/drivers/ieee1275/ofnet.c search_net_devices (struct grub_ieee1275_devalias *alias) · line 454

Quoted 5 lines
grub-core/net/drivers/ieee1275/ofnet.c +4 -0
+      grub_error_push ();
+      ofnet_free_netbuf(card->txbuf, card->txbufsize);
+      grub_error_pop ();
+      goto fail;
     }

Should not we free card->rcvbuf and/or card->txbuf if module is unloaded or something like that?

Yes, I think so. Thanks for pointing at this.

It's interesting that none of uboot, efi, ieee1275 drivers seems to care of freeing the card data structure on module unload. All they do is similar to:

FOR_NET_CARDS_SAFE (card, next)
if (the card is handled by us)

    grub_net_card_unregister (card);

whereas from grub-core/net/net.c I don't see that
grub_net_card_unregister() frees memory.

It seems that the job of freeing card's memory is expected to be handled in drivers and none of the drivers care about it, excluding pxe, where 'grub_pxe_card' is statically allocated. Or am I missing something?

As for ieee1275 I'm thinking about something like:

grub-core/net/drivers/ieee1275/ofnet.c +20 -1
index 6bd3b92..12a4289 100644
@@ -473,9 +473,28 @@ GRUB_MOD_INIT(ofnet)
 GRUB_MOD_FINI(ofnet)
 {
   struct grub_net_card *card, *next;
+  struct grub_ofnetcard_data *ofdata;
 
   FOR_NET_CARDS_SAFE (card, next)
     if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
-      grub_net_card_unregister (card);
+      {
+       grub_net_card_unregister (card);
+
+       /*
+        * The fact that we are here means the card was successfully
+        * initialized in the past, so all the below pointers are valid,
+        * and we may free associated memory without checks.
+        */
+
+       ofdata = (struct grub_ofnetcard_data *) card->data;
+       grub_free (ofdata->path);
+       grub_free (ofdata->suffix);
+       grub_free (ofdata);
+
+       ofnet_free_netbuf (card->txbuf, card->txbufsize);
+       ofnet_free_netbuf (card->rcvbuf, card->rcvbufsize);
+
+       grub_free (card);
+      }
   grub_ieee1275_net_config = 0;
 }
Quoted 8 lines
Quoted 5 lines

(not tested)

I think it deserves a separate patch. In one patch we are adding the receive buffer, and in another we are making the ieee1275 driver to free all card resources on unload.

Make sense for me. Could you do the same thing for other modules (at least for those mentioned by you) too? Of course in separate patches.

I'll do this for ieee1275. As for efi/uboot, I think I can also do this, but later, since testing this may take some time. I'd prefer to play with efi/uboot after finishing with this series :)

Regarding this series. It seems we have all questions answered and the ball is on my side. I'll try to come up with V2 someday next week.

Thank you for your time reviewing this!

Quoted 8 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#13 lore raw
In reply to Stanislav Kholmanskikh
Quoted 2 lines · Stanislav Kholmanskikh

Add wrappers for memory allocation using alloc-mem and free-mem commands from the User Interface.

Please tell why it is needed. Additionally, please forgive me if it is stupid question, why are you using command line to allocate/free memory? There is a lack of better API in IEEE 1275?

Quoted 30 lines
---
 grub-core/kern/ieee1275/openfw.c |   68 ++++++++++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h |    2 +
 2 files changed, 70 insertions(+), 0 deletions(-)
grub-core/kern/ieee1275/openfw.c +16 -0
index ddb7783..35225ec 100644
@@ -561,3 +561,71 @@ grub_ieee1275_canonicalise_devname (const char *path)
   return NULL;
 }
 
+/* Allocate memory with alloc-mem */
+void *
+grub_ieee1275_alloc_mem (grub_size_t len)
+{
+  struct alloc_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t catch;
+    grub_ieee1275_cell_t result;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 589

Quoted 10 lines
grub-core/kern/ieee1275/openfw.c +10 -0
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
+      return NULL;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "alloc-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1
+      || args.catch)

I think that this can be in one line.

Quoted 25 lines
grub-core/kern/ieee1275/openfw.c +25 -0
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("alloc-mem failed"));
+      return NULL;
+    }
+  else
+    return (void *)args.result;
+}
+
+/* Free memory allocated by alloc-mem */
+grub_err_t
+grub_ieee1275_free_mem (void *addr, grub_size_t len)
+{
+  struct free_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t addr;
+    grub_ieee1275_cell_t catch;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 624

Quoted 10 lines
grub-core/kern/ieee1275/openfw.c +10 -0
+      return grub_errno;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 3, 1);
+  args.addr = (grub_ieee1275_cell_t)addr;
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "free-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN(&args) == -1
+      || args.catch)

Ditto.

Daniel

#14 lore raw

Hi, Daniel.

Thank you for review.

My comments are below.

Quoted 7 lines · Daniel Kiper
Quoted 2 lines · Stanislav Kholmanskikh

Add wrappers for memory allocation using alloc-mem and free-mem commands from the User Interface.

Please tell why it is needed. Additionally, please forgive me if it is stupid question, why are you using command line to allocate/free memory? There is a lack of better API in IEEE 1275?

In the current git code in grub-core/net/drivers/ieee1275/ofnet.c the search_net_devices() function uses the "alloc-mem" command for allocation of the transmit buffer for the case when GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN is set.

Yes, besides "alloc-mem", "free-mem", there are other options for allocating memory. But, to be honest, I don't know why grub_malloc() cannot be used for the receive buffer on those systems. From the flag's name it seems those systems don't have a MMU. Ok, but grub_malloc() is called from many places in grub, and, presumably, grub works on those systems. I don't have such a MacRISC system to try grub_malloc() for the buffers (grub-core/kern/ieee1275/cmain.c).

In patch 2 I'm implementing a receive buffer, so I decided to keep this case as is, but move "alloc-mem", "free-mem" into functions.

Does it answer your questions? If yes, I'll update the commit message for V2 with a more detailed explanation why these functions are needed.

Quoted 35 lines
Quoted 35 lines
---
 grub-core/kern/ieee1275/openfw.c |   68 ++++++++++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h |    2 +
 2 files changed, 70 insertions(+), 0 deletions(-)
grub-core/kern/ieee1275/openfw.c +20 -0
index ddb7783..35225ec 100644
@@ -561,3 +561,71 @@ grub_ieee1275_canonicalise_devname (const char *path)
   return NULL;
 }
 
+/* Allocate memory with alloc-mem */
+void *
+grub_ieee1275_alloc_mem (grub_size_t len)
+{
+  struct alloc_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t catch;
+    grub_ieee1275_cell_t result;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
+      return NULL;
+    }
+
Quoted 8 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 589

Quoted 6 lines
grub-core/kern/ieee1275/openfw.c +6 -0
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "alloc-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1
+      || args.catch)

I think that this can be in one line.

Ok.

Quoted 30 lines
Quoted 30 lines
grub-core/kern/ieee1275/openfw.c +29 -0
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("alloc-mem failed"));
+      return NULL;
+    }
+  else
+    return (void *)args.result;
+}
+
+/* Free memory allocated by alloc-mem */
+grub_err_t
+grub_ieee1275_free_mem (void *addr, grub_size_t len)
+{
+  struct free_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
+    grub_ieee1275_cell_t addr;
+    grub_ieee1275_cell_t catch;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
+      return grub_errno;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 3, 1);
Quoted 8 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 624

Quoted 6 lines
grub-core/kern/ieee1275/openfw.c +6 -0
+  args.addr = (grub_ieee1275_cell_t)addr;
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "free-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN(&args) == -1
+      || args.catch)

Ditto.

Ok.

Quoted 8 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#15 lore raw
Quoted 22 lines · On Fri, Nov 18, 2016, 13:33 Stanislav Kholmanskikh <…

Hi, Daniel.

Thank you for review.

My comments are below.

Quoted 5 lines · Daniel Kiper
Quoted 2 lines · Stanislav Kholmanskikh

Add wrappers for memory allocation using alloc-mem and free-mem commands from the User Interface.

Please tell why it is needed. Additionally, please forgive me if it is

stupid

Quoted 1 line

question, why are you using command line to allocate/free memory? There

is

Quoted 1 line

a lack of better API in IEEE 1275?

In the current git code in grub-core/net/drivers/ieee1275/ofnet.c the search_net_devices() function uses the "alloc-mem" command for allocation of the transmit buffer for the case when GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN is set.

Quoted 8 lines

Yes, besides "alloc-mem", "free-mem", there are other options for allocating memory. But, to be honest, I don't know why grub_malloc() cannot be used for the receive buffer on those systems. From the flag's name it seems those systems don't have a MMU. Ok, but grub_malloc() is called from many places in grub, and, presumably, grub works on those systems. I don't have such a MacRISC system to try grub_malloc() for the buffers (grub-core/kern/ieee1275/cmain.c).

On my test system if I try to use grub_malloc, then the network driver tries to do some dubious pointer arithmetic and puts data in a completely wrong location unless alloc-mem was used

Quoted 114 lines

In patch 2 I'm implementing a receive buffer, so I decided to keep this case as is, but move "alloc-mem", "free-mem" into functions.

Does it answer your questions? If yes, I'll update the commit message for V2 with a more detailed explanation why these functions are needed.

Quoted 2 lines
Quoted 2 lines
Signed-off-by Stanislav Kholmanskikh <

[email protected]>

Quoted 2 lines
Quoted 2 lines
---
 grub-core/kern/ieee1275/openfw.c |   68

++++++++++++++++++++++++++++++++++++++

Quoted 4 lines

Review

Quoted 4 lines
 include/grub/ieee1275/ieee1275.h |    2 +
 2 files changed, 70 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/grub-core/kern/ieee1275/openfw.c

Quoted 4 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char

Quoted 4 lines

index ddb7783..35225ec 100644

grub-core/kern/ieee1275/openfw.c +0 -0
@@ -561,3 +561,71 @@ grub_ieee1275_canonicalise_devname (const char

*path)

Quoted 12 lines
Quoted 12 lines
grub-core/kern/ieee1275/openfw.c +9 -0
   return NULL;
 }
 
+/* Allocate memory with alloc-mem */
+void *
+grub_ieee1275_alloc_mem (grub_size_t len)
+{
+  struct alloc_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
Quoted 8 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path)

Quoted 8 lines
grub-core/kern/ieee1275/openfw.c +8 -0
+    grub_ieee1275_cell_t catch;
+    grub_ieee1275_cell_t result;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not

supported"));

Quoted 11 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 589

Quoted 9 lines
grub-core/kern/ieee1275/openfw.c +9 -0
+      return NULL;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 2, 2);
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "alloc-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1
+      || args.catch)

I think that this can be in one line.

Ok.

Quoted 18 lines
Quoted 18 lines
grub-core/kern/ieee1275/openfw.c +17 -0
+    {
+      grub_error (GRUB_ERR_INVALID_COMMAND, N_("alloc-mem failed"));
+      return NULL;
+    }
+  else
+    return (void *)args.result;
+}
+
+/* Free memory allocated by alloc-mem */
+grub_err_t
+grub_ieee1275_free_mem (void *addr, grub_size_t len)
+{
+  struct free_args
+  {
+    struct grub_ieee1275_common_hdr common;
+    grub_ieee1275_cell_t method;
+    grub_ieee1275_cell_t len;
Quoted 8 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path)

Quoted 8 lines
grub-core/kern/ieee1275/openfw.c +8 -0
+    grub_ieee1275_cell_t addr;
+    grub_ieee1275_cell_t catch;
+  }
+  args;
+
+  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
+    {
+      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not

supported"));

Quoted 4 lines
Quoted 4 lines
grub-core/kern/ieee1275/openfw.c +4 -0
+      return grub_errno;
+    }
+
+  INIT_IEEE1275_COMMON (&args.common, "interpret", 3, 1);
Quoted 8 lines

grub-core/kern/ieee1275/openfw.c grub_ieee1275_canonicalise_devname (const char *path) · line 624

Quoted 6 lines
grub-core/kern/ieee1275/openfw.c +6 -0
+  args.addr = (grub_ieee1275_cell_t)addr;
+  args.len = len;
+  args.method = (grub_ieee1275_cell_t) "free-mem";
+
+  if (IEEE1275_CALL_ENTRY_FN(&args) == -1
+      || args.catch)

Ditto.

Ok.

Quoted 8 lines

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel