Received: by 10.213.65.68 with SMTP id h4csp1783889imn; Mon, 19 Mar 2018 13:10:10 -0700 (PDT) X-Google-Smtp-Source: AG47ELsn1AEqQJEFodCrcBWwvEZVlvJ2fXaUNirxoqY7IJrxAbuXgh+FzaKGvMnDcK5/6qTPK7EH X-Received: by 2002:a17:902:f81:: with SMTP id 1-v6mr13670898plz.265.1521490210190; Mon, 19 Mar 2018 13:10:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521490210; cv=none; d=google.com; s=arc-20160816; b=l9I+7NIuue1pVcG/jO7zm40A1hNxh/IsIPpIpJKC1n+xnWAPsj9SazhDnd1GZHd8O4 eDQAaA5f3VEPY5yTsYw7mRSHg/ZEXFSNHOnFouxQSrXmK0Z0h4uXti38z+3WnQhG+WJ8 kMEJcTiCMAo1u4aJ/BVMk+1BjJ4uVovZfp7at31znlK552x4uKCfcCqfLsCeVdmhjupM ReAQeagpiYO7tpFfHM0VTbY40lFX8ZZhA3/Wt0XlGSDMa87o1fdfBXVlIdJYEJJybhil 6+v87RCwWe1iz6TaS/YlMw9YyGkDXDcDKxS9WCfSeJwvmHQTzgc9kHvegocVARlOm4Ov ev1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=d/XgSZ17ffe6brS00XHyihrcV9XMZB9W46RNwfSrKYg=; b=eZF52Y+K+CAdEpuCOxBc6d3d0YGJPxPT7rkr7GCiEyCf+CQZ0rIYDVBM8/43vYUDbL 8UJ8nXGLgVjo0xforCpKDzAOZu31Bw7XUpQ7lSYDnIcP0Yk5M2B+KFUko6Zbjvyh6tbC 3vKBk7wcTcmAVjfHcdRDFUlKjF7eHpEgHkTPnn37cp4SQH9vDpyIAY9HDz2wlcC4Bkih A3bTsODGm0dNUJWw3k9qz6dh6shSlqP77b0T/dYJO5L3MuMTVU32HmjeNnabA1tpbme8 QNAg4jMZo2iZc51vCM8zC4eUioq8r5mL2KdZCGq0np/4I7zjdVEx6qDch7s4WaDuNVW9 RXBA== ARC-Authentication-Results: i=1; mx.google.com; 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 u6-v6si575284pld.628.2018.03.19.13.09.55; Mon, 19 Mar 2018 13:10:10 -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; 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 S1031422AbeCSUIR (ORCPT + 99 others); Mon, 19 Mar 2018 16:08:17 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:45464 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S969117AbeCSSRO (ORCPT ); Mon, 19 Mar 2018 14:17:14 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 27F4A124E; Mon, 19 Mar 2018 18:17:13 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tejun Heo , Jann Horn , Kent Overstreet , Linus Torvalds Subject: [PATCH 4.4 123/134] fs/aio: Add explicit RCU grace period when freeing kioctx Date: Mon, 19 Mar 2018 19:06:46 +0100 Message-Id: <20180319171907.024713687@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180319171849.024066323@linuxfoundation.org> References: <20180319171849.024066323@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tejun Heo commit a6d7cff472eea87d96899a20fa718d2bab7109f3 upstream. While fixing refcounting, e34ecee2ae79 ("aio: Fix a trinity splat") incorrectly removed explicit RCU grace period before freeing kioctx. The intention seems to be depending on the internal RCU grace periods of percpu_ref; however, percpu_ref uses a different flavor of RCU, sched-RCU. This can lead to kioctx being freed while RCU read protected dereferences are still in progress. Fix it by updating free_ioctx() to go through call_rcu() explicitly. v2: Comment added to explain double bouncing. Signed-off-by: Tejun Heo Reported-by: Jann Horn Fixes: e34ecee2ae79 ("aio: Fix a trinity splat") Cc: Kent Overstreet Cc: Linus Torvalds Cc: stable@vger.kernel.org # v3.13+ Signed-off-by: Greg Kroah-Hartman --- fs/aio.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) --- a/fs/aio.c +++ b/fs/aio.c @@ -115,7 +115,8 @@ struct kioctx { struct page **ring_pages; long nr_pages; - struct work_struct free_work; + struct rcu_head free_rcu; + struct work_struct free_work; /* see free_ioctx() */ /* * signals when all in-flight requests are done @@ -573,6 +574,12 @@ static int kiocb_cancel(struct aio_kiocb return cancel(&kiocb->common); } +/* + * free_ioctx() should be RCU delayed to synchronize against the RCU + * protected lookup_ioctx() and also needs process context to call + * aio_free_ring(), so the double bouncing through kioctx->free_rcu and + * ->free_work. + */ static void free_ioctx(struct work_struct *work) { struct kioctx *ctx = container_of(work, struct kioctx, free_work); @@ -586,6 +593,14 @@ static void free_ioctx(struct work_struc kmem_cache_free(kioctx_cachep, ctx); } +static void free_ioctx_rcufn(struct rcu_head *head) +{ + struct kioctx *ctx = container_of(head, struct kioctx, free_rcu); + + INIT_WORK(&ctx->free_work, free_ioctx); + schedule_work(&ctx->free_work); +} + static void free_ioctx_reqs(struct percpu_ref *ref) { struct kioctx *ctx = container_of(ref, struct kioctx, reqs); @@ -594,8 +609,8 @@ static void free_ioctx_reqs(struct percp if (ctx->rq_wait && atomic_dec_and_test(&ctx->rq_wait->count)) complete(&ctx->rq_wait->comp); - INIT_WORK(&ctx->free_work, free_ioctx); - schedule_work(&ctx->free_work); + /* Synchronize against RCU protected table->table[] dereferences */ + call_rcu(&ctx->free_rcu, free_ioctx_rcufn); } /* @@ -817,7 +832,7 @@ static int kill_ioctx(struct mm_struct * table->table[ctx->id] = NULL; spin_unlock(&mm->ioctx_lock); - /* percpu_ref_kill() will do the necessary call_rcu() */ + /* free_ioctx_reqs() will do the necessary RCU synchronization */ wake_up_all(&ctx->wait); /*