Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1973652ybh; Tue, 14 Jul 2020 12:02:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyP6ux7IhNuOwjNzmcxF7IU9Wfx+rBXGDGhN5pUKYPyca6ae1kj30maSbeOgFpnDccMGxu7 X-Received: by 2002:a17:906:9408:: with SMTP id q8mr5812674ejx.496.1594753348317; Tue, 14 Jul 2020 12:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594753348; cv=none; d=google.com; s=arc-20160816; b=j+y80W5rwDP6IvgL39xrrFLXC6VuUGZKd2k0S+fSrqqhxIpWsyqd5EbtkebWbFz9G2 nUVNC1AUupAxuzFFn8w9kcgPheQvxZGykGab1gtbgjg2ckRVH6fQ7X+kJxhG2ZcJJR9B Wfl3HxXrGbyJk4kOpH8kDIEsOHJ+jIYQ6C1K31U1Twb74q1CwiewzyCV2LJlO+ZYO2iT EB2+EKa989MHDzjbJquI8+HTDv47LNOFhcttfSFrVa8eqq+KYFJ2CQ7XOoWnGHdNIvQr VGEj+lU70P3M6FuDVd/XoLV4yPrPW6sK2gX3YdMED+Hvpx42gYOsdFRQm+CtqRI4DqUQ JVOQ== 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=l38gaJjon4CYv/yfvwqdCp5YyTcFdAmTeecVotxVhhQ=; b=k1LuuNBDhydtR0J3AZ03MRL3+43ai9xsUcJ91lUvtJGkr/qFigQriWqRY/LY9sw4RV xqgMyGD/fZeMrkBxVpOa//02kbDXhxZ8Xjz/yp2MiNzIiY6ZSPEPVl5o4Ict3EjnCwas vV4HreuS+IFrvnU5pWsEi2i3E+7f3CsI/1qWMqtDidV/0DxMGe19WHhJdGqDWS6nyydQ 9m1lzql515z3Wfd+D6cvtxIzQdxQGeZzIjJHqiS7x1VFcMgcCwvbpSnAfgKMc/sbLGYR 5MS2ZRLPviVaa0Plvo2wLiV2iWYvBYWHVABf927j5Q+PTFyFFJRHA13DM2f9t85HOErm eVQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="K2VbF/Qr"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ca17si11893152ejb.533.2020.07.14.12.02.04; Tue, 14 Jul 2020 12:02:28 -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=@kernel.org header.s=default header.b="K2VbF/Qr"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730757AbgGNS7v (ORCPT + 99 others); Tue, 14 Jul 2020 14:59:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:58448 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731298AbgGNS7r (ORCPT ); Tue, 14 Jul 2020 14:59:47 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 D8B5122507; Tue, 14 Jul 2020 18:59:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594753186; bh=Svt3QHf5poWXDrA1Oyizpu5gksOyZccShXbHmiBr1wU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K2VbF/Qrj08GkW/IljDaeO3YNbtUwh2N6hVSduDG1k9vqsddbGoGcRQ0rhgMxyGT7 qk8j9Clz7GwC6HidA6TiTeM278OzkVnN1bA/dQ9xkblTqt+DRY16+cUUY0o63hE4Hf Va7STUNHG9BLyuXkxOxu/qUC8whdxojpvfgzYHTE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jens Axboe Subject: [PATCH 5.7 125/166] io_uring: account user memory freed when exit has been queued Date: Tue, 14 Jul 2020 20:44:50 +0200 Message-Id: <20200714184121.818351741@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714184115.844176932@linuxfoundation.org> References: <20200714184115.844176932@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 commit 309fc03a3284af62eb6082fb60327045a1dabf57 upstream. We currently account the memory after the exit work has been run, but that leaves a gap where a process has closed its ring and until the memory has been accounted as freed. If the memlocked ulimit is borderline, then that can introduce spurious setup errors returning -ENOMEM because the free work hasn't been run yet. Account this as freed when we close the ring, as not to expose a tiny gap where setting up a new ring can fail. Fixes: 85faa7b8346e ("io_uring: punt final io_ring_ctx wait-and-free to workqueue") Cc: stable@vger.kernel.org # v5.7 Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -7402,9 +7402,6 @@ static void io_ring_ctx_free(struct io_r io_mem_free(ctx->sq_sqes); percpu_ref_exit(&ctx->refs); - if (ctx->account_mem) - io_unaccount_mem(ctx->user, - ring_pages(ctx->sq_entries, ctx->cq_entries)); free_uid(ctx->user); put_cred(ctx->creds); kfree(ctx->completions); @@ -7500,6 +7497,16 @@ static void io_ring_ctx_wait_and_kill(st if (ctx->rings) io_cqring_overflow_flush(ctx, true); idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx); + + /* + * Do this upfront, so we won't have a grace period where the ring + * is closed but resources aren't reaped yet. This can cause + * spurious failure in setting up a new ring. + */ + if (ctx->account_mem) + io_unaccount_mem(ctx->user, + ring_pages(ctx->sq_entries, ctx->cq_entries)); + INIT_WORK(&ctx->exit_work, io_ring_exit_work); queue_work(system_wq, &ctx->exit_work); }