Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Three fixes for deadlocks #24672

Merged
merged 3 commits into from
Apr 11, 2023

Conversation

bimmlerd
Copy link
Member

@bimmlerd bimmlerd commented Mar 31, 2023

This PR collects three commits with deadlock fixes:

  1. Fixes a deadlock involving writeHeaderFile. See endpoint, proxy: Fix deadlock in writeHeaderfile #24582 for details
  2. Removes the lock ordering constraint of DNSProxy > IPCache by hoisting IPCache access out of the DNSProxy critical section in GetRules
  3. Resolves a potential deadlock involving IPCache and Endpoint by avoiding to hold the IPCache mutex in GetNamedPorts. IPCache.InjectLabels holds the ipcache mutex while calling into endpointManager.UpdatePolicyMaps, which holds each EP lock in turn. On the other hand, GetNamedPorts holds an EP lock and calls into IPCache.

Please review commit by commit, and carefully - all changes are subtle.

Could use some help in word-smithing the release note.

[Marking this as backport/author, atomic.Bool and friends aren't available in Go 1.18 for v1.12]

Fixes: #23836
Ref: #20915

Solve control-plane deadlock issues leading to outages. A typical log line indicative of this issue is `probe=l7-proxy msg="No response from probe within 15 seconds"`

cc @gandro @joestringer @christarazi

@bimmlerd bimmlerd added kind/bug This is a bug in the Cilium logic. needs-backport/1.12 affects/v1.12 This issue affects v1.12 branch backport/author The backport will be carried out by the author of the PR. needs-backport/1.13 This PR / issue needs backporting to the v1.13 branch affects/v1.13 This issue affects v1.13 branch labels Mar 31, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot added dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. labels Mar 31, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot added this to Needs backport from master in 1.13.2 Mar 31, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot added this to Needs backport from master in 1.12.9 Mar 31, 2023
@bimmlerd bimmlerd added the release-note/bug This PR fixes an issue in a previous release of Cilium. label Mar 31, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot removed the dont-merge/needs-release-note-label The author needs to describe the release impact of these changes. label Mar 31, 2023
@bimmlerd bimmlerd force-pushed the pr/bimmlerd/three-fixes-for-deadlocks branch from 433249c to 62f0948 Compare March 31, 2023 12:22
@bimmlerd bimmlerd marked this pull request as ready for review March 31, 2023 12:29
@bimmlerd bimmlerd requested review from a team as code owners March 31, 2023 12:29
@bimmlerd
Copy link
Member Author

bimmlerd commented Mar 31, 2023

/test

Job 'Cilium-PR-K8s-1.25-kernel-4.19' failed:

Click to show.

Test Name

K8sDatapathConfig Host firewall With native routing and endpoint routes

Failure Output

FAIL: Error deleting resource /home/jenkins/workspace/Cilium-PR-K8s-1.25-kernel-4.19/src/github.com/cilium/cilium/test/k8s/manifests/host-policies.yaml: Cannot retrieve "cilium-c47sh"'s policy revision: cannot get policy revision: ""

If it is a flake and a GitHub issue doesn't already exist to track it, comment /mlh new-flake Cilium-PR-K8s-1.25-kernel-4.19 so I can create one.

@bimmlerd bimmlerd force-pushed the pr/bimmlerd/three-fixes-for-deadlocks branch from 62f0948 to 054b7f6 Compare March 31, 2023 14:12
@bimmlerd
Copy link
Member Author

bimmlerd commented Mar 31, 2023

/test

fixed a legitimate test failure in priv unit tests; retriggering

Job 'Cilium-PR-K8s-1.16-kernel-4.19' failed:

Click to show.

Test Name

K8sUpdates Tests upgrade and downgrade from a Cilium stable image to master

Failure Output

FAIL: Expected

If it is a flake and a GitHub issue doesn't already exist to track it, comment /mlh new-flake Cilium-PR-K8s-1.16-kernel-4.19 so I can create one.

Copy link
Member

@jrajahalme jrajahalme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

nit: It seems atomic.Pointer is used after all

pkg/ipcache/ipcache.go Show resolved Hide resolved
@christarazi christarazi added area/daemon Impacts operation of the Cilium daemon. area/proxy Impacts proxy components, including DNS, Kafka, Envoy and/or XDS servers. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies. labels Mar 31, 2023
@bimmlerd
Copy link
Member Author

bimmlerd commented Apr 11, 2023

/test

Rebased on master to get #24389 since helm charts were out of sync. We'll have to rebase again once #24803 is in, since that caused other CI breakage we saw.

Also following up with Casey on the controller write-cni-file failure 'failed to render CNI configuration file: invalid CNI chaining mode: we saw.

gandro and others added 3 commits April 11, 2023 13:24
This fixes a deadlock with the IPCache and the endpoint mutex where
writeHeaderfile calls into the proxy to fetch the endpoint's DNSRules
while holding on to the endpoint mutex. That pattern causes a deadlock,
because the proxy code itself then calls into IPCache while the endpoint
mutex is held. This violates the assumed lock ordering, as we require
that the IPCache mutex is always acquired _before_ any endpoint mutex.

We fix this deadlock by hoisting call to update the endpoint's DNSRules
out of writeHeaderfile and into the syncEndpointHeaderFile function,
which is triggered whenever there is a change in DNSRules.

Fixes: cilium#23836
Ref: cilium#20915

Co-authored-by: David Bimmler <david.bimmler@isovalent.com>
Signed-off-by: Sebastian Wicki <sebastian@isovalent.com>
Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
GetRules held the DNSProxy lock while accessing IPCache. This
established a partial lock order in which DNSProxy > IPCache. (Read
DNSProxy > IPCache as "DNSProxy lock held while attempting to acquire
IPCache lock." We use '>' as a convenient notation as it intuitively
implies transitivity.)

Since we also have IPCache > Endpoint (cf InjectLabels), this
established DNSProxy > Endpoint by transitivity. That, however, is
incompatible with instances where we hold the Endpoint lock while
acquiring the DNSProxy lock (as that establishes Endpoint > DNSProxy).
Having both EP > DNSProxy and DNSProxy > EP is not allowed and leads to
deadlocks. To break the cycle, hoist the IPCache lookups out of the
DNSProxy critical section.

Without DNSProxy > IPCache, Endpoint > DNSProxy is fine, as there's no
more DNSPRoxy > IPCache > Endpoint and wait cycle.

Of course, any other instance where X > IPCache and Endpoint > X can
still result in a deadlock. Somewhat insidiously, these wait chains can
be arbitrarily long, and hence arbitrarily difficult to find.

Suggested-by: Sebastian Wicki <sebastian@isovalent.com>
Suggested-by: Chris Tarazi <chris@isovalent.com>
Suggested-by: Joe Stringer <joe@cilium.io>
Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
We have a partial lock ordering of IPCache > Endpoint as established in
InjectLabels, where the IPCache lock is held and all endpoint locks are
acquired one by one. On the other hand, GetNamedPorts was called in a
context with the Endpoint lock held (cf *Endpoint.GetNamedPortLocked),
and attempts to acquire the IPCache lock. This would establish EP >
IPCache, which potentially deadlocks with the above.

To fix it, remove the requirement for a write lock on IPCache for
GetNamedPorts. Instead, we always pre-compute the namedPorts map in
Upsert and Delete and simply return it in GetNamedPorts. To ensure
atomicity, we use atomics to load and store read-only pointer to the
namedPorts map and the needNamedPorts flag.

Co-authored-by: Sebastian Wicki <sebastian@isovalent.com>
Signed-off-by: David Bimmler <david.bimmler@isovalent.com>
@gandro gandro force-pushed the pr/bimmlerd/three-fixes-for-deadlocks branch from bba2da0 to 5b155ce Compare April 11, 2023 11:24
@gandro
Copy link
Member

gandro commented Apr 11, 2023

Previous CI was green except ci-datapath and EKS. ci-datapath was broken in cilium-cli, c.f. cilium/cilium-cli@0c03922. EKS was broken due to a known issue on master which just got fixed. Rebased again to pull in the EKS changes.

Restarting the tests again.

@gandro
Copy link
Member

gandro commented Apr 11, 2023

/test

@joestringer
Copy link
Member

It looks like the ci-datapath issue is being addressed and otherwise the tests are passing & reviews are in. Ready to merge?

@gandro
Copy link
Member

gandro commented Apr 11, 2023

/ci-datapath

@gandro
Copy link
Member

gandro commented Apr 11, 2023

It looks like the ci-datapath issue is being addressed and otherwise the tests are passing & reviews are in. Ready to merge?

The ci-datapath fix was just merged. Let me try to re-run it (I think it should work without a rebase), to see if we can get all green for increased confidence. But if it's still broken, yeah, let's merge it.

@maintainer-s-little-helper maintainer-s-little-helper bot added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Apr 11, 2023
@gandro
Copy link
Member

gandro commented Apr 11, 2023

This is ready to merge.

@gandro gandro merged commit 93cd67f into cilium:master Apr 11, 2023
42 checks passed
@gandro gandro added backport-pending/1.12 backport-pending/1.13 The backport for Cilium 1.13.x for this PR is in progress. and removed needs-backport/1.12 needs-backport/1.13 This PR / issue needs backporting to the v1.13 branch labels Apr 11, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Needs backport from master to Backport pending to v1.12 in 1.12.9 Apr 11, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Needs backport from master to Backport pending to v1.13 in 1.13.2 Apr 11, 2023
@bimmlerd bimmlerd deleted the pr/bimmlerd/three-fixes-for-deadlocks branch April 12, 2023 10:26
@gandro gandro mentioned this pull request Apr 12, 2023
2 tasks
@joestringer joestringer added backport-done/1.13 The backport for Cilium 1.13.x for this PR is done. and removed backport-pending/1.13 The backport for Cilium 1.13.x for this PR is in progress. labels Apr 12, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Backport pending to v1.13 to Backport done to v1.13 in 1.13.2 Apr 12, 2023
@joestringer joestringer added backport-done/1.12 The backport for Cilium 1.12.x for this PR is done. and removed backport-pending/1.12 labels Apr 12, 2023
@maintainer-s-little-helper maintainer-s-little-helper bot moved this from Backport pending to v1.12 to Backport done to v1.12 in 1.12.9 Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects/v1.12 This issue affects v1.12 branch affects/v1.13 This issue affects v1.13 branch area/daemon Impacts operation of the Cilium daemon. area/proxy Impacts proxy components, including DNS, Kafka, Envoy and/or XDS servers. backport/author The backport will be carried out by the author of the PR. backport-done/1.12 The backport for Cilium 1.12.x for this PR is done. backport-done/1.13 The backport for Cilium 1.13.x for this PR is done. kind/bug This is a bug in the Cilium logic. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-blocker/1.12 This issue will prevent the release of the next version of Cilium. release-blocker/1.13 This issue will prevent the release of the next version of Cilium. release-note/bug This PR fixes an issue in a previous release of Cilium. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
No open projects
1.12.9
Backport done to v1.12
1.13.2
Backport done to v1.13
Development

Successfully merging this pull request may close these issues.

Scale-down event of EKS kube-apiserver causes network outage
6 participants