diff -druNp --no-dereference linux-libre-4.16.4-gnu/arch/x86/kernel/acpi/boot.c linux-libre-4.16.5-gnu/arch/x86/kernel/acpi/boot.c
--- linux-libre-4.16.4-gnu/arch/x86/kernel/acpi/boot.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/arch/x86/kernel/acpi/boot.c	2018-04-26 05:00:39.000000000 -0400
@@ -215,6 +215,10 @@ acpi_parse_x2apic(struct acpi_subtable_h
 	apic_id = processor->local_apic_id;
 	enabled = processor->lapic_flags & ACPI_MADT_ENABLED;
 
+	/* Ignore invalid ID */
+	if (apic_id == 0xffffffff)
+		return 0;
+
 	/*
 	 * We need to register disabled CPU as well to permit
 	 * counting disabled CPUs. This allows us to size
diff -druNp --no-dereference linux-libre-4.16.4-gnu/arch/x86/kernel/tsc.c linux-libre-4.16.5-gnu/arch/x86/kernel/tsc.c
--- linux-libre-4.16.4-gnu/arch/x86/kernel/tsc.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/arch/x86/kernel/tsc.c	2018-04-26 05:00:39.000000000 -0400
@@ -317,7 +317,7 @@ static unsigned long calc_hpet_ref(u64 d
 	hpet2 -= hpet1;
 	tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
 	do_div(tmp, 1000000);
-	do_div(deltatsc, tmp);
+	deltatsc = div64_u64(deltatsc, tmp);
 
 	return (unsigned long) deltatsc;
 }
diff -druNp --no-dereference linux-libre-4.16.4-gnu/arch/x86/kvm/mmu.c linux-libre-4.16.5-gnu/arch/x86/kvm/mmu.c
--- linux-libre-4.16.4-gnu/arch/x86/kvm/mmu.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/arch/x86/kvm/mmu.c	2018-04-26 05:00:39.000000000 -0400
@@ -3031,7 +3031,7 @@ static int kvm_handle_bad_page(struct kv
 		return RET_PF_RETRY;
 	}
 
-	return RET_PF_EMULATE;
+	return -EFAULT;
 }
 
 static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu,
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/clocksource/timer-imx-tpm.c linux-libre-4.16.5-gnu/drivers/clocksource/timer-imx-tpm.c
--- linux-libre-4.16.4-gnu/drivers/clocksource/timer-imx-tpm.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/clocksource/timer-imx-tpm.c	2018-04-26 05:00:39.000000000 -0400
@@ -105,7 +105,7 @@ static int tpm_set_next_event(unsigned l
 	 * of writing CNT registers which may cause the min_delta event got
 	 * missed, so we need add a ETIME check here in case it happened.
 	 */
-	return (int)((next - now) <= 0) ? -ETIME : 0;
+	return (int)(next - now) <= 0 ? -ETIME : 0;
 }
 
 static int tpm_set_state_oneshot(struct clock_event_device *evt)
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/drm_dp_dual_mode_helper.c linux-libre-4.16.5-gnu/drivers/gpu/drm/drm_dp_dual_mode_helper.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/drm_dp_dual_mode_helper.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/drm_dp_dual_mode_helper.c	2018-04-26 05:00:39.000000000 -0400
@@ -350,19 +350,44 @@ int drm_dp_dual_mode_set_tmds_output(enu
 {
 	uint8_t tmds_oen = enable ? 0 : DP_DUAL_MODE_TMDS_DISABLE;
 	ssize_t ret;
+	int retry;
 
 	if (type < DRM_DP_DUAL_MODE_TYPE2_DVI)
 		return 0;
 
-	ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
-				     &tmds_oen, sizeof(tmds_oen));
-	if (ret) {
-		DRM_DEBUG_KMS("Failed to %s TMDS output buffers\n",
-			      enable ? "enable" : "disable");
-		return ret;
+	/*
+	 * LSPCON adapters in low-power state may ignore the first write, so
+	 * read back and verify the written value a few times.
+	 */
+	for (retry = 0; retry < 3; retry++) {
+		uint8_t tmp;
+
+		ret = drm_dp_dual_mode_write(adapter, DP_DUAL_MODE_TMDS_OEN,
+					     &tmds_oen, sizeof(tmds_oen));
+		if (ret) {
+			DRM_DEBUG_KMS("Failed to %s TMDS output buffers (%d attempts)\n",
+				      enable ? "enable" : "disable",
+				      retry + 1);
+			return ret;
+		}
+
+		ret = drm_dp_dual_mode_read(adapter, DP_DUAL_MODE_TMDS_OEN,
+					    &tmp, sizeof(tmp));
+		if (ret) {
+			DRM_DEBUG_KMS("I2C read failed during TMDS output buffer %s (%d attempts)\n",
+				      enable ? "enabling" : "disabling",
+				      retry + 1);
+			return ret;
+		}
+
+		if (tmp == tmds_oen)
+			return 0;
 	}
 
-	return 0;
+	DRM_DEBUG_KMS("I2C write value mismatch during TMDS output buffer %s\n",
+		      enable ? "enabling" : "disabling");
+
+	return -EIO;
 }
 EXPORT_SYMBOL(drm_dp_dual_mode_set_tmds_output);
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/gvt/dmabuf.c linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/gvt/dmabuf.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/gvt/dmabuf.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/gvt/dmabuf.c	2018-04-26 05:00:39.000000000 -0400
@@ -323,6 +323,7 @@ static void update_fb_info(struct vfio_d
 		      struct intel_vgpu_fb_info *fb_info)
 {
 	gvt_dmabuf->drm_format = fb_info->drm_format;
+	gvt_dmabuf->drm_format_mod = fb_info->drm_format_mod;
 	gvt_dmabuf->width = fb_info->width;
 	gvt_dmabuf->height = fb_info->height;
 	gvt_dmabuf->stride = fb_info->stride;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/gvt/kvmgt.c linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/gvt/kvmgt.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/gvt/kvmgt.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/gvt/kvmgt.c	2018-04-26 05:00:39.000000000 -0400
@@ -1284,7 +1284,7 @@ static long intel_vgpu_ioctl(struct mdev
 
 	}
 
-	return 0;
+	return -ENOTTY;
 }
 
 static ssize_t
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/i915_gem_execbuffer.c linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/i915_gem_execbuffer.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/i915_gem_execbuffer.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/i915_gem_execbuffer.c	2018-04-26 05:00:39.000000000 -0400
@@ -728,7 +728,7 @@ static int eb_lookup_vmas(struct i915_ex
 
 		err = radix_tree_insert(handles_vma, handle, vma);
 		if (unlikely(err)) {
-			kfree(lut);
+			kmem_cache_free(eb->i915->luts, lut);
 			goto err_obj;
 		}
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/intel_audio.c linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/intel_audio.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/intel_audio.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/intel_audio.c	2018-04-26 05:00:39.000000000 -0400
@@ -729,7 +729,7 @@ static void i915_audio_component_codec_w
 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
 	u32 tmp;
 
-	if (!IS_GEN9_BC(dev_priv))
+	if (!IS_GEN9(dev_priv))
 		return;
 
 	i915_audio_component_get_power(kdev);
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/intel_bios.c linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/intel_bios.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/i915/intel_bios.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/i915/intel_bios.c	2018-04-26 05:00:39.000000000 -0400
@@ -1255,7 +1255,6 @@ static void parse_ddi_port(struct drm_i9
 		return;
 
 	aux_channel = child->aux_channel;
-	ddc_pin = child->ddc_pin;
 
 	is_dvi = child->device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
 	is_dp = child->device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
@@ -1302,9 +1301,15 @@ static void parse_ddi_port(struct drm_i9
 		DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
 
 	if (is_dvi) {
-		info->alternate_ddc_pin = map_ddc_pin(dev_priv, ddc_pin);
-
-		sanitize_ddc_pin(dev_priv, port);
+		ddc_pin = map_ddc_pin(dev_priv, child->ddc_pin);
+		if (intel_gmbus_is_valid_pin(dev_priv, ddc_pin)) {
+			info->alternate_ddc_pin = ddc_pin;
+			sanitize_ddc_pin(dev_priv, port);
+		} else {
+			DRM_DEBUG_KMS("Port %c has invalid DDC pin %d, "
+				      "sticking to defaults\n",
+				      port_name(port), ddc_pin);
+		}
 	}
 
 	if (is_dp) {
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/vc4/vc4_bo.c linux-libre-4.16.5-gnu/drivers/gpu/drm/vc4/vc4_bo.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/vc4/vc4_bo.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/vc4/vc4_bo.c	2018-04-26 05:00:39.000000000 -0400
@@ -195,6 +195,7 @@ static void vc4_bo_destroy(struct vc4_bo
 	vc4_bo_set_label(obj, -1);
 
 	if (bo->validated_shader) {
+		kfree(bo->validated_shader->uniform_addr_offsets);
 		kfree(bo->validated_shader->texture_samples);
 		kfree(bo->validated_shader);
 		bo->validated_shader = NULL;
@@ -591,6 +592,7 @@ void vc4_free_object(struct drm_gem_obje
 	}
 
 	if (bo->validated_shader) {
+		kfree(bo->validated_shader->uniform_addr_offsets);
 		kfree(bo->validated_shader->texture_samples);
 		kfree(bo->validated_shader);
 		bo->validated_shader = NULL;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/gpu/drm/vc4/vc4_validate_shaders.c linux-libre-4.16.5-gnu/drivers/gpu/drm/vc4/vc4_validate_shaders.c
--- linux-libre-4.16.4-gnu/drivers/gpu/drm/vc4/vc4_validate_shaders.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/gpu/drm/vc4/vc4_validate_shaders.c	2018-04-26 05:00:39.000000000 -0400
@@ -942,6 +942,7 @@ vc4_validate_shader(struct drm_gem_cma_o
 fail:
 	kfree(validation_state.branch_targets);
 	if (validated_shader) {
+		kfree(validated_shader->uniform_addr_offsets);
 		kfree(validated_shader->texture_samples);
 		kfree(validated_shader);
 	}
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/infiniband/hw/mlx5/qp.c linux-libre-4.16.5-gnu/drivers/infiniband/hw/mlx5/qp.c
--- linux-libre-4.16.4-gnu/drivers/infiniband/hw/mlx5/qp.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/infiniband/hw/mlx5/qp.c	2018-04-26 05:00:39.000000000 -0400
@@ -3157,7 +3157,8 @@ static int __mlx5_ib_modify_qp(struct ib
 	 * If we moved a kernel QP to RESET, clean up all old CQ
 	 * entries and reinitialize the QP.
 	 */
-	if (new_state == IB_QPS_RESET && !ibqp->uobject) {
+	if (new_state == IB_QPS_RESET &&
+	    !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
 		mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
 				 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
 		if (send_cq != recv_cq)
diff -druNp --no-dereference linux-libre-4.16.4-gnu/drivers/net/wireless/mac80211_hwsim.c linux-libre-4.16.5-gnu/drivers/net/wireless/mac80211_hwsim.c
--- linux-libre-4.16.4-gnu/drivers/net/wireless/mac80211_hwsim.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/drivers/net/wireless/mac80211_hwsim.c	2018-04-26 05:00:39.000000000 -0400
@@ -3484,8 +3484,11 @@ static void __net_exit hwsim_exit_net(st
 		list_del(&data->list);
 		rhashtable_remove_fast(&hwsim_radios_rht, &data->rht,
 				       hwsim_rht_params);
-		INIT_WORK(&data->destroy_work, destroy_radio);
-		queue_work(hwsim_wq, &data->destroy_work);
+		spin_unlock_bh(&hwsim_radio_lock);
+		mac80211_hwsim_del_radio(data,
+					 wiphy_name(data->hw->wiphy),
+					 NULL);
+		spin_lock_bh(&hwsim_radio_lock);
 	}
 	spin_unlock_bh(&hwsim_radio_lock);
 }
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/btrfs/delayed-ref.c linux-libre-4.16.5-gnu/fs/btrfs/delayed-ref.c
--- linux-libre-4.16.4-gnu/fs/btrfs/delayed-ref.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/btrfs/delayed-ref.c	2018-04-26 05:00:39.000000000 -0400
@@ -553,8 +553,10 @@ add_delayed_ref_head(struct btrfs_fs_inf
 		     struct btrfs_delayed_ref_head *head_ref,
 		     struct btrfs_qgroup_extent_record *qrecord,
 		     u64 bytenr, u64 num_bytes, u64 ref_root, u64 reserved,
-		     int action, int is_data, int *qrecord_inserted_ret,
+		     int action, int is_data, int is_system,
+		     int *qrecord_inserted_ret,
 		     int *old_ref_mod, int *new_ref_mod)
+
 {
 	struct btrfs_delayed_ref_head *existing;
 	struct btrfs_delayed_ref_root *delayed_refs;
@@ -598,6 +600,7 @@ add_delayed_ref_head(struct btrfs_fs_inf
 	head_ref->ref_mod = count_mod;
 	head_ref->must_insert_reserved = must_insert_reserved;
 	head_ref->is_data = is_data;
+	head_ref->is_system = is_system;
 	head_ref->ref_tree = RB_ROOT;
 	INIT_LIST_HEAD(&head_ref->ref_add_list);
 	RB_CLEAR_NODE(&head_ref->href_node);
@@ -785,6 +788,7 @@ int btrfs_add_delayed_tree_ref(struct bt
 	struct btrfs_delayed_ref_root *delayed_refs;
 	struct btrfs_qgroup_extent_record *record = NULL;
 	int qrecord_inserted;
+	int is_system = (ref_root == BTRFS_CHUNK_TREE_OBJECTID);
 
 	BUG_ON(extent_op && extent_op->is_data);
 	ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
@@ -813,8 +817,8 @@ int btrfs_add_delayed_tree_ref(struct bt
 	 */
 	head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
 					bytenr, num_bytes, 0, 0, action, 0,
-					&qrecord_inserted, old_ref_mod,
-					new_ref_mod);
+					is_system, &qrecord_inserted,
+					old_ref_mod, new_ref_mod);
 
 	add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
 			     num_bytes, parent, ref_root, level, action);
@@ -881,7 +885,7 @@ int btrfs_add_delayed_data_ref(struct bt
 	 */
 	head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
 					bytenr, num_bytes, ref_root, reserved,
-					action, 1, &qrecord_inserted,
+					action, 1, 0, &qrecord_inserted,
 					old_ref_mod, new_ref_mod);
 
 	add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
@@ -911,9 +915,14 @@ int btrfs_add_delayed_extent_op(struct b
 	delayed_refs = &trans->transaction->delayed_refs;
 	spin_lock(&delayed_refs->lock);
 
+	/*
+	 * extent_ops just modify the flags of an extent and they don't result
+	 * in ref count changes, hence it's safe to pass false/0 for is_system
+	 * argument
+	 */
 	add_delayed_ref_head(fs_info, trans, head_ref, NULL, bytenr,
 			     num_bytes, 0, 0, BTRFS_UPDATE_DELAYED_HEAD,
-			     extent_op->is_data, NULL, NULL, NULL);
+			     extent_op->is_data, 0, NULL, NULL, NULL);
 
 	spin_unlock(&delayed_refs->lock);
 	return 0;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/btrfs/delayed-ref.h linux-libre-4.16.5-gnu/fs/btrfs/delayed-ref.h
--- linux-libre-4.16.4-gnu/fs/btrfs/delayed-ref.h	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/btrfs/delayed-ref.h	2018-04-26 05:00:39.000000000 -0400
@@ -139,6 +139,7 @@ struct btrfs_delayed_ref_head {
 	 */
 	unsigned int must_insert_reserved:1;
 	unsigned int is_data:1;
+	unsigned int is_system:1;
 	unsigned int processing:1;
 };
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/btrfs/extent-tree.c linux-libre-4.16.5-gnu/fs/btrfs/extent-tree.c
--- linux-libre-4.16.4-gnu/fs/btrfs/extent-tree.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/btrfs/extent-tree.c	2018-04-26 05:00:39.000000000 -0400
@@ -2615,13 +2615,19 @@ static int cleanup_ref_head(struct btrfs
 	trace_run_delayed_ref_head(fs_info, head, 0);
 
 	if (head->total_ref_mod < 0) {
-		struct btrfs_block_group_cache *cache;
+		struct btrfs_space_info *space_info;
+		u64 flags;
 
-		cache = btrfs_lookup_block_group(fs_info, head->bytenr);
-		ASSERT(cache);
-		percpu_counter_add(&cache->space_info->total_bytes_pinned,
+		if (head->is_data)
+			flags = BTRFS_BLOCK_GROUP_DATA;
+		else if (head->is_system)
+			flags = BTRFS_BLOCK_GROUP_SYSTEM;
+		else
+			flags = BTRFS_BLOCK_GROUP_METADATA;
+		space_info = __find_space_info(fs_info, flags);
+		ASSERT(space_info);
+		percpu_counter_add(&space_info->total_bytes_pinned,
 				   -head->num_bytes);
-		btrfs_put_block_group(cache);
 
 		if (head->is_data) {
 			spin_lock(&delayed_refs->lock);
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/btrfs/inode.c linux-libre-4.16.5-gnu/fs/btrfs/inode.c
--- linux-libre-4.16.4-gnu/fs/btrfs/inode.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/btrfs/inode.c	2018-04-26 05:00:39.000000000 -0400
@@ -44,6 +44,7 @@
 #include <linux/uio.h>
 #include <linux/magic.h>
 #include <linux/iversion.h>
+#include <asm/unaligned.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "transaction.h"
@@ -5951,11 +5952,13 @@ static int btrfs_filldir(void *addr, int
 		struct dir_entry *entry = addr;
 		char *name = (char *)(entry + 1);
 
-		ctx->pos = entry->offset;
-		if (!dir_emit(ctx, name, entry->name_len, entry->ino,
-			      entry->type))
+		ctx->pos = get_unaligned(&entry->offset);
+		if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
+					 get_unaligned(&entry->ino),
+					 get_unaligned(&entry->type)))
 			return 1;
-		addr += sizeof(struct dir_entry) + entry->name_len;
+		addr += sizeof(struct dir_entry) +
+			get_unaligned(&entry->name_len);
 		ctx->pos++;
 	}
 	return 0;
@@ -6045,14 +6048,15 @@ again:
 		}
 
 		entry = addr;
-		entry->name_len = name_len;
+		put_unaligned(name_len, &entry->name_len);
 		name_ptr = (char *)(entry + 1);
 		read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
 				   name_len);
-		entry->type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
+		put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)],
+				&entry->type);
 		btrfs_dir_item_key_to_cpu(leaf, di, &location);
-		entry->ino = location.objectid;
-		entry->offset = found_key.offset;
+		put_unaligned(location.objectid, &entry->ino);
+		put_unaligned(found_key.offset, &entry->offset);
 		entries++;
 		addr += sizeof(struct dir_entry) + name_len;
 		total_len += sizeof(struct dir_entry) + name_len;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/cifs/dir.c linux-libre-4.16.5-gnu/fs/cifs/dir.c
--- linux-libre-4.16.4-gnu/fs/cifs/dir.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/cifs/dir.c	2018-04-26 05:00:39.000000000 -0400
@@ -684,6 +684,9 @@ int cifs_mknod(struct inode *inode, stru
 		goto mknod_out;
 	}
 
+	if (!S_ISCHR(mode) && !S_ISBLK(mode))
+		goto mknod_out;
+
 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
 		goto mknod_out;
 
@@ -692,10 +695,8 @@ int cifs_mknod(struct inode *inode, stru
 
 	buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
 	if (buf == NULL) {
-		kfree(full_path);
 		rc = -ENOMEM;
-		free_xid(xid);
-		return rc;
+		goto mknod_out;
 	}
 
 	if (backup_cred(cifs_sb))
@@ -742,7 +743,7 @@ int cifs_mknod(struct inode *inode, stru
 		pdev->minor = cpu_to_le64(MINOR(device_number));
 		rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
 							&bytes_written, iov, 1);
-	} /* else if (S_ISFIFO) */
+	}
 	tcon->ses->server->ops->close(xid, tcon, &fid);
 	d_drop(direntry);
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/cifs/smbdirect.c linux-libre-4.16.5-gnu/fs/cifs/smbdirect.c
--- linux-libre-4.16.4-gnu/fs/cifs/smbdirect.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/cifs/smbdirect.c	2018-04-26 05:00:39.000000000 -0400
@@ -2194,6 +2194,8 @@ int smbd_send(struct smbd_connection *in
 						goto done;
 				}
 				i++;
+				if (i == rqst->rq_nvec)
+					break;
 			}
 			start = i;
 			buflen = 0;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/fs/super.c linux-libre-4.16.5-gnu/fs/super.c
--- linux-libre-4.16.4-gnu/fs/super.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/fs/super.c	2018-04-26 05:00:39.000000000 -0400
@@ -166,6 +166,7 @@ static void destroy_unused_super(struct
 	security_sb_free(s);
 	put_user_ns(s->s_user_ns);
 	kfree(s->s_subtype);
+	free_prealloced_shrinker(&s->s_shrink);
 	/* no delays needed */
 	destroy_super_work(&s->destroy_work);
 }
@@ -251,6 +252,8 @@ static struct super_block *alloc_super(s
 	s->s_shrink.count_objects = super_cache_count;
 	s->s_shrink.batch = 1024;
 	s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE;
+	if (prealloc_shrinker(&s->s_shrink))
+		goto fail;
 	return s;
 
 fail:
@@ -517,11 +520,7 @@ retry:
 	hlist_add_head(&s->s_instances, &type->fs_supers);
 	spin_unlock(&sb_lock);
 	get_filesystem(type);
-	err = register_shrinker(&s->s_shrink);
-	if (err) {
-		deactivate_locked_super(s);
-		s = ERR_PTR(err);
-	}
+	register_shrinker_prepared(&s->s_shrink);
 	return s;
 }
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/include/linux/netfilter/x_tables.h linux-libre-4.16.5-gnu/include/linux/netfilter/x_tables.h
--- linux-libre-4.16.4-gnu/include/linux/netfilter/x_tables.h	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/include/linux/netfilter/x_tables.h	2018-04-26 05:00:39.000000000 -0400
@@ -301,6 +301,7 @@ int xt_data_to_user(void __user *dst, co
 
 void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
 				 struct xt_counters_info *info, bool compat);
+struct xt_counters *xt_counters_alloc(unsigned int counters);
 
 struct xt_table *xt_register_table(struct net *net,
 				   const struct xt_table *table,
@@ -509,7 +510,7 @@ void xt_compat_unlock(u_int8_t af);
 
 int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta);
 void xt_compat_flush_offsets(u_int8_t af);
-void xt_compat_init_offsets(u_int8_t af, unsigned int number);
+int xt_compat_init_offsets(u8 af, unsigned int number);
 int xt_compat_calc_jump(u_int8_t af, unsigned int offset);
 
 int xt_compat_match_offset(const struct xt_match *match);
diff -druNp --no-dereference linux-libre-4.16.4-gnu/include/linux/shrinker.h linux-libre-4.16.5-gnu/include/linux/shrinker.h
--- linux-libre-4.16.4-gnu/include/linux/shrinker.h	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/include/linux/shrinker.h	2018-04-26 05:00:39.000000000 -0400
@@ -75,6 +75,9 @@ struct shrinker {
 #define SHRINKER_NUMA_AWARE	(1 << 0)
 #define SHRINKER_MEMCG_AWARE	(1 << 1)
 
-extern int register_shrinker(struct shrinker *);
-extern void unregister_shrinker(struct shrinker *);
+extern int prealloc_shrinker(struct shrinker *shrinker);
+extern void register_shrinker_prepared(struct shrinker *shrinker);
+extern int register_shrinker(struct shrinker *shrinker);
+extern void unregister_shrinker(struct shrinker *shrinker);
+extern void free_prealloced_shrinker(struct shrinker *shrinker);
 #endif
diff -druNp --no-dereference linux-libre-4.16.4-gnu/kernel/events/callchain.c linux-libre-4.16.5-gnu/kernel/events/callchain.c
--- linux-libre-4.16.4-gnu/kernel/events/callchain.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/kernel/events/callchain.c	2018-04-26 05:00:39.000000000 -0400
@@ -119,19 +119,22 @@ int get_callchain_buffers(int event_max_
 		goto exit;
 	}
 
+	/*
+	 * If requesting per event more than the global cap,
+	 * return a different error to help userspace figure
+	 * this out.
+	 *
+	 * And also do it here so that we have &callchain_mutex held.
+	 */
+	if (event_max_stack > sysctl_perf_event_max_stack) {
+		err = -EOVERFLOW;
+		goto exit;
+	}
+
 	if (count > 1) {
 		/* If the allocation failed, give up */
 		if (!callchain_cpus_entries)
 			err = -ENOMEM;
-		/*
-		 * If requesting per event more than the global cap,
-		 * return a different error to help userspace figure
-		 * this out.
-		 *
-		 * And also do it here so that we have &callchain_mutex held.
-		 */
-		if (event_max_stack > sysctl_perf_event_max_stack)
-			err = -EOVERFLOW;
 		goto exit;
 	}
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/kernel/events/core.c linux-libre-4.16.5-gnu/kernel/events/core.c
--- linux-libre-4.16.4-gnu/kernel/events/core.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/kernel/events/core.c	2018-04-26 05:00:39.000000000 -0400
@@ -9730,9 +9730,9 @@ static int perf_copy_attr(struct perf_ev
 		 * __u16 sample size limit.
 		 */
 		if (attr->sample_stack_user >= USHRT_MAX)
-			ret = -EINVAL;
+			return -EINVAL;
 		else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
-			ret = -EINVAL;
+			return -EINVAL;
 	}
 
 	if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
diff -druNp --no-dereference linux-libre-4.16.4-gnu/kernel/time/alarmtimer.c linux-libre-4.16.5-gnu/kernel/time/alarmtimer.c
--- linux-libre-4.16.4-gnu/kernel/time/alarmtimer.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/kernel/time/alarmtimer.c	2018-04-26 05:00:39.000000000 -0400
@@ -326,6 +326,17 @@ static int alarmtimer_resume(struct devi
 }
 #endif
 
+static void
+__alarm_init(struct alarm *alarm, enum alarmtimer_type type,
+	     enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
+{
+	timerqueue_init(&alarm->node);
+	alarm->timer.function = alarmtimer_fired;
+	alarm->function = function;
+	alarm->type = type;
+	alarm->state = ALARMTIMER_STATE_INACTIVE;
+}
+
 /**
  * alarm_init - Initialize an alarm structure
  * @alarm: ptr to alarm to be initialized
@@ -335,13 +346,9 @@ static int alarmtimer_resume(struct devi
 void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
 		enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
 {
-	timerqueue_init(&alarm->node);
 	hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
-			HRTIMER_MODE_ABS);
-	alarm->timer.function = alarmtimer_fired;
-	alarm->function = function;
-	alarm->type = type;
-	alarm->state = ALARMTIMER_STATE_INACTIVE;
+		     HRTIMER_MODE_ABS);
+	__alarm_init(alarm, type, function);
 }
 EXPORT_SYMBOL_GPL(alarm_init);
 
@@ -719,6 +726,8 @@ static int alarmtimer_do_nsleep(struct a
 
 	__set_current_state(TASK_RUNNING);
 
+	destroy_hrtimer_on_stack(&alarm->timer);
+
 	if (!alarm->data)
 		return 0;
 
@@ -740,6 +749,15 @@ static int alarmtimer_do_nsleep(struct a
 	return -ERESTART_RESTARTBLOCK;
 }
 
+static void
+alarm_init_on_stack(struct alarm *alarm, enum alarmtimer_type type,
+		    enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
+{
+	hrtimer_init_on_stack(&alarm->timer, alarm_bases[type].base_clockid,
+			      HRTIMER_MODE_ABS);
+	__alarm_init(alarm, type, function);
+}
+
 /**
  * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  * @restart: ptr to restart block
@@ -752,7 +770,7 @@ static long __sched alarm_timer_nsleep_r
 	ktime_t exp = restart->nanosleep.expires;
 	struct alarm alarm;
 
-	alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
+	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
 
 	return alarmtimer_do_nsleep(&alarm, exp, type);
 }
@@ -784,7 +802,7 @@ static int alarm_timer_nsleep(const cloc
 	if (!capable(CAP_WAKE_ALARM))
 		return -EPERM;
 
-	alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
+	alarm_init_on_stack(&alarm, type, alarmtimer_nsleep_wakeup);
 
 	exp = timespec64_to_ktime(*tsreq);
 	/* Convert (if necessary) to absolute time */
diff -druNp --no-dereference linux-libre-4.16.4-gnu/kernel/time/posix-cpu-timers.c linux-libre-4.16.5-gnu/kernel/time/posix-cpu-timers.c
--- linux-libre-4.16.4-gnu/kernel/time/posix-cpu-timers.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/kernel/time/posix-cpu-timers.c	2018-04-26 05:00:39.000000000 -0400
@@ -1205,10 +1205,12 @@ void set_process_cpu_timer(struct task_s
 			   u64 *newval, u64 *oldval)
 {
 	u64 now;
+	int ret;
 
 	WARN_ON_ONCE(clock_idx == CPUCLOCK_SCHED);
+	ret = cpu_timer_sample_group(clock_idx, tsk, &now);
 
-	if (oldval && cpu_timer_sample_group(clock_idx, tsk, &now) != -EINVAL) {
+	if (oldval && ret != -EINVAL) {
 		/*
 		 * We are setting itimer. The *oldval is absolute and we update
 		 * it to be relative, *newval argument is relative and we update
diff -druNp --no-dereference linux-libre-4.16.4-gnu/Makefile linux-libre-4.16.5-gnu/Makefile
--- linux-libre-4.16.4-gnu/Makefile	2018-04-24 05:31:15.000000000 -0400
+++ linux-libre-4.16.5-gnu/Makefile	2018-04-26 08:57:49.000000000 -0400
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 VERSION = 4
 PATCHLEVEL = 16
-SUBLEVEL = 4
+SUBLEVEL = 5
 EXTRAVERSION = -gnu
 NAME = Fearless Coyote
 
diff -druNp --no-dereference linux-libre-4.16.4-gnu/mm/vmscan.c linux-libre-4.16.5-gnu/mm/vmscan.c
--- linux-libre-4.16.4-gnu/mm/vmscan.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/mm/vmscan.c	2018-04-26 05:00:39.000000000 -0400
@@ -258,7 +258,7 @@ unsigned long lruvec_lru_size(struct lru
 /*
  * Add a shrinker callback to be called from the vm.
  */
-int register_shrinker(struct shrinker *shrinker)
+int prealloc_shrinker(struct shrinker *shrinker)
 {
 	size_t size = sizeof(*shrinker->nr_deferred);
 
@@ -268,10 +268,29 @@ int register_shrinker(struct shrinker *s
 	shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
 	if (!shrinker->nr_deferred)
 		return -ENOMEM;
+	return 0;
+}
 
+void free_prealloced_shrinker(struct shrinker *shrinker)
+{
+	kfree(shrinker->nr_deferred);
+	shrinker->nr_deferred = NULL;
+}
+
+void register_shrinker_prepared(struct shrinker *shrinker)
+{
 	down_write(&shrinker_rwsem);
 	list_add_tail(&shrinker->list, &shrinker_list);
 	up_write(&shrinker_rwsem);
+}
+
+int register_shrinker(struct shrinker *shrinker)
+{
+	int err = prealloc_shrinker(shrinker);
+
+	if (err)
+		return err;
+	register_shrinker_prepared(shrinker);
 	return 0;
 }
 EXPORT_SYMBOL(register_shrinker);
diff -druNp --no-dereference linux-libre-4.16.4-gnu/net/bridge/netfilter/ebtables.c linux-libre-4.16.5-gnu/net/bridge/netfilter/ebtables.c
--- linux-libre-4.16.4-gnu/net/bridge/netfilter/ebtables.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/net/bridge/netfilter/ebtables.c	2018-04-26 05:00:39.000000000 -0400
@@ -1821,10 +1821,14 @@ static int compat_table_info(const struc
 {
 	unsigned int size = info->entries_size;
 	const void *entries = info->entries;
+	int ret;
 
 	newinfo->entries_size = size;
 
-	xt_compat_init_offsets(NFPROTO_BRIDGE, info->nentries);
+	ret = xt_compat_init_offsets(NFPROTO_BRIDGE, info->nentries);
+	if (ret)
+		return ret;
+
 	return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
 							entries, newinfo);
 }
@@ -2268,7 +2272,9 @@ static int compat_do_replace(struct net
 
 	xt_compat_lock(NFPROTO_BRIDGE);
 
-	xt_compat_init_offsets(NFPROTO_BRIDGE, tmp.nentries);
+	ret = xt_compat_init_offsets(NFPROTO_BRIDGE, tmp.nentries);
+	if (ret < 0)
+		goto out_unlock;
 	ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
 	if (ret < 0)
 		goto out_unlock;
diff -druNp --no-dereference linux-libre-4.16.4-gnu/net/ipv4/netfilter/arp_tables.c linux-libre-4.16.5-gnu/net/ipv4/netfilter/arp_tables.c
--- linux-libre-4.16.4-gnu/net/ipv4/netfilter/arp_tables.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/net/ipv4/netfilter/arp_tables.c	2018-04-26 05:00:39.000000000 -0400
@@ -781,7 +781,9 @@ static int compat_table_info(const struc
 	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
 	newinfo->initial_entries = 0;
 	loc_cpu_entry = info->entries;
-	xt_compat_init_offsets(NFPROTO_ARP, info->number);
+	ret = xt_compat_init_offsets(NFPROTO_ARP, info->number);
+	if (ret)
+		return ret;
 	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
 		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
 		if (ret != 0)
@@ -895,7 +897,7 @@ static int __do_replace(struct net *net,
 	struct arpt_entry *iter;
 
 	ret = 0;
-	counters = vzalloc(num_counters * sizeof(struct xt_counters));
+	counters = xt_counters_alloc(num_counters);
 	if (!counters) {
 		ret = -ENOMEM;
 		goto out;
@@ -1167,7 +1169,7 @@ static int translate_compat_table(struct
 	struct compat_arpt_entry *iter0;
 	struct arpt_replace repl;
 	unsigned int size;
-	int ret = 0;
+	int ret;
 
 	info = *pinfo;
 	entry0 = *pentry0;
@@ -1176,7 +1178,9 @@ static int translate_compat_table(struct
 
 	j = 0;
 	xt_compat_lock(NFPROTO_ARP);
-	xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
+	ret = xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
+	if (ret)
+		goto out_unlock;
 	/* Walk through entries, checking offsets. */
 	xt_entry_foreach(iter0, entry0, compatr->size) {
 		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
diff -druNp --no-dereference linux-libre-4.16.4-gnu/net/ipv4/netfilter/ip_tables.c linux-libre-4.16.5-gnu/net/ipv4/netfilter/ip_tables.c
--- linux-libre-4.16.4-gnu/net/ipv4/netfilter/ip_tables.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/net/ipv4/netfilter/ip_tables.c	2018-04-26 05:00:39.000000000 -0400
@@ -945,7 +945,9 @@ static int compat_table_info(const struc
 	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
 	newinfo->initial_entries = 0;
 	loc_cpu_entry = info->entries;
-	xt_compat_init_offsets(AF_INET, info->number);
+	ret = xt_compat_init_offsets(AF_INET, info->number);
+	if (ret)
+		return ret;
 	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
 		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
 		if (ret != 0)
@@ -1057,7 +1059,7 @@ __do_replace(struct net *net, const char
 	struct ipt_entry *iter;
 
 	ret = 0;
-	counters = vzalloc(num_counters * sizeof(struct xt_counters));
+	counters = xt_counters_alloc(num_counters);
 	if (!counters) {
 		ret = -ENOMEM;
 		goto out;
@@ -1418,7 +1420,9 @@ translate_compat_table(struct net *net,
 
 	j = 0;
 	xt_compat_lock(AF_INET);
-	xt_compat_init_offsets(AF_INET, compatr->num_entries);
+	ret = xt_compat_init_offsets(AF_INET, compatr->num_entries);
+	if (ret)
+		goto out_unlock;
 	/* Walk through entries, checking offsets. */
 	xt_entry_foreach(iter0, entry0, compatr->size) {
 		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
diff -druNp --no-dereference linux-libre-4.16.4-gnu/net/ipv6/netfilter/ip6_tables.c linux-libre-4.16.5-gnu/net/ipv6/netfilter/ip6_tables.c
--- linux-libre-4.16.4-gnu/net/ipv6/netfilter/ip6_tables.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/net/ipv6/netfilter/ip6_tables.c	2018-04-26 05:00:39.000000000 -0400
@@ -962,7 +962,9 @@ static int compat_table_info(const struc
 	memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
 	newinfo->initial_entries = 0;
 	loc_cpu_entry = info->entries;
-	xt_compat_init_offsets(AF_INET6, info->number);
+	ret = xt_compat_init_offsets(AF_INET6, info->number);
+	if (ret)
+		return ret;
 	xt_entry_foreach(iter, loc_cpu_entry, info->size) {
 		ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
 		if (ret != 0)
@@ -1075,7 +1077,7 @@ __do_replace(struct net *net, const char
 	struct ip6t_entry *iter;
 
 	ret = 0;
-	counters = vzalloc(num_counters * sizeof(struct xt_counters));
+	counters = xt_counters_alloc(num_counters);
 	if (!counters) {
 		ret = -ENOMEM;
 		goto out;
@@ -1425,7 +1427,7 @@ translate_compat_table(struct net *net,
 	struct compat_ip6t_entry *iter0;
 	struct ip6t_replace repl;
 	unsigned int size;
-	int ret = 0;
+	int ret;
 
 	info = *pinfo;
 	entry0 = *pentry0;
@@ -1434,7 +1436,9 @@ translate_compat_table(struct net *net,
 
 	j = 0;
 	xt_compat_lock(AF_INET6);
-	xt_compat_init_offsets(AF_INET6, compatr->num_entries);
+	ret = xt_compat_init_offsets(AF_INET6, compatr->num_entries);
+	if (ret)
+		goto out_unlock;
 	/* Walk through entries, checking offsets. */
 	xt_entry_foreach(iter0, entry0, compatr->size) {
 		ret = check_compat_entry_size_and_hooks(iter0, info, &size,
diff -druNp --no-dereference linux-libre-4.16.4-gnu/net/netfilter/x_tables.c linux-libre-4.16.5-gnu/net/netfilter/x_tables.c
--- linux-libre-4.16.4-gnu/net/netfilter/x_tables.c	2018-04-24 03:43:11.000000000 -0400
+++ linux-libre-4.16.5-gnu/net/netfilter/x_tables.c	2018-04-26 05:00:39.000000000 -0400
@@ -40,6 +40,7 @@ MODULE_AUTHOR("Harald Welte <laforge@net
 MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
 
 #define XT_PCPU_BLOCK_SIZE 4096
+#define XT_MAX_TABLE_SIZE	(512 * 1024 * 1024)
 
 struct compat_delta {
 	unsigned int offset; /* offset in kernel */
@@ -553,14 +554,8 @@ int xt_compat_add_offset(u_int8_t af, un
 {
 	struct xt_af *xp = &xt[af];
 
-	if (!xp->compat_tab) {
-		if (!xp->number)
-			return -EINVAL;
-		xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
-		if (!xp->compat_tab)
-			return -ENOMEM;
-		xp->cur = 0;
-	}
+	if (WARN_ON(!xp->compat_tab))
+		return -ENOMEM;
 
 	if (xp->cur >= xp->number)
 		return -EINVAL;
@@ -603,10 +598,28 @@ int xt_compat_calc_jump(u_int8_t af, uns
 }
 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
 
-void xt_compat_init_offsets(u_int8_t af, unsigned int number)
+int xt_compat_init_offsets(u8 af, unsigned int number)
 {
+	size_t mem;
+
+	if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
+		return -EINVAL;
+
+	if (WARN_ON(xt[af].compat_tab))
+		return -EINVAL;
+
+	mem = sizeof(struct compat_delta) * number;
+	if (mem > XT_MAX_TABLE_SIZE)
+		return -ENOMEM;
+
+	xt[af].compat_tab = vmalloc(mem);
+	if (!xt[af].compat_tab)
+		return -ENOMEM;
+
 	xt[af].number = number;
 	xt[af].cur = 0;
+
+	return 0;
 }
 EXPORT_SYMBOL(xt_compat_init_offsets);
 
@@ -805,6 +818,9 @@ EXPORT_SYMBOL(xt_check_entry_offsets);
  */
 unsigned int *xt_alloc_entry_offsets(unsigned int size)
 {
+	if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
+		return NULL;
+
 	return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
 
 }
@@ -1029,7 +1045,7 @@ struct xt_table_info *xt_alloc_table_inf
 	struct xt_table_info *info = NULL;
 	size_t sz = sizeof(*info) + size;
 
-	if (sz < sizeof(*info))
+	if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
 		return NULL;
 
 	/* __GFP_NORETRY is not fully supported by kvmalloc but it should
@@ -1198,6 +1214,21 @@ static int xt_jumpstack_alloc(struct xt_
 	return 0;
 }
 
+struct xt_counters *xt_counters_alloc(unsigned int counters)
+{
+	struct xt_counters *mem;
+
+	if (counters == 0 || counters > INT_MAX / sizeof(*mem))
+		return NULL;
+
+	counters *= sizeof(*mem);
+	if (counters > XT_MAX_TABLE_SIZE)
+		return NULL;
+
+	return vzalloc(counters);
+}
+EXPORT_SYMBOL(xt_counters_alloc);
+
 struct xt_table_info *
 xt_replace_table(struct xt_table *table,
 	      unsigned int num_counters,
