From patchwork Fri Nov 10 19:45:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 10053657 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C514760365 for ; Fri, 10 Nov 2017 19:45:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4B882B40D for ; Fri, 10 Nov 2017 19:45:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A7CCB2B436; Fri, 10 Nov 2017 19:45:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3A75E2B40D for ; Fri, 10 Nov 2017 19:45:17 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 570DD21B00DC4; Fri, 10 Nov 2017 11:41:14 -0800 (PST) X-Original-To: linux-nvdimm@lists.01.org Delivered-To: linux-nvdimm@lists.01.org Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=dave.jiang@intel.com; receiver=linux-nvdimm@lists.01.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id C9A1721B00DC4 for ; Fri, 10 Nov 2017 11:41:12 -0800 (PST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Nov 2017 11:45:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,375,1505804400"; d="scan'208";a="1306256" Received: from djiang5-desk3.ch.intel.com ([143.182.137.38]) by fmsmga001.fm.intel.com with ESMTP; 10 Nov 2017 11:45:12 -0800 Subject: [PATCH] ndctl: daxctl: fix mmap size From: Dave Jiang To: dan.j.williams@intel.com Date: Fri, 10 Nov 2017 12:45:11 -0700 Message-ID: <151034311187.59853.17265892705257743788.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-BeenThere: linux-nvdimm@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Linux-nvdimm developer list." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-nvdimm@lists.01.org Errors-To: linux-nvdimm-bounces@lists.01.org Sender: "Linux-nvdimm" X-Virus-Scanned: ClamAV using ClamSMTP The size for mmap needs to be aligned to the region alignment. Add helper funciton to determine the actual size to be mmap'd. Signed-off-by: Dave Jiang --- daxctl/io.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/daxctl/io.c b/daxctl/io.c index 2f8cb4a..97a4169 100644 --- a/daxctl/io.c +++ b/daxctl/io.c @@ -80,9 +80,26 @@ static bool is_stdinout(struct io_dev *io_dev) io_dev->fd == STDOUT_FILENO) ? true : false; } +static int get_mmap_size(struct io_dev *io_dev, size_t size, size_t *map_size) +{ + unsigned long align; + + align = ndctl_dax_get_align(io_dev->dax); + if (align == ULLONG_MAX) + return -ERANGE; + + if (size <= align) + *map_size = align; + else + *map_size = (size / align) * align; + + return 0; +} + static int setup_device(struct io_dev *io_dev, size_t size) { int flags, rc; + size_t map_size; if (is_stdinout(io_dev)) return 0; @@ -104,8 +121,12 @@ static int setup_device(struct io_dev *io_dev, size_t size) if (!io_dev->is_dax) return 0; + rc = get_mmap_size(io_dev, size, &map_size); + if (rc < 0) + return rc; + flags = (io_dev->direction == IO_READ) ? PROT_READ : PROT_WRITE; - io_dev->mmap = mmap(NULL, size, flags, MAP_SHARED, io_dev->fd, 0); + io_dev->mmap = mmap(NULL, map_size, flags, MAP_SHARED, io_dev->fd, 0); if (io_dev->mmap == MAP_FAILED) { rc = -errno; perror("mmap");