From db12b8c9c0eca9a7145a85808b48a1ca7f6e66e5 Mon Sep 17 00:00:00 2001 From: Ikki Zhu <79439153+qnos@users.noreply.github.com> Date: Wed, 21 Jun 2023 07:21:08 +0800 Subject: [PATCH] dx010 fix possible cpld race read issue (#15339) #### Why I did it fix possible cpld race read issue between watchdog and reboot cause process ##### Work item tracking - Microsoft ADO **(number only)**: #### How I did it Use flock to limit parallel access to cpld sys file #### How to verify it It can be simulate and verified with following python script ```python3 import signal import subprocess import threading exit_flag = False def run_command(cmd): status = True result = "" try: p = subprocess.Popen( cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) raw_data, err = p.communicate() if err == '': result = raw_data.strip() except: status = False return status, result def get_cpld_reg_value(getreg_path, register): #cmd = "echo {1} > {0}; cat {0}".format(getreg_path, register) cmd = "flock {0} -c 'echo {1} > {0}; cat {0}'".format(getreg_path, register) status, result = run_command(cmd) return result if status else None def cpld_read(thread_num, cpld_reg): while not exit_flag: val = get_cpld_reg_value("/sys/devices/platform/dx010_cpld/getreg", cpld_reg) print(f"Thread {thread_num}: get cpld reg {cpld_reg}, value {val}") def signal_handler(sig, frame): global exit_flag print("Ctrl+C detected. Quitting...") exit_flag = True if __name__ == '__main__': # Register the signal handler for Ctrl+C signal.signal(signal.SIGINT, signal_handler) t1 = threading.Thread(target=cpld_read, args=(1, '0x103',)) t2 = threading.Thread(target=cpld_read, args=(2, '0x141',)) t1.start() t2.start() t1.join() t2.join() ``` --- .../x86_64-cel_seastone-r0/sonic_platform/component.py | 2 +- .../celestica/x86_64-cel_seastone-r0/sonic_platform/helper.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py index 03f879805a..5b71c0a38a 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/component.py @@ -54,7 +54,7 @@ class Component(ComponentBase): def get_register_value(self, register): # Retrieves the cpld register value - cmd = "echo {1} > {0}; cat {0}".format(GETREG_PATH, register) + cmd = "flock {0} -c 'echo {1} > {0}; cat {0}'".format(GETREG_PATH, register) p = subprocess.Popen( cmd, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) raw_data, err = p.communicate() diff --git a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/helper.py b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/helper.py index 140c62c086..61bc676540 100644 --- a/device/celestica/x86_64-cel_seastone-r0/sonic_platform/helper.py +++ b/device/celestica/x86_64-cel_seastone-r0/sonic_platform/helper.py @@ -77,7 +77,7 @@ class APIHelper(): return True def get_cpld_reg_value(self, getreg_path, register): - cmd = "echo {1} > {0}; cat {0}".format(getreg_path, register) + cmd = "flock {0} -c 'echo {1} > {0}; cat {0}'".format(getreg_path, register) status, result = self.run_command(cmd) return result if status else None