From patchwork Tue Mar 25 14:47:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rakesh Pandit X-Patchwork-Id: 3888061 X-Patchwork-Delegate: dave@jikos.cz Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C1194BF540 for ; Tue, 25 Mar 2014 14:47:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E65732020A for ; Tue, 25 Mar 2014 14:47:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5DA78201FD for ; Tue, 25 Mar 2014 14:47:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752801AbaCYOrN (ORCPT ); Tue, 25 Mar 2014 10:47:13 -0400 Received: from nbl-ex10-fe01.nebula.fi ([188.117.32.121]:42814 "EHLO ex10.nebula.fi" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752394AbaCYOrM (ORCPT ); Tue, 25 Mar 2014 10:47:12 -0400 Received: from hercules.tuxera.com (194.100.106.164) by ex10.nebula.fi (188.117.32.115) with Microsoft SMTP Server (TLS) id 14.3.174.1; Tue, 25 Mar 2014 16:47:09 +0200 Date: Tue, 25 Mar 2014 16:47:07 +0200 From: Rakesh Pandit To: Subject: [PATCH] Btrfs-progs: scrub: disable thread cancelability during mutex locks Message-ID: <20140325144705.GA7499@hercules.tuxera.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: [194.100.106.164] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP scrub_progress_cycle thread runs in asynchronous type but locks mutex while reading shared data. This patch disables cancelability for a brief time while locks are on so as to make sure they are unlocked before thread is canceled. scrub_write_progress gets called from scrub_progress_cycle in asynchronous thread but cancelability is disabled after mutex is locked. This patch moves the call to set cancelability type before mutex lock and makes corresponding changes to labels for error handling. Signed-off-by: Rakesh Pandit --- cmds-scrub.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/cmds-scrub.c b/cmds-scrub.c index d3bd148..4338a0b 100644 --- a/cmds-scrub.c +++ b/cmds-scrub.c @@ -773,31 +773,31 @@ static int scrub_write_progress(pthread_mutex_t *m, const char *fsid, int fd = -1; int old; - ret = pthread_mutex_lock(m); + ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old); if (ret) { err = -ret; - goto fail; + goto out3; } - ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old); + ret = pthread_mutex_lock(m); if (ret) { err = -ret; - goto out; + goto out2; } fd = scrub_open_file_w(SCRUB_DATA_FILE, fsid, "tmp"); if (fd < 0) { err = fd; - goto out; + goto out1; } err = scrub_write_file(fd, fsid, data, n); if (err) - goto out; + goto out1; err = scrub_rename_file(SCRUB_DATA_FILE, fsid, "tmp"); if (err) - goto out; + goto out1; -out: +out1: if (fd >= 0) { ret = close(fd); if (ret) @@ -808,11 +808,12 @@ out: if (ret && !err) err = -ret; -fail: +out2: ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old); if (ret && !err) err = -ret; +out3: return err; } @@ -944,6 +945,10 @@ static void *scrub_progress_cycle(void *ctx) * result we got for the current write and go * on. flag should be set on next cycle, then. */ + perr = pthread_setcancelstate( + PTHREAD_CANCEL_DISABLE, &old); + if (perr) + goto out; perr = pthread_mutex_lock(&sp_shared->progress_mutex); if (perr) goto out; @@ -952,12 +957,20 @@ static void *scrub_progress_cycle(void *ctx) &sp_shared->progress_mutex); if (perr) goto out; + perr = pthread_setcancelstate( + PTHREAD_CANCEL_ENABLE, &old); + if (perr) + goto out; memcpy(sp, sp_last, sizeof(*sp)); continue; } perr = pthread_mutex_unlock(&sp_shared->progress_mutex); if (perr) goto out; + perr = pthread_setcancelstate( + PTHREAD_CANCEL_ENABLE, &old); + if (perr) + goto out; memcpy(sp, sp_shared, sizeof(*sp)); memcpy(sp_last, sp_shared, sizeof(*sp)); }