From patchwork Fri Jun 15 20:33:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Jiang X-Patchwork-Id: 10467617 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 71280601C2 for ; Fri, 15 Jun 2018 20:33:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6009D28A21 for ; Fri, 15 Jun 2018 20:33:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54C9528C90; Fri, 15 Jun 2018 20:33:46 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF7C328A21 for ; Fri, 15 Jun 2018 20:33:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BC5B6B0003; Fri, 15 Jun 2018 16:33:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 76B726B0006; Fri, 15 Jun 2018 16:33:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6356B6B0007; Fri, 15 Jun 2018 16:33:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f70.google.com (mail-pg0-f70.google.com [74.125.83.70]) by kanga.kvack.org (Postfix) with ESMTP id 1DBF26B0003 for ; Fri, 15 Jun 2018 16:33:44 -0400 (EDT) Received: by mail-pg0-f70.google.com with SMTP id d1-v6so3708242pga.15 for ; Fri, 15 Jun 2018 13:33:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:subject:from :to:cc:date:message-id:user-agent:mime-version :content-transfer-encoding; bh=Y9tD/hYtTsOoLgC/IcqwmtpNgQPdCNoymPrKAoI+Giw=; b=Z6Id/CvOluxCKXgAJI/8oE38LcqyXChWJ29VpP2rI4X04nhoDW3Etq15mSEOwAmSsS aXrQuR07+T3rEIH1DnPZUuTey3EjrgT4/WqdDRX6DvGuMqqbq7jb8Ew3ye7lZe16zIPv kIN6sWUkT0UFC9UwgWPMMqXwr9dgStoUUNoeRYiv2nqOA1xunxmc3ePQ7h7IpGlbcj9F N0k+eM5sretWvIKRRztjY966eH7NECL94YAFLbcMjvRMav7nQmhfDwNqqDnaIMYd2x81 bH5QxDcCjR6zffORIhkSxuphgqPVRPrbRff8K4SeZw73Zm9IC5NJ+NGcFp9hF4IsZB6Z q0Xw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of dave.jiang@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=dave.jiang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APt69E0Q5UXGyi5ZKflC6E+OiSvMeHFxIyTTBfpbzHoYwAfmN2R6yfb5 EbLxf+bJCPLyNZoICs5+A0p2rdzs9fwUWLUOVEc3hsGDxvv9xyPuoWX1hR6R7vb/dLRiW8/aP0p PsXWf+M+dcMk0omP8PNWAgkYhNHTr1rkKRn0R8qG7Q5N3L+TqG73/YnaB0DrMul7EUQ== X-Received: by 2002:aa7:8510:: with SMTP id v16-v6mr3532962pfn.77.1529094823748; Fri, 15 Jun 2018 13:33:43 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLFqx9mrBKo6zGgzWeG7JPMS2vR+AGucZ7461Huio9uglQIvAHlu5DuH8ryq7WDFVrUeSUB X-Received: by 2002:aa7:8510:: with SMTP id v16-v6mr3532857pfn.77.1529094821356; Fri, 15 Jun 2018 13:33:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529094821; cv=none; d=google.com; s=arc-20160816; b=gVh7CwaaF9YpRs4/ZaWOs8WfRanHh0QYsgrK5pWwUzKcccrxasPGKJ8gfEoRktBspj mUrQ++uskNQv+kNwll/0aEmm4gtMus/4G/toTcg0gSnkBOLao8UAL6/S9t0HbYbvJJGH kU9Fe8FoxgiiPjhquiqR7jaVad2S/lSQH/9er3KduSkZaMijIl0znaaHGOhjQWp+aLA2 0HFcHud6KnCYgrasIcUWBjX4oSIFmFYTCZBjOljpyWOR+LWEm/ncMRg9LlUQ95hnXH2/ C2rxrG+8krR9CqkoXcIl7rsON53WxgEvlolLnUbmffLVaPXN2d1jqg+ogOJsA0zfGdB4 t5+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:message-id:date :cc:to:from:subject:arc-authentication-results; bh=Y9tD/hYtTsOoLgC/IcqwmtpNgQPdCNoymPrKAoI+Giw=; b=n+SbzpJOYKhcnD1kImevDF9c7XmsmKrwYfJAqZfCKJK6ZWWIxi+DJNjGnJUzt8Ndra 56ESv9pDHevxlpxxtq/6fWyGEy+hzKvq6Vi27GtWgW/zQWeXDve3eo7Fzu4Zina7CH0R 0EJMagyEP4SlI4n6l1MsoA81BPz9gNe4hDcvQ/7bOwtRpp5nbqpVU66SBFxk0Dbm2DtE V2qWa0wzm4aMRBcLST13mxtmulxJV/0gIieAudTdAc7mNwfo7/M/MimxRoebfYGddb89 HNlf3nh8kyyZvVKxEa2mFW+/RKFSgzP4Cn6hy2cjLza/Caj/L+qY65N7V+Q6Au+3unws i8lw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dave.jiang@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=dave.jiang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga01.intel.com (mga01.intel.com. [192.55.52.88]) by mx.google.com with ESMTPS id r7-v6si6980090pgq.675.2018.06.15.13.33.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jun 2018 13:33:41 -0700 (PDT) Received-SPF: pass (google.com: domain of dave.jiang@intel.com designates 192.55.52.88 as permitted sender) client-ip=192.55.52.88; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dave.jiang@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=dave.jiang@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Jun 2018 13:33:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,228,1526367600"; d="scan'208";a="208462145" Received: from djiang5-desk3.ch.intel.com ([143.182.136.93]) by orsmga004.jf.intel.com with ESMTP; 15 Jun 2018 13:33:39 -0700 Subject: [PATCH v2] mm: disallow mapping that conflict for devm_memremap_pages() From: Dave Jiang To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, dan.j.williams@intel.com, elliott@hpe.com, linux-nvdimm@lists.01.org Date: Fri, 15 Jun 2018 13:33:39 -0700 Message-ID: <152909478401.50143.312364396244072931.stgit@djiang5-desk3.ch.intel.com> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP When pmem namespaces created are smaller than section size, this can cause issue during removal and gpf was observed: [ 249.613597] general protection fault: 0000 1 SMP PTI [ 249.725203] CPU: 36 PID: 3941 Comm: ndctl Tainted: G W 4.14.28-1.el7uek.x86_64 #2 [ 249.745495] task: ffff88acda150000 task.stack: ffffc900233a4000 [ 249.752107] RIP: 0010:__put_page+0x56/0x79 [ 249.844675] Call Trace: [ 249.847410] devm_memremap_pages_release+0x155/0x23a [ 249.852953] release_nodes+0x21e/0x260 [ 249.857138] devres_release_all+0x3c/0x48 [ 249.861606] device_release_driver_internal+0x15c/0x207 [ 249.867439] device_release_driver+0x12/0x14 [ 249.872204] unbind_store+0xba/0xd8 [ 249.876098] drv_attr_store+0x27/0x31 [ 249.880186] sysfs_kf_write+0x3f/0x46 [ 249.884266] kernfs_fop_write+0x10f/0x18b [ 249.888734] __vfs_write+0x3a/0x16d [ 249.892628] ? selinux_file_permission+0xe5/0x116 [ 249.897881] ? security_file_permission+0x41/0xbb [ 249.903133] vfs_write+0xb2/0x1a1 [ 249.906835] ? syscall_trace_enter+0x1ce/0x2b8 [ 249.911795] SyS_write+0x55/0xb9 [ 249.915397] do_syscall_64+0x79/0x1ae [ 249.919485] entry_SYSCALL_64_after_hwframe+0x3d/0x0 Add code to check whether we have mapping already in the same section and prevent additional mapping from created if that is the case. Signed-off-by: Dave Jiang --- v2: Change dev_warn() to dev_WARN() to provide helpful backtrace. (Robert E) kernel/memremap.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/kernel/memremap.c b/kernel/memremap.c index 5857267a4af5..a734b1747466 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -176,10 +176,27 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) unsigned long pfn, pgoff, order; pgprot_t pgprot = PAGE_KERNEL; int error, nid, is_ram; + struct dev_pagemap *conflict_pgmap; align_start = res->start & ~(SECTION_SIZE - 1); align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE) - align_start; + align_end = align_start + align_size - 1; + + conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL); + if (conflict_pgmap) { + dev_WARN(dev, "Conflicting mapping in same section\n"); + put_dev_pagemap(conflict_pgmap); + return ERR_PTR(-ENOMEM); + } + + conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL); + if (conflict_pgmap) { + dev_WARN(dev, "Conflicting mapping in same section\n"); + put_dev_pagemap(conflict_pgmap); + return ERR_PTR(-ENOMEM); + } + is_ram = region_intersects(align_start, align_size, IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE); @@ -199,7 +216,6 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) mutex_lock(&pgmap_lock); error = 0; - align_end = align_start + align_size - 1; foreach_order_pgoff(res, order, pgoff) { error = __radix_tree_insert(&pgmap_radix,