Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1260616ybl; Fri, 23 Aug 2019 16:24:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqxGIjkE252qhJHzsGnu2bpANLu2loTj3yLmLdMzSSpxBW00N5ShwnEUbN5qyO4UJJ6RKoey X-Received: by 2002:a63:ed50:: with SMTP id m16mr5990431pgk.209.1566602692353; Fri, 23 Aug 2019 16:24:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566602692; cv=none; d=google.com; s=arc-20160816; b=pU99TMnB6Yf9tzCSIKjQX0id5EzFhwTOplnxA2sVpoNo7qTKiRsOk2ZcPAL+XZViWh j1nHI02bzcU1zjpDYHiq+bHfkYz+61GqvAuckshn+fdj/RHAzrznJsOkB9UHjCrhcAzX CFsoay3kzyn/kebRUjoVC+321+F27uIh9S5PGI9DkD3SnyU8FV4sqbtjMngpjilPVEAV 5HOcvy65FgK8NXwEnsXYa1zYrKV741P0kBKfW4GYGk1ERor5F119loTWe7kI7xOyBOgq vtUAuwPuJgC1YXDScrLAljjf9EEX3uBRmhHDtXaDaU4GRMB9PTjUFnKtfNVL74i08A6T V1Gg== 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 :message-id:in-reply-to:subject:cc:to:from:date; bh=WRQoyS0/y/SGlAIhdc87y2bjr4+o8i4NJcTnPhR9O5o=; b=sjf463OF0Bj4AiC4QJDsjecoBwGDG58XJtXvyhHH5IsVm3zzTZ8wMkjHvGGnuldOxZ BW/LfJGFPYO17iD9jGHlDu76vKxoD++UwN95/VNkBa27R7mEod6gRPEM7jZ19MvkZnLY iMOIo8136LrktVcBHTSOgViHRuBzJVNymrPhddY2Ztn0KeG6gw2iuPcTpWZnd6gNhcqa LJvAvGqPek1JTfXmX2EeCCYJ0kXx8y3WV4Jobf1AMGokME6L/xYRr8JY7qd5Mk392IRT jeTo5kSZiScjOsbyBHcGDYPna1MmNzkhsnu7y1fBl9+zI9kH/9J2kP5TizmY8oLn1UHA EPXw== 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 bb14si3334798pjb.66.2019.08.23.16.24.36; Fri, 23 Aug 2019 16:24:52 -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 S2405710AbfHWNzc (ORCPT + 99 others); Fri, 23 Aug 2019 09:55:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43140 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390343AbfHWNzc (ORCPT ); Fri, 23 Aug 2019 09:55:32 -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 94DE1300157A; Fri, 23 Aug 2019 13:55:31 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B36DF19C77; Fri, 23 Aug 2019 13:55:26 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id x7NDtQpQ002147; Fri, 23 Aug 2019 09:55:26 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id x7NDtQo9002143; Fri, 23 Aug 2019 09:55:26 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Fri, 23 Aug 2019 09:55:26 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Zhang Tao cc: agk@redhat.com, snitzer@redhat.com, Zhang Tao , dm-devel@redhat.com, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] dm: make dm_table_find_target return NULL In-Reply-To: Message-ID: References: <1566351211-13280-1-git-send-email-kontais@zoho.com> User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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.46]); Fri, 23 Aug 2019 13:55:31 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently, if we pass too high sector number to dm_table_find_target, it returns zeroed dm_target structure and callers test if the structure is zeroed with the macro dm_target_is_valid. However, returning NULL is common practice to indicate errors. This patch refactors the dm code, so that dm_table_find_target returns NULL and its callers test the returned value for NULL. The macro dm_target_is_valid is deleted. In alloc_targets, we no longer allocate an extra zeroed target. Signed-off-by: Mikulas Patocka --- drivers/md/dm-ioctl.c | 2 +- drivers/md/dm-table.c | 8 +++----- drivers/md/dm.c | 8 ++++---- drivers/md/dm.h | 5 ----- 4 files changed, 8 insertions(+), 15 deletions(-) Index: linux-2.6/drivers/md/dm.c =================================================================== --- linux-2.6.orig/drivers/md/dm.c 2019-08-23 15:45:46.000000000 +0200 +++ linux-2.6/drivers/md/dm.c 2019-08-23 15:45:46.000000000 +0200 @@ -457,7 +457,7 @@ static int dm_blk_report_zones(struct ge return -EIO; tgt = dm_table_find_target(map, sector); - if (!dm_target_is_valid(tgt)) { + if (!tgt) { ret = -EIO; goto out; } @@ -1072,7 +1072,7 @@ static struct dm_target *dm_dax_get_live return NULL; ti = dm_table_find_target(map, sector); - if (!dm_target_is_valid(ti)) + if (!ti) return NULL; return ti; @@ -1572,7 +1572,7 @@ static int __split_and_process_non_flush int r; ti = dm_table_find_target(ci->map, ci->sector); - if (!dm_target_is_valid(ti)) + if (!ti) return -EIO; if (__process_abnormal_io(ci, ti, &r)) @@ -1748,7 +1748,7 @@ static blk_qc_t dm_process_bio(struct ma if (!ti) { ti = dm_table_find_target(map, bio->bi_iter.bi_sector); - if (unlikely(!ti || !dm_target_is_valid(ti))) { + if (unlikely(!ti)) { bio_io_error(bio); return ret; } Index: linux-2.6/drivers/md/dm-ioctl.c =================================================================== --- linux-2.6.orig/drivers/md/dm-ioctl.c 2019-08-23 15:45:46.000000000 +0200 +++ linux-2.6/drivers/md/dm-ioctl.c 2019-08-23 15:45:46.000000000 +0200 @@ -1592,7 +1592,7 @@ static int target_message(struct file *f } ti = dm_table_find_target(table, tmsg->sector); - if (!dm_target_is_valid(ti)) { + if (!ti) { DMWARN("Target message sector outside device."); r = -EINVAL; } else if (ti->type->message) Index: linux-2.6/drivers/md/dm-table.c =================================================================== --- linux-2.6.orig/drivers/md/dm-table.c 2019-08-23 15:45:46.000000000 +0200 +++ linux-2.6/drivers/md/dm-table.c 2019-08-23 15:47:18.000000000 +0200 @@ -163,10 +163,8 @@ static int alloc_targets(struct dm_table /* * Allocate both the target array and offset array at once. - * Append an empty entry to catch sectors beyond the end of - * the device. */ - n_highs = (sector_t *) dm_vcalloc(num + 1, sizeof(struct dm_target) + + n_highs = (sector_t *) dm_vcalloc(num, sizeof(struct dm_target) + sizeof(sector_t)); if (!n_highs) return -ENOMEM; @@ -1359,7 +1357,7 @@ struct dm_target *dm_table_get_target(st /* * Search the btree for the correct target. * - * Caller should check returned pointer with dm_target_is_valid() + * Caller should check returned pointer for NULL * to trap I/O beyond end of device. */ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector) @@ -1368,7 +1366,7 @@ struct dm_target *dm_table_find_target(s sector_t *node; if (unlikely(sector >= dm_table_get_size(t))) - return &t->targets[t->num_targets]; + return NULL; for (l = 0; l < t->depth; l++) { n = get_child(n, k); Index: linux-2.6/drivers/md/dm.h =================================================================== --- linux-2.6.orig/drivers/md/dm.h 2019-08-23 15:45:46.000000000 +0200 +++ linux-2.6/drivers/md/dm.h 2019-08-23 15:45:46.000000000 +0200 @@ -86,11 +86,6 @@ struct target_type *dm_get_immutable_tar int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t); /* - * To check the return value from dm_table_find_target(). - */ -#define dm_target_is_valid(t) ((t)->table) - -/* * To check whether the target type is bio-based or not (request-based). */ #define dm_target_bio_based(t) ((t)->type->map != NULL)