From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ms11p00im-qufo17291201.me.com (ms11p00im-qufo17291201.me.com [17.58.38.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3547A2E419 for ; Fri, 19 Jul 2024 12:42:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.58.38.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721392957; cv=none; b=qjJAO3MwFTBA4hX9YOuSRgOzGNUjutnJ8R771yV3SPDjBdHMQD35TnOiFmK0JfvwBgIF3H9NzTa9bml2GP1avycJjK04oQcZqCGfHbb4wy1e2b3N1xg8+e4ro+kameQYEq+6QxZune2RQ8T5FvVqCtnceRvCXUEwhikw2CCve4g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721392957; c=relaxed/simple; bh=mwzUxrsk/9rScZ1wWwFH1uGFUegCvA3Id5pUCFAbeic=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bLqEkrVsm2oEioQWO9xpzoymCpB4Nu4lYxqmxcijc9KuuLmQL9bbqjEaTPwk6vchOAlmNGc3WLKB1+yiMPnGgep30RdgIe1PXl2VdsY5V3lvIYOXo/0RNKEGTFpnUs9KgfSZ/jbEWMTqxQulYVaxAT2lVmjL+eap9tHeV6uox9k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com; spf=pass smtp.mailfrom=icloud.com; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b=f4Kq8w3L; arc=none smtp.client-ip=17.58.38.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=icloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=icloud.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=icloud.com header.i=@icloud.com header.b="f4Kq8w3L" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=1a1hai; t=1721392955; bh=exqzxrKJuQHr8RJcwLZ7c+564S4prGSgbviUlFL3wsU=; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; b=f4Kq8w3LFjI+6Vg/KIEPH0AcB08rsfyqsGhIT791aI3cDD3O5MIJTBU0xHAAr6Xix rN28TuymuOy8NvpmeN6QCW6Q9hNO61wlk5qR6gbe08rzCPMYNenVtfqkNrE7079fVV Qbi0xGybJPD6ckUnVY5rKjC6uDuOGATEHEbEssM74aOLj6R52aUt6OW84L0GrelY9r lA6ryqNm5qtih5td43X47/Yp4bvW4WxLoRTkCkK35on3BbuEkZ1nMKHj6L2eEGDbGg iMvPKFjhvpE7R8osO76bSQVaX0shDpzh0navQdzruf/Z5faLLaEnZeNBdRCn4qzDjW zBcS8KhSjkx5Q== Received: from [192.168.1.26] (ms11p00im-dlb-asmtpmailmevip.me.com [17.57.154.19]) by ms11p00im-qufo17291201.me.com (Postfix) with ESMTPSA id 48013C801BA; Fri, 19 Jul 2024 12:42:30 +0000 (UTC) Message-ID: <7ee69110-733e-4c43-83bc-65347d3cd270@icloud.com> Date: Fri, 19 Jul 2024 20:42:23 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] driver core: Fix error handling in driver API device_rename() To: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" , Benjamin Thery , "Eric W. Biederman" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, Zijun Hu References: <20240717-device_rename_fix-v1-1-54d85024518f@quicinc.com> <2024071756-uproot-relieve-6e27@gregkh> Content-Language: en-US From: Zijun Hu In-Reply-To: <2024071756-uproot-relieve-6e27@gregkh> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Proofpoint-GUID: 62_sPECnq4TmNCqmf52j84geIkQ4AMzI X-Proofpoint-ORIG-GUID: 62_sPECnq4TmNCqmf52j84geIkQ4AMzI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-07-19_06,2024-07-18_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 bulkscore=0 adultscore=0 spamscore=0 suspectscore=0 clxscore=1015 malwarescore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2407190097 On 2024/7/17 23:03, Greg Kroah-Hartman wrote: > On Wed, Jul 17, 2024 at 10:50:05PM +0800, Zijun Hu wrote: >> From: Zijun Hu >> >> Call failure of device_rename(@dev, @new_name) maybe unexpectedly change >> link name within @dev's class directory to @new_name, fixed by correcting >> error handling for the API. > > I'm sorry, but I don't understand the text here, what exactly are you > doing? > let me explain what is the issue by inline comments within present code firstly, i will make commit message clear for v2 when necessary @@ -4528,29 +4528,30 @@ int device_rename(struct device *dev, const char *new_name) goto out; } if (dev->class) { struct subsys_private *sp = class_to_subsys(dev->class); if (!sp) { error = -EINVAL; goto out; } - + /* 1) rename the link name to new name */ error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name, new_name, kobject_namespace(kobj)); subsys_put(sp); if (error) goto out; } error = kobject_rename(kobj, new_name); + /* but forget to revert the jobs done by 1) if below error really happens */ if (error) goto out; out: put_device(dev); kfree(old_device_name); return error; } what i am doing is to undo the 1) job when the given error happens. >> Fixes: f349cf34731c ("driver core: Implement ns directory support for device classes.") >> Signed-off-by: Zijun Hu >> --- >> drivers/base/core.c | 17 +++++++++++------ >> 1 file changed, 11 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/base/core.c b/drivers/base/core.c >> index 2b4c0624b704..a05b7186cf33 100644 >> --- a/drivers/base/core.c >> +++ b/drivers/base/core.c >> @@ -4512,9 +4512,11 @@ EXPORT_SYMBOL_GPL(device_destroy); >> */ >> int device_rename(struct device *dev, const char *new_name) >> { >> + struct subsys_private *sp = NULL; >> struct kobject *kobj = &dev->kobj; >> char *old_device_name = NULL; >> int error; >> + bool is_link_renamed = false; >> >> dev = get_device(dev); >> if (!dev) >> @@ -4529,7 +4531,7 @@ int device_rename(struct device *dev, const char *new_name) >> } >> >> if (dev->class) { >> - struct subsys_private *sp = class_to_subsys(dev->class); >> + sp = class_to_subsys(dev->class); >> >> if (!sp) { >> error = -EINVAL; >> @@ -4537,17 +4539,20 @@ int device_rename(struct device *dev, const char *new_name) >> } >> >> error = sysfs_rename_link_ns(&sp->subsys.kobj, kobj, old_device_name, >> - new_name, kobject_namespace(kobj)); >> - subsys_put(sp); >> + new_name, kobject_namespace(kobj)); > > Why did you change the indentation here? > it is caused by that my ~/.vim/plugin/linuxsty.vim is broken, it is a link but the target is loss. will fix this indentation issue within v2. >> if (error) >> goto out; >> + >> + is_link_renamed = true; >> } >> >> error = kobject_rename(kobj, new_name); >> - if (error) >> - goto out; >> - >> out: >> + if (error && is_link_renamed) >> + sysfs_rename_link_ns(&sp->subsys.kobj, kobj, new_name, >> + old_device_name, kobject_namespace(kobj)); >> + subsys_put(sp); > > How was this found? What in-kernel code causes this problem? And how > was this tested? > find it by reading code, no other in-kernel code cause this issue but the API itself, i wrote a simple code to test it. > thanks, > > greg k-h