Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3694019ybv; Mon, 10 Feb 2020 04:50:55 -0800 (PST) X-Google-Smtp-Source: APXvYqxSMNuzBS3YWXBOcARV5nIAzVLX5VU9/gDl0N0qznMpRl6/0n8FazZ/XCuoUVORhUY2iy4Z X-Received: by 2002:a9d:69ce:: with SMTP id v14mr962971oto.248.1581339055799; Mon, 10 Feb 2020 04:50:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339055; cv=none; d=google.com; s=arc-20160816; b=uqtTo8hSA/l3ocboT8/lZReLYdhLj0hHfDQ10PWB7IIJxf3cVA0iI0rvXUfyG0k/mf 1sk/EH0PNtBQ3Gx/ElN6ylGg6m2saWNtBAGYab5Qz46J6LcdL5FYxc9NokerWEFdYB84 5iF216MurxvLLd+0rcRxpWTvqWKK9JiR9c+j5daBG3I2nOYn2nAxWiZcEsfEZb06xKtL j99QEWN3WgKW92q/kCE6ks1keJCytrjoQYWq9+c+lt+8XjIO3ee1bjnsuW0ByTUWghfp XQJK/kmjDDASi2T1pZEKYqW9pmUbbafMo2AEsZ04UcHL/n5NuapA5oyu113IRd9RyGG6 i8ig== 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=VlSY6/Bwfdyjl4fMwSn045683pJIfYrQ1+uih7wiQKk=; b=kfuE8q7a8Qgl3gtH92NADyHuigZsdZfoWMasIyQM6JNsj8GlztG57e3DvtXPOiUbbb mkhKjpLDU5VTJa4ibPiSGZIP36WYhVaH4wke+rUloVEjSZSS9fELwPiL9boA1S480y07 fVEOtEh6ECp/lNinxm0ZGC9iFo6YsKIXmGxQUEbF8RyD+Nz2GJY6KvqOZBTVStRA1jnK IcWcFsyIRXiv7X3SZXFa7PYhLVnFJjhPoi9RI8nqchLottTQ5ueqQSce3GBXT6Zmn8r/ fHfpwusgHApsvXa6DlsQTXf2gi22RDn6CLiIk4/p36+CQky7mA9hvQR+D06vms1EtkOi mjcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=lO0LFUUi; 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 v14si140049oto.127.2020.02.10.04.50.43; Mon, 10 Feb 2020 04:50:55 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=lO0LFUUi; 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 S1730576AbgBJMoH (ORCPT + 99 others); Mon, 10 Feb 2020 07:44:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:40296 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729713AbgBJMkW (ORCPT ); Mon, 10 Feb 2020 07:40:22 -0500 Received: from localhost (unknown [209.37.97.194]) (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 CE8792051A; Mon, 10 Feb 2020 12:40:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338421; bh=/i6l/U5Vb1wmXbSl/H8EJrzujIaF09aGsl8Hfcv7yBk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lO0LFUUi7Ansd85CMEqTooQuBG6YrxJz8K45wiXCtLrtSrsVTisNcenYAHncy2zVH EBrtyC2kBDlso+vc5RcYVyqevOgvEdRWqGo8MLzzQujANopG/0meflkxSuWY0IsvPE oVYrxXcz6PArFmnn1/IuC0E6csfFmh0nWIboB2g0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Wheeler , Joe Thornber , Mike Snitzer Subject: [PATCH 5.5 141/367] dm space map common: fix to ensure new block isnt already in use Date: Mon, 10 Feb 2020 04:30:54 -0800 Message-Id: <20200210122437.860278179@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@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: Joe Thornber commit 4feaef830de7ffdd8352e1fe14ad3bf13c9688f8 upstream. The space-maps track the reference counts for disk blocks allocated by both the thin-provisioning and cache targets. There are variants for tracking metadata blocks and data blocks. Transactionality is implemented by never touching blocks from the previous transaction, so we can rollback in the event of a crash. When allocating a new block we need to ensure the block is free (has reference count of 0) in both the current and previous transaction. Prior to this fix we were doing this by searching for a free block in the previous transaction, and relying on a 'begin' counter to track where the last allocation in the current transaction was. This 'begin' field was not being updated in all code paths (eg, increment of a data block reference count due to breaking sharing of a neighbour block in the same btree leaf). This fix keeps the 'begin' field, but now it's just a hint to speed up the search. Instead the current transaction is searched for a free block, and then the old transaction is double checked to ensure it's free. Much simpler. This fixes reports of sm_disk_new_block()'s BUG_ON() triggering when DM thin-provisioning's snapshots are heavily used. Reported-by: Eric Wheeler Cc: stable@vger.kernel.org Signed-off-by: Joe Thornber Signed-off-by: Mike Snitzer Signed-off-by: Greg Kroah-Hartman --- drivers/md/persistent-data/dm-space-map-common.c | 27 +++++++++++++++++++++ drivers/md/persistent-data/dm-space-map-common.h | 2 + drivers/md/persistent-data/dm-space-map-disk.c | 6 +++- drivers/md/persistent-data/dm-space-map-metadata.c | 5 +++ 4 files changed, 37 insertions(+), 3 deletions(-) --- a/drivers/md/persistent-data/dm-space-map-common.c +++ b/drivers/md/persistent-data/dm-space-map-common.c @@ -380,6 +380,33 @@ int sm_ll_find_free_block(struct ll_disk return -ENOSPC; } +int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll, + dm_block_t begin, dm_block_t end, dm_block_t *b) +{ + int r; + uint32_t count; + + do { + r = sm_ll_find_free_block(new_ll, begin, new_ll->nr_blocks, b); + if (r) + break; + + /* double check this block wasn't used in the old transaction */ + if (*b >= old_ll->nr_blocks) + count = 0; + else { + r = sm_ll_lookup(old_ll, *b, &count); + if (r) + break; + + if (count) + begin = *b + 1; + } + } while (count); + + return r; +} + static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b, int (*mutator)(void *context, uint32_t old, uint32_t *new), void *context, enum allocation_event *ev) --- a/drivers/md/persistent-data/dm-space-map-common.h +++ b/drivers/md/persistent-data/dm-space-map-common.h @@ -109,6 +109,8 @@ int sm_ll_lookup_bitmap(struct ll_disk * int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result); int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin, dm_block_t end, dm_block_t *result); +int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll, + dm_block_t begin, dm_block_t end, dm_block_t *result); int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, enum allocation_event *ev); int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev); int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev); --- a/drivers/md/persistent-data/dm-space-map-disk.c +++ b/drivers/md/persistent-data/dm-space-map-disk.c @@ -167,8 +167,10 @@ static int sm_disk_new_block(struct dm_s enum allocation_event ev; struct sm_disk *smd = container_of(sm, struct sm_disk, sm); - /* FIXME: we should loop round a couple of times */ - r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b); + /* + * Any block we allocate has to be free in both the old and current ll. + */ + r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b); if (r) return r; --- a/drivers/md/persistent-data/dm-space-map-metadata.c +++ b/drivers/md/persistent-data/dm-space-map-metadata.c @@ -448,7 +448,10 @@ static int sm_metadata_new_block_(struct enum allocation_event ev; struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm); - r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b); + /* + * Any block we allocate has to be free in both the old and current ll. + */ + r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b); if (r) return r;