From 6ea5f4c4dd1dfa95dff164500cd38eeb78182d9d Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 4 Dec 2012 19:43:47 +0100
Subject: [PATCH] kernel: Remove NULL checks after kmalloc(..., M_INTWAIT).

M_INTWAIT will not return NULL unless M_NULLOK is passed too.
---
 sys/bus/cam/scsi/scsi_da.c                     |    5 -----
 sys/dev/crypto/hifn/hifn7751.c                 |    5 -----
 sys/dev/netif/ath/ath/if_ath.c                 |   17 -----------------
 sys/dev/netif/iwn/if_iwn.c                     |    6 ------
 sys/kern/subr_bus.c                            |   14 --------------
 sys/netproto/802_11/wlan/ieee80211_hwmp.c      |    4 ----
 sys/netproto/802_11/wlan/ieee80211_mesh.c      |    6 ------
 sys/netproto/802_11/wlan/ieee80211_regdomain.c |    6 ------
 sys/opencrypto/cryptosoft.c                    |    2 --
 sys/platform/pc32/i386/nexus.c                 |    2 --
 sys/platform/pc64/x86_64/nexus.c               |    2 --
 11 files changed, 0 insertions(+), 69 deletions(-)

diff --git a/sys/bus/cam/scsi/scsi_da.c b/sys/bus/cam/scsi/scsi_da.c
index 60658a3..8706d57 100644
--- a/sys/bus/cam/scsi/scsi_da.c
+++ b/sys/bus/cam/scsi/scsi_da.c
@@ -1587,11 +1587,6 @@ dastart(struct cam_periph *periph, union ccb *start_ccb)
 
 		rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
 				   M_INTWAIT | M_ZERO);
-		if (rcaplong == NULL) {
-			kprintf("dastart: Couldn't allocate read_capacity\n");
-			/* da_free_periph??? */
-			break;
-		}
 		csio = &start_ccb->csio;
 		scsi_read_capacity_16(csio,
 				    /*retries*/ 4,
diff --git a/sys/dev/crypto/hifn/hifn7751.c b/sys/dev/crypto/hifn/hifn7751.c
index e8fd15a..71984c5 100644
--- a/sys/dev/crypto/hifn/hifn7751.c
+++ b/sys/dev/crypto/hifn/hifn7751.c
@@ -2513,11 +2513,6 @@ hifn_process(device_t dev, struct cryptop *crp, int hint)
 	}
 
 	cmd = kmalloc(sizeof(struct hifn_command), M_DEVBUF, M_INTWAIT | M_ZERO);
-	if (cmd == NULL) {
-		hifnstats.hst_nomem++;
-		err = ENOMEM;
-		goto errout;
-	}
 
 	if (crp->crp_flags & CRYPTO_F_IMBUF) {
 		cmd->src_m = (struct mbuf *)crp->crp_buf;
diff --git a/sys/dev/netif/ath/ath/if_ath.c b/sys/dev/netif/ath/ath/if_ath.c
index 2f22372..8449fae 100644
--- a/sys/dev/netif/ath/ath/if_ath.c
+++ b/sys/dev/netif/ath/ath/if_ath.c
@@ -3385,11 +3385,6 @@ ath_descdma_setup(struct ath_softc *sc,
 	/* allocate rx buffers */
 	bsize = sizeof(struct ath_buf) * nbuf;
 	bf = kmalloc(bsize, M_ATHDEV, M_INTWAIT | M_ZERO);
-	if (bf == NULL) {
-		if_printf(ifp, "malloc of %s buffers failed, size %u\n",
-			dd->dd_name, bsize);
-		goto fail3;
-	}
 	dd->dd_bufptr = bf;
 
 	STAILQ_INIT(head);
@@ -3504,10 +3499,6 @@ ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
 	struct ath_node *an;
 
 	an = kmalloc(space, M_80211_NODE, M_INTWAIT|M_ZERO);
-	if (an == NULL) {
-		/* XXX stat+msg */
-		return NULL;
-	}
 	ath_rate_node_init(sc, an);
 
 	DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
@@ -6154,10 +6145,6 @@ ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
 		 * Copy in data.
 		 */
 		indata = kmalloc(insize, M_TEMP, M_INTWAIT);
-		if (indata == NULL) {
-			error = ENOMEM;
-			goto bad;
-		}
 		error = copyin(ad->ad_in_data, indata, insize);
 		if (error)
 			goto bad;
@@ -6171,10 +6158,6 @@ ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
 		 * may want to be more defensive.
 		 */
 		outdata = kmalloc(outsize, M_TEMP, M_INTWAIT);
-		if (outdata == NULL) {
-			error = ENOMEM;
-			goto bad;
-		}
 	}
 	if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
 		if (outsize < ad->ad_out_size)
diff --git a/sys/dev/netif/iwn/if_iwn.c b/sys/dev/netif/iwn/if_iwn.c
index 525aa03..1c634cc 100644
--- a/sys/dev/netif/iwn/if_iwn.c
+++ b/sys/dev/netif/iwn/if_iwn.c
@@ -4640,12 +4640,6 @@ iwn_scan(struct iwn_softc *sc)
 	uint8_t *buf, *frm, txant;
 
 	buf = kmalloc(IWN_SCAN_MAXSZ, M_DEVBUF, M_INTWAIT | M_ZERO);
-	if (buf == NULL) {
-		device_printf(sc->sc_dev,
-		    "%s: could not allocate buffer for scan command\n",
-		    __func__);
-		return ENOMEM;
-	}
 	hdr = (struct iwn_scan_hdr *)buf;
 
 	/*
diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index 4a1dcfa..6d9612e 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -594,8 +594,6 @@ devclass_find_internal(const char *classname, const char *parentname,
 		PDEBUG(("creating %s", classname));
 		dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
 			    M_BUS, M_INTWAIT | M_ZERO);
-		if (!dc)
-			return(NULL);
 		dc->parent = NULL;
 		dc->name = (char*) (dc + 1);
 		strcpy(dc->name, classname);
@@ -655,8 +653,6 @@ devclass_add_driver(devclass_t dc, driver_t *driver)
 	PDEBUG(("%s", DRIVERNAME(driver)));
 
 	dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
-	if (!dl)
-		return(ENOMEM);
 
 	/*
 	 * Compile the driver's methods. Also increase the reference count
@@ -816,8 +812,6 @@ devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
 			count++;
 
 	list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
-	if (list == NULL)
-		return(ENOMEM);
 
 	count = 0;
 	for (i = 0; i < dc->maxunit; i++)
@@ -1338,8 +1332,6 @@ device_get_children(device_t dev, device_t **devlistp, int *devcountp)
 		count++;
 
 	list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
-	if (!list)
-		return(ENOMEM);
 
 	count = 0;
 	TAILQ_FOREACH(child, &dev->children, link) {
@@ -1834,8 +1826,6 @@ resource_new_name(const char *name, int unit)
 
 	new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
 		     M_INTWAIT | M_ZERO);
-	if (new == NULL)
-		return(-1);
 	if (devtab && devtab_count > 0)
 		bcopy(devtab, new, devtab_count * sizeof(*new));
 	new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
@@ -1861,8 +1851,6 @@ resource_new_resname(int j, const char *resname, resource_type type)
 
 	i = devtab[j].resource_count;
 	new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
-	if (new == NULL)
-		return(-1);
 	if (devtab[j].resources && i > 0)
 		bcopy(devtab[j].resources, new, i * sizeof(*new));
 	new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
@@ -2223,8 +2211,6 @@ resource_list_add(struct resource_list *rl, int type, int rid,
 	if (rle == NULL) {
 		rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
 			     M_INTWAIT);
-		if (!rle)
-			panic("resource_list_add: can't record entry");
 		SLIST_INSERT_HEAD(rl, rle, link);
 		rle->type = type;
 		rle->rid = rid;
diff --git a/sys/netproto/802_11/wlan/ieee80211_hwmp.c b/sys/netproto/802_11/wlan/ieee80211_hwmp.c
index 2d730dc..0ce7a27 100644
--- a/sys/netproto/802_11/wlan/ieee80211_hwmp.c
+++ b/sys/netproto/802_11/wlan/ieee80211_hwmp.c
@@ -238,10 +238,6 @@ hwmp_vattach(struct ieee80211vap *vap)
 
 	hs = kmalloc(sizeof(struct ieee80211_hwmp_state), M_80211_VAP,
 	    M_INTWAIT | M_ZERO);
-	if (hs == NULL) {
-		kprintf("%s: couldn't alloc HWMP state\n", __func__);
-		return;
-	}
 	hs->hs_maxhops = IEEE80211_HWMP_DEFAULT_MAXHOPS;
 	callout_init_mp(&hs->hs_roottimer);
 	vap->iv_hwmp = hs;
diff --git a/sys/netproto/802_11/wlan/ieee80211_mesh.c b/sys/netproto/802_11/wlan/ieee80211_mesh.c
index 725cac2..ddc1db5 100644
--- a/sys/netproto/802_11/wlan/ieee80211_mesh.c
+++ b/sys/netproto/802_11/wlan/ieee80211_mesh.c
@@ -495,10 +495,6 @@ mesh_vattach(struct ieee80211vap *vap)
 	vap->iv_recv_mgmt = mesh_recv_mgmt;
 	ms = kmalloc(sizeof(struct ieee80211_mesh_state), M_80211_VAP,
 	    M_INTWAIT | M_ZERO);
-	if (ms == NULL) {
-		kprintf("%s: couldn't alloc MBSS state\n", __func__);
-		return;
-	}
 	vap->iv_mesh = ms;
 	ms->ms_seq = 0;
 	ms->ms_flags = (IEEE80211_MESHFLAGS_AP | IEEE80211_MESHFLAGS_FWD);
@@ -2583,8 +2579,6 @@ mesh_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
 			ireq->i_len = len;
 			/* XXX M_WAIT? */
 			p = kmalloc(len, M_TEMP, M_INTWAIT | M_ZERO);
-			if (p == NULL)
-				return ENOMEM;
 			off = 0;
 			TAILQ_FOREACH(rt, &ms->ms_routes, rt_next) {
 				if (off >= len)
diff --git a/sys/netproto/802_11/wlan/ieee80211_regdomain.c b/sys/netproto/802_11/wlan/ieee80211_regdomain.c
index e7f3c7a..8371537 100644
--- a/sys/netproto/802_11/wlan/ieee80211_regdomain.c
+++ b/sys/netproto/802_11/wlan/ieee80211_regdomain.c
@@ -242,12 +242,6 @@ ieee80211_alloc_countryie(struct ieee80211com *ic)
 
 	aie = kmalloc(IEEE80211_COUNTRY_MAX_SIZE, M_80211_NODE_IE,
 	    M_INTWAIT | M_ZERO);
-	if (aie == NULL) {
-		if_printf(ic->ic_ifp,
-		    "%s: unable to allocate memory for country ie\n", __func__);
-		/* XXX stat */
-		return NULL;
-	}
 	ie = (struct ieee80211_country_ie *) aie->ie_data;
 	ie->ie = IEEE80211_ELEMID_COUNTRY;
 	if (rd->isocc[0] == '\0') {
diff --git a/sys/opencrypto/cryptosoft.c b/sys/opencrypto/cryptosoft.c
index e274486..1f90012 100644
--- a/sys/opencrypto/cryptosoft.c
+++ b/sys/opencrypto/cryptosoft.c
@@ -768,8 +768,6 @@ swcr_compdec(struct cryptodesc *crd, struct swcr_data *sw,
 	 * copy in a buffer.
 	 */
 	data = kmalloc(crd->crd_len, M_CRYPTO_DATA, M_INTWAIT);
-	if (data == NULL)
-		return (EINVAL);
 	crypto_copydata(flags, buf, crd->crd_skip, crd->crd_len, data);
 
 	if (crd->crd_flags & CRD_F_COMP)
diff --git a/sys/platform/pc32/i386/nexus.c b/sys/platform/pc32/i386/nexus.c
index 3a36f10..9c9ce4a 100644
--- a/sys/platform/pc32/i386/nexus.c
+++ b/sys/platform/pc32/i386/nexus.c
@@ -289,8 +289,6 @@ nexus_add_child(device_t bus, device_t parent, int order,
 	struct nexus_device	*ndev;
 
 	ndev = kmalloc(sizeof(struct nexus_device), M_NEXUSDEV, M_INTWAIT|M_ZERO);
-	if (!ndev)
-		return(0);
 	resource_list_init(&ndev->nx_resources);
 	ndev->nx_pcibus = -1;
 
diff --git a/sys/platform/pc64/x86_64/nexus.c b/sys/platform/pc64/x86_64/nexus.c
index f5adb64..d2ee821 100644
--- a/sys/platform/pc64/x86_64/nexus.c
+++ b/sys/platform/pc64/x86_64/nexus.c
@@ -287,8 +287,6 @@ nexus_add_child(device_t bus, device_t parent, int order,
 	struct nexus_device	*ndev;
 
 	ndev = kmalloc(sizeof(struct nexus_device), M_NEXUSDEV, M_INTWAIT|M_ZERO);
-	if (!ndev)
-		return(0);
 	resource_list_init(&ndev->nx_resources);
 	ndev->nx_pcibus = -1;
 
-- 
1.7.7.2

