From 08d0aa70fde8857b4801198b35994ba4ff2fc2b1 Mon Sep 17 00:00:00 2001 From: Luis Chamberlain Date: Mon, 15 Jul 2024 17:20:18 +0800 Subject: [PATCH 1/2] nvdimm/pmem: use add_disk() error handling mainline inclusion from mainline-v5.16-rc1 commit 5a192ccc32e2981f721343c750b8cfb4c3f41007 category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IAD301 CVE: NA Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5a192ccc32e2981f721343c750b8cfb4c3f41007 -------------------------------- Now that device_add_disk() supports returning an error, use that. We must unwind alloc_dax() on error. Reviewed-by: Christoph Hellwig Signed-off-by: Luis Chamberlain Link: https://lore.kernel.org/r/20211103230437.1639990-7-mcgrof@kernel.org Signed-off-by: Jens Axboe Conflicts: drivers/nvdimm/pmem.c [ Commit accf58afb689f81daadde("nvdimm/pmem: cleanup the disk if pmem_release_disk() is yet assigned") is not applied. ] Signed-off-by: Zhihao Cheng --- drivers/nvdimm/pmem.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/nvdimm/pmem.c b/drivers/nvdimm/pmem.c index d5dd79b59b16..9947cc2bd2fc 100644 --- a/drivers/nvdimm/pmem.c +++ b/drivers/nvdimm/pmem.c @@ -498,7 +498,13 @@ static int pmem_attach_disk(struct device *dev, gendev = disk_to_dev(disk); gendev->groups = pmem_attribute_groups; - device_add_disk(dev, disk, NULL); + rc = device_add_disk_safe(dev, disk, NULL); + if (rc) { + kill_dax(dax_dev); + put_dax(dax_dev); + put_disk(disk); + return rc; + } if (devm_add_action_or_reset(dev, pmem_release_disk, pmem)) return -ENOMEM; -- Gitee From b438e57f123fc748b995a825e6ef214095f1661b Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 15 Jul 2024 17:20:19 +0800 Subject: [PATCH 2/2] dax: alloc_dax() return ERR_PTR(-EOPNOTSUPP) for CONFIG_DAX=n mainline inclusion from mainline-v6.9-rc1 commit 6d439c18d9b190ab1e0f1196bc45590f95752bf1 category: bugfix bugzilla: https://gitee.com/openeuler/kernel/issues/IAD301 CVE: NA Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6d439c18d9b190ab1e0f1196bc45590f95752bf1 -------------------------------- Change the return value from NULL to PTR_ERR(-EOPNOTSUPP) for CONFIG_DAX=n to be consistent with the fact that CONFIG_DAX=y never returns NULL. This is done in preparation for using cpu_dcache_is_aliasing() in a following change which will properly support architectures which detect data cache aliasing at runtime. Link: https://lkml.kernel.org/r/20240215144633.96437-3-mathieu.desnoyers@efficios.com Fixes: 4e4ced93794a ("dax: Move mandatory ->zero_page_range() check in alloc_dax()") Signed-off-by: Mathieu Desnoyers Reviewed-by: Dan Williams Cc: Dan Williams Cc: Vishal Verma Cc: Dave Jiang Cc: Matthew Wilcox Cc: Arnd Bergmann Cc: Russell King Cc: Alasdair Kergon Cc: Christoph Hellwig Cc: Dave Chinner Cc: Heiko Carstens Cc: kernel test robot Cc: Michael Sclafani Cc: Mike Snitzer Cc: Mikulas Patocka Signed-off-by: Andrew Morton Signed-off-by: Zhihao Cheng --- drivers/dax/super.c | 5 +++++ include/linux/dax.h | 6 +----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/dax/super.c b/drivers/dax/super.c index 260a247c60d2..a2463320b86b 100644 --- a/drivers/dax/super.c +++ b/drivers/dax/super.c @@ -433,6 +433,11 @@ static int dax_host_hash(const char *host) * that any fault handlers or operations that might have seen * dax_alive(), have completed. Any operations that start after * synchronize_srcu() has run will abort upon seeing !dax_alive(). + * + * Note, because alloc_dax() returns an ERR_PTR() on error, callers + * typically store its result into a local variable in order to check + * the result. Therefore, care must be taken to populate the struct + * device dax_dev field make sure the dax_dev is not leaked. */ void kill_dax(struct dax_device *dax_dev) { diff --git a/include/linux/dax.h b/include/linux/dax.h index b52f084aa643..f6f26a66ead0 100644 --- a/include/linux/dax.h +++ b/include/linux/dax.h @@ -80,11 +80,7 @@ static inline struct dax_device *dax_get_by_host(const char *host) static inline struct dax_device *alloc_dax(void *private, const char *host, const struct dax_operations *ops, unsigned long flags) { - /* - * Callers should check IS_ENABLED(CONFIG_DAX) to know if this - * NULL is an error or expected. - */ - return NULL; + return ERR_PTR(-EOPNOTSUPP); } static inline void put_dax(struct dax_device *dax_dev) { -- Gitee