Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 15 Jan 2018 21:21:51 +0000 (UTC)
From:      "Pedro F. Giffuni" <pfg@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r328025 - in head/sys: net netinet6
Message-ID:  <201801152121.w0FLLpmc067121@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: pfg
Date: Mon Jan 15 21:21:51 2018
New Revision: 328025
URL: https://svnweb.freebsd.org/changeset/base/328025

Log:
  net*: make some use of mallocarray(9).
  
  Focus on code where we are doing multiplications within malloc(9). None of
  these ire likely to overflow, however the change is still useful as some
  static checkers can benefit from the allocation attributes we use for
  mallocarray.
  
  This initial sweep only covers malloc(9) calls with M_NOWAIT. No good
  reason but I started doing the changes before r327796 and at that time it
  was convenient to make sure the sorrounding code could handle NULL values.
  
  X-Differential revision: https://reviews.freebsd.org/D13837

Modified:
  head/sys/net/if_vlan.c
  head/sys/net/iflib.c
  head/sys/netinet6/in6_jail.c

Modified: head/sys/net/if_vlan.c
==============================================================================
--- head/sys/net/if_vlan.c	Mon Jan 15 21:19:21 2018	(r328024)
+++ head/sys/net/if_vlan.c	Mon Jan 15 21:21:51 2018	(r328025)
@@ -479,7 +479,7 @@ vlan_growhash(struct ifvlantrunk *trunk, int howmuch)
 		return;
 
 	/* M_NOWAIT because we're called with trunk mutex held */
-	hash2 = malloc(sizeof(struct ifvlanhead) * n2, M_VLAN, M_NOWAIT);
+	hash2 = mallocarray(n2, sizeof(struct ifvlanhead), M_VLAN, M_NOWAIT);
 	if (hash2 == NULL) {
 		printf("%s: out of memory -- hash size not changed\n",
 		    __func__);

Modified: head/sys/net/iflib.c
==============================================================================
--- head/sys/net/iflib.c	Mon Jan 15 21:19:21 2018	(r328024)
+++ head/sys/net/iflib.c	Mon Jan 15 21:21:51 2018	(r328025)
@@ -1550,15 +1550,15 @@ iflib_txsd_alloc(iflib_txq_t txq)
 		goto fail;
 	}
 	if (!(txq->ift_sds.ifsd_flags =
-	    (uint8_t *) malloc(sizeof(uint8_t) *
-	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+	    (uint8_t *) mallocarray(scctx->isc_ntxd[txq->ift_br_offset],
+	    sizeof(uint8_t), M_IFLIB, M_NOWAIT | M_ZERO))) {
 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
 		err = ENOMEM;
 		goto fail;
 	}
 	if (!(txq->ift_sds.ifsd_m =
-	    (struct mbuf **) malloc(sizeof(struct mbuf *) *
-	    scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+	    (struct mbuf **) mallocarray(scctx->isc_ntxd[txq->ift_br_offset],
+	    sizeof(struct mbuf *), M_IFLIB, M_NOWAIT | M_ZERO))) {
 		device_printf(dev, "Unable to allocate tx_buffer memory\n");
 		err = ENOMEM;
 		goto fail;
@@ -1570,7 +1570,8 @@ iflib_txsd_alloc(iflib_txq_t txq)
 		return (0);
 
 	if (!(txq->ift_sds.ifsd_map =
-	    (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+	    (bus_dmamap_t *) mallocarray(scctx->isc_ntxd[txq->ift_br_offset],
+	    sizeof(bus_dmamap_t), M_IFLIB, M_NOWAIT | M_ZERO))) {
 		device_printf(dev, "Unable to allocate tx_buffer map memory\n");
 		err = ENOMEM;
 		goto fail;
@@ -1726,22 +1727,22 @@ iflib_rxsd_alloc(iflib_rxq_t rxq)
 			goto fail;
 		}
 		if (!(fl->ifl_sds.ifsd_flags =
-		      (uint8_t *) malloc(sizeof(uint8_t) *
-					 scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+		      (uint8_t *) mallocarray(scctx->isc_nrxd[rxq->ifr_fl_offset],
+		          sizeof(uint8_t), M_IFLIB, M_NOWAIT | M_ZERO))) {
 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
 			err = ENOMEM;
 			goto fail;
 		}
 		if (!(fl->ifl_sds.ifsd_m =
-		      (struct mbuf **) malloc(sizeof(struct mbuf *) *
-					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+		      (struct mbuf **) mallocarray(scctx->isc_nrxd[rxq->ifr_fl_offset],
+		          sizeof(struct mbuf *), M_IFLIB, M_NOWAIT | M_ZERO))) {
 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
 			err = ENOMEM;
 			goto fail;
 		}
 		if (!(fl->ifl_sds.ifsd_cl =
-		      (caddr_t *) malloc(sizeof(caddr_t) *
-					      scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+		      (caddr_t *) mallocarray(scctx->isc_nrxd[rxq->ifr_fl_offset],
+		          sizeof(caddr_t), M_IFLIB, M_NOWAIT | M_ZERO))) {
 			device_printf(dev, "Unable to allocate tx_buffer memory\n");
 			err = ENOMEM;
 			goto fail;
@@ -1753,7 +1754,8 @@ iflib_rxsd_alloc(iflib_rxq_t rxq)
 			continue;
 
 		if (!(fl->ifl_sds.ifsd_map =
-		      (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
+		      (bus_dmamap_t *) mallocarray(scctx->isc_nrxd[rxq->ifr_fl_offset],
+		          sizeof(bus_dmamap_t), M_IFLIB, M_NOWAIT | M_ZERO))) {
 			device_printf(dev, "Unable to allocate tx_buffer map memory\n");
 			err = ENOMEM;
 			goto fail;
@@ -4745,8 +4747,8 @@ iflib_queues_alloc(if_ctx_t ctx)
 
 /* Allocate the TX ring struct memory */
 	if (!(txq =
-	    (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
-	    ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
+	    (iflib_txq_t) mallocarray(ntxqsets, sizeof(struct iflib_txq),
+	        M_IFLIB, M_NOWAIT | M_ZERO))) {
 		device_printf(dev, "Unable to allocate TX ring memory\n");
 		err = ENOMEM;
 		goto fail;
@@ -4754,8 +4756,8 @@ iflib_queues_alloc(if_ctx_t ctx)
 
 	/* Now allocate the RX */
 	if (!(rxq =
-	    (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
-	    nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
+	    (iflib_rxq_t) mallocarray(nrxqsets, sizeof(struct iflib_rxq),
+	        M_IFLIB, M_NOWAIT | M_ZERO))) {
 		device_printf(dev, "Unable to allocate RX ring memory\n");
 		err = ENOMEM;
 		goto rx_fail;
@@ -4849,7 +4851,8 @@ iflib_queues_alloc(if_ctx_t ctx)
 		}
 		rxq->ifr_nfl = nfree_lists;
 		if (!(fl =
-			  (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
+			  (iflib_fl_t) mallocarray(nfree_lists, sizeof(struct iflib_fl),
+			      M_IFLIB, M_NOWAIT | M_ZERO))) {
 			device_printf(dev, "Unable to allocate free list memory\n");
 			err = ENOMEM;
 			goto err_tx_desc;

Modified: head/sys/netinet6/in6_jail.c
==============================================================================
--- head/sys/netinet6/in6_jail.c	Mon Jan 15 21:19:21 2018	(r328024)
+++ head/sys/netinet6/in6_jail.c	Mon Jan 15 21:21:51 2018	(r328025)
@@ -103,8 +103,8 @@ prison_restrict_ip6(struct prison *pr, struct in6_addr
 			 */
 			used = 1;
 			if (newip6 == NULL) {
-				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
-				    M_PRISON, M_NOWAIT);
+				newip6 = mallocarray(ppr->pr_ip6s,
+				    sizeof(*newip6), M_PRISON, M_NOWAIT);
 				if (newip6 != NULL)
 					used = 0;
 			}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201801152121.w0FLLpmc067121>