From patchwork Tue Nov 11 14:48:29 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Cohen X-Patchwork-Id: 40578 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ee0-f70.google.com (mail-ee0-f70.google.com [74.125.83.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7AC49218DE for ; Tue, 11 Nov 2014 14:50:38 +0000 (UTC) Received: by mail-ee0-f70.google.com with SMTP id b57sf6940975eek.5 for ; Tue, 11 Nov 2014 06:50:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:date:from:user-agent :mime-version:to:subject:references:in-reply-to:cc:precedence :list-id:list-unsubscribe:list-archive:list-post:list-help :list-subscribe:sender:errors-to:x-original-sender :x-original-authentication-results:mailing-list:content-type; bh=+J7DLwtHK8AbOkpkkndlA8KT5tmgEPA/x3zgIBrnirk=; b=i8o3EGhcOuPQ1tZ2Vm4nxdUCBvGbZDGnkIpTG2UQf447rO8r/8I6H9sf/v1oV+oc+O 3kS77ROjNabBISd/Pzurg66iQvUTmhfBzMkXBx9iu8G5sQ9S67f+uJtuZCf7FYUb+SVb QLhAKSKXyemOrESf2w3DH7RAxGOKjReEvNMZKhzIbiRR47KKYWkrDiNLEBpR1biixZAt GgzlFlty8tryusSbKWWA0hefJnDpEBEwJ5gXyHsq56MZSohdYmc4dYqzao4xITxONpCX /3+5xPkj8+DCQ2Xmfr3ZW0qmA43eXedcviUAiZQaPPXfzW9244JK1YGh3BR7AkYGEqAJ 7Hig== X-Gm-Message-State: ALoCoQleoH5GMgNeFwuj+NEFg9JkFyy3zmVNnBMEgLZI9xg5jSenCLftQcN27OSP3PjkrXg3z5+g X-Received: by 10.180.106.67 with SMTP id gs3mr5397582wib.3.1415717437737; Tue, 11 Nov 2014 06:50:37 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.23.229 with SMTP id p5ls269109laf.4.gmail; Tue, 11 Nov 2014 06:50:37 -0800 (PST) X-Received: by 10.152.87.67 with SMTP id v3mr2348831laz.97.1415717437511; Tue, 11 Nov 2014 06:50:37 -0800 (PST) Received: from mail-la0-f54.google.com (mail-la0-f54.google.com. [209.85.215.54]) by mx.google.com with ESMTPS id gd7si31644879lbc.83.2014.11.11.06.50.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 11 Nov 2014 06:50:37 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.54 as permitted sender) client-ip=209.85.215.54; Received: by mail-la0-f54.google.com with SMTP id s18so9755626lam.27 for ; Tue, 11 Nov 2014 06:50:37 -0800 (PST) X-Received: by 10.112.62.166 with SMTP id z6mr9661585lbr.74.1415717437402; Tue, 11 Nov 2014 06:50:37 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp267141lbc; Tue, 11 Nov 2014 06:50:35 -0800 (PST) X-Received: by 10.68.227.104 with SMTP id rz8mr40444262pbc.4.1415717434009; Tue, 11 Nov 2014 06:50:34 -0800 (PST) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id id7si18719541pbc.167.2014.11.11.06.50.33 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 11 Nov 2014 06:50:34 -0800 (PST) Received-SPF: none (google.com: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org does not designate permitted sender hosts) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1XoCkg-0001HN-3u; Tue, 11 Nov 2014 14:49:02 +0000 Received: from mx1.redhat.com ([209.132.183.28]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1XoCkc-0001DH-VO for linux-arm-kernel@lists.infradead.org; Tue, 11 Nov 2014 14:48:59 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sABEmTlC027676 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 11 Nov 2014 09:48:30 -0500 Received: from [10.10.48.197] (vpn-48-197.rdu2.redhat.com [10.10.48.197]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sABEmTBB030842; Tue, 11 Nov 2014 09:48:29 -0500 Message-ID: <546221BD.8000207@redhat.com> Date: Tue, 11 Nov 2014 09:48:29 -0500 From: William Cohen User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Will Deacon Subject: Re: [PATCH] Correct the race condition in aarch64_insn_patch_text_sync() References: <1415637362-30754-1-git-send-email-wcohen@redhat.com> <20141110170846.GH23942@arm.com> <546113F4.1050304@redhat.com> <20141111112844.GC16265@arm.com> In-Reply-To: <20141111112844.GC16265@arm.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20141111_064859_065943_829583B9 X-CRM114-Status: GOOD ( 27.54 ) X-Spam-Score: -5.6 (-----) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-5.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high trust [209.132.183.28 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H3 RBL: Good reputation (+3) [209.132.183.28 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.5 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 RCVD_IN_MSPIKE_WL Mailspike good senders Cc: Catalin Marinas , "dave.long@linaro.org" , "linux-arm-kernel@lists.infradead.org" X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: wcohen@redhat.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.54 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 On 11/11/2014 06:28 AM, Will Deacon wrote: > On Mon, Nov 10, 2014 at 07:37:24PM +0000, William Cohen wrote: >> On 11/10/2014 12:08 PM, Will Deacon wrote: >>> On Mon, Nov 10, 2014 at 04:36:02PM +0000, William Cohen wrote: >>>> diff --git a/arch/arm64/kernel/insn.c b/arch/arm64/kernel/insn.c >>>> index e007714..e6266db 100644 >>>> --- a/arch/arm64/kernel/insn.c >>>> +++ b/arch/arm64/kernel/insn.c >>>> @@ -153,8 +153,10 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg) >>>> int i, ret = 0; >>>> struct aarch64_insn_patch *pp = arg; >>>> >>>> - /* The first CPU becomes master */ >>>> - if (atomic_inc_return(&pp->cpu_count) == 1) { >>>> + /* Make sure all the processors are in this functionaarch64_insn_patch_text_cb( >>>> + before patching the code. The last CPU to this function >>>> + does the update. */ >>>> + if (atomic_dec_return(&pp->cpu_count) == 0) { >>>> for (i = 0; ret == 0 && i < pp->insn_cnt; i++) >>>> ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i], >>>> pp->new_insns[i]); >>>> @@ -163,7 +165,8 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg) >>>> * which ends with "dsb; isb" pair guaranteeing global >>>> * visibility. >>>> */ >>>> - atomic_set(&pp->cpu_count, -1); >>>> + /* Notifiy other processors with an additional decrement. */ >>>> + atomic_dec(&pp->cpu_count); >>>> } else { >>>> while (atomic_read(&pp->cpu_count) != -1) >>>> cpu_relax(); >>>> @@ -185,6 +188,7 @@ int __kprobes aarch64_insn_patch_text_sync(void *addrs[], u32 insns[], int cnt) >>>> if (cnt <= 0) >>>> return -EINVAL; >>>> >>>> + atomic_set(&patch.cpu_count, num_online_cpus()); >>> >>> I think this is still racy with hotplug before stop_machine has done >>> get_online_cpus. How about we leave the increment in the callback and change >>> the exit condition to compare with num_online_cpus() instead? >> >> Thanks for the feedback. I am no expert in the corner cases involved with >> hotplug. Dave Long suggested something similar with num_online_cpus in >> the arch64_insn_patch_text_cb() and using increments and checking the >> num_cpus_online() inside aarch64_insn_patch_text_cb(). Moving the >> num_cpu_online() inside the aarch64_insn_patch_text_cb() is sufficient to >> avoid race conditions with hotplug? If so, would the attached patch be >> appropriate? > > Yes, because stop_machine() does {get,put}_online_cpus() around the > invocation. > >> From d02e3244c436234d0d07500be6d4df64feb2052a Mon Sep 17 00:00:00 2001 >> From: William Cohen >> Date: Mon, 10 Nov 2014 14:26:44 -0500 >> Subject: [PATCH] Correct the race condition in aarch64_insn_patch_text_sync() >> >> When experimenting with patches to provide kprobes support for aarch64 >> smp machines would hang when inserting breakpoints into kernel code. >> The hangs were caused by a race condition in the code called by >> aarch64_insn_patch_text_sync(). The first processor in the >> aarch64_insn_patch_text_cb() function would patch the code while other >> processors were still entering the function and incrementing the >> cpu_count field. This resulted in some processors never observing the >> exit condition and exiting the function. Thus, processors in the >> system hung. >> >> The patching function now waits for all processors to enter the >> patching function before changing code to ensure that none of the >> processors are in code that is going to be patched. Once all the >> processors have entered the function, the last processor to enter the >> patching function performs the patching and signals that the patching >> is complete with one last increment of the cpu_count field to make it >> num_cpus_online()+1. >> >> Signed-off-by: William Cohen >> --- >> arch/arm64/kernel/insn.c | 12 ++++++++---- >> 1 file changed, 8 insertions(+), 4 deletions(-) >> >> diff --git a/arch/arm64/kernel/insn.c b/arch/arm64/kernel/insn.c >> index e007714..4fdddf1 100644 >> --- a/arch/arm64/kernel/insn.c >> +++ b/arch/arm64/kernel/insn.c >> @@ -151,10 +151,13 @@ struct aarch64_insn_patch { >> static int __kprobes aarch64_insn_patch_text_cb(void *arg) >> { >> int i, ret = 0; >> + int count = num_online_cpus(); >> struct aarch64_insn_patch *pp = arg; >> >> - /* The first CPU becomes master */ >> - if (atomic_inc_return(&pp->cpu_count) == 1) { >> + /* Make sure all the processors are in this function >> + before patching the code. The last CPU to this function >> + does the update. */ >> + if (atomic_inc_return(&pp->cpu_count) == count) { > > Actually, you can leave this hunk alone and leave the first CPU to do the > patching. Hi Will, If it doesn't matter which processor is doing the update, do the processors all need to wait for the last one to get to this function before continuing on? Or would it be acceptable to allow processors to continue once the first processor completes the patch operation? That could reduce the amount of time that processors spin waiting for other processors to enter arch64_insn_patch_text_cb. > >> for (i = 0; ret == 0 && i < pp->insn_cnt; i++) >> ret = aarch64_insn_patch_text_nosync(pp->text_addrs[i], >> pp->new_insns[i]); >> @@ -163,9 +166,10 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg) >> * which ends with "dsb; isb" pair guaranteeing global >> * visibility. >> */ >> - atomic_set(&pp->cpu_count, -1); >> + /* Notifiy other processors with an additional increment. */ > > Notify > >> + atomic_inc(&pp->cpu_count); >> } else { >> - while (atomic_read(&pp->cpu_count) != -1) >> + while (atomic_read(&pp->cpu_count) <= count) >> cpu_relax(); > > Then make this 'cpu_count <= num_online_cpus()' > > Will > Attached is a patch that addresses the current comment. -Will Cohen Acked-by: Will Deacon >From 41c728aeee2185fd30ec6a8ba223a2caec875f47 Mon Sep 17 00:00:00 2001 From: William Cohen Date: Tue, 11 Nov 2014 09:41:27 -0500 Subject: [PATCH] Correct the race condition in aarch64_insn_patch_text_sync() When experimenting with patches to provide kprobes support for aarch64 smp machines would hang when inserting breakpoints into kernel code. The hangs were caused by a race condition in the code called by aarch64_insn_patch_text_sync(). The first processor in the aarch64_insn_patch_text_cb() function would patch the code while other processors were still entering the function and incrementing the cpu_count field. This resulted in some processors never observing the exit condition and exiting the function. Thus, processors in the system hung. The first processor to enter the patching function performs the patching and signals that the patching is complete with an increment of the cpu_count field. When all the processors have incremented the cpu_count field the cpu_count will be num_cpus_online()+1 and they will return to normal execution. Signed-off-by: William Cohen --- arch/arm64/kernel/insn.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/insn.c b/arch/arm64/kernel/insn.c index e007714..8cd27fe 100644 --- a/arch/arm64/kernel/insn.c +++ b/arch/arm64/kernel/insn.c @@ -163,9 +163,10 @@ static int __kprobes aarch64_insn_patch_text_cb(void *arg) * which ends with "dsb; isb" pair guaranteeing global * visibility. */ - atomic_set(&pp->cpu_count, -1); + /* Notify other processors with an additional increment. */ + atomic_inc(&pp->cpu_count); } else { - while (atomic_read(&pp->cpu_count) != -1) + while (atomic_read(&pp->cpu_count) <= num_online_cpus()) cpu_relax(); isb(); } -- 1.8.3.1