Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1441305pxa; Thu, 13 Aug 2020 08:35:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYOPYN04/Ac+7MqEef5kyDonM//hPav6DI1YKiYw5ZyaLwnNdU5zN3QYLsS4GelXRqUklh X-Received: by 2002:aa7:d9d7:: with SMTP id v23mr5497737eds.112.1597332947845; Thu, 13 Aug 2020 08:35:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597332947; cv=none; d=google.com; s=arc-20160816; b=TPErs4Cs7AGjbTZ/6RxgRpXQEXEnDBIr7xfvsCd2tOVqKF1n3kjr+evX6V8fclcY8l 3Y3h0MDj5K/bi15dEnaV1gYxAQhDkj9Eg4GoLV3IlNkmOd+eDVBitVCRoonmQfM3Qz3X qc7Qrum+7hCZr99ZS2G1TyGlZvsQyFQglvcQ/nNBnkKzHpZzxGTl1BKSBagMFBYX9Xkq Lp11IWSR10X8ept1qqPavHIKQN2qvt4K4cfDf+nDosH//2bWV97zjTcGCDIh1yvd77CT 68QXGfvXT6+zzku0DeuI5IDCDBd/s47HDOP3AoSSVX2gncquG6nLEiugcf4eW+lSreQW Sv2g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xzivr37W6Sz/OFkat8WISQrphMPdi2PlV4uzOfubSgg=; b=qV7dQBVdO5+F/8ghepdO12Vo1IDFJ/Dw1VitvgfntiRJUpE4b7eCB2y5Wq4APHVZz6 GILxRLV0O6KWsg60KBkFR4EVS4FFZ3MTdt1ZiWo9Ww3B7c/X8mot91/U8QuS0wZbiR4v 5v239rBTWJBBeBBz7NUWjMo3KHQYKpIL8eRe1Wql7eXOcnLHIypngJWYkIVCWaYIeWI0 o/lXqYKdSGnla0KgrfHdyhP6xVAQ+BFu7aDSx9PV4e/pnUjXwWbZFMeGBngLQ1BQ3mEL tmApvsiwn716mvPzKcl0KNqJo2Jx4sq9urLga/oTv102VcMfNbJ99FxWJoQY9IA1tj46 ybUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gGvfwBtk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lx7si3485135ejb.752.2020.08.13.08.35.24; Thu, 13 Aug 2020 08:35:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gGvfwBtk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726851AbgHMPeP (ORCPT + 99 others); Thu, 13 Aug 2020 11:34:15 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:59879 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726798AbgHMPeO (ORCPT ); Thu, 13 Aug 2020 11:34:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1597332852; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xzivr37W6Sz/OFkat8WISQrphMPdi2PlV4uzOfubSgg=; b=gGvfwBtkh5RtP18LZlcKA9J+cw7xGLCj5l+bfM0324WOmoi8NqG3kv8oxRLZ+MaLkSdlYo hrul+0KRTn5d4EmXhB0mpW6XbB6em9FEFsya3yTDU/a1qfEQLkHfjzNShrDf1QgkiGG8AZ 3d1n8UU1aBoaZ4c8qoM9b9HTsyrXVhw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-391-14sqnvJ0PGe7-Ac8Ox3DLQ-1; Thu, 13 Aug 2020 11:34:10 -0400 X-MC-Unique: 14sqnvJ0PGe7-Ac8Ox3DLQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C53A1807321; Thu, 13 Aug 2020 15:34:08 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-113-140.ams2.redhat.com [10.36.113.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6443C60C04; Thu, 13 Aug 2020 15:33:49 +0000 (UTC) From: Stefano Garzarella To: Jens Axboe Cc: Christian Brauner , Jann Horn , Jeff Moyer , linux-fsdevel@vger.kernel.org, Sargun Dhillon , Kees Cook , Alexander Viro , Kernel Hardening , Stefan Hajnoczi , linux-kernel@vger.kernel.org, Aleksa Sarai , io-uring@vger.kernel.org Subject: [PATCH v4 3/3] io_uring: allow disabling rings during the creation Date: Thu, 13 Aug 2020 17:32:54 +0200 Message-Id: <20200813153254.93731-4-sgarzare@redhat.com> In-Reply-To: <20200813153254.93731-1-sgarzare@redhat.com> References: <20200813153254.93731-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a new IORING_SETUP_R_DISABLED flag to start the rings disabled, allowing the user to register restrictions, buffers, files, before to start processing SQEs. When IORING_SETUP_R_DISABLED is set, SQE are not processed and SQPOLL kthread is not started. The restrictions registration are allowed only when the rings are disable to prevent concurrency issue while processing SQEs. The rings can be enabled using IORING_REGISTER_ENABLE_RINGS opcode with io_uring_register(2). Suggested-by: Jens Axboe Signed-off-by: Stefano Garzarella --- v4: - fixed io_uring_enter() exit path when ring is disabled v3: - enabled restrictions only when the rings start RFC v2: - removed return value of io_sq_offload_start() --- fs/io_uring.c | 52 ++++++++++++++++++++++++++++++----- include/uapi/linux/io_uring.h | 2 ++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index cb365e6e0af7..09fedc380a41 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -226,6 +226,7 @@ struct io_restriction { DECLARE_BITMAP(sqe_op, IORING_OP_LAST); u8 sqe_flags_allowed; u8 sqe_flags_required; + bool registered; }; struct io_ring_ctx { @@ -7420,8 +7421,8 @@ static int io_init_wq_offload(struct io_ring_ctx *ctx, return ret; } -static int io_sq_offload_start(struct io_ring_ctx *ctx, - struct io_uring_params *p) +static int io_sq_offload_create(struct io_ring_ctx *ctx, + struct io_uring_params *p) { int ret; @@ -7458,7 +7459,6 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, ctx->sqo_thread = NULL; goto err; } - wake_up_process(ctx->sqo_thread); } else if (p->flags & IORING_SETUP_SQ_AFF) { /* Can't have SQ_AFF without SQPOLL */ ret = -EINVAL; @@ -7479,6 +7479,12 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx, return ret; } +static void io_sq_offload_start(struct io_ring_ctx *ctx) +{ + if ((ctx->flags & IORING_SETUP_SQPOLL) && ctx->sqo_thread) + wake_up_process(ctx->sqo_thread); +} + static inline void __io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) { @@ -8218,6 +8224,9 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, if (!percpu_ref_tryget(&ctx->refs)) goto out_fput; + if (ctx->flags & IORING_SETUP_R_DISABLED) + goto out_fput; + /* * For SQ polling, the thread will do all submissions and completions. * Just return the requested submit count, and wake the thread if @@ -8532,10 +8541,13 @@ static int io_uring_create(unsigned entries, struct io_uring_params *p, if (ret) goto err; - ret = io_sq_offload_start(ctx, p); + ret = io_sq_offload_create(ctx, p); if (ret) goto err; + if (!(p->flags & IORING_SETUP_R_DISABLED)) + io_sq_offload_start(ctx); + memset(&p->sq_off, 0, sizeof(p->sq_off)); p->sq_off.head = offsetof(struct io_rings, sq.head); p->sq_off.tail = offsetof(struct io_rings, sq.tail); @@ -8598,7 +8610,8 @@ static long io_uring_setup(u32 entries, struct io_uring_params __user *params) if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE | - IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ)) + IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ | + IORING_SETUP_R_DISABLED)) return -EINVAL; return io_uring_create(entries, &p, params); @@ -8681,8 +8694,12 @@ static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, size_t size; int i, ret; + /* Restrictions allowed only if rings started disabled */ + if (!(ctx->flags & IORING_SETUP_R_DISABLED)) + return -EINVAL; + /* We allow only a single restrictions registration */ - if (ctx->restricted) + if (ctx->restrictions.registered) return -EBUSY; if (!arg || nr_args > IORING_MAX_RESTRICTIONS) @@ -8732,7 +8749,7 @@ static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, } } - ctx->restricted = 1; + ctx->restrictions.registered = true; ret = 0; out: @@ -8744,6 +8761,21 @@ static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg, return ret; } +static int io_register_enable_rings(struct io_ring_ctx *ctx) +{ + if (!(ctx->flags & IORING_SETUP_R_DISABLED)) + return -EINVAL; + + if (ctx->restrictions.registered) + ctx->restricted = 1; + + ctx->flags &= ~IORING_SETUP_R_DISABLED; + + io_sq_offload_start(ctx); + + return 0; +} + static bool io_register_op_must_quiesce(int op) { switch (op) { @@ -8865,6 +8897,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, break; ret = io_unregister_personality(ctx, nr_args); break; + case IORING_REGISTER_ENABLE_RINGS: + ret = -EINVAL; + if (arg || nr_args) + break; + ret = io_register_enable_rings(ctx); + break; case IORING_REGISTER_RESTRICTIONS: ret = io_register_restrictions(ctx, arg, nr_args); break; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index be54bc3cf173..ddb30513e027 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -95,6 +95,7 @@ enum { #define IORING_SETUP_CQSIZE (1U << 3) /* app defines CQ size */ #define IORING_SETUP_CLAMP (1U << 4) /* clamp SQ/CQ ring sizes */ #define IORING_SETUP_ATTACH_WQ (1U << 5) /* attach to existing wq */ +#define IORING_SETUP_R_DISABLED (1U << 6) /* start with ring disabled */ enum { IORING_OP_NOP, @@ -268,6 +269,7 @@ enum { IORING_REGISTER_PERSONALITY, IORING_UNREGISTER_PERSONALITY, IORING_REGISTER_RESTRICTIONS, + IORING_REGISTER_ENABLE_RINGS, /* this goes last */ IORING_REGISTER_LAST -- 2.26.2