Skip to contentSkip to conversation

remove duplicate ifdefs

RFC PATCH Superseded Shrikanth Hegde 3 participants 7 messages Jan 18, 2024 – Jan 22, 2024

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shrikanth Hegde

author reviewer
#0 lore raw

cover remove duplicate ifdefs

When going through the code observed a case in scheduler, where #ifdef CONFIG_SMP was used to inside an #ifdef CONFIG_SMP. That didn't make sense since first one is good enough and second one is a duplicate.

This could improve code readability. No functional change is intended. Maybe this is not an issue these days as language servers can parse the config and users can read the code without bothering about whats true and whats not.

Does this change makes sense?

Since this might be present in other code areas wrote a very basic python script which helps in finding these cases. It doesn't handle any complicated #defines or space separated "# if". At some places the log collected had to be manually corrected due to space separated ifdefs. Thats why its not a treewide change.
There might be an opportunity for other files as well.

Logic is very simple. If there is #ifdef or #if or #ifndef add that variable to list. Upon every subsequent #ifdef or #if or #ifndef check if the same variable is in the list. If yes flag
an error. Verification was done manually later checking for any #undef or any error due to script. These were the ones that flagged out and made sense after going through code.

ifdefs were collected using grep in below way and that file was used as
the input to the script.
grep -rIwn --include="*.c*" --include="*.h" -e "#if" -e "#ifndef" -e "#ifdef" -e "#else" -e "#endif" * > /tmp/input.txt

---------------------------------------------------------------------
script used:
---------------------------------------------------------------------
import os
import argparse

def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--file",
help="file to input to script",
type=str)
parser.add_argument("--verbose",
help="Print additional debugging info, 0 to disable ", type=int)
args = parser.parse_args()
return args

def parseFiles(args):
file_to_parse = open(args.file, "r")
lines = file_to_parse.readlines()
check_length = len(lines)
ifdefs_list = []
i=0

while i < check_length:
line = lines[i]
last_word = line.strip().split(":")[2]
last_word = last_word.split("/")[0]

if (args.verbose):
print(line)
last_word_splits = last_word.split()
if (args.verbose):
print(last_word_splits)
if last_word_splits[0] == "#ifdef" or last_word_splits[0] == "#ifndef" or last_word_splits[0] == "#if":
if last_word_splits[1] in ifdefs_list:
print("This is duplicate and may be fixed: %s, parent_list:\n" % (line)) print(ifdefs_list)
ifdefs_list.append(last_word_splits[1])
if last_word_splits[0] == "#endif"":
ifdefs_list.pop()

i=i+1

if __name__ == "__main__":
args = parse_args()
parseFiles(args)
-------------------------------------------------------------------------

Shrikanth Hegde (3):
sched: remove duplicate ifdefs
fs: remove depulicate ifdefs
arch/powerpc: remove duplicate ifdefs

 arch/powerpc/include/asm/paca.h           | 4 ----
 arch/powerpc/kernel/asm-offsets.c         | 2 --
 arch/powerpc/platforms/powermac/feature.c | 2 --
 arch/powerpc/xmon/xmon.c                  | 2 --
 fs/ntfs/inode.c                           | 2 --
 fs/xfs/xfs_sysfs.c                        | 4 ----
 kernel/sched/core.c                       | 4 +---
 kernel/sched/fair.c                       | 2 --
 8 files changed, 1 insertion(+), 21 deletions(-)

--
2.39.3

author reviewer
#1 lore raw

RFC PATCH 1/3 sched: remove duplicate ifdefs

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

In the scheduler code, there are two places where above pattern can be observed. Hence second ifdef is a duplicate and not needed. Plus a minor comment update to reflect the else case.

No functional change is intended here. It only aims to improve code readability.

---
 kernel/sched/core.c | 4 +---
 kernel/sched/fair.c | 2 --
 2 files changed, 1 insertion(+), 5 deletions(-)
kernel/sched/core.c +1 -3
index 038eeaf76d2d..1bfb186fd67f 100644
@@ -1792,7 +1792,6 @@ static void cpu_util_update_eff(struct cgroup_subsys_state *css);
 #endif
 
 #ifdef CONFIG_SYSCTL
-#ifdef CONFIG_UCLAMP_TASK
 #ifdef CONFIG_UCLAMP_TASK_GROUP
 static void uclamp_update_root_tg(void)
 {
@@ -1898,7 +1897,6 @@ static int sysctl_sched_uclamp_handler(struct ctl_table *table, int write,
 	return result;
 }
 #endif
-#endif
 
 static int uclamp_validate(struct task_struct *p,
 			   const struct sched_attr *attr)
@@ -2065,7 +2063,7 @@ static void __init init_uclamp(void)
 	}
 }
 
-#else /* CONFIG_UCLAMP_TASK */
+#else /* !CONFIG_UCLAMP_TASK */
 static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { }
 static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
 static inline int uclamp_validate(struct task_struct *p,
kernel/sched/fair.c +0 -2
index f2bb83675e4a..6158a6752c25 100644
@@ -10166,10 +10166,8 @@ static int idle_cpu_without(int cpu, struct task_struct *p)
 	 * be computed and tested before calling idle_cpu_without().
 	 */
 
-#ifdef CONFIG_SMP
 	if (rq->ttwu_pending)
 		return 0;
-#endif
 
 	return 1;
 }

--
2.39.3

author reviewer
#2 lore raw

RFC PATCH 2/3 fs: remove duplicate ifdefs

In reply to Shrikanth Hegde

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

There are few places in fs code where above pattern was seen. No functional change is intended here. It only aims to improve code readability.

---
 fs/ntfs/inode.c    | 2 --
 fs/xfs/xfs_sysfs.c | 4 ----
 2 files changed, 6 deletions(-)
fs/ntfs/inode.c +0 -2
index aba1e22db4e9..d2c8622d53d1 100644
@@ -2859,11 +2859,9 @@ int ntfs_truncate(struct inode *vi)
  *
  * See ntfs_truncate() description above for details.
  */
-#ifdef NTFS_RW
 void ntfs_truncate_vfs(struct inode *vi) {
 	ntfs_truncate(vi);
 }
-#endif
 
 /**
  * ntfs_setattr - called from notify_change() when an attribute is being changed
fs/xfs/xfs_sysfs.c +0 -4
index 17485666b672..d2391eec37fe 100644
@@ -193,7 +193,6 @@ always_cow_show(
 }
 XFS_SYSFS_ATTR_RW(always_cow);
 
-#ifdef DEBUG
 /*
  * Override how many threads the parallel work queue is allowed to create.
  * This has to be a debug-only global (instead of an errortag) because one of
@@ -260,7 +259,6 @@ larp_show(
 	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.larp);
 }
 XFS_SYSFS_ATTR_RW(larp);
-#endif /* DEBUG */
 
 STATIC ssize_t
 bload_leaf_slack_store(
@@ -319,10 +317,8 @@ static struct attribute *xfs_dbg_attrs[] = {
 	ATTR_LIST(log_recovery_delay),
 	ATTR_LIST(mount_delay),
 	ATTR_LIST(always_cow),
-#ifdef DEBUG
 	ATTR_LIST(pwork_threads),
 	ATTR_LIST(larp),
-#endif
 	ATTR_LIST(bload_leaf_slack),
 	ATTR_LIST(bload_node_slack),
 	NULL,

--
2.39.3

Reviewed-by on v1 2/3
#3 lore raw

Re: [RFC PATCH 2/3] fs: remove duplicate ifdefs

Quoted 58 lines · Shrikanth Hegde

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

There are few places in fs code where above pattern was seen. No functional change is intended here. It only aims to improve code readability.

---
 fs/ntfs/inode.c    | 2 --
 fs/xfs/xfs_sysfs.c | 4 ----
 2 files changed, 6 deletions(-)
fs/ntfs/inode.c +0 -2
index aba1e22db4e9..d2c8622d53d1 100644
@@ -2859,11 +2859,9 @@ int ntfs_truncate(struct inode *vi)
  *
  * See ntfs_truncate() description above for details.
  */
-#ifdef NTFS_RW
 void ntfs_truncate_vfs(struct inode *vi) {
 	ntfs_truncate(vi);
 }
-#endif
 
 /**
  * ntfs_setattr - called from notify_change() when an attribute is being changed
fs/xfs/xfs_sysfs.c +0 -2
index 17485666b672..d2391eec37fe 100644
@@ -193,7 +193,6 @@ always_cow_show(
 }
 XFS_SYSFS_ATTR_RW(always_cow);
 
-#ifdef DEBUG
 /*
  * Override how many threads the parallel work queue is allowed to create.
  * This has to be a debug-only global (instead of an errortag) because one of
@@ -260,7 +259,6 @@ larp_show(
 	return snprintf(buf, PAGE_SIZE, "%d\n", xfs_globals.larp);
 }
 XFS_SYSFS_ATTR_RW(larp);
-#endif /* DEBUG */
 
 STATIC ssize_t
 bload_leaf_slack_store(

fs/xfs/xfs_sysfs.c static struct attribute *xfs_dbg_attrs[] = { · line 325

Quoted 7 lines
fs/xfs/xfs_sysfs.c +0 -2
@@ -319,10 +317,8 @@ static struct attribute *xfs_dbg_attrs[] = {
 	ATTR_LIST(log_recovery_delay),
 	ATTR_LIST(mount_delay),
 	ATTR_LIST(always_cow),
-#ifdef DEBUG
 	ATTR_LIST(pwork_threads),
 	ATTR_LIST(larp),
-#endif

The xfs part seems fine to me bcause I think some bot already complained about this...

--D

Quoted 7 lines
fs/xfs/xfs_sysfs.c +0 -1
 	ATTR_LIST(bload_leaf_slack),
 	ATTR_LIST(bload_node_slack),
 	NULL,
--

2.39.3

#4 lore raw

Re: [RFC PATCH 2/3] fs: remove duplicate ifdefs

In reply to Shrikanth Hegde
Quoted 11 lines · Shrikanth Hegde

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

Quoted 4 lines

There are few places in fs code where above pattern was seen. No functional change is intended here. It only aims to improve code readability.

Can you please post the xfs changes as a separate patch along with Darrick's RVB tag? This will make it easy for me to apply the resulting patch to the XFS tree.

Signature · 1 line
Chandan
author reviewer
#5 lore raw

Re: [RFC PATCH 2/3] fs: remove duplicate ifdefs

Quoted 12 lines · Chandan Babu R
Quoted 11 lines · Shrikanth Hegde

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

Quoted 8 lines
Quoted 4 lines

There are few places in fs code where above pattern was seen. No functional change is intended here. It only aims to improve code readability.

Can you please post the xfs changes as a separate patch along with Darrick's RVB tag? This will make it easy for me to apply the resulting patch to the XFS tree.

Ok. will split the fs patches into two and send v2 soon.

Thanks.

Quoted 1 line
author reviewer
#6 lore raw

RFC PATCH 3/3 arch/powerpc: remove duplicate ifdefs

In reply to Shrikanth Hegde

when a ifdef is used in the below manner, second one could be considered as duplicate.

ifdef DEFINE_A
...code block...
ifdef DEFINE_A
...code block...
endif
...code block...
endif

few places in arch/powerpc where this pattern was seen. In addition to that in paca.h, CONFIG_PPC_BOOK3S_64 was defined back to back. merged the two ifdefs.

No functional change is intended here. It only aims to improve code readability.

---
 arch/powerpc/include/asm/paca.h           | 4 ----
 arch/powerpc/kernel/asm-offsets.c         | 2 --
 arch/powerpc/platforms/powermac/feature.c | 2 --
 arch/powerpc/xmon/xmon.c                  | 2 --
 4 files changed, 10 deletions(-)
arch/powerpc/include/asm/paca.h +0 -4
index e667d455ecb4..1d58da946739 100644
@@ -163,9 +163,7 @@ struct paca_struct {
 	u64 kstack;			/* Saved Kernel stack addr */
 	u64 saved_r1;			/* r1 save for RTAS calls or PM or EE=0 */
 	u64 saved_msr;			/* MSR saved here by enter_rtas */
-#ifdef CONFIG_PPC64
 	u64 exit_save_r1;		/* Syscall/interrupt R1 save */
-#endif
 #ifdef CONFIG_PPC_BOOK3E_64
 	u16 trap_save;			/* Used when bad stack is encountered */
 #endif
@@ -214,8 +212,6 @@ struct paca_struct {
 	/* Non-maskable exceptions that are not performance critical */
 	u64 exnmi[EX_SIZE];	/* used for system reset (nmi) */
 	u64 exmc[EX_SIZE];	/* used for machine checks */
-#endif
-#ifdef CONFIG_PPC_BOOK3S_64
 	/* Exclusive stacks for system reset and machine check exception. */
 	void *nmi_emergency_sp;
 	void *mc_emergency_sp;
arch/powerpc/kernel/asm-offsets.c +0 -2
index 9f14d95b8b32..f029755f9e69 100644
@@ -246,9 +246,7 @@ int main(void)
 	OFFSET(PACAHWCPUID, paca_struct, hw_cpu_id);
 	OFFSET(PACAKEXECSTATE, paca_struct, kexec_state);
 	OFFSET(PACA_DSCR_DEFAULT, paca_struct, dscr_default);
-#ifdef CONFIG_PPC64
 	OFFSET(PACA_EXIT_SAVE_R1, paca_struct, exit_save_r1);
-#endif
 #ifdef CONFIG_PPC_BOOK3E_64
 	OFFSET(PACA_TRAP_SAVE, paca_struct, trap_save);
 #endif
arch/powerpc/platforms/powermac/feature.c +0 -2
index 81c9fbae88b1..2cc257f75c50 100644
@@ -2333,7 +2333,6 @@ static struct pmac_mb_def pmac_mb_defs[] = {
 		PMAC_TYPE_POWERMAC_G5,		g5_features,
 		0,
 	},
-#ifdef CONFIG_PPC64
 	{	"PowerMac7,3",			"PowerMac G5",
 		PMAC_TYPE_POWERMAC_G5,		g5_features,
 		0,
@@ -2359,7 +2358,6 @@ static struct pmac_mb_def pmac_mb_defs[] = {
 		0,
 	},
 #endif /* CONFIG_PPC64 */
-#endif /* CONFIG_PPC64 */
 };
 
 /*
arch/powerpc/xmon/xmon.c +0 -2
index b3b94cd37713..f413c220165c 100644
@@ -643,10 +643,8 @@ static int xmon_core(struct pt_regs *regs, volatile int fromipi)
 			touch_nmi_watchdog();
 		} else {
 			cmd = 1;
-#ifdef CONFIG_SMP
 			if (xmon_batch)
 				cmd = batch_cmds(regs);
-#endif
 			if (!locked_down && cmd)
 				cmd = cmds(regs);
 			if (locked_down || cmd != 0) {

--
2.39.3