Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86357b19 authored by Andy Grover's avatar Andy Grover Committed by David S. Miller
Browse files

RDS: Fix potential race around rds_i[bw]_allocation



"At rds_ib_recv_refill_one(), it first executes atomic_read(&rds_ib_allocation)
for if-condition checking,

and then executes atomic_inc(&rds_ib_allocation) if the condition was
not satisfied.

However, if any other code which updates rds_ib_allocation executes
between these two atomic operation executions,
it seems that it may result race condition. (especially when
rds_ib_allocation + 1 == rds_ib_sysctl_max_recv_allocation)"

This patch fixes this by using atomic_inc_unless to eliminate the
possibility of allocating more than rds_ib_sysctl_max_recv_allocation
and then decrementing the count if the allocation fails. It also
makes an identical change to the iwarp transport.

Reported-by: default avatarShin Hong <hongshin@gmail.com>
Signed-off-by: default avatarAndy Grover <andy.grover@oracle.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 244546f0
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -143,15 +143,16 @@ static int rds_ib_recv_refill_one(struct rds_connection *conn,
	int ret = -ENOMEM;
	int ret = -ENOMEM;


	if (recv->r_ibinc == NULL) {
	if (recv->r_ibinc == NULL) {
		if (atomic_read(&rds_ib_allocation) >= rds_ib_sysctl_max_recv_allocation) {
		if (!atomic_add_unless(&rds_ib_allocation, 1, rds_ib_sysctl_max_recv_allocation)) {
			rds_ib_stats_inc(s_ib_rx_alloc_limit);
			rds_ib_stats_inc(s_ib_rx_alloc_limit);
			goto out;
			goto out;
		}
		}
		recv->r_ibinc = kmem_cache_alloc(rds_ib_incoming_slab,
		recv->r_ibinc = kmem_cache_alloc(rds_ib_incoming_slab,
						 kptr_gfp);
						 kptr_gfp);
		if (recv->r_ibinc == NULL)
		if (recv->r_ibinc == NULL) {
			atomic_dec(&rds_ib_allocation);
			goto out;
			goto out;
		atomic_inc(&rds_ib_allocation);
		}
		INIT_LIST_HEAD(&recv->r_ibinc->ii_frags);
		INIT_LIST_HEAD(&recv->r_ibinc->ii_frags);
		rds_inc_init(&recv->r_ibinc->ii_inc, conn, conn->c_faddr);
		rds_inc_init(&recv->r_ibinc->ii_inc, conn, conn->c_faddr);
	}
	}
+4 −3
Original line number Original line Diff line number Diff line
@@ -143,15 +143,16 @@ static int rds_iw_recv_refill_one(struct rds_connection *conn,
	int ret = -ENOMEM;
	int ret = -ENOMEM;


	if (recv->r_iwinc == NULL) {
	if (recv->r_iwinc == NULL) {
		if (atomic_read(&rds_iw_allocation) >= rds_iw_sysctl_max_recv_allocation) {
		if (!atomic_add_unless(&rds_iw_allocation, 1, rds_iw_sysctl_max_recv_allocation)) {
			rds_iw_stats_inc(s_iw_rx_alloc_limit);
			rds_iw_stats_inc(s_iw_rx_alloc_limit);
			goto out;
			goto out;
		}
		}
		recv->r_iwinc = kmem_cache_alloc(rds_iw_incoming_slab,
		recv->r_iwinc = kmem_cache_alloc(rds_iw_incoming_slab,
						 kptr_gfp);
						 kptr_gfp);
		if (recv->r_iwinc == NULL)
		if (recv->r_iwinc == NULL) {
			atomic_dec(&rds_iw_allocation);
			goto out;
			goto out;
		atomic_inc(&rds_iw_allocation);
		}
		INIT_LIST_HEAD(&recv->r_iwinc->ii_frags);
		INIT_LIST_HEAD(&recv->r_iwinc->ii_frags);
		rds_inc_init(&recv->r_iwinc->ii_inc, conn, conn->c_faddr);
		rds_inc_init(&recv->r_iwinc->ii_inc, conn, conn->c_faddr);
	}
	}