-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sysfs: minimize heap allocations of sysfs paths
11a0918 ("nvme: allow to overwrite base sysfs path") added support for changing the sysfs path via an environment variable. Unfortunately, it added a heap allocation every time a sysfs path was requested. Modify the callers to not free the paths, which allows a string constant to be returned if the path isn't overridden, avoiding an allocation. Cache the path in a static variable so that if it is overridden, the heap-allocated string only needs to be constructed once and afterwards can be reused. Create a file sysfs.c to consolidate this logic instead of spreading them across 3 files. Also introduce a helper to factor out the duplicated code. Fixes: 11a0918 ("nvme: allow to overwrite base sysfs path") Signed-off-by: Caleb Sander Mateos <[email protected]>
- Loading branch information
1 parent
c2f23b3
commit 6cf4c71
Showing
6 changed files
with
102 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,68 +6,12 @@ | |
* Authors: Keith Busch <[email protected]> | ||
* Chaitanya Kulkarni <[email protected]> | ||
*/ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <dirent.h> | ||
#include <libgen.h> | ||
|
||
#include <sys/param.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#include "filters.h" | ||
#include "types.h" | ||
#include "util.h" | ||
#include "cleanup.h" | ||
|
||
#define PATH_SYSFS_NVME "/sys/class/nvme" | ||
#define PATH_SYSFS_NVME_SUBSYSTEM "/sys/class/nvme-subsystem" | ||
#define PATH_SYSFS_BLOCK "/sys/block" | ||
|
||
char *nvme_ctrl_sysfs_dir(void) | ||
{ | ||
char *basepath = getenv("LIBNVME_SYSFS_PATH"); | ||
char *str; | ||
|
||
if (!basepath) | ||
return strdup(PATH_SYSFS_NVME); | ||
|
||
if (!asprintf(&str, "%s" PATH_SYSFS_NVME, basepath)) | ||
return NULL; | ||
|
||
return str; | ||
} | ||
|
||
char *nvme_ns_sysfs_dir(void) | ||
{ | ||
char *basepath = getenv("LIBNVME_SYSFS_PATH"); | ||
char *str; | ||
|
||
if (!basepath) | ||
return strdup(PATH_SYSFS_BLOCK); | ||
|
||
if (!asprintf(&str, "%s" PATH_SYSFS_BLOCK, basepath)) | ||
return NULL; | ||
|
||
return str; | ||
} | ||
|
||
char *nvme_subsys_sysfs_dir(void) | ||
{ | ||
char *basepath = getenv("LIBNVME_SYSFS_PATH"); | ||
char *str; | ||
|
||
if (!basepath) | ||
return strdup(PATH_SYSFS_NVME_SUBSYSTEM); | ||
|
||
if (!asprintf(&str, "%s" PATH_SYSFS_NVME_SUBSYSTEM, basepath)) | ||
return NULL; | ||
|
||
return str; | ||
} | ||
#include "private.h" | ||
|
||
int nvme_namespace_filter(const struct dirent *d) | ||
{ | ||
|
@@ -132,7 +76,7 @@ int nvme_subsys_filter(const struct dirent *d) | |
|
||
int nvme_scan_subsystems(struct dirent ***subsys) | ||
{ | ||
_cleanup_free_ char *dir = nvme_subsys_sysfs_dir(); | ||
const char *dir = nvme_subsys_sysfs_dir(); | ||
|
||
return scandir(dir, subsys, nvme_subsys_filter, alphasort); | ||
} | ||
|
@@ -145,7 +89,7 @@ int nvme_scan_subsystem_namespaces(nvme_subsystem_t s, struct dirent ***ns) | |
|
||
int nvme_scan_ctrls(struct dirent ***ctrls) | ||
{ | ||
_cleanup_free_ char *dir = nvme_ctrl_sysfs_dir(); | ||
const char *dir = nvme_ctrl_sysfs_dir(); | ||
|
||
return scandir(dir, ctrls, nvme_ctrls_filter, alphasort); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include "private.h" | ||
|
||
#define PATH_UUID_IBM "/proc/device-tree/ibm,partition-uuid" | ||
#define PATH_SYSFS_BLOCK "/sys/block" | ||
#define PATH_SYSFS_SLOTS "/sys/bus/pci/slots" | ||
#define PATH_SYSFS_NVME_SUBSYSTEM "/sys/class/nvme-subsystem" | ||
#define PATH_SYSFS_NVME "/sys/class/nvme" | ||
#define PATH_DMI_ENTRIES "/sys/firmware/dmi/entries" | ||
|
||
static const char *make_sysfs_dir(const char *path) | ||
{ | ||
char *basepath = getenv("LIBNVME_SYSFS_PATH"); | ||
char *str; | ||
|
||
if (!basepath) | ||
return path; | ||
|
||
asprintf(&str, "%s%s", basepath, path); | ||
return str; | ||
} | ||
|
||
const char *nvme_subsys_sysfs_dir(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_SYSFS_NVME_SUBSYSTEM); | ||
} | ||
|
||
const char *nvme_ctrl_sysfs_dir(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_SYSFS_NVME); | ||
} | ||
|
||
const char *nvme_ns_sysfs_dir(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_SYSFS_BLOCK); | ||
} | ||
|
||
const char *nvme_slots_sysfs_dir(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_SYSFS_SLOTS); | ||
} | ||
|
||
const char *nvme_uuid_ibm_filename(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_UUID_IBM); | ||
} | ||
|
||
const char *nvme_dmi_entries_dir(void) | ||
{ | ||
static const char *str = NULL; | ||
|
||
if (str) | ||
return str; | ||
|
||
return str = make_sysfs_dir(PATH_DMI_ENTRIES); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters