Skip to contentSkip to conversation

tools/counter: Add checking directory exists for make clean

PATCH In review Anh Tuan Phan 3 participants 6 messages Jul 7, 2023 – Jul 14, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/counter: Add checking directory exists for make clean

rmdir requires the directory exist so it causes "make -C tools clean" failed if someone only builds other tools but not counter. This commit adds checking the directory exist before doing rmdir.

---
 tools/counter/Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/counter/Makefile +3 -1
index a0f4cab71fe5..2907f3b3094b 100644
@@ -40,7 +40,9 @@ $(OUTPUT)counter_example: $(COUNTER_EXAMPLE)
 clean:
 	rm -f $(ALL_PROGRAMS)
 	rm -rf $(OUTPUT)include/linux/counter.h
-	rmdir -p $(OUTPUT)include/linux
+	@if [ -d $(OUTPUT)include/linux ]; then \
+		rmdir -p $(OUTPUT)include/linux; \
+	fi
 	find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete
 
 install: $(ALL_PROGRAMS)
Signature · 5 lines
2.34.1
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 26 lines · Anh Tuan Phan

rmdir requires the directory exist so it causes "make -C tools clean" failed if someone only builds other tools but not counter. This commit adds checking the directory exist before doing rmdir.

---
 tools/counter/Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/counter/Makefile +3 -1
index a0f4cab71fe5..2907f3b3094b 100644
@@ -40,7 +40,9 @@ $(OUTPUT)counter_example: $(COUNTER_EXAMPLE)
 clean:
 	rm -f $(ALL_PROGRAMS)
 	rm -rf $(OUTPUT)include/linux/counter.h
-	rmdir -p $(OUTPUT)include/linux
+	@if [ -d $(OUTPUT)include/linux ]; then \
+		rmdir -p $(OUTPUT)include/linux; \
+	fi
 	find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete
 
 install: $(ALL_PROGRAMS)
Signature · 1 line
2.34.1

Hi Anh,

Please CC <[email protected]> and <[email protected]> as well in the future so Counter users and developers can become aware of relevant patches.

One worry I have with this approach is the possible race condition where the check for existence succeeds but the directory is deleted by another agent before our rmdir executes. However, I'm not sure how we could achieve such behavior atomically to prevent the issue.

One alternative I've considered is perhaps a single find command to search for and delete empty directories:

    find $(or $(OUTPUT),.) -type d -empty -delete

But this will delete directories not created by the makefile which I consider an unexpected behavior for the user (or at least very rude of the script to do).

Perhaps we should delete the directory tree explicitly:

    rm -df $(OUTPUT)include/linux
    rm -df $(OUTPUT)include

Although we lose the symmetry of rmdir to our previous mkdir, this should prevent the race condition issue and succeed whether the directories still exist or not.

William Breathitt Gray

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#2 lore raw

Hi William, the race condition possibility is a good point. Thanks for suggesting me. Will send a new version with using rm -df instead and add the appropriate CC lists. Thank you!

Quoted 58 lines · William Breathitt Gray
Quoted 26 lines · Anh Tuan Phan

rmdir requires the directory exist so it causes "make -C tools clean" failed if someone only builds other tools but not counter. This commit adds checking the directory exist before doing rmdir.

---
 tools/counter/Makefile | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/counter/Makefile +3 -1
index a0f4cab71fe5..2907f3b3094b 100644
@@ -40,7 +40,9 @@ $(OUTPUT)counter_example: $(COUNTER_EXAMPLE)
 clean:
 	rm -f $(ALL_PROGRAMS)
 	rm -rf $(OUTPUT)include/linux/counter.h
-	rmdir -p $(OUTPUT)include/linux
+	@if [ -d $(OUTPUT)include/linux ]; then \
+		rmdir -p $(OUTPUT)include/linux; \
+	fi
 	find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete
 
 install: $(ALL_PROGRAMS)
Signature · 1 line
2.34.1

Hi Anh,

Please CC <[email protected]> and <[email protected]> as well in the future so Counter users and developers can become aware of relevant patches.

One worry I have with this approach is the possible race condition where the check for existence succeeds but the directory is deleted by another agent before our rmdir executes. However, I'm not sure how we could achieve such behavior atomically to prevent the issue.

One alternative I've considered is perhaps a single find command to search for and delete empty directories:

    find $(or $(OUTPUT),.) -type d -empty -delete

But this will delete directories not created by the makefile which I consider an unexpected behavior for the user (or at least very rude of the script to do).

Perhaps we should delete the directory tree explicitly:

    rm -df $(OUTPUT)include/linux
    rm -df $(OUTPUT)include

Although we lose the symmetry of rmdir to our previous mkdir, this should prevent the race condition issue and succeed whether the directories still exist or not.

William Breathitt Gray

#3 lore raw
Quoted 3 lines · Anh Tuan Phan

Hi William, the race condition possibility is a good point. Thanks for suggesting me. Will send a new version with using rm -df instead and add the appropriate CC lists. Thank you!

Hi Anh,

Because this will fix a failure, please include a Fixes tag as well that references the commit that introduces the rmdir line.

Thanks,

William Breathitt Gray

#4 lore raw
In reply to Anh Tuan Phan
Quoted 14 lines · Anh Tuan Phan

rmdir requires the directory exist so it causes "make -C tools clean" failed if someone only builds other tools but not counter. This commit adds checking the directory exist before doing rmdir.

---
  tools/counter/Makefile | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
tools/counter/Makefile +0 -0
index a0f4cab71fe5..2907f3b3094b 100644

tools/counter/Makefile $(OUTPUT)counter_example: $(COUNTER_EXAMPLE) · line 48

Quoted 10 lines
tools/counter/Makefile +3 -1
@@ -40,7 +40,9 @@ $(OUTPUT)counter_example: $(COUNTER_EXAMPLE)
  clean:
  	rm -f $(ALL_PROGRAMS)
  	rm -rf $(OUTPUT)include/linux/counter.h
-	rmdir -p $(OUTPUT)include/linux
+	@if [ -d $(OUTPUT)include/linux ]; then \
+		rmdir -p $(OUTPUT)include/linux; \
+	fi
  	find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete
 
  install: $(ALL_PROGRAMS)

Please cc appropriate linux kernel mailing lists and reviewers. You can find them by running get_maintainers.pl script.

thanks,
-- Shuah

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#5 lore raw
Quoted 24 lines · Shuah Khan
Quoted 23 lines · Anh Tuan Phan

rmdir requires the directory exist so it causes "make -C tools clean" failed if someone only builds other tools but not counter. This commit adds checking the directory exist before doing rmdir.

---
  tools/counter/Makefile | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
tools/counter/Makefile +0 -0
index a0f4cab71fe5..2907f3b3094b 100644
@@ -40,7 +40,9 @@ $(OUTPUT)counter_example: $(COUNTER_EXAMPLE)

  clean:
      rm -f $(ALL_PROGRAMS)
      rm -rf $(OUTPUT)include/linux/counter.h
-    rmdir -p $(OUTPUT)include/linux
+    @if [ -d $(OUTPUT)include/linux ]; then \
+        rmdir -p $(OUTPUT)include/linux; \
+    fi
      find $(or $(OUTPUT),.) -name '*.o' -delete -o -name '\.*.d' -delete

Quoted 7 lines
Quoted 1 line

  install: $(ALL_PROGRAMS)

Please cc appropriate linux kernel mailing lists and reviewers. You can find them by running get_maintainers.pl script.

thanks,
-- Shuah

Thanks Shuah, will update the CC list as your suggestion.

Quoted 2 lines

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees