Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp2254513ybg; Sun, 27 Oct 2019 14:29:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqw+hWEssQHypRdKtlJOPa6Hcfh9m0qQB1+cMO3t85DsDWIBoKeQ7NtrDOMbcZTvzGQ+kWD2 X-Received: by 2002:a17:906:ce30:: with SMTP id sd16mr4540793ejb.238.1572211763692; Sun, 27 Oct 2019 14:29:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572211763; cv=none; d=google.com; s=arc-20160816; b=s7t30hu8am2TaguF0qwCu+ttYLr8gnBic5p0WIK7EZ2cPnwHNyXGXEAHQX/7n+m8RK sM7mHKsfQa54v+5F7VCPV/9gpoWdKyTFkMQt+uJSP/DZOM7s3ENA+xyKiIdgLnSr3dyR +JAs4d+oVg8idqPhiDS0FGkG7mt4wQ3EjSoFcBep/pP99NvT3tUKjchOY9n1es2Lmgko EZ14qeW1YMno2bpN+6FhCl0plN7zV4W9JEJX6nXkQNz12sStPH+ikzrnHlKxJZcNcbR3 PO8j6DHZegYDxtg8QSug2LCHHVlOu5Mu67MISkbmknFMg+Hl19++Zpob6u+4rJxKP58s KLSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LgLFqcBkc6ok1nF/Q7CQkvv/udFvwLl071StH2LZquU=; b=b2JRP3WvTs94i4U0oYh8yiYKhsv7trSP7RNDxmXBEz7QSfAwWjn57Z6tD6OCR3kn6S Tu3wINNTiDxseHPY9GYpYgtO+zbC7CN6zEQuq/rfUBsW7mf6HwBJ90b9rGmwhOeuFLk+ 367cXput+7CRLb1vC46kKGoh8/juSE7W8CfDqfezN26Fj7mElpDIPCsI9MdMI5ne2Q2O /5gkXhGZboNJXCG0q1mRdO/98EpIQAABjSQSXSiQsM3RA4To90J6ZewNRzwXLG+UsPcZ Qz1lyrG1ZGee7/apii5zt2bKz+veU6myrNh8wKRMs5/Y3A250ftg/o2R8JwDQtLo97E4 OPjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fqKZ2UTj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y61si6658059ede.24.2019.10.27.14.28.59; Sun, 27 Oct 2019 14:29:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fqKZ2UTj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732768AbfJ0VZe (ORCPT + 99 others); Sun, 27 Oct 2019 17:25:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:47430 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732136AbfJ0VZc (ORCPT ); Sun, 27 Oct 2019 17:25:32 -0400 Received: from localhost (100.50.158.77.rev.sfr.net [77.158.50.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3F47C21783; Sun, 27 Oct 2019 21:25:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572211531; bh=C/mSX7vY8N1eOip8zq49LLoWltaTLCDefQ7EnzLpoiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fqKZ2UTjobWImgtTrpSGXxdcqwkjd+0daEl36Fivifi4AVQbplwdy5GrOvsvXMn5p XWNi9I40X5kPIqIwZALtePv9RIKXLScfJaxpDkp2rKlzHSN/8CzDB+r+ggc9T5dMku jLE+6rhzEivh986/oSsvWDuqxZVzC4Z2sxiV0u3U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Begunkov , Jens Axboe , Sasha Levin Subject: [PATCH 5.3 140/197] io_uring: used cached copies of sq->dropped and cq->overflow Date: Sun, 27 Oct 2019 22:00:58 +0100 Message-Id: <20191027203359.264971269@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191027203351.684916567@linuxfoundation.org> References: <20191027203351.684916567@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jens Axboe [ Upstream commit 498ccd9eda49117c34e0041563d0da6ac40e52b8 ] We currently use the ring values directly, but that can lead to issues if the application is malicious and changes these values on our behalf. Created in-kernel cached versions of them, and just overwrite the user side when we update them. This is similar to how we treat the sq/cq ring tail/head updates. Reported-by: Pavel Begunkov Reviewed-by: Pavel Begunkov Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- fs/io_uring.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index d447f43d64a24..3c8906494a8e1 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -221,6 +221,7 @@ struct io_ring_ctx { unsigned sq_entries; unsigned sq_mask; unsigned sq_thread_idle; + unsigned cached_sq_dropped; struct io_uring_sqe *sq_sqes; struct list_head defer_list; @@ -237,6 +238,7 @@ struct io_ring_ctx { /* CQ ring */ struct io_cq_ring *cq_ring; unsigned cached_cq_tail; + atomic_t cached_cq_overflow; unsigned cq_entries; unsigned cq_mask; struct wait_queue_head cq_wait; @@ -431,7 +433,8 @@ static inline bool io_sequence_defer(struct io_ring_ctx *ctx, if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN) return false; - return req->sequence != ctx->cached_cq_tail + ctx->sq_ring->dropped; + return req->sequence != ctx->cached_cq_tail + ctx->sq_ring->dropped + + atomic_read(&ctx->cached_cq_overflow); } static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) @@ -511,9 +514,8 @@ static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, WRITE_ONCE(cqe->res, res); WRITE_ONCE(cqe->flags, 0); } else { - unsigned overflow = READ_ONCE(ctx->cq_ring->overflow); - - WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1); + WRITE_ONCE(ctx->cq_ring->overflow, + atomic_inc_return(&ctx->cached_cq_overflow)); } } @@ -2272,7 +2274,8 @@ static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) /* drop invalid entries */ ctx->cached_sq_head++; - ring->dropped++; + ctx->cached_sq_dropped++; + WRITE_ONCE(ring->dropped, ctx->cached_sq_dropped); return false; } -- 2.20.1