Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp726666imm; Wed, 17 Oct 2018 07:24:39 -0700 (PDT) X-Google-Smtp-Source: ACcGV639/j0lPID4ZPIbSYvIPysURR6Hm+5kgCbmmTzrX7LgYcHM8tS27BGNbdht2COP+zyzHdkc X-Received: by 2002:a17:902:650f:: with SMTP id b15-v6mr23639994plk.2.1539786279851; Wed, 17 Oct 2018 07:24:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539786279; cv=none; d=google.com; s=arc-20160816; b=WpLO0PgpjFyplmZac3CWizWCFuMGc0Ea8TA0KbQYd274YHtUbRrlQZ/vH06pCTzOVJ UP8GHZvvRC8/VA3oejD0+wu4MY+rJGfH9GDiNbXDbnfO3hNd4wW1pTcDsHKsBwj7FpR4 s6CzAuV2tu0hKzZKsOoiWKm9UbfCmAEKZRnW+lLLVuq8JY3A1WsORoW6B7MOjMHuBVKH HE7PfOUAOfW3Ob62iy39RghitUV/Xg3JL4qGgj1DnKLBN/1FEsr8geDdSg9vkzKoMy1A G5gALjf2IHXZmhECvMg8Mu6dXnDnhMUfD32/z3I7Z9hFh+jfWVBBdS+voHAXyti75Ufb V8cg== 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:cc:to:from :subject:organization; bh=peWy1w6ju/X6PVuBKy6K0Ws60ASNKU+z0dr1ss1K9xI=; b=B7NxdezuF27Uuzt1uYJWVDQmKeUAKsTzQzUYhA1CkgHn9PLcFWjk5at08LdBG+uF+8 F/oh/JHMpLX2s4mmT+B/rVKW7UnnlJxQuKQtWHPYwOlXKpzrQQzXLQH5NgqzyaYFQdWc ZV7A5cSvj9GIB34ZnOTbhYodVWw8o3i5wfQvSBFphoc73Dx5tzDbeDIy/NVS2rCAs6zB EK7NJYe7bS3EHH69un2T69yl20x2XB8J+R0f4YBPwkfl0fcLbK1S9yr2pyyqRnLC1fW3 00VoOPE5jgG3JHinpPLOy7WbaeATD7Z77xemDNTEbuenuf1fK4x9nFBdpEFUzIh5ptvx v0Hw== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w5-v6si17607613pgw.184.2018.10.17.07.24.23; Wed, 17 Oct 2018 07:24:39 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727727AbeJQWTg (ORCPT + 99 others); Wed, 17 Oct 2018 18:19:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39304 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727013AbeJQWTg (ORCPT ); Wed, 17 Oct 2018 18:19:36 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 893693084027; Wed, 17 Oct 2018 14:23:40 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-250.rdu2.redhat.com [10.10.120.250]) by smtp.corp.redhat.com (Postfix) with ESMTP id E24C82B5AC; Wed, 17 Oct 2018 14:23:38 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 2/4] fscache: Fix race in fscache_op_complete() due to split atomic_sub & read From: David Howells To: gregkh@linux-foundation.org Cc: Kiran Kumar Modukuri , viro@zeniv.linux.org.uk, sandeen@redhat.com, dhowells@redhat.com, linux-cachefs@redhat.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Date: Wed, 17 Oct 2018 15:23:38 +0100 Message-ID: <153978621809.8478.2198040871218302573.stgit@warthog.procyon.org.uk> In-Reply-To: <153978619457.8478.3813964117489247515.stgit@warthog.procyon.org.uk> References: <153978619457.8478.3813964117489247515.stgit@warthog.procyon.org.uk> User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Wed, 17 Oct 2018 14:23:40 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: kiran.modukuri The code in fscache_retrieval_complete is using atomic_sub followed by an atomic_read: atomic_sub(n_pages, &op->n_pages); if (atomic_read(&op->n_pages) <= 0) fscache_op_complete(&op->op, true); This causes two threads doing a decrement of n_pages to race with each other seeing the op->refcount 0 at same time - and they end up calling fscache_op_complete() in both the threads leading to an assertion failure. Fix this by using atomic_sub_return() instead of two calls. The oops looks something like: FS-Cache: Assertion failed FS-Cache: 0 > 0 is false ... kernel BUG at /usr/src/linux-4.4.0/fs/fscache/operation.c:449! ... Workqueue: fscache_operation fscache_op_work_func [fscache] ... RIP: 0010:[] fscache_op_complete+0x10d/0x180 [fscache] ... Call Trace: [] cachefiles_read_copier+0x3a9/0x410 [cachefiles] [] fscache_op_work_func+0x22/0x50 [fscache] [] process_one_work+0x150/0x3f0 [] worker_thread+0x11a/0x470 [] ? __schedule+0x359/0x980 [] ? rescuer_thread+0x310/0x310 [] kthread+0xd6/0xf0 [] ? kthread_park+0x60/0x60 [] ret_from_fork+0x3f/0x70 [] ? kthread_park+0x60/0x60 This seen this in 4.4.x kernels and the same bug affects fscache in latest upstreams kernels. Fixes: 1bb4b7f98f36 ("FS-Cache: The retrieval remaining-pages counter needs to be atomic_t") Signed-off-by: Kiran Kumar Modukuri Signed-off-by: David Howells --- include/linux/fscache-cache.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/fscache-cache.h b/include/linux/fscache-cache.h index 34cf0fdd7dc7..bf98ed803af2 100644 --- a/include/linux/fscache-cache.h +++ b/include/linux/fscache-cache.h @@ -196,11 +196,11 @@ static inline void fscache_enqueue_retrieval(struct fscache_retrieval *op) static inline void fscache_retrieval_complete(struct fscache_retrieval *op, int n_pages) { - atomic_sub(n_pages, &op->n_pages); - if (atomic_read(&op->n_pages) <= 0) + if (atomic_sub_return(n_pages, &op->n_pages) <= 0) fscache_op_complete(&op->op, false); } + /** * fscache_put_retrieval - Drop a reference to a retrieval operation * @op: The retrieval operation affected