From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4427C388F9 for ; Sat, 31 Oct 2020 11:48:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92FFF2065D for ; Sat, 31 Oct 2020 11:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604144931; bh=0f+liBFOx5aRkrtaNJZpuxJqaIdXm7vvq9XZUoYNoyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=YhcBigBxogeatpcIt5bS39YFdZnTwtxXRxaE2BVWaIoJUngOzT22UtiluxYcuTNDM QYuIXkncFOc0tjDGeN/dkCcupluT12oLjFqQZSR1ykvx2cMo8XAPeQG9KaLd9fj5De 5nBCIhKzrbcLPGPaKXSzk3PDXs5/YPPIVS1HHjXU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728054AbgJaLsv (ORCPT ); Sat, 31 Oct 2020 07:48:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:43580 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727186AbgJaLnd (ORCPT ); Sat, 31 Oct 2020 07:43:33 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 A1AE020731; Sat, 31 Oct 2020 11:43:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604144613; bh=0f+liBFOx5aRkrtaNJZpuxJqaIdXm7vvq9XZUoYNoyI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rWJUtd+E/S+QRBZsCRCX8CW6T8cQrsd2xzJO1yaOA+4jRpXqnPHUnkfSunjKVbiqB NbsquEBexVPoqk4NI2opJNRK46zic2vG/QQEvPQZ7cLTjdS/ygbEaDWLMfqRKc8gB4 Zyp1G5qlqK+4DcXNhdF4+tS2XZfZkA2FgQmo56zw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Matthew Wilcox (Oracle)" , Jens Axboe Subject: [PATCH 5.9 15/74] io_uring: Fix XArray usage in io_uring_add_task_file Date: Sat, 31 Oct 2020 12:35:57 +0100 Message-Id: <20201031113500.781089648@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201031113500.031279088@linuxfoundation.org> References: <20201031113500.031279088@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Matthew Wilcox (Oracle)" commit 236434c3438c4da3dfbd6aeeab807577b85e951a upstream. The xas_store() wasn't paired with an xas_nomem() loop, so if it couldn't allocate memory using GFP_NOWAIT, it would leak the reference to the file descriptor. Also the node pointed to by the xas could be freed between the call to xas_load() under the rcu_read_lock() and the acquisition of the xa_lock. It's easier to just use the normal xa_load/xa_store interface here. Signed-off-by: Matthew Wilcox (Oracle) [axboe: fix missing assign after alloc, cur_uring -> tctx rename] Signed-off-by: Jens Axboe Signed-off-by: Greg Kroah-Hartman --- fs/io_uring.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -8336,27 +8336,24 @@ static void io_uring_cancel_task_request */ static int io_uring_add_task_file(struct file *file) { - if (unlikely(!current->io_uring)) { + struct io_uring_task *tctx = current->io_uring; + + if (unlikely(!tctx)) { int ret; ret = io_uring_alloc_task_context(current); if (unlikely(ret)) return ret; + tctx = current->io_uring; } - if (current->io_uring->last != file) { - XA_STATE(xas, ¤t->io_uring->xa, (unsigned long) file); - void *old; + if (tctx->last != file) { + void *old = xa_load(&tctx->xa, (unsigned long)file); - rcu_read_lock(); - old = xas_load(&xas); - if (old != file) { + if (!old) { get_file(file); - xas_lock(&xas); - xas_store(&xas, file); - xas_unlock(&xas); + xa_store(&tctx->xa, (unsigned long)file, file, GFP_KERNEL); } - rcu_read_unlock(); - current->io_uring->last = file; + tctx->last = file; } return 0;