Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright(c) 2013-2016 Intel Corporation. All rights reserved.
0004  */
0005 #include <linux/memremap.h>
0006 #include <linux/blkdev.h>
0007 #include <linux/device.h>
0008 #include <linux/sizes.h>
0009 #include <linux/slab.h>
0010 #include <linux/fs.h>
0011 #include <linux/mm.h>
0012 #include "nd-core.h"
0013 #include "pfn.h"
0014 #include "nd.h"
0015 
0016 static void nd_pfn_release(struct device *dev)
0017 {
0018     struct nd_region *nd_region = to_nd_region(dev->parent);
0019     struct nd_pfn *nd_pfn = to_nd_pfn(dev);
0020 
0021     dev_dbg(dev, "trace\n");
0022     nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
0023     ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
0024     kfree(nd_pfn->uuid);
0025     kfree(nd_pfn);
0026 }
0027 
0028 struct nd_pfn *to_nd_pfn(struct device *dev)
0029 {
0030     struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
0031 
0032     WARN_ON(!is_nd_pfn(dev));
0033     return nd_pfn;
0034 }
0035 EXPORT_SYMBOL(to_nd_pfn);
0036 
0037 static ssize_t mode_show(struct device *dev,
0038         struct device_attribute *attr, char *buf)
0039 {
0040     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0041 
0042     switch (nd_pfn->mode) {
0043     case PFN_MODE_RAM:
0044         return sprintf(buf, "ram\n");
0045     case PFN_MODE_PMEM:
0046         return sprintf(buf, "pmem\n");
0047     default:
0048         return sprintf(buf, "none\n");
0049     }
0050 }
0051 
0052 static ssize_t mode_store(struct device *dev,
0053         struct device_attribute *attr, const char *buf, size_t len)
0054 {
0055     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0056     ssize_t rc = 0;
0057 
0058     device_lock(dev);
0059     nvdimm_bus_lock(dev);
0060     if (dev->driver)
0061         rc = -EBUSY;
0062     else {
0063         size_t n = len - 1;
0064 
0065         if (strncmp(buf, "pmem\n", n) == 0
0066                 || strncmp(buf, "pmem", n) == 0) {
0067             nd_pfn->mode = PFN_MODE_PMEM;
0068         } else if (strncmp(buf, "ram\n", n) == 0
0069                 || strncmp(buf, "ram", n) == 0)
0070             nd_pfn->mode = PFN_MODE_RAM;
0071         else if (strncmp(buf, "none\n", n) == 0
0072                 || strncmp(buf, "none", n) == 0)
0073             nd_pfn->mode = PFN_MODE_NONE;
0074         else
0075             rc = -EINVAL;
0076     }
0077     dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0078             buf[len - 1] == '\n' ? "" : "\n");
0079     nvdimm_bus_unlock(dev);
0080     device_unlock(dev);
0081 
0082     return rc ? rc : len;
0083 }
0084 static DEVICE_ATTR_RW(mode);
0085 
0086 static ssize_t align_show(struct device *dev,
0087         struct device_attribute *attr, char *buf)
0088 {
0089     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0090 
0091     return sprintf(buf, "%ld\n", nd_pfn->align);
0092 }
0093 
0094 static unsigned long *nd_pfn_supported_alignments(unsigned long *alignments)
0095 {
0096 
0097     alignments[0] = PAGE_SIZE;
0098 
0099     if (has_transparent_hugepage()) {
0100         alignments[1] = HPAGE_PMD_SIZE;
0101         if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
0102             alignments[2] = HPAGE_PUD_SIZE;
0103     }
0104 
0105     return alignments;
0106 }
0107 
0108 /*
0109  * Use pmd mapping if supported as default alignment
0110  */
0111 static unsigned long nd_pfn_default_alignment(void)
0112 {
0113 
0114     if (has_transparent_hugepage())
0115         return HPAGE_PMD_SIZE;
0116     return PAGE_SIZE;
0117 }
0118 
0119 static ssize_t align_store(struct device *dev,
0120         struct device_attribute *attr, const char *buf, size_t len)
0121 {
0122     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0123     unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
0124     ssize_t rc;
0125 
0126     device_lock(dev);
0127     nvdimm_bus_lock(dev);
0128     rc = nd_size_select_store(dev, buf, &nd_pfn->align,
0129             nd_pfn_supported_alignments(aligns));
0130     dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0131             buf[len - 1] == '\n' ? "" : "\n");
0132     nvdimm_bus_unlock(dev);
0133     device_unlock(dev);
0134 
0135     return rc ? rc : len;
0136 }
0137 static DEVICE_ATTR_RW(align);
0138 
0139 static ssize_t uuid_show(struct device *dev,
0140         struct device_attribute *attr, char *buf)
0141 {
0142     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0143 
0144     if (nd_pfn->uuid)
0145         return sprintf(buf, "%pUb\n", nd_pfn->uuid);
0146     return sprintf(buf, "\n");
0147 }
0148 
0149 static ssize_t uuid_store(struct device *dev,
0150         struct device_attribute *attr, const char *buf, size_t len)
0151 {
0152     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0153     ssize_t rc;
0154 
0155     device_lock(dev);
0156     rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
0157     dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0158             buf[len - 1] == '\n' ? "" : "\n");
0159     device_unlock(dev);
0160 
0161     return rc ? rc : len;
0162 }
0163 static DEVICE_ATTR_RW(uuid);
0164 
0165 static ssize_t namespace_show(struct device *dev,
0166         struct device_attribute *attr, char *buf)
0167 {
0168     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0169     ssize_t rc;
0170 
0171     nvdimm_bus_lock(dev);
0172     rc = sprintf(buf, "%s\n", nd_pfn->ndns
0173             ? dev_name(&nd_pfn->ndns->dev) : "");
0174     nvdimm_bus_unlock(dev);
0175     return rc;
0176 }
0177 
0178 static ssize_t namespace_store(struct device *dev,
0179         struct device_attribute *attr, const char *buf, size_t len)
0180 {
0181     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0182     ssize_t rc;
0183 
0184     device_lock(dev);
0185     nvdimm_bus_lock(dev);
0186     rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
0187     dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
0188             buf[len - 1] == '\n' ? "" : "\n");
0189     nvdimm_bus_unlock(dev);
0190     device_unlock(dev);
0191 
0192     return rc;
0193 }
0194 static DEVICE_ATTR_RW(namespace);
0195 
0196 static ssize_t resource_show(struct device *dev,
0197         struct device_attribute *attr, char *buf)
0198 {
0199     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0200     ssize_t rc;
0201 
0202     device_lock(dev);
0203     if (dev->driver) {
0204         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
0205         u64 offset = __le64_to_cpu(pfn_sb->dataoff);
0206         struct nd_namespace_common *ndns = nd_pfn->ndns;
0207         u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
0208         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
0209 
0210         rc = sprintf(buf, "%#llx\n", (unsigned long long) nsio->res.start
0211                 + start_pad + offset);
0212     } else {
0213         /* no address to convey if the pfn instance is disabled */
0214         rc = -ENXIO;
0215     }
0216     device_unlock(dev);
0217 
0218     return rc;
0219 }
0220 static DEVICE_ATTR_ADMIN_RO(resource);
0221 
0222 static ssize_t size_show(struct device *dev,
0223         struct device_attribute *attr, char *buf)
0224 {
0225     struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
0226     ssize_t rc;
0227 
0228     device_lock(dev);
0229     if (dev->driver) {
0230         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
0231         u64 offset = __le64_to_cpu(pfn_sb->dataoff);
0232         struct nd_namespace_common *ndns = nd_pfn->ndns;
0233         u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
0234         u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
0235         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
0236 
0237         rc = sprintf(buf, "%llu\n", (unsigned long long)
0238                 resource_size(&nsio->res) - start_pad
0239                 - end_trunc - offset);
0240     } else {
0241         /* no size to convey if the pfn instance is disabled */
0242         rc = -ENXIO;
0243     }
0244     device_unlock(dev);
0245 
0246     return rc;
0247 }
0248 static DEVICE_ATTR_RO(size);
0249 
0250 static ssize_t supported_alignments_show(struct device *dev,
0251         struct device_attribute *attr, char *buf)
0252 {
0253     unsigned long aligns[MAX_NVDIMM_ALIGN] = { [0] = 0, };
0254 
0255     return nd_size_select_show(0,
0256             nd_pfn_supported_alignments(aligns), buf);
0257 }
0258 static DEVICE_ATTR_RO(supported_alignments);
0259 
0260 static struct attribute *nd_pfn_attributes[] = {
0261     &dev_attr_mode.attr,
0262     &dev_attr_namespace.attr,
0263     &dev_attr_uuid.attr,
0264     &dev_attr_align.attr,
0265     &dev_attr_resource.attr,
0266     &dev_attr_size.attr,
0267     &dev_attr_supported_alignments.attr,
0268     NULL,
0269 };
0270 
0271 static struct attribute_group nd_pfn_attribute_group = {
0272     .attrs = nd_pfn_attributes,
0273 };
0274 
0275 const struct attribute_group *nd_pfn_attribute_groups[] = {
0276     &nd_pfn_attribute_group,
0277     &nd_device_attribute_group,
0278     &nd_numa_attribute_group,
0279     NULL,
0280 };
0281 
0282 static const struct device_type nd_pfn_device_type = {
0283     .name = "nd_pfn",
0284     .release = nd_pfn_release,
0285     .groups = nd_pfn_attribute_groups,
0286 };
0287 
0288 bool is_nd_pfn(struct device *dev)
0289 {
0290     return dev ? dev->type == &nd_pfn_device_type : false;
0291 }
0292 EXPORT_SYMBOL(is_nd_pfn);
0293 
0294 static struct lock_class_key nvdimm_pfn_key;
0295 
0296 struct device *nd_pfn_devinit(struct nd_pfn *nd_pfn,
0297         struct nd_namespace_common *ndns)
0298 {
0299     struct device *dev;
0300 
0301     if (!nd_pfn)
0302         return NULL;
0303 
0304     nd_pfn->mode = PFN_MODE_NONE;
0305     nd_pfn->align = nd_pfn_default_alignment();
0306     dev = &nd_pfn->dev;
0307     device_initialize(&nd_pfn->dev);
0308     lockdep_set_class(&nd_pfn->dev.mutex, &nvdimm_pfn_key);
0309     if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
0310         dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
0311                 dev_name(ndns->claim));
0312         put_device(dev);
0313         return NULL;
0314     }
0315     return dev;
0316 }
0317 
0318 static struct nd_pfn *nd_pfn_alloc(struct nd_region *nd_region)
0319 {
0320     struct nd_pfn *nd_pfn;
0321     struct device *dev;
0322 
0323     nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
0324     if (!nd_pfn)
0325         return NULL;
0326 
0327     nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
0328     if (nd_pfn->id < 0) {
0329         kfree(nd_pfn);
0330         return NULL;
0331     }
0332 
0333     dev = &nd_pfn->dev;
0334     dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
0335     dev->type = &nd_pfn_device_type;
0336     dev->parent = &nd_region->dev;
0337 
0338     return nd_pfn;
0339 }
0340 
0341 struct device *nd_pfn_create(struct nd_region *nd_region)
0342 {
0343     struct nd_pfn *nd_pfn;
0344     struct device *dev;
0345 
0346     if (!is_memory(&nd_region->dev))
0347         return NULL;
0348 
0349     nd_pfn = nd_pfn_alloc(nd_region);
0350     dev = nd_pfn_devinit(nd_pfn, NULL);
0351 
0352     nd_device_register(dev);
0353     return dev;
0354 }
0355 
0356 /*
0357  * nd_pfn_clear_memmap_errors() clears any errors in the volatile memmap
0358  * space associated with the namespace. If the memmap is set to DRAM, then
0359  * this is a no-op. Since the memmap area is freshly initialized during
0360  * probe, we have an opportunity to clear any badblocks in this area.
0361  */
0362 static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
0363 {
0364     struct nd_region *nd_region = to_nd_region(nd_pfn->dev.parent);
0365     struct nd_namespace_common *ndns = nd_pfn->ndns;
0366     void *zero_page = page_address(ZERO_PAGE(0));
0367     struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
0368     int num_bad, meta_num, rc, bb_present;
0369     sector_t first_bad, meta_start;
0370     struct nd_namespace_io *nsio;
0371 
0372     if (nd_pfn->mode != PFN_MODE_PMEM)
0373         return 0;
0374 
0375     nsio = to_nd_namespace_io(&ndns->dev);
0376     meta_start = (SZ_4K + sizeof(*pfn_sb)) >> 9;
0377     meta_num = (le64_to_cpu(pfn_sb->dataoff) >> 9) - meta_start;
0378 
0379     /*
0380      * re-enable the namespace with correct size so that we can access
0381      * the device memmap area.
0382      */
0383     devm_namespace_disable(&nd_pfn->dev, ndns);
0384     rc = devm_namespace_enable(&nd_pfn->dev, ndns, le64_to_cpu(pfn_sb->dataoff));
0385     if (rc)
0386         return rc;
0387 
0388     do {
0389         unsigned long zero_len;
0390         u64 nsoff;
0391 
0392         bb_present = badblocks_check(&nd_region->bb, meta_start,
0393                 meta_num, &first_bad, &num_bad);
0394         if (bb_present) {
0395             dev_dbg(&nd_pfn->dev, "meta: %x badblocks at %llx\n",
0396                     num_bad, first_bad);
0397             nsoff = ALIGN_DOWN((nd_region->ndr_start
0398                     + (first_bad << 9)) - nsio->res.start,
0399                     PAGE_SIZE);
0400             zero_len = ALIGN(num_bad << 9, PAGE_SIZE);
0401             while (zero_len) {
0402                 unsigned long chunk = min(zero_len, PAGE_SIZE);
0403 
0404                 rc = nvdimm_write_bytes(ndns, nsoff, zero_page,
0405                             chunk, 0);
0406                 if (rc)
0407                     break;
0408 
0409                 zero_len -= chunk;
0410                 nsoff += chunk;
0411             }
0412             if (rc) {
0413                 dev_err(&nd_pfn->dev,
0414                     "error clearing %x badblocks at %llx\n",
0415                     num_bad, first_bad);
0416                 return rc;
0417             }
0418         }
0419     } while (bb_present);
0420 
0421     return 0;
0422 }
0423 
0424 static bool nd_supported_alignment(unsigned long align)
0425 {
0426     int i;
0427     unsigned long supported[MAX_NVDIMM_ALIGN] = { [0] = 0, };
0428 
0429     if (align == 0)
0430         return false;
0431 
0432     nd_pfn_supported_alignments(supported);
0433     for (i = 0; supported[i]; i++)
0434         if (align == supported[i])
0435             return true;
0436     return false;
0437 }
0438 
0439 /**
0440  * nd_pfn_validate - read and validate info-block
0441  * @nd_pfn: fsdax namespace runtime state / properties
0442  * @sig: 'devdax' or 'fsdax' signature
0443  *
0444  * Upon return the info-block buffer contents (->pfn_sb) are
0445  * indeterminate when validation fails, and a coherent info-block
0446  * otherwise.
0447  */
0448 int nd_pfn_validate(struct nd_pfn *nd_pfn, const char *sig)
0449 {
0450     u64 checksum, offset;
0451     struct resource *res;
0452     enum nd_pfn_mode mode;
0453     struct nd_namespace_io *nsio;
0454     unsigned long align, start_pad;
0455     struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
0456     struct nd_namespace_common *ndns = nd_pfn->ndns;
0457     const uuid_t *parent_uuid = nd_dev_to_uuid(&ndns->dev);
0458 
0459     if (!pfn_sb || !ndns)
0460         return -ENODEV;
0461 
0462     if (!is_memory(nd_pfn->dev.parent))
0463         return -ENODEV;
0464 
0465     if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0))
0466         return -ENXIO;
0467 
0468     if (memcmp(pfn_sb->signature, sig, PFN_SIG_LEN) != 0)
0469         return -ENODEV;
0470 
0471     checksum = le64_to_cpu(pfn_sb->checksum);
0472     pfn_sb->checksum = 0;
0473     if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
0474         return -ENODEV;
0475     pfn_sb->checksum = cpu_to_le64(checksum);
0476 
0477     if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
0478         return -ENODEV;
0479 
0480     if (__le16_to_cpu(pfn_sb->version_minor) < 1) {
0481         pfn_sb->start_pad = 0;
0482         pfn_sb->end_trunc = 0;
0483     }
0484 
0485     if (__le16_to_cpu(pfn_sb->version_minor) < 2)
0486         pfn_sb->align = 0;
0487 
0488     if (__le16_to_cpu(pfn_sb->version_minor) < 4) {
0489         pfn_sb->page_struct_size = cpu_to_le16(64);
0490         pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
0491     }
0492 
0493     switch (le32_to_cpu(pfn_sb->mode)) {
0494     case PFN_MODE_RAM:
0495     case PFN_MODE_PMEM:
0496         break;
0497     default:
0498         return -ENXIO;
0499     }
0500 
0501     align = le32_to_cpu(pfn_sb->align);
0502     offset = le64_to_cpu(pfn_sb->dataoff);
0503     start_pad = le32_to_cpu(pfn_sb->start_pad);
0504     if (align == 0)
0505         align = 1UL << ilog2(offset);
0506     mode = le32_to_cpu(pfn_sb->mode);
0507 
0508     if ((le32_to_cpu(pfn_sb->page_size) > PAGE_SIZE) &&
0509             (mode == PFN_MODE_PMEM)) {
0510         dev_err(&nd_pfn->dev,
0511                 "init failed, page size mismatch %d\n",
0512                 le32_to_cpu(pfn_sb->page_size));
0513         return -EOPNOTSUPP;
0514     }
0515 
0516     if ((le16_to_cpu(pfn_sb->page_struct_size) < sizeof(struct page)) &&
0517             (mode == PFN_MODE_PMEM)) {
0518         dev_err(&nd_pfn->dev,
0519                 "init failed, struct page size mismatch %d\n",
0520                 le16_to_cpu(pfn_sb->page_struct_size));
0521         return -EOPNOTSUPP;
0522     }
0523 
0524     /*
0525      * Check whether the we support the alignment. For Dax if the
0526      * superblock alignment is not matching, we won't initialize
0527      * the device.
0528      */
0529     if (!nd_supported_alignment(align) &&
0530             !memcmp(pfn_sb->signature, DAX_SIG, PFN_SIG_LEN)) {
0531         dev_err(&nd_pfn->dev, "init failed, alignment mismatch: "
0532                 "%ld:%ld\n", nd_pfn->align, align);
0533         return -EOPNOTSUPP;
0534     }
0535 
0536     if (!nd_pfn->uuid) {
0537         /*
0538          * When probing a namepace via nd_pfn_probe() the uuid
0539          * is NULL (see: nd_pfn_devinit()) we init settings from
0540          * pfn_sb
0541          */
0542         nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
0543         if (!nd_pfn->uuid)
0544             return -ENOMEM;
0545         nd_pfn->align = align;
0546         nd_pfn->mode = mode;
0547     } else {
0548         /*
0549          * When probing a pfn / dax instance we validate the
0550          * live settings against the pfn_sb
0551          */
0552         if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
0553             return -ENODEV;
0554 
0555         /*
0556          * If the uuid validates, but other settings mismatch
0557          * return EINVAL because userspace has managed to change
0558          * the configuration without specifying new
0559          * identification.
0560          */
0561         if (nd_pfn->align != align || nd_pfn->mode != mode) {
0562             dev_err(&nd_pfn->dev,
0563                     "init failed, settings mismatch\n");
0564             dev_dbg(&nd_pfn->dev, "align: %lx:%lx mode: %d:%d\n",
0565                     nd_pfn->align, align, nd_pfn->mode,
0566                     mode);
0567             return -EOPNOTSUPP;
0568         }
0569     }
0570 
0571     if (align > nvdimm_namespace_capacity(ndns)) {
0572         dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
0573                 align, nvdimm_namespace_capacity(ndns));
0574         return -EOPNOTSUPP;
0575     }
0576 
0577     /*
0578      * These warnings are verbose because they can only trigger in
0579      * the case where the physical address alignment of the
0580      * namespace has changed since the pfn superblock was
0581      * established.
0582      */
0583     nsio = to_nd_namespace_io(&ndns->dev);
0584     res = &nsio->res;
0585     if (offset >= resource_size(res)) {
0586         dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
0587                 dev_name(&ndns->dev));
0588         return -EOPNOTSUPP;
0589     }
0590 
0591     if ((align && !IS_ALIGNED(res->start + offset + start_pad, align))
0592             || !IS_ALIGNED(offset, PAGE_SIZE)) {
0593         dev_err(&nd_pfn->dev,
0594                 "bad offset: %#llx dax disabled align: %#lx\n",
0595                 offset, align);
0596         return -EOPNOTSUPP;
0597     }
0598 
0599     if (!IS_ALIGNED(res->start + le32_to_cpu(pfn_sb->start_pad),
0600                 memremap_compat_align())) {
0601         dev_err(&nd_pfn->dev, "resource start misaligned\n");
0602         return -EOPNOTSUPP;
0603     }
0604 
0605     if (!IS_ALIGNED(res->end + 1 - le32_to_cpu(pfn_sb->end_trunc),
0606                 memremap_compat_align())) {
0607         dev_err(&nd_pfn->dev, "resource end misaligned\n");
0608         return -EOPNOTSUPP;
0609     }
0610 
0611     return 0;
0612 }
0613 EXPORT_SYMBOL(nd_pfn_validate);
0614 
0615 int nd_pfn_probe(struct device *dev, struct nd_namespace_common *ndns)
0616 {
0617     int rc;
0618     struct nd_pfn *nd_pfn;
0619     struct device *pfn_dev;
0620     struct nd_pfn_sb *pfn_sb;
0621     struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
0622 
0623     if (ndns->force_raw)
0624         return -ENODEV;
0625 
0626     switch (ndns->claim_class) {
0627     case NVDIMM_CCLASS_NONE:
0628     case NVDIMM_CCLASS_PFN:
0629         break;
0630     default:
0631         return -ENODEV;
0632     }
0633 
0634     nvdimm_bus_lock(&ndns->dev);
0635     nd_pfn = nd_pfn_alloc(nd_region);
0636     pfn_dev = nd_pfn_devinit(nd_pfn, ndns);
0637     nvdimm_bus_unlock(&ndns->dev);
0638     if (!pfn_dev)
0639         return -ENOMEM;
0640     pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
0641     nd_pfn = to_nd_pfn(pfn_dev);
0642     nd_pfn->pfn_sb = pfn_sb;
0643     rc = nd_pfn_validate(nd_pfn, PFN_SIG);
0644     dev_dbg(dev, "pfn: %s\n", rc == 0 ? dev_name(pfn_dev) : "<none>");
0645     if (rc < 0) {
0646         nd_detach_ndns(pfn_dev, &nd_pfn->ndns);
0647         put_device(pfn_dev);
0648     } else
0649         nd_device_register(pfn_dev);
0650 
0651     return rc;
0652 }
0653 EXPORT_SYMBOL(nd_pfn_probe);
0654 
0655 /*
0656  * We hotplug memory at sub-section granularity, pad the reserved area
0657  * from the previous section base to the namespace base address.
0658  */
0659 static unsigned long init_altmap_base(resource_size_t base)
0660 {
0661     unsigned long base_pfn = PHYS_PFN(base);
0662 
0663     return SUBSECTION_ALIGN_DOWN(base_pfn);
0664 }
0665 
0666 static unsigned long init_altmap_reserve(resource_size_t base)
0667 {
0668     unsigned long reserve = nd_info_block_reserve() >> PAGE_SHIFT;
0669     unsigned long base_pfn = PHYS_PFN(base);
0670 
0671     reserve += base_pfn - SUBSECTION_ALIGN_DOWN(base_pfn);
0672     return reserve;
0673 }
0674 
0675 static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
0676 {
0677     struct range *range = &pgmap->range;
0678     struct vmem_altmap *altmap = &pgmap->altmap;
0679     struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
0680     u64 offset = le64_to_cpu(pfn_sb->dataoff);
0681     u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
0682     u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
0683     u32 reserve = nd_info_block_reserve();
0684     struct nd_namespace_common *ndns = nd_pfn->ndns;
0685     struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
0686     resource_size_t base = nsio->res.start + start_pad;
0687     resource_size_t end = nsio->res.end - end_trunc;
0688     struct vmem_altmap __altmap = {
0689         .base_pfn = init_altmap_base(base),
0690         .reserve = init_altmap_reserve(base),
0691         .end_pfn = PHYS_PFN(end),
0692     };
0693 
0694     *range = (struct range) {
0695         .start = nsio->res.start + start_pad,
0696         .end = nsio->res.end - end_trunc,
0697     };
0698     pgmap->nr_range = 1;
0699     if (nd_pfn->mode == PFN_MODE_RAM) {
0700         if (offset < reserve)
0701             return -EINVAL;
0702         nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
0703     } else if (nd_pfn->mode == PFN_MODE_PMEM) {
0704         nd_pfn->npfns = PHYS_PFN((range_len(range) - offset));
0705         if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
0706             dev_info(&nd_pfn->dev,
0707                     "number of pfns truncated from %lld to %ld\n",
0708                     le64_to_cpu(nd_pfn->pfn_sb->npfns),
0709                     nd_pfn->npfns);
0710         memcpy(altmap, &__altmap, sizeof(*altmap));
0711         altmap->free = PHYS_PFN(offset - reserve);
0712         altmap->alloc = 0;
0713         pgmap->flags |= PGMAP_ALTMAP_VALID;
0714     } else
0715         return -ENXIO;
0716 
0717     return 0;
0718 }
0719 
0720 static int nd_pfn_init(struct nd_pfn *nd_pfn)
0721 {
0722     struct nd_namespace_common *ndns = nd_pfn->ndns;
0723     struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
0724     resource_size_t start, size;
0725     struct nd_region *nd_region;
0726     unsigned long npfns, align;
0727     u32 end_trunc;
0728     struct nd_pfn_sb *pfn_sb;
0729     phys_addr_t offset;
0730     const char *sig;
0731     u64 checksum;
0732     int rc;
0733 
0734     pfn_sb = devm_kmalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
0735     if (!pfn_sb)
0736         return -ENOMEM;
0737 
0738     nd_pfn->pfn_sb = pfn_sb;
0739     if (is_nd_dax(&nd_pfn->dev))
0740         sig = DAX_SIG;
0741     else
0742         sig = PFN_SIG;
0743 
0744     rc = nd_pfn_validate(nd_pfn, sig);
0745     if (rc == 0)
0746         return nd_pfn_clear_memmap_errors(nd_pfn);
0747     if (rc != -ENODEV)
0748         return rc;
0749 
0750     /* no info block, do init */;
0751     memset(pfn_sb, 0, sizeof(*pfn_sb));
0752 
0753     nd_region = to_nd_region(nd_pfn->dev.parent);
0754     if (nd_region->ro) {
0755         dev_info(&nd_pfn->dev,
0756                 "%s is read-only, unable to init metadata\n",
0757                 dev_name(&nd_region->dev));
0758         return -ENXIO;
0759     }
0760 
0761     /*
0762      * Note, we use 64 here for the standard size of struct page,
0763      * debugging options may cause it to be larger in which case the
0764      * implementation will limit the pfns advertised through
0765      * ->direct_access() to those that are included in the memmap.
0766      */
0767     start = nsio->res.start;
0768     size = resource_size(&nsio->res);
0769     npfns = PHYS_PFN(size - SZ_8K);
0770     align = max(nd_pfn->align, memremap_compat_align());
0771 
0772     /*
0773      * When @start is misaligned fail namespace creation. See
0774      * the 'struct nd_pfn_sb' commentary on why ->start_pad is not
0775      * an option.
0776      */
0777     if (!IS_ALIGNED(start, memremap_compat_align())) {
0778         dev_err(&nd_pfn->dev, "%s: start %pa misaligned to %#lx\n",
0779                 dev_name(&ndns->dev), &start,
0780                 memremap_compat_align());
0781         return -EINVAL;
0782     }
0783     end_trunc = start + size - ALIGN_DOWN(start + size, align);
0784     if (nd_pfn->mode == PFN_MODE_PMEM) {
0785         /*
0786          * The altmap should be padded out to the block size used
0787          * when populating the vmemmap. This *should* be equal to
0788          * PMD_SIZE for most architectures.
0789          *
0790          * Also make sure size of struct page is less than 64. We
0791          * want to make sure we use large enough size here so that
0792          * we don't have a dynamic reserve space depending on
0793          * struct page size. But we also want to make sure we notice
0794          * when we end up adding new elements to struct page.
0795          */
0796         BUILD_BUG_ON(sizeof(struct page) > MAX_STRUCT_PAGE_SIZE);
0797         offset = ALIGN(start + SZ_8K + MAX_STRUCT_PAGE_SIZE * npfns, align)
0798             - start;
0799     } else if (nd_pfn->mode == PFN_MODE_RAM)
0800         offset = ALIGN(start + SZ_8K, align) - start;
0801     else
0802         return -ENXIO;
0803 
0804     if (offset >= size) {
0805         dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
0806                 dev_name(&ndns->dev));
0807         return -ENXIO;
0808     }
0809 
0810     npfns = PHYS_PFN(size - offset - end_trunc);
0811     pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
0812     pfn_sb->dataoff = cpu_to_le64(offset);
0813     pfn_sb->npfns = cpu_to_le64(npfns);
0814     memcpy(pfn_sb->signature, sig, PFN_SIG_LEN);
0815     memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
0816     memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
0817     pfn_sb->version_major = cpu_to_le16(1);
0818     pfn_sb->version_minor = cpu_to_le16(4);
0819     pfn_sb->end_trunc = cpu_to_le32(end_trunc);
0820     pfn_sb->align = cpu_to_le32(nd_pfn->align);
0821     pfn_sb->page_struct_size = cpu_to_le16(MAX_STRUCT_PAGE_SIZE);
0822     pfn_sb->page_size = cpu_to_le32(PAGE_SIZE);
0823     checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
0824     pfn_sb->checksum = cpu_to_le64(checksum);
0825 
0826     rc = nd_pfn_clear_memmap_errors(nd_pfn);
0827     if (rc)
0828         return rc;
0829 
0830     return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb), 0);
0831 }
0832 
0833 /*
0834  * Determine the effective resource range and vmem_altmap from an nd_pfn
0835  * instance.
0836  */
0837 int nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
0838 {
0839     int rc;
0840 
0841     if (!nd_pfn->uuid || !nd_pfn->ndns)
0842         return -ENODEV;
0843 
0844     rc = nd_pfn_init(nd_pfn);
0845     if (rc)
0846         return rc;
0847 
0848     /* we need a valid pfn_sb before we can init a dev_pagemap */
0849     return __nvdimm_setup_pfn(nd_pfn, pgmap);
0850 }
0851 EXPORT_SYMBOL_GPL(nvdimm_setup_pfn);